From 0e8c72dcabdd551615c97fe4ec847de8ea0c0a06 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Wed, 21 Nov 2018 16:18:57 +0530 Subject: [PATCH 01/19] Adding HttpClient and middleware classes --- .../graph/httpcore/AuthenticationHandler.java | 24 +++++ .../microsoft/graph/httpcore/HttpClients.java | 44 ++++++++ .../httpcore/IAuthenticationProvider.java | 12 +++ .../graph/httpcore/RedirectHandler.java | 73 +++++++++++++ .../graph/httpcore/RetryHandler.java | 101 ++++++++++++++++++ 5 files changed, 254 insertions(+) create mode 100644 src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java create mode 100644 src/main/java/com/microsoft/graph/httpcore/HttpClients.java create mode 100644 src/main/java/com/microsoft/graph/httpcore/IAuthenticationProvider.java create mode 100644 src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java create mode 100644 src/main/java/com/microsoft/graph/httpcore/RetryHandler.java diff --git a/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java b/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java new file mode 100644 index 000000000..82747d1ee --- /dev/null +++ b/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java @@ -0,0 +1,24 @@ +package com.microsoft.graph.httpcore; + +import java.io.IOException; + +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpRequestInterceptor; +import org.apache.http.protocol.HttpContext; + +public class AuthenticationHandler implements HttpRequestInterceptor { + + private IAuthenticationProvider authProvider; + + public AuthenticationHandler(IAuthenticationProvider authProvider) { + this.authProvider = authProvider; + } + + @Override + public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { + // TODO Auto-generated method stub + authProvider.authenticateRequest(request); + } + +} diff --git a/src/main/java/com/microsoft/graph/httpcore/HttpClients.java b/src/main/java/com/microsoft/graph/httpcore/HttpClients.java new file mode 100644 index 000000000..8eb239b05 --- /dev/null +++ b/src/main/java/com/microsoft/graph/httpcore/HttpClients.java @@ -0,0 +1,44 @@ +package com.microsoft.graph.httpcore; + +import org.apache.http.client.config.RequestConfig; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; + +public class HttpClients { + private HttpClients() { + super(); + } + + /** + * Creates builder object for construction of custom + * {@link CloseableHttpClient} instances. + */ + public static HttpClientBuilder custom() { + return HttpClientBuilder.create(); + } + + /** + * Creates {@link CloseableHttpClient} instance with default + * configuration. + */ + public static CloseableHttpClient createDefault() { + //return HttpClientBuilder.create().build(); + + RequestConfig config = RequestConfig.custom().setMaxRedirects(5).build(); + + IAuthenticationProvider auth = null; + return HttpClientBuilder.create().addInterceptorFirst(new AuthenticationHandler(null)) + .setRedirectStrategy(new RedirectHandler()) + .setServiceUnavailableRetryStrategy(new RetryHandler()) + .setDefaultRequestConfig(config) + .build(); + } + + /** + * Creates {@link CloseableHttpClient} instance with default + * configuration based on system properties. + */ + public static CloseableHttpClient createSystem() { + return HttpClientBuilder.create().useSystemProperties().build(); + } +} diff --git a/src/main/java/com/microsoft/graph/httpcore/IAuthenticationProvider.java b/src/main/java/com/microsoft/graph/httpcore/IAuthenticationProvider.java new file mode 100644 index 000000000..c969cfe11 --- /dev/null +++ b/src/main/java/com/microsoft/graph/httpcore/IAuthenticationProvider.java @@ -0,0 +1,12 @@ +package com.microsoft.graph.httpcore; + +import org.apache.http.HttpRequest; + +public interface IAuthenticationProvider { + /** + * Authenticates the request + * + * @param request the request to authenticate + */ + void authenticateRequest(final HttpRequest request); +} diff --git a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java new file mode 100644 index 000000000..6e1e7bcfd --- /dev/null +++ b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java @@ -0,0 +1,73 @@ +package com.microsoft.graph.httpcore; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.apache.http.Header; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.ProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.methods.RequestBuilder; +import org.apache.http.impl.client.DefaultRedirectStrategy; +import org.apache.http.protocol.HttpContext; +import org.apache.http.util.Args; + +public class RedirectHandler extends DefaultRedirectStrategy{ + + public static final RedirectHandler INSTANCE = new RedirectHandler(); + + @Override + public boolean isRedirected( + final HttpRequest request, + final HttpResponse response, + final HttpContext context) throws ProtocolException { + Args.notNull(request, "HTTP request"); + Args.notNull(response, "HTTP response"); + + final int statusCode = response.getStatusLine().getStatusCode(); + final Header locationHeader = response.getFirstHeader("location"); + if(locationHeader == null) + return false; + + if(statusCode == HttpStatus.SC_MOVED_TEMPORARILY || + statusCode == HttpStatus.SC_MOVED_PERMANENTLY || + statusCode == HttpStatus.SC_TEMPORARY_REDIRECT || + statusCode == HttpStatus.SC_SEE_OTHER) + return true; + + return false; + } + + @Override + public HttpUriRequest getRedirect( + final HttpRequest request, + final HttpResponse response, + final HttpContext context) throws ProtocolException { + final URI uri = getLocationURI(request, response, context); + final String method = request.getRequestLine().getMethod(); + if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) { + return new HttpHead(uri); + } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) + return new HttpGet(uri); + else { + final int status = response.getStatusLine().getStatusCode(); + if(status != HttpStatus.SC_SEE_OTHER) { + try { + final URI requestURI = new URI(request.getRequestLine().getUri()); + if(!uri.getHost().equalsIgnoreCase(requestURI.getHost())) { + request.removeHeaders("Authorization"); + } + return RequestBuilder.copy(request).setUri(uri).build(); + } + catch (final URISyntaxException ex) { + throw new ProtocolException(ex.getMessage(), ex); + } + } + return new HttpGet(uri); + } + } +} diff --git a/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java b/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java new file mode 100644 index 000000000..b49b26a8a --- /dev/null +++ b/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java @@ -0,0 +1,101 @@ +package com.microsoft.graph.httpcore; + +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.client.ServiceUnavailableRetryStrategy; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpCoreContext; +import org.apache.http.util.Args; + +public class RetryHandler implements ServiceUnavailableRetryStrategy{ + + /** + * Maximum number of allowed retries if the server responds with a HTTP code + * in our retry code list. Default value is 1. + */ + private final int maxRetries; + + /** + * Retry interval between subsequent requests, in milliseconds. Default + * value is 1 second. + */ + private long retryInterval; + private final int DELAY_SECONDS = 10; + private final String RETRY_AFTER = "Retry-After"; + private final String TRANSFER_ENCODING = "Transfer-Encoding"; + + private final int MSClientErrorCodeTooManyRequests = 429; + private final int MSClientErrorCodeServiceUnavailable = 503; + private final int MSClientErrorCodeGatewayTimeout = 504; + + public RetryHandler(final int maxRetries, final int retryInterval) { + super(); + Args.positive(maxRetries, "Max retries"); + Args.positive(retryInterval, "Retry interval"); + this.maxRetries = maxRetries; + this.retryInterval = retryInterval; + } + + public RetryHandler() { + this(1, 1000); + } + + @Override + public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { + boolean shouldRetry = false; + int statusCode = response.getStatusLine().getStatusCode(); + shouldRetry = (executionCount < maxRetries) && checkStatus(statusCode) && isBuffered(response, context); + + if(shouldRetry) { + Header header = response.getFirstHeader(RETRY_AFTER); + if(header != null) + retryInterval = Long.parseLong(header.getValue()); + else + retryInterval = (long)Math.pow(2.0, (double)executionCount) * DELAY_SECONDS; + } + return shouldRetry; + } + + @Override + public long getRetryInterval() { + // TODO Auto-generated method stub + return retryInterval; + } + + private boolean checkStatus(int statusCode) { + if (statusCode == MSClientErrorCodeTooManyRequests || statusCode == MSClientErrorCodeServiceUnavailable + || statusCode == MSClientErrorCodeGatewayTimeout) + return true; + return false; + } + + private boolean isBuffered(HttpResponse response, HttpContext context) { + HttpRequest request = (HttpRequest)context.getAttribute( HttpCoreContext.HTTP_REQUEST); + String methodName = request.getRequestLine().getMethod(); + + boolean isHTTPMethodPutPatchOrPost = methodName.equalsIgnoreCase(HttpPost.METHOD_NAME) || + methodName.equalsIgnoreCase(HttpPut.METHOD_NAME) || + methodName.equalsIgnoreCase(HttpPatch.METHOD_NAME); + + Header transferEncoding = response.getFirstHeader(TRANSFER_ENCODING); + boolean isTransferEncodingChunked = (transferEncoding != null) && + transferEncoding.getValue().equalsIgnoreCase("chunked"); + + HttpEntity entity = null; + if(request instanceof HttpEntityEnclosingRequestBase) { + HttpEntityEnclosingRequestBase httprequest = (HttpEntityEnclosingRequestBase)request; + entity = httprequest.getEntity(); + } + + if(entity != null && isHTTPMethodPutPatchOrPost && isTransferEncodingChunked) + return false; + return true; + } + +} From 00e75841a64cf9948b6a6f1a42f1cac9ef6aa4b8 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Wed, 21 Nov 2018 16:31:57 +0530 Subject: [PATCH 02/19] Updating httpClient with default creation logic --- .../microsoft/graph/httpcore/HttpClients.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/graph/httpcore/HttpClients.java b/src/main/java/com/microsoft/graph/httpcore/HttpClients.java index 8eb239b05..bf0a60f92 100644 --- a/src/main/java/com/microsoft/graph/httpcore/HttpClients.java +++ b/src/main/java/com/microsoft/graph/httpcore/HttpClients.java @@ -22,17 +22,27 @@ public static HttpClientBuilder custom() { * configuration. */ public static CloseableHttpClient createDefault() { - //return HttpClientBuilder.create().build(); - RequestConfig config = RequestConfig.custom().setMaxRedirects(5).build(); - - IAuthenticationProvider auth = null; return HttpClientBuilder.create().addInterceptorFirst(new AuthenticationHandler(null)) .setRedirectStrategy(new RedirectHandler()) .setServiceUnavailableRetryStrategy(new RetryHandler()) .setDefaultRequestConfig(config) .build(); } + + /** + * Creates {@link CloseableHttpClient} instance with default + * configuration and provided authProvider + */ + public static CloseableHttpClient createDefault(IAuthenticationProvider auth) { + RequestConfig config = RequestConfig.custom().setMaxRedirects(5).build(); + + return HttpClientBuilder.create().addInterceptorFirst(new AuthenticationHandler(auth)) + .setRedirectStrategy(new RedirectHandler()) + .setServiceUnavailableRetryStrategy(new RetryHandler()) + .setDefaultRequestConfig(config) + .build(); + } /** * Creates {@link CloseableHttpClient} instance with default From 7b9154e2724463618743dac10d04650e35a41781 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Mon, 26 Nov 2018 18:03:53 +0530 Subject: [PATCH 03/19] Adding batch request content files --- .../graph/content/MSBatchRequestContent.java | 107 ++++++++++++++++++ .../graph/content/MSBatchRequestStep.java | 29 +++++ .../graph/content/MSBatchResponseContent.java | 31 +++++ 3 files changed, 167 insertions(+) create mode 100644 src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java create mode 100644 src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java create mode 100644 src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java diff --git a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java new file mode 100644 index 000000000..d649cf70d --- /dev/null +++ b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java @@ -0,0 +1,107 @@ +package com.microsoft.graph.content; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.HttpRequest; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.util.EntityUtils; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +public class MSBatchRequestContent { + private List batchRequestStepsArray; + private final int maxNumberOfRequests = 20; + + public MSBatchRequestContent(List batchRequestStepsArray) { + batchRequestStepsArray = new ArrayList<>(); + if(batchRequestStepsArray.size() <= maxNumberOfRequests) { + for(MSBatchRequestStep requestStep: batchRequestStepsArray) + addBatchRequestStep(requestStep); + } + } + + public MSBatchRequestContent() { + batchRequestStepsArray = new ArrayList<>(); + } + + public void addBatchRequestStep(MSBatchRequestStep batchRequestStep) { + if(batchRequestStep.getRequestId().compareTo("") == 0) + return; + if(batchRequestStepsArray.size() == maxNumberOfRequests) + return; + for(MSBatchRequestStep requestStep: batchRequestStepsArray) { + if(batchRequestStep.getRequestId().compareTo(requestStep.getRequestId()) == 0) + return; + } + batchRequestStepsArray.add(batchRequestStep); + } + + public void removeBatchRequesStepWithId(String requestId) { + for (int i = batchRequestStepsArray.size()-1; i >= 0; i--) + { + MSBatchRequestStep requestStep = batchRequestStepsArray.get(i); + for (int j = requestStep.getArrayOfDependsOnIds().size() - 1; j >= 0; j--) + { + String dependsOnId = requestStep.getArrayOfDependsOnIds().get(j); + if(dependsOnId.compareTo(requestId) == 0) + { + requestStep.getArrayOfDependsOnIds().remove(j); + } + } + if(requestId.compareTo(requestStep.getRequestId()) == 0) + batchRequestStepsArray.remove(i); + } + } + + public String getBatchRequestContent() { + Map>> batchRequestContentMap = new HashMap(); + List> batchContentArray = new ArrayList(); + for(MSBatchRequestStep requestStep : batchRequestStepsArray) { + batchContentArray.add(getBatchRequestMapFromRequestStep(requestStep)); + } + batchRequestContentMap.put("requests", batchContentArray); + return JSONValue.toJSONString(batchRequestContentMap); + } + + private Map getBatchRequestMapFromRequestStep(MSBatchRequestStep batchRequestStep){ + Map contentmap = new HashMap(); + contentmap.put("id", batchRequestStep.getRequestId()); + contentmap.put("url", batchRequestStep.getRequest().getRequestLine().getUri()); + contentmap.put("method", batchRequestStep.getRequest().getRequestLine().getMethod()); + Header[] headers = batchRequestStep.getRequest().getAllHeaders(); + if(headers != null) { + JSONObject obj = new JSONObject(); + for(Header header: headers) { + obj.put(header.getName(), header.getValue()); + } + contentmap.put("headers", obj.toJSONString()); + } + HttpEntity entity = null; + HttpRequest request = batchRequestStep.getRequest(); + if(request instanceof HttpEntityEnclosingRequestBase) { + HttpEntityEnclosingRequestBase httprequest = (HttpEntityEnclosingRequestBase)request; + entity = httprequest.getEntity(); + } + if(entity != null) { + try { + String body = EntityUtils.toString(entity); + contentmap.put("body", body); + } + catch(Exception e) { + } + } + + List arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds(); + if(arrayOfDependsOnIds != null) { + contentmap.put("dependsOn", JSONValue.toJSONString(arrayOfDependsOnIds)); + } + + return contentmap; + } + +} diff --git a/src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java b/src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java new file mode 100644 index 000000000..d87bf27da --- /dev/null +++ b/src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java @@ -0,0 +1,29 @@ +package com.microsoft.graph.content; + +import java.util.List; + +import org.apache.http.HttpRequest; + +public class MSBatchRequestStep { + private String requestId; + private HttpRequest request; + private List arrayOfDependsOnIds; + + public MSBatchRequestStep(String requestId, HttpRequest request, List arrayOfDependsOnIds) { + this.requestId = requestId; + this.request = request; + this.arrayOfDependsOnIds = arrayOfDependsOnIds; + } + + public String getRequestId() { + return requestId; + } + + public HttpRequest getRequest() { + return request; + } + + public List getArrayOfDependsOnIds(){ + return arrayOfDependsOnIds; + } +} diff --git a/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java b/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java new file mode 100644 index 000000000..ac30ccb0e --- /dev/null +++ b/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java @@ -0,0 +1,31 @@ +package com.microsoft.graph.content; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +public class MSBatchResponseContent { + + private JSONObject batchResponseObj; + + public MSBatchResponseContent(String batchResponseData ) { + JSONParser parser = new JSONParser(); + try { + batchResponseObj = (JSONObject) parser.parse(batchResponseData); + } + catch(ParseException e) { + } + } + + public String getResponseById(String requestId) { + if(batchResponseObj.get(requestId) != null) + return batchResponseObj.get(requestId).toString(); + return null; + } + + public String getResponses() { + if(batchResponseObj != null) + return batchResponseObj.toJSONString(); + return null; + } +} From ed236bdaf652571ba41c05e8e0850f936f9fabb4 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Mon, 26 Nov 2018 18:04:30 +0530 Subject: [PATCH 04/19] Taking dependency on json-simple library --- build.gradle | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build.gradle b/build.gradle index 0307d7bc2..53d3467ec 100644 --- a/build.gradle +++ b/build.gradle @@ -28,5 +28,9 @@ dependencies { // Use Apache HttpClient compile 'org.apache.httpcomponents:httpclient:4.5.6' + + // https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple + compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1' + } From fc3dfae889ef4100aeda5cd1738836711f46d5af Mon Sep 17 00:00:00 2001 From: deagrawa Date: Thu, 3 Jan 2019 11:56:30 +0530 Subject: [PATCH 05/19] Test files for middlewares classes --- .../graph/content/MSBatchRequestContent.java | 14 ++--- .../graph/content/MSBatchResponseContent.java | 48 +++++++++++++-- .../graph/httpcore/RedirectHandler.java | 4 +- .../content/MSBatchRequestContentTest.java | 60 +++++++++++++++++++ .../graph/content/MSBatchRequestStepTest.java | 25 ++++++++ .../content/MSBatchResponseContentTest.java | 40 +++++++++++++ 6 files changed, 176 insertions(+), 15 deletions(-) create mode 100644 src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java create mode 100644 src/test/java/com/microsoft/graph/content/MSBatchRequestStepTest.java create mode 100644 src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java diff --git a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java index d649cf70d..8174306aa 100644 --- a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java +++ b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java @@ -18,7 +18,7 @@ public class MSBatchRequestContent { private final int maxNumberOfRequests = 20; public MSBatchRequestContent(List batchRequestStepsArray) { - batchRequestStepsArray = new ArrayList<>(); + this.batchRequestStepsArray = new ArrayList<>(); if(batchRequestStepsArray.size() <= maxNumberOfRequests) { for(MSBatchRequestStep requestStep: batchRequestStepsArray) addBatchRequestStep(requestStep); @@ -29,16 +29,16 @@ public MSBatchRequestContent() { batchRequestStepsArray = new ArrayList<>(); } - public void addBatchRequestStep(MSBatchRequestStep batchRequestStep) { + public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) { if(batchRequestStep.getRequestId().compareTo("") == 0) - return; + return false; if(batchRequestStepsArray.size() == maxNumberOfRequests) - return; + return false; for(MSBatchRequestStep requestStep: batchRequestStepsArray) { if(batchRequestStep.getRequestId().compareTo(requestStep.getRequestId()) == 0) - return; + return false; } - batchRequestStepsArray.add(batchRequestStep); + return batchRequestStepsArray.add(batchRequestStep); } public void removeBatchRequesStepWithId(String requestId) { @@ -74,7 +74,7 @@ private Map getBatchRequestMapFromRequestStep(MSBatchRequestStep contentmap.put("url", batchRequestStep.getRequest().getRequestLine().getUri()); contentmap.put("method", batchRequestStep.getRequest().getRequestLine().getMethod()); Header[] headers = batchRequestStep.getRequest().getAllHeaders(); - if(headers != null) { + if(headers != null && headers.length != 0) { JSONObject obj = new JSONObject(); for(Header header: headers) { obj.put(header.getName(), header.getValue()); diff --git a/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java b/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java index ac30ccb0e..cefea1fa7 100644 --- a/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java +++ b/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java @@ -1,5 +1,14 @@ package com.microsoft.graph.content; +import java.util.Set; + +import org.apache.http.Consts; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicHttpResponse; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -11,21 +20,48 @@ public class MSBatchResponseContent { public MSBatchResponseContent(String batchResponseData ) { JSONParser parser = new JSONParser(); try { - batchResponseObj = (JSONObject) parser.parse(batchResponseData); + if(batchResponseData != null) + batchResponseObj = (JSONObject) parser.parse(batchResponseData); } catch(ParseException e) { } } - public String getResponseById(String requestId) { - if(batchResponseObj.get(requestId) != null) - return batchResponseObj.get(requestId).toString(); - return null; + public HttpResponse getResponseById(String requestId) { + if(batchResponseObj == null) + return null; + + JSONArray responses = (JSONArray)batchResponseObj.get("responses"); + if(responses == null) + return null; + + for(Object response: responses) { + JSONObject jsonresponse = (JSONObject)response; + String id = (String)jsonresponse.get("id"); + if(id.compareTo(requestId) == 0) { + HttpResponse httpresponse = new BasicHttpResponse(null, ((Long)jsonresponse.get("status")).intValue(), null); + if(jsonresponse.get("body") != null) { + HttpEntity entity = new StringEntity(jsonresponse.get("body").toString(), ContentType.APPLICATION_JSON); + httpresponse.setEntity(entity); + } + if(jsonresponse.get("headers") != null){ + JSONObject jsonheaders = (JSONObject)jsonresponse.get("headers"); + for(Object key: jsonheaders.keySet()) { + String strkey = (String)key; + String strvalue = (String)jsonheaders.get(strkey); + httpresponse.setHeader(strkey, strvalue); + } + } + return httpresponse; + + } + } + return null; } public String getResponses() { if(batchResponseObj != null) - return batchResponseObj.toJSONString(); + return ((JSONArray)batchResponseObj.get("responses")).toJSONString(); return null; } } diff --git a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java index 6e1e7bcfd..0935868e0 100644 --- a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java @@ -58,9 +58,9 @@ public HttpUriRequest getRedirect( if(status != HttpStatus.SC_SEE_OTHER) { try { final URI requestURI = new URI(request.getRequestLine().getUri()); - if(!uri.getHost().equalsIgnoreCase(requestURI.getHost())) { + if(!uri.getHost().equalsIgnoreCase(requestURI.getHost()) || + !uri.getScheme().equalsIgnoreCase(requestURI.getScheme())) request.removeHeaders("Authorization"); - } return RequestBuilder.copy(request).setUri(uri).build(); } catch (final URISyntaxException ex) { diff --git a/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java b/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java new file mode 100644 index 000000000..390bb52b3 --- /dev/null +++ b/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java @@ -0,0 +1,60 @@ +package com.microsoft.graph.content; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.HttpRequest; +import org.apache.http.client.methods.HttpGet; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MSBatchRequestContentTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testMSBatchRequestContentCreation() { + + List requestStepArray = new ArrayList<>(); + for(int i=0;i<5;i++) { + HttpRequest request = new HttpGet("http://graph.microsoft.com"); + List arrayOfDependsOnIds = new ArrayList(); + MSBatchRequestStep requestStep = new MSBatchRequestStep("" + i, request, arrayOfDependsOnIds); + requestStepArray.add(requestStep); + } + MSBatchRequestContent requestContent = new MSBatchRequestContent(requestStepArray); + assertTrue(requestContent.getBatchRequestContent() != null); + } + + @Test + public void testGetBatchRequestContent() { + HttpRequest request = new HttpGet("http://graph.microsoft.com"); + List arrayOfDependsOnIds = new ArrayList(); + MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds); + MSBatchRequestContent requestContent = new MSBatchRequestContent(); + requestContent.addBatchRequestStep(requestStep); + String content = requestContent.getBatchRequestContent(); + String expectedContent = "{\"requests\":[{\"method\":\"GET\",\"dependsOn\":\"[]\",\"id\":\"1\",\"url\":\"http:\\/\\/graph.microsoft.com\"}]}"; + assertTrue(content.compareTo(expectedContent) == 0); + } + +} diff --git a/src/test/java/com/microsoft/graph/content/MSBatchRequestStepTest.java b/src/test/java/com/microsoft/graph/content/MSBatchRequestStepTest.java new file mode 100644 index 000000000..fa98ec302 --- /dev/null +++ b/src/test/java/com/microsoft/graph/content/MSBatchRequestStepTest.java @@ -0,0 +1,25 @@ +package com.microsoft.graph.content; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.HttpRequest; +import org.apache.http.client.methods.HttpGet; +import org.junit.Test; + +public class MSBatchRequestStepTest { + + @Test + public void testMSBatchRequestStepCreation() { + HttpRequest request = new HttpGet("http://graph.microsoft.com"); + List arrayOfDependsOnIds = new ArrayList(); + MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds); + assertTrue("Test BatchRequestStep creation", requestStep != null); + assertTrue("Test Request id", requestStep.getRequestId().compareTo("1") == 0); + assertTrue("Test Request object", requestStep.getRequest() == request); + assertTrue("Test Array of depends on Ids", requestStep.getArrayOfDependsOnIds() != null); + } + +} diff --git a/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java b/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java new file mode 100644 index 000000000..5f4bbac74 --- /dev/null +++ b/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java @@ -0,0 +1,40 @@ +package com.microsoft.graph.content; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Arrays; + +import org.apache.http.HttpResponse; +import org.apache.http.ParseException; +import org.apache.http.util.EntityUtils; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class MSBatchResponseContentTest { + + @Test + public void testNullMSBatchResponseContent() { + String responsedata = null; + MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata); + assertTrue(batchresponse.getResponses() == null); + } + + @Test + public void testValidMSBatchResponseContent() { + String responsedata = "{\"responses\": [{ \"id\": \"1\", \"status\": 302, \"headers\": { \"location\": \"https://b0mpua-by3301.files.1drv.com/y23vmagahszhxzlcvhasdhasghasodfi\" } }, { \"id\": \"3\", \"status\": 401, \"body\": { \"error\": { \"code\": \"Forbidden\", \"message\": \"...\" } } }, { \"id\": \"2\", \"status\": 200, \"body\": { \"@odata.context\": \"https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.plannerTask)\", \"value\": [] } }, { \"id\": \"4\", \"status\": 204, \"body\": null } ] }"; + MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata); + assertTrue(batchresponse.getResponses() != null); + } + + @Test + public void testGetMSBatchResponseContentByID() { + String responsedata = "{\"responses\": [{ \"id\": \"1\", \"status\": 302, \"headers\": { \"location\": \"https://b0mpua-by3301.files.1drv.com/y23vmagahszhxzlcvhasdhasghasodfi\" } }, { \"id\": \"3\", \"status\": 401, \"body\": { \"error\": { \"code\": \"Forbidden\", \"message\": \"...\" } } }, { \"id\": \"2\", \"status\": 200, \"body\": { \"@odata.context\": \"https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.plannerTask)\", \"value\": [] } }, { \"id\": \"4\", \"status\": 204, \"body\": null } ] }"; + MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata); + HttpResponse response = batchresponse.getResponseById("1"); + assertTrue(response != null); + } +} From 23518672e36cf6cc605f49c1bb1fb13dcf91bd24 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Wed, 16 Jan 2019 11:58:21 +0530 Subject: [PATCH 06/19] Add test files for Authentication handler and clean test file for batch request content --- .../content/MSBatchRequestContentTest.java | 16 ------- .../httpcore/AuthenticationHandlerTest.java | 47 +++++++++++++++++++ 2 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java diff --git a/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java b/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java index 390bb52b3..5013f0dae 100644 --- a/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java +++ b/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java @@ -15,22 +15,6 @@ public class MSBatchRequestContentTest { - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - @Before - public void setUp() throws Exception { - } - - @After - public void tearDown() throws Exception { - } - @Test public void testMSBatchRequestContentCreation() { diff --git a/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java new file mode 100644 index 000000000..fe3a68ccf --- /dev/null +++ b/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java @@ -0,0 +1,47 @@ +package com.microsoft.graph.httpcore; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import org.apache.http.Header; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.protocol.HttpClientContext; +import org.junit.Test; + +public class AuthenticationHandlerTest { + + static String token = "TEST-TOKEN"; + + public static class AuthProvider implements IAuthenticationProvider{ + public static String getToken() { + return "Bearer " + token; + } + public void authenticateRequest(HttpRequest request) { + // TODO Auto-generated method stub + request.addHeader("Authorization", AuthProvider.getToken()); + } + } + + @Test + public void testAuthenticationHandler() { + AuthProvider authProvider = new AuthProvider(); + AuthenticationHandler authHandler = new AuthenticationHandler(authProvider); + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); + HttpClientContext localContext = HttpClientContext.create(); + + try { + authHandler.process(httpget, localContext); + Header header = httpget.getFirstHeader("Authorization"); + assertTrue(header.getValue().equals("Bearer " + token)); + } catch (HttpException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + fail("Authentication handler failure"); + } + } + +} From bdc8a32e928a6de0ec00d2922bd56665465c7239 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Wed, 16 Jan 2019 13:07:28 +0530 Subject: [PATCH 07/19] Add test file for Redirect Handler middleware --- .../graph/httpcore/RedirectHandlerTest.java | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java diff --git a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java new file mode 100644 index 000000000..63c382a11 --- /dev/null +++ b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java @@ -0,0 +1,149 @@ +package com.microsoft.graph.httpcore; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.HttpVersion; +import org.apache.http.ProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.message.BasicHttpResponse; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RedirectHandlerTest { + + @Test + public void testIsRedirectedFailure() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); + HttpClientContext localContext = HttpClientContext.create(); + try { + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); + assertTrue(!isRedirected); + } catch (ProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + fail("Redirect handler isRedirect failure"); + } + } + + @Test + public void testIsRedirectedFailure1() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad Request"); + HttpClientContext localContext = HttpClientContext.create(); + try { + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); + assertTrue(!isRedirected); + } catch (ProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + fail("Redirect handler isRedirect failure"); + } + } + + @Test + public void testIsRedirectedSuccess() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + HttpClientContext localContext = HttpClientContext.create(); + try { + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); + assertTrue(isRedirected); + } catch (ProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + fail("Redirect handler isRedirect failure"); + } + } + + @Test + public void testGetRedirectForGetMethod() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + HttpClientContext localContext = HttpClientContext.create(); + try { + HttpRequest request = redirectHandler.getRedirect(httpget, response, localContext); + assertTrue(request != null); + final String method = request.getRequestLine().getMethod(); + assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); + } catch (ProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + fail("Redirect handler isRedirect failure"); + } + } + + @Test + public void testGetRedirectForHeadMethod() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpHead httphead = new HttpHead("https://graph.microsoft.com/v1.0/"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + HttpClientContext localContext = HttpClientContext.create(); + try { + HttpRequest request = redirectHandler.getRedirect(httphead, response, localContext); + assertTrue(request != null); + final String method = request.getRequestLine().getMethod(); + assertTrue(method.equalsIgnoreCase(HttpHead.METHOD_NAME)); + } catch (ProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + fail("Redirect handler isRedirect failure"); + } + } + + @Test + public void testGetRedirectForPostMethod() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + HttpClientContext localContext = HttpClientContext.create(); + try { + HttpRequest request = redirectHandler.getRedirect(httppost, response, localContext); + assertTrue(request != null); + final String method = request.getRequestLine().getMethod(); + assertTrue(method.equalsIgnoreCase(HttpPost.METHOD_NAME)); + } catch (ProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + fail("Redirect handler isRedirect failure"); + } + } + + @Test + public void testGetRedirectForPostMethod1() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_SEE_OTHER, "See Other"); + response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + HttpClientContext localContext = HttpClientContext.create(); + try { + HttpRequest request = redirectHandler.getRedirect(httppost, response, localContext); + assertTrue(request != null); + final String method = request.getRequestLine().getMethod(); + assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); + } catch (ProtocolException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + fail("Redirect handler isRedirect failure"); + } + } + +} From c86ca674d89869d010fbc8ec28e5cd8d411c9c07 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Wed, 16 Jan 2019 18:54:12 +0530 Subject: [PATCH 08/19] Add test file for retry handler middleware --- .../graph/httpcore/RetryHandlerTest.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java diff --git a/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java new file mode 100644 index 000000000..b35c8c464 --- /dev/null +++ b/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java @@ -0,0 +1,105 @@ +package com.microsoft.graph.httpcore; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.UnsupportedEncodingException; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.HttpVersion; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.protocol.HttpCoreContext; +import org.junit.Test; + +public class RetryHandlerTest { + + @Test + public void testRetryHandlerCreation() { + RetryHandler retryhandler = new RetryHandler(2, 2000); + assertTrue(retryhandler.getRetryInterval() == 2000); + } + + @Test + public void testRetryRequestWithMaxRetryAttempts() { + RetryHandler retryhandler = new RetryHandler(2, 2000); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout"); + HttpClientContext localContext = HttpClientContext.create(); + assertFalse(retryhandler.retryRequest(response, 3, localContext)); + } + + @Test + public void testRetryRequestForStatusCode() { + RetryHandler retryhandler = new RetryHandler(2, 2000); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error"); + HttpClientContext localContext = HttpClientContext.create(); + assertFalse(retryhandler.retryRequest(response, 1, localContext)); + } + + @Test + public void testRetryRequestWithTransferEncoding() { + RetryHandler retryhandler = new RetryHandler(2, 2000); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error"); + response.setHeader("Transfer-Encoding", "chunked"); + HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + + try { + HttpEntity entity = new StringEntity("TEST"); + httppost.setEntity(entity); + HttpClientContext localContext = HttpClientContext.create(); + localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, httppost); + assertFalse(retryhandler.retryRequest(response, 1, localContext)); + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + fail("Retry handler testRetryHandlerRetryRequest3 test failure"); + } + } + + @Test + public void testRetryRequestWithExponentialBackOff() { + RetryHandler retryhandler = new RetryHandler(2, 2000); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error"); + HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + + try { + HttpEntity entity = new StringEntity("TEST"); + httppost.setEntity(entity); + HttpClientContext localContext = HttpClientContext.create(); + localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, httppost); + assertTrue(retryhandler.retryRequest(response, 1, localContext)); + assertTrue(retryhandler.getRetryInterval() == 20); + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + fail("Retry handler testRetryHandlerRetryRequest3 test failure"); + } + } + + @Test + public void testRetryHandlerRetryRequestWithRetryAfterHeader() { + RetryHandler retryhandler = new RetryHandler(2, 2000); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error"); + response.setHeader("Retry-After", "100"); + HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + + try { + HttpEntity entity = new StringEntity("TEST"); + httppost.setEntity(entity); + HttpClientContext localContext = HttpClientContext.create(); + localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, httppost); + assertTrue(retryhandler.retryRequest(response, 1, localContext)); + assertTrue(retryhandler.getRetryInterval() == 100); + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + fail("Retry handler testRetryHandlerRetryRequestWithRetryAfterHeader test failure"); + } + } + +} From b38f97793267dd9e4a44e6ebd99591adff1172a6 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Thu, 17 Jan 2019 18:07:11 +0530 Subject: [PATCH 09/19] Updating test files for middlewares and HttpClient --- .../microsoft/graph/httpcore/HttpClients.java | 13 ----------- .../httpcore/AuthenticationHandlerTest.java | 2 -- .../graph/httpcore/HttpClientsTest.java | 23 +++++++++++++++++++ .../graph/httpcore/RedirectHandlerTest.java | 10 -------- 4 files changed, 23 insertions(+), 25 deletions(-) create mode 100644 src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java diff --git a/src/main/java/com/microsoft/graph/httpcore/HttpClients.java b/src/main/java/com/microsoft/graph/httpcore/HttpClients.java index bf0a60f92..e51eb5921 100644 --- a/src/main/java/com/microsoft/graph/httpcore/HttpClients.java +++ b/src/main/java/com/microsoft/graph/httpcore/HttpClients.java @@ -17,19 +17,6 @@ public static HttpClientBuilder custom() { return HttpClientBuilder.create(); } - /** - * Creates {@link CloseableHttpClient} instance with default - * configuration. - */ - public static CloseableHttpClient createDefault() { - RequestConfig config = RequestConfig.custom().setMaxRedirects(5).build(); - return HttpClientBuilder.create().addInterceptorFirst(new AuthenticationHandler(null)) - .setRedirectStrategy(new RedirectHandler()) - .setServiceUnavailableRetryStrategy(new RetryHandler()) - .setDefaultRequestConfig(config) - .build(); - } - /** * Creates {@link CloseableHttpClient} instance with default * configuration and provided authProvider diff --git a/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java index fe3a68ccf..1cfd6abe3 100644 --- a/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java @@ -21,7 +21,6 @@ public static String getToken() { return "Bearer " + token; } public void authenticateRequest(HttpRequest request) { - // TODO Auto-generated method stub request.addHeader("Authorization", AuthProvider.getToken()); } } @@ -38,7 +37,6 @@ public void testAuthenticationHandler() { Header header = httpget.getFirstHeader("Authorization"); assertTrue(header.getValue().equals("Bearer " + token)); } catch (HttpException | IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); fail("Authentication handler failure"); } diff --git a/src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java b/src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java new file mode 100644 index 000000000..474ccfc15 --- /dev/null +++ b/src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java @@ -0,0 +1,23 @@ +package com.microsoft.graph.httpcore; + +import static org.junit.Assert.assertTrue; + +import org.apache.http.HttpRequest; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.Test; + +public class HttpClientsTest { + + @Test + public void testHttpClientCreation() { + IAuthenticationProvider authprovider = new IAuthenticationProvider() { + @Override + public void authenticateRequest(HttpRequest request) { + request.addHeader("Authorization", "TOKEN"); + } + }; + CloseableHttpClient httpclient = HttpClients.createDefault(authprovider); + assertTrue(httpclient != null); + } + +} diff --git a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java index 63c382a11..fb433276e 100644 --- a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java @@ -13,10 +13,6 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.message.BasicHttpResponse; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; public class RedirectHandlerTest { @@ -31,7 +27,6 @@ public void testIsRedirectedFailure() { boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); assertTrue(!isRedirected); } catch (ProtocolException e) { - // TODO Auto-generated catch block e.printStackTrace(); fail("Redirect handler isRedirect failure"); } @@ -47,7 +42,6 @@ public void testIsRedirectedFailure1() { boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); assertTrue(!isRedirected); } catch (ProtocolException e) { - // TODO Auto-generated catch block e.printStackTrace(); fail("Redirect handler isRedirect failure"); } @@ -64,7 +58,6 @@ public void testIsRedirectedSuccess() { boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); assertTrue(isRedirected); } catch (ProtocolException e) { - // TODO Auto-generated catch block e.printStackTrace(); fail("Redirect handler isRedirect failure"); } @@ -83,7 +76,6 @@ public void testGetRedirectForGetMethod() { final String method = request.getRequestLine().getMethod(); assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); } catch (ProtocolException e) { - // TODO Auto-generated catch block e.printStackTrace(); fail("Redirect handler isRedirect failure"); } @@ -102,7 +94,6 @@ public void testGetRedirectForHeadMethod() { final String method = request.getRequestLine().getMethod(); assertTrue(method.equalsIgnoreCase(HttpHead.METHOD_NAME)); } catch (ProtocolException e) { - // TODO Auto-generated catch block e.printStackTrace(); fail("Redirect handler isRedirect failure"); } @@ -140,7 +131,6 @@ public void testGetRedirectForPostMethod1() { final String method = request.getRequestLine().getMethod(); assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); } catch (ProtocolException e) { - // TODO Auto-generated catch block e.printStackTrace(); fail("Redirect handler isRedirect failure"); } From 266331ea927d328cf6ef29e4af836ba1c299dc04 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Thu, 17 Jan 2019 18:07:59 +0530 Subject: [PATCH 10/19] Updating build setting to include apache httpclient as an API dependency --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 53d3467ec..ab9da0ab6 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ dependencies { testImplementation 'junit:junit:4.12' // Use Apache HttpClient - compile 'org.apache.httpcomponents:httpclient:4.5.6' + api 'org.apache.httpcomponents:httpclient:4.5.6' // https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1' From 0ecb41c1bf0348fdcbc5b6e9032f6a2241e1ae8e Mon Sep 17 00:00:00 2001 From: deagrawa Date: Thu, 17 Jan 2019 18:40:52 +0530 Subject: [PATCH 11/19] Update variables used in Retry and Redirect handler tests. --- .../graph/httpcore/RedirectHandlerTest.java | 42 ++++++++++--------- .../graph/httpcore/RetryHandlerTest.java | 24 ++++++----- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java index fb433276e..8e6a3bfa5 100644 --- a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java @@ -16,11 +16,14 @@ import org.junit.Test; public class RedirectHandlerTest { + + String testmeurl = "https://graph.microsoft.com/v1.0/me/"; + String testurl = "https://graph.microsoft.com/v1.0/"; @Test public void testIsRedirectedFailure() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; - HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); + HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); HttpClientContext localContext = HttpClientContext.create(); try { @@ -28,14 +31,14 @@ public void testIsRedirectedFailure() { assertTrue(!isRedirected); } catch (ProtocolException e) { e.printStackTrace(); - fail("Redirect handler isRedirect failure"); + fail("Redirect handler testIsRedirectedFailure failure"); } } @Test public void testIsRedirectedFailure1() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; - HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); + HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad Request"); HttpClientContext localContext = HttpClientContext.create(); try { @@ -43,32 +46,32 @@ public void testIsRedirectedFailure1() { assertTrue(!isRedirected); } catch (ProtocolException e) { e.printStackTrace(); - fail("Redirect handler isRedirect failure"); + fail("Redirect handler testIsRedirectedFailure1 failure"); } } @Test public void testIsRedirectedSuccess() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; - HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/me/"); + HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); - response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + response.setHeader("location", testmeurl); HttpClientContext localContext = HttpClientContext.create(); try { boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); assertTrue(isRedirected); } catch (ProtocolException e) { e.printStackTrace(); - fail("Redirect handler isRedirect failure"); + fail("Redirect handler testIsRedirectedSuccess failure"); } } @Test public void testGetRedirectForGetMethod() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; - HttpGet httpget = new HttpGet("https://graph.microsoft.com/v1.0/"); + HttpGet httpget = new HttpGet(testurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); - response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + response.setHeader("location", testmeurl); HttpClientContext localContext = HttpClientContext.create(); try { HttpRequest request = redirectHandler.getRedirect(httpget, response, localContext); @@ -77,16 +80,16 @@ public void testGetRedirectForGetMethod() { assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); } catch (ProtocolException e) { e.printStackTrace(); - fail("Redirect handler isRedirect failure"); + fail("Redirect handler testGetRedirectForGetMethod failure"); } } @Test public void testGetRedirectForHeadMethod() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; - HttpHead httphead = new HttpHead("https://graph.microsoft.com/v1.0/"); + HttpHead httphead = new HttpHead(testurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); - response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + response.setHeader("location", testmeurl); HttpClientContext localContext = HttpClientContext.create(); try { HttpRequest request = redirectHandler.getRedirect(httphead, response, localContext); @@ -95,16 +98,16 @@ public void testGetRedirectForHeadMethod() { assertTrue(method.equalsIgnoreCase(HttpHead.METHOD_NAME)); } catch (ProtocolException e) { e.printStackTrace(); - fail("Redirect handler isRedirect failure"); + fail("Redirect handler testGetRedirectForHeadMethod failure"); } } @Test public void testGetRedirectForPostMethod() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; - HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + HttpPost httppost = new HttpPost(testurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); - response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + response.setHeader("location", testmeurl); HttpClientContext localContext = HttpClientContext.create(); try { HttpRequest request = redirectHandler.getRedirect(httppost, response, localContext); @@ -112,18 +115,17 @@ public void testGetRedirectForPostMethod() { final String method = request.getRequestLine().getMethod(); assertTrue(method.equalsIgnoreCase(HttpPost.METHOD_NAME)); } catch (ProtocolException e) { - // TODO Auto-generated catch block e.printStackTrace(); - fail("Redirect handler isRedirect failure"); + fail("Redirect handler testGetRedirectForPostMethod failure"); } } @Test public void testGetRedirectForPostMethod1() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; - HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + HttpPost httppost = new HttpPost(testurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_SEE_OTHER, "See Other"); - response.setHeader("location", "https://graph.microsoft.com/v1.0/me/"); + response.setHeader("location", testmeurl); HttpClientContext localContext = HttpClientContext.create(); try { HttpRequest request = redirectHandler.getRedirect(httppost, response, localContext); @@ -132,7 +134,7 @@ public void testGetRedirectForPostMethod1() { assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); } catch (ProtocolException e) { e.printStackTrace(); - fail("Redirect handler isRedirect failure"); + fail("Redirect handler testGetRedirectForPostMethod1 failure"); } } diff --git a/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java index b35c8c464..4768bcdc9 100644 --- a/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java @@ -18,16 +18,20 @@ import org.junit.Test; public class RetryHandlerTest { + + int maxRetries = 2; + int retryInterval = 2000; + String testurl = "https://graph.microsoft.com/v1.0/"; @Test public void testRetryHandlerCreation() { - RetryHandler retryhandler = new RetryHandler(2, 2000); - assertTrue(retryhandler.getRetryInterval() == 2000); + RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval); + assertTrue(retryhandler.getRetryInterval() == retryInterval); } @Test public void testRetryRequestWithMaxRetryAttempts() { - RetryHandler retryhandler = new RetryHandler(2, 2000); + RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout"); HttpClientContext localContext = HttpClientContext.create(); assertFalse(retryhandler.retryRequest(response, 3, localContext)); @@ -35,7 +39,7 @@ public void testRetryRequestWithMaxRetryAttempts() { @Test public void testRetryRequestForStatusCode() { - RetryHandler retryhandler = new RetryHandler(2, 2000); + RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error"); HttpClientContext localContext = HttpClientContext.create(); assertFalse(retryhandler.retryRequest(response, 1, localContext)); @@ -43,10 +47,10 @@ public void testRetryRequestForStatusCode() { @Test public void testRetryRequestWithTransferEncoding() { - RetryHandler retryhandler = new RetryHandler(2, 2000); + RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error"); response.setHeader("Transfer-Encoding", "chunked"); - HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + HttpPost httppost = new HttpPost(testurl); try { HttpEntity entity = new StringEntity("TEST"); @@ -63,9 +67,9 @@ public void testRetryRequestWithTransferEncoding() { @Test public void testRetryRequestWithExponentialBackOff() { - RetryHandler retryhandler = new RetryHandler(2, 2000); + RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error"); - HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + HttpPost httppost = new HttpPost(testurl); try { HttpEntity entity = new StringEntity("TEST"); @@ -83,10 +87,10 @@ public void testRetryRequestWithExponentialBackOff() { @Test public void testRetryHandlerRetryRequestWithRetryAfterHeader() { - RetryHandler retryhandler = new RetryHandler(2, 2000); + RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error"); response.setHeader("Retry-After", "100"); - HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/"); + HttpPost httppost = new HttpPost(testurl); try { HttpEntity entity = new StringEntity("TEST"); From 9b38515579db638cbf832104d07d76e741b366ba Mon Sep 17 00:00:00 2001 From: deagrawa Date: Fri, 18 Jan 2019 12:47:25 +0530 Subject: [PATCH 12/19] Update getRedirect logic in RedirectHandler --- .../graph/httpcore/RedirectHandler.java | 43 ++++++++----------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java index 0935868e0..3b8110bf0 100644 --- a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java @@ -44,30 +44,23 @@ public boolean isRedirected( @Override public HttpUriRequest getRedirect( - final HttpRequest request, - final HttpResponse response, - final HttpContext context) throws ProtocolException { - final URI uri = getLocationURI(request, response, context); - final String method = request.getRequestLine().getMethod(); - if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) { - return new HttpHead(uri); - } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) - return new HttpGet(uri); - else { - final int status = response.getStatusLine().getStatusCode(); - if(status != HttpStatus.SC_SEE_OTHER) { - try { - final URI requestURI = new URI(request.getRequestLine().getUri()); - if(!uri.getHost().equalsIgnoreCase(requestURI.getHost()) || - !uri.getScheme().equalsIgnoreCase(requestURI.getScheme())) - request.removeHeaders("Authorization"); - return RequestBuilder.copy(request).setUri(uri).build(); - } - catch (final URISyntaxException ex) { - throw new ProtocolException(ex.getMessage(), ex); - } - } - return new HttpGet(uri); - } + final HttpRequest request, + final HttpResponse response, + final HttpContext context) throws ProtocolException { + final URI uri = getLocationURI(request, response, context); + try { + final URI requestURI = new URI(request.getRequestLine().getUri()); + if(!uri.getHost().equalsIgnoreCase(requestURI.getHost()) || + !uri.getScheme().equalsIgnoreCase(requestURI.getScheme())) + request.removeHeaders("Authorization"); + } + catch (final URISyntaxException ex) { + throw new ProtocolException(ex.getMessage(), ex); + } + + final int status = response.getStatusLine().getStatusCode(); + if(status == HttpStatus.SC_SEE_OTHER) + return new HttpGet(uri); + return RequestBuilder.copy(request).setUri(uri).build(); } } From f1c1de5c3081fdd2c3fcedfe7355166ea7b4dfdc Mon Sep 17 00:00:00 2001 From: deagrawa Date: Fri, 18 Jan 2019 12:56:33 +0530 Subject: [PATCH 13/19] Add support to redirect when status code received is 308 in RedirectHandler --- .../java/com/microsoft/graph/httpcore/RedirectHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java index 3b8110bf0..1fe483272 100644 --- a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java @@ -36,7 +36,8 @@ public boolean isRedirected( if(statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT || - statusCode == HttpStatus.SC_SEE_OTHER) + statusCode == HttpStatus.SC_SEE_OTHER || + statusCode == 308) return true; return false; From 6ef44658fc981ef3aff5340bba83840b3161535a Mon Sep 17 00:00:00 2001 From: deagrawa Date: Fri, 18 Jan 2019 12:59:43 +0530 Subject: [PATCH 14/19] BatchResponseContent - remove unsed import statements BatchRequestContent - parametrize object creation of collection classes. --- .../com/microsoft/graph/content/MSBatchRequestContent.java | 6 +++--- .../com/microsoft/graph/content/MSBatchResponseContent.java | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java index 8174306aa..2612cafb4 100644 --- a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java +++ b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java @@ -59,8 +59,8 @@ public void removeBatchRequesStepWithId(String requestId) { } public String getBatchRequestContent() { - Map>> batchRequestContentMap = new HashMap(); - List> batchContentArray = new ArrayList(); + Map>> batchRequestContentMap = new HashMap<>(); + List> batchContentArray = new ArrayList<>(); for(MSBatchRequestStep requestStep : batchRequestStepsArray) { batchContentArray.add(getBatchRequestMapFromRequestStep(requestStep)); } @@ -69,7 +69,7 @@ public String getBatchRequestContent() { } private Map getBatchRequestMapFromRequestStep(MSBatchRequestStep batchRequestStep){ - Map contentmap = new HashMap(); + Map contentmap = new HashMap<>(); contentmap.put("id", batchRequestStep.getRequestId()); contentmap.put("url", batchRequestStep.getRequest().getRequestLine().getUri()); contentmap.put("method", batchRequestStep.getRequest().getRequestLine().getMethod()); diff --git a/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java b/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java index cefea1fa7..06aef03fe 100644 --- a/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java +++ b/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java @@ -1,8 +1,5 @@ package com.microsoft.graph.content; -import java.util.Set; - -import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.entity.ContentType; From 3df3bd3e37311002247c64c15e309e78d1e03c15 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Fri, 18 Jan 2019 13:23:36 +0530 Subject: [PATCH 15/19] Update RetryHandler options like delay and retryInterval --- .../java/com/microsoft/graph/httpcore/RetryHandler.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java b/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java index b49b26a8a..d0a7f6901 100644 --- a/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java @@ -26,7 +26,7 @@ public class RetryHandler implements ServiceUnavailableRetryStrategy{ * value is 1 second. */ private long retryInterval; - private final int DELAY_SECONDS = 10; + private final int DELAY_MILLISECONDS = 1000; private final String RETRY_AFTER = "Retry-After"; private final String TRANSFER_ENCODING = "Transfer-Encoding"; @@ -43,7 +43,7 @@ public RetryHandler(final int maxRetries, final int retryInterval) { } public RetryHandler() { - this(1, 1000); + this(2, 1000); } @Override @@ -57,14 +57,13 @@ public boolean retryRequest(HttpResponse response, int executionCount, HttpConte if(header != null) retryInterval = Long.parseLong(header.getValue()); else - retryInterval = (long)Math.pow(2.0, (double)executionCount) * DELAY_SECONDS; + retryInterval = (long)Math.pow(2.0, (double)executionCount) * DELAY_MILLISECONDS; } return shouldRetry; } @Override public long getRetryInterval() { - // TODO Auto-generated method stub return retryInterval; } From 84a405a0619e2cf56d8178688563a80d719a28c6 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Mon, 21 Jan 2019 11:18:38 +0530 Subject: [PATCH 16/19] Fix review comments on PR: https://github.com/microsoftgraph/msgraph-sdk-java-core/pull/2 --- .../java/com/microsoft/graph/content/MSBatchRequestContent.java | 2 +- .../com/microsoft/graph/content/MSBatchResponseContent.java | 1 + .../com/microsoft/graph/httpcore/AuthenticationHandler.java | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java index 2612cafb4..b84c94521 100644 --- a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java +++ b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java @@ -93,9 +93,9 @@ private Map getBatchRequestMapFromRequestStep(MSBatchRequestStep contentmap.put("body", body); } catch(Exception e) { + e.printStackTrace(); } } - List arrayOfDependsOnIds = batchRequestStep.getArrayOfDependsOnIds(); if(arrayOfDependsOnIds != null) { contentmap.put("dependsOn", JSONValue.toJSONString(arrayOfDependsOnIds)); diff --git a/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java b/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java index 06aef03fe..42c0f44dc 100644 --- a/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java +++ b/src/main/java/com/microsoft/graph/content/MSBatchResponseContent.java @@ -21,6 +21,7 @@ public MSBatchResponseContent(String batchResponseData ) { batchResponseObj = (JSONObject) parser.parse(batchResponseData); } catch(ParseException e) { + e.printStackTrace(); } } diff --git a/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java b/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java index 82747d1ee..1657df1d3 100644 --- a/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java @@ -17,7 +17,6 @@ public AuthenticationHandler(IAuthenticationProvider authProvider) { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { - // TODO Auto-generated method stub authProvider.authenticateRequest(request); } From 75355258e7447d8a18fe54bb19d2062c38e1bd1d Mon Sep 17 00:00:00 2001 From: deagrawa Date: Mon, 21 Jan 2019 11:51:49 +0530 Subject: [PATCH 17/19] Fixing review comments on PR : https://github.com/microsoftgraph/msgraph-sdk-java-core/pull/2 --- .../microsoft/graph/content/MSBatchRequestContent.java | 9 +++++++-- .../microsoft/graph/httpcore/AuthenticationHandler.java | 3 ++- .../graph/httpcore/IAuthenticationProvider.java | 6 +++--- .../graph/httpcore/AuthenticationHandlerTest.java | 7 ++----- .../com/microsoft/graph/httpcore/HttpClientsTest.java | 5 ++--- .../com/microsoft/graph/httpcore/RetryHandlerTest.java | 2 +- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java index b84c94521..17040beea 100644 --- a/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java +++ b/src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java @@ -41,7 +41,8 @@ public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) { return batchRequestStepsArray.add(batchRequestStep); } - public void removeBatchRequesStepWithId(String requestId) { + public boolean removeBatchRequesStepWithId(String requestId) { + boolean ret = false; for (int i = batchRequestStepsArray.size()-1; i >= 0; i--) { MSBatchRequestStep requestStep = batchRequestStepsArray.get(i); @@ -51,11 +52,15 @@ public void removeBatchRequesStepWithId(String requestId) { if(dependsOnId.compareTo(requestId) == 0) { requestStep.getArrayOfDependsOnIds().remove(j); + ret = true; } } - if(requestId.compareTo(requestStep.getRequestId()) == 0) + if(requestId.compareTo(requestStep.getRequestId()) == 0) { batchRequestStepsArray.remove(i); + ret = true; + } } + return ret; } public String getBatchRequestContent() { diff --git a/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java b/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java index 1657df1d3..de09aa8ff 100644 --- a/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/AuthenticationHandler.java @@ -17,7 +17,8 @@ public AuthenticationHandler(IAuthenticationProvider authProvider) { @Override public void process(HttpRequest request, HttpContext context) throws HttpException, IOException { - authProvider.authenticateRequest(request); + String token = authProvider.getAccessToken(); + request.addHeader("Authorization", "Bearer " + token); } } diff --git a/src/main/java/com/microsoft/graph/httpcore/IAuthenticationProvider.java b/src/main/java/com/microsoft/graph/httpcore/IAuthenticationProvider.java index c969cfe11..26e174155 100644 --- a/src/main/java/com/microsoft/graph/httpcore/IAuthenticationProvider.java +++ b/src/main/java/com/microsoft/graph/httpcore/IAuthenticationProvider.java @@ -4,9 +4,9 @@ public interface IAuthenticationProvider { /** - * Authenticates the request + * Get Access Token * - * @param request the request to authenticate */ - void authenticateRequest(final HttpRequest request); + + String getAccessToken(); } diff --git a/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java index 1cfd6abe3..6f3e44bf6 100644 --- a/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/AuthenticationHandlerTest.java @@ -17,11 +17,8 @@ public class AuthenticationHandlerTest { static String token = "TEST-TOKEN"; public static class AuthProvider implements IAuthenticationProvider{ - public static String getToken() { - return "Bearer " + token; - } - public void authenticateRequest(HttpRequest request) { - request.addHeader("Authorization", AuthProvider.getToken()); + public String getAccessToken() { + return token; } } diff --git a/src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java b/src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java index 474ccfc15..fa7ad43e4 100644 --- a/src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/HttpClientsTest.java @@ -2,7 +2,6 @@ import static org.junit.Assert.assertTrue; -import org.apache.http.HttpRequest; import org.apache.http.impl.client.CloseableHttpClient; import org.junit.Test; @@ -12,8 +11,8 @@ public class HttpClientsTest { public void testHttpClientCreation() { IAuthenticationProvider authprovider = new IAuthenticationProvider() { @Override - public void authenticateRequest(HttpRequest request) { - request.addHeader("Authorization", "TOKEN"); + public String getAccessToken() { + return "TOKEN"; } }; CloseableHttpClient httpclient = HttpClients.createDefault(authprovider); diff --git a/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java index 4768bcdc9..63240e40d 100644 --- a/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java @@ -77,7 +77,7 @@ public void testRetryRequestWithExponentialBackOff() { HttpClientContext localContext = HttpClientContext.create(); localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, httppost); assertTrue(retryhandler.retryRequest(response, 1, localContext)); - assertTrue(retryhandler.getRetryInterval() == 20); + assertTrue(retryhandler.getRetryInterval() == 2000); } catch (UnsupportedEncodingException e) { e.printStackTrace(); From 05ab9f4a0db680632b95cab10bf1fd824db87a58 Mon Sep 17 00:00:00 2001 From: deagrawa Date: Mon, 21 Jan 2019 16:05:09 +0530 Subject: [PATCH 18/19] Add more test cases for Content classes and Redirect Handler to increase code coverage to more than 90% --- build.gradle | 1 + .../content/MSBatchRequestContentTest.java | 58 +++++++++++++-- .../content/MSBatchResponseContentTest.java | 23 ++++++ .../graph/httpcore/RedirectHandlerTest.java | 72 +++++++++++++++++++ 4 files changed, 149 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index ab9da0ab6..faa8338ca 100644 --- a/build.gradle +++ b/build.gradle @@ -8,6 +8,7 @@ // Apply the java-library plugin to add support for Java Library apply plugin: 'java-library' +apply plugin: 'jacoco' // In this section you declare where to find the dependencies of your project repositories { diff --git a/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java b/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java index 5013f0dae..7f923c63d 100644 --- a/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java +++ b/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java @@ -15,13 +15,14 @@ public class MSBatchRequestContentTest { + String testurl = "http://graph.microsoft.com"; + @Test public void testMSBatchRequestContentCreation() { - List requestStepArray = new ArrayList<>(); for(int i=0;i<5;i++) { - HttpRequest request = new HttpGet("http://graph.microsoft.com"); - List arrayOfDependsOnIds = new ArrayList(); + HttpRequest request = new HttpGet(testurl); + List arrayOfDependsOnIds = new ArrayList<>(); MSBatchRequestStep requestStep = new MSBatchRequestStep("" + i, request, arrayOfDependsOnIds); requestStepArray.add(requestStep); } @@ -31,8 +32,8 @@ public void testMSBatchRequestContentCreation() { @Test public void testGetBatchRequestContent() { - HttpRequest request = new HttpGet("http://graph.microsoft.com"); - List arrayOfDependsOnIds = new ArrayList(); + HttpRequest request = new HttpGet(testurl); + List arrayOfDependsOnIds = new ArrayList<>(); MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds); MSBatchRequestContent requestContent = new MSBatchRequestContent(); requestContent.addBatchRequestStep(requestStep); @@ -40,5 +41,52 @@ public void testGetBatchRequestContent() { String expectedContent = "{\"requests\":[{\"method\":\"GET\",\"dependsOn\":\"[]\",\"id\":\"1\",\"url\":\"http:\\/\\/graph.microsoft.com\"}]}"; assertTrue(content.compareTo(expectedContent) == 0); } + + @Test + public void testGetBatchRequestContentWithHeader() { + HttpRequest request = new HttpGet(testurl); + request.setHeader("testkey", "testvalue"); + List arrayOfDependsOnIds = new ArrayList<>(); + MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds); + MSBatchRequestContent requestContent = new MSBatchRequestContent(); + requestContent.addBatchRequestStep(requestStep); + String content = requestContent.getBatchRequestContent(); + String expectedContent = "{\"requests\":[{\"headers\":\"{\\\"testkey\\\":\\\"testvalue\\\"}\",\"method\":\"GET\",\"dependsOn\":\"[]\",\"id\":\"1\",\"url\":\"http:\\/\\/graph.microsoft.com\"}]}"; + assertTrue(content.compareTo(expectedContent) == 0); + } + + @Test + public void testRemoveBatchRequesStepWithId() { + HttpRequest request = new HttpGet(testurl); + List arrayOfDependsOnIds = new ArrayList<>(); + MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds); + MSBatchRequestContent requestContent = new MSBatchRequestContent(); + requestContent.addBatchRequestStep(requestStep); + requestContent.removeBatchRequesStepWithId("1"); + String content = requestContent.getBatchRequestContent(); + String expectedContent = "{\"requests\":[]}"; + assertTrue(content.compareTo(expectedContent) == 0); + } + + @Test + public void testRemoveBatchRequesStepWithId1() { + HttpRequest request = new HttpGet(testurl); + List arrayOfDependsOnIds = new ArrayList<>(); + MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds); + + HttpRequest request1 = new HttpGet(testurl); + List arrayOfDependsOnIds1 = new ArrayList<>(); + arrayOfDependsOnIds1.add("1"); + MSBatchRequestStep requestStep1 = new MSBatchRequestStep("2", request1, arrayOfDependsOnIds1); + + MSBatchRequestContent requestContent = new MSBatchRequestContent(); + requestContent.addBatchRequestStep(requestStep); + requestContent.addBatchRequestStep(requestStep1); + + requestContent.removeBatchRequesStepWithId("1"); + String content = requestContent.getBatchRequestContent(); + String expectedContent = "{\"requests\":[{\"method\":\"GET\",\"dependsOn\":\"[]\",\"id\":\"2\",\"url\":\"http:\\/\\/graph.microsoft.com\"}]}"; + assertTrue(content.compareTo(expectedContent) == 0); + } } diff --git a/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java b/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java index 5f4bbac74..da393580f 100644 --- a/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java +++ b/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java @@ -30,6 +30,29 @@ public void testValidMSBatchResponseContent() { assertTrue(batchresponse.getResponses() != null); } + @Test + public void testInvalidMSBatchResponseContent() { + //passing empty responses + String responsedata = "{\"responses\": [] }"; + MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata); + assertTrue(batchresponse.getResponseById("1") == null); + } + + @Test + public void testInvalidMSBatchResponseContent1() { + //passing null response json string + MSBatchResponseContent batchresponse = new MSBatchResponseContent(null); + assertTrue(batchresponse.getResponseById("1") == null); + } + + @Test + public void testInvalidMSBatchResponseContent2() { + //passing malformed json response + String invalidResponsedata = "{responses: [] }"; + MSBatchResponseContent batchresponse = new MSBatchResponseContent(invalidResponsedata); + assertTrue(batchresponse.getResponses() == null); + } + @Test public void testGetMSBatchResponseContentByID() { String responsedata = "{\"responses\": [{ \"id\": \"1\", \"status\": 302, \"headers\": { \"location\": \"https://b0mpua-by3301.files.1drv.com/y23vmagahszhxzlcvhasdhasghasodfi\" } }, { \"id\": \"3\", \"status\": 401, \"body\": { \"error\": { \"code\": \"Forbidden\", \"message\": \"...\" } } }, { \"id\": \"2\", \"status\": 200, \"body\": { \"@odata.context\": \"https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.plannerTask)\", \"value\": [] } }, { \"id\": \"4\", \"status\": 204, \"body\": null } ] }"; diff --git a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java index 8e6a3bfa5..5bee73cfb 100644 --- a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.apache.http.Header; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; @@ -19,6 +20,7 @@ public class RedirectHandlerTest { String testmeurl = "https://graph.microsoft.com/v1.0/me/"; String testurl = "https://graph.microsoft.com/v1.0/"; + String differenthosturl = "https://graph.abc.com/v1.0/"; @Test public void testIsRedirectedFailure() { @@ -40,6 +42,7 @@ public void testIsRedirectedFailure1() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad Request"); + response.setHeader("location", testmeurl); HttpClientContext localContext = HttpClientContext.create(); try { boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); @@ -66,6 +69,54 @@ public void testIsRedirectedSuccess() { } } + @Test + public void testIsRedirectedSuccess1() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpGet httpget = new HttpGet(testmeurl); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_PERMANENTLY, "Moved Permanently"); + response.setHeader("location", testmeurl); + HttpClientContext localContext = HttpClientContext.create(); + try { + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); + assertTrue(isRedirected); + } catch (ProtocolException e) { + e.printStackTrace(); + fail("Redirect handler testIsRedirectedSuccess1 failure"); + } + } + + @Test + public void testIsRedirectedSuccess2() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpGet httpget = new HttpGet(testmeurl); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_TEMPORARY_REDIRECT, "Temporary Redirect"); + response.setHeader("location", testmeurl); + HttpClientContext localContext = HttpClientContext.create(); + try { + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); + assertTrue(isRedirected); + } catch (ProtocolException e) { + e.printStackTrace(); + fail("Redirect handler testIsRedirectedSuccess2 failure"); + } + } + + @Test + public void testIsRedirectedSuccess3() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpGet httpget = new HttpGet(testmeurl); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_SEE_OTHER, "See Other"); + response.setHeader("location", testmeurl); + HttpClientContext localContext = HttpClientContext.create(); + try { + boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext); + assertTrue(isRedirected); + } catch (ProtocolException e) { + e.printStackTrace(); + fail("Redirect handler testIsRedirectedSuccess3 failure"); + } + } + @Test public void testGetRedirectForGetMethod() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; @@ -83,6 +134,27 @@ public void testGetRedirectForGetMethod() { fail("Redirect handler testGetRedirectForGetMethod failure"); } } + + @Test + public void testGetRedirectForGetMethodForAuthHeader() { + RedirectHandler redirectHandler = RedirectHandler.INSTANCE; + HttpGet httpget = new HttpGet(testurl); + httpget.addHeader("Authorization", "TOKEN"); + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); + response.setHeader("location", differenthosturl); + HttpClientContext localContext = HttpClientContext.create(); + try { + HttpRequest request = redirectHandler.getRedirect(httpget, response, localContext); + assertTrue(request != null); + final String method = request.getRequestLine().getMethod(); + assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME)); + Header header = request.getFirstHeader("Authorization"); + assertTrue(header == null); + } catch (ProtocolException e) { + e.printStackTrace(); + fail("Redirect handler testGetRedirectForGetMethodForAuthHeader failure"); + } + } @Test public void testGetRedirectForHeadMethod() { From ae3ced17420b199840e446ad52311b13c2e1a82e Mon Sep 17 00:00:00 2001 From: deagrawa Date: Mon, 21 Jan 2019 17:01:20 +0530 Subject: [PATCH 19/19] Give meaningful names to test methods in test classes. --- .../graph/content/MSBatchRequestContentTest.java | 2 +- .../graph/content/MSBatchResponseContentTest.java | 9 +++------ .../graph/httpcore/RedirectHandlerTest.java | 14 +++++++------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java b/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java index 7f923c63d..c15448051 100644 --- a/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java +++ b/src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java @@ -69,7 +69,7 @@ public void testRemoveBatchRequesStepWithId() { } @Test - public void testRemoveBatchRequesStepWithId1() { + public void testRemoveBatchRequesStepWithIdByAddingMultipleBatchSteps() { HttpRequest request = new HttpGet(testurl); List arrayOfDependsOnIds = new ArrayList<>(); MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds); diff --git a/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java b/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java index da393580f..bf9652aff 100644 --- a/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java +++ b/src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java @@ -31,23 +31,20 @@ public void testValidMSBatchResponseContent() { } @Test - public void testInvalidMSBatchResponseContent() { - //passing empty responses + public void testInvalidMSBatchResponseContentWithEmptyResponse() { String responsedata = "{\"responses\": [] }"; MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata); assertTrue(batchresponse.getResponseById("1") == null); } @Test - public void testInvalidMSBatchResponseContent1() { - //passing null response json string + public void testInvalidMSBatchResponseContentWithNullResponseString() { MSBatchResponseContent batchresponse = new MSBatchResponseContent(null); assertTrue(batchresponse.getResponseById("1") == null); } @Test - public void testInvalidMSBatchResponseContent2() { - //passing malformed json response + public void testInvalidMSBatchResponseContentWithMalformedResponse() { String invalidResponsedata = "{responses: [] }"; MSBatchResponseContent batchresponse = new MSBatchResponseContent(invalidResponsedata); assertTrue(batchresponse.getResponses() == null); diff --git a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java index 5bee73cfb..1e998f761 100644 --- a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java @@ -23,7 +23,7 @@ public class RedirectHandlerTest { String differenthosturl = "https://graph.abc.com/v1.0/"; @Test - public void testIsRedirectedFailure() { + public void testIsRedirectedFailureByNoLocationHeader() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); @@ -38,7 +38,7 @@ public void testIsRedirectedFailure() { } @Test - public void testIsRedirectedFailure1() { + public void testIsRedirectedFailureByStatusCodeBadRequest() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad Request"); @@ -54,7 +54,7 @@ public void testIsRedirectedFailure1() { } @Test - public void testIsRedirectedSuccess() { + public void testIsRedirectedSuccessWithStatusCodeMovedTemporarily() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily"); @@ -70,7 +70,7 @@ public void testIsRedirectedSuccess() { } @Test - public void testIsRedirectedSuccess1() { + public void testIsRedirectedSuccessWithStatusCodeMovedPermanently() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_PERMANENTLY, "Moved Permanently"); @@ -86,7 +86,7 @@ public void testIsRedirectedSuccess1() { } @Test - public void testIsRedirectedSuccess2() { + public void testIsRedirectedSuccessWithStatusCodeTemporaryRedirect() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_TEMPORARY_REDIRECT, "Temporary Redirect"); @@ -102,7 +102,7 @@ public void testIsRedirectedSuccess2() { } @Test - public void testIsRedirectedSuccess3() { + public void testIsRedirectedSuccessWithStatusCodeSeeOther() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; HttpGet httpget = new HttpGet(testmeurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_SEE_OTHER, "See Other"); @@ -193,7 +193,7 @@ public void testGetRedirectForPostMethod() { } @Test - public void testGetRedirectForPostMethod1() { + public void testGetRedirectForPostMethodWithStatusCodeSeeOther() { RedirectHandler redirectHandler = RedirectHandler.INSTANCE; HttpPost httppost = new HttpPost(testurl); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_SEE_OTHER, "See Other");