Skip to content

Commit fc3dfae

Browse files
committed
Test files for middlewares classes
1 parent ed236bd commit fc3dfae

File tree

6 files changed

+176
-15
lines changed

6 files changed

+176
-15
lines changed

src/main/java/com/microsoft/graph/content/MSBatchRequestContent.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class MSBatchRequestContent {
1818
private final int maxNumberOfRequests = 20;
1919

2020
public MSBatchRequestContent(List<MSBatchRequestStep> batchRequestStepsArray) {
21-
batchRequestStepsArray = new ArrayList<>();
21+
this.batchRequestStepsArray = new ArrayList<>();
2222
if(batchRequestStepsArray.size() <= maxNumberOfRequests) {
2323
for(MSBatchRequestStep requestStep: batchRequestStepsArray)
2424
addBatchRequestStep(requestStep);
@@ -29,16 +29,16 @@ public MSBatchRequestContent() {
2929
batchRequestStepsArray = new ArrayList<>();
3030
}
3131

32-
public void addBatchRequestStep(MSBatchRequestStep batchRequestStep) {
32+
public boolean addBatchRequestStep(MSBatchRequestStep batchRequestStep) {
3333
if(batchRequestStep.getRequestId().compareTo("") == 0)
34-
return;
34+
return false;
3535
if(batchRequestStepsArray.size() == maxNumberOfRequests)
36-
return;
36+
return false;
3737
for(MSBatchRequestStep requestStep: batchRequestStepsArray) {
3838
if(batchRequestStep.getRequestId().compareTo(requestStep.getRequestId()) == 0)
39-
return;
39+
return false;
4040
}
41-
batchRequestStepsArray.add(batchRequestStep);
41+
return batchRequestStepsArray.add(batchRequestStep);
4242
}
4343

4444
public void removeBatchRequesStepWithId(String requestId) {
@@ -74,7 +74,7 @@ private Map<String, String> getBatchRequestMapFromRequestStep(MSBatchRequestStep
7474
contentmap.put("url", batchRequestStep.getRequest().getRequestLine().getUri());
7575
contentmap.put("method", batchRequestStep.getRequest().getRequestLine().getMethod());
7676
Header[] headers = batchRequestStep.getRequest().getAllHeaders();
77-
if(headers != null) {
77+
if(headers != null && headers.length != 0) {
7878
JSONObject obj = new JSONObject();
7979
for(Header header: headers) {
8080
obj.put(header.getName(), header.getValue());
Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package com.microsoft.graph.content;
22

3+
import java.util.Set;
4+
5+
import org.apache.http.Consts;
6+
import org.apache.http.HttpEntity;
7+
import org.apache.http.HttpResponse;
8+
import org.apache.http.entity.ContentType;
9+
import org.apache.http.entity.StringEntity;
10+
import org.apache.http.message.BasicHttpResponse;
11+
import org.json.simple.JSONArray;
312
import org.json.simple.JSONObject;
413
import org.json.simple.parser.JSONParser;
514
import org.json.simple.parser.ParseException;
@@ -11,21 +20,48 @@ public class MSBatchResponseContent {
1120
public MSBatchResponseContent(String batchResponseData ) {
1221
JSONParser parser = new JSONParser();
1322
try {
14-
batchResponseObj = (JSONObject) parser.parse(batchResponseData);
23+
if(batchResponseData != null)
24+
batchResponseObj = (JSONObject) parser.parse(batchResponseData);
1525
}
1626
catch(ParseException e) {
1727
}
1828
}
1929

20-
public String getResponseById(String requestId) {
21-
if(batchResponseObj.get(requestId) != null)
22-
return batchResponseObj.get(requestId).toString();
23-
return null;
30+
public HttpResponse getResponseById(String requestId) {
31+
if(batchResponseObj == null)
32+
return null;
33+
34+
JSONArray responses = (JSONArray)batchResponseObj.get("responses");
35+
if(responses == null)
36+
return null;
37+
38+
for(Object response: responses) {
39+
JSONObject jsonresponse = (JSONObject)response;
40+
String id = (String)jsonresponse.get("id");
41+
if(id.compareTo(requestId) == 0) {
42+
HttpResponse httpresponse = new BasicHttpResponse(null, ((Long)jsonresponse.get("status")).intValue(), null);
43+
if(jsonresponse.get("body") != null) {
44+
HttpEntity entity = new StringEntity(jsonresponse.get("body").toString(), ContentType.APPLICATION_JSON);
45+
httpresponse.setEntity(entity);
46+
}
47+
if(jsonresponse.get("headers") != null){
48+
JSONObject jsonheaders = (JSONObject)jsonresponse.get("headers");
49+
for(Object key: jsonheaders.keySet()) {
50+
String strkey = (String)key;
51+
String strvalue = (String)jsonheaders.get(strkey);
52+
httpresponse.setHeader(strkey, strvalue);
53+
}
54+
}
55+
return httpresponse;
56+
57+
}
58+
}
59+
return null;
2460
}
2561

2662
public String getResponses() {
2763
if(batchResponseObj != null)
28-
return batchResponseObj.toJSONString();
64+
return ((JSONArray)batchResponseObj.get("responses")).toJSONString();
2965
return null;
3066
}
3167
}

src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public HttpUriRequest getRedirect(
5858
if(status != HttpStatus.SC_SEE_OTHER) {
5959
try {
6060
final URI requestURI = new URI(request.getRequestLine().getUri());
61-
if(!uri.getHost().equalsIgnoreCase(requestURI.getHost())) {
61+
if(!uri.getHost().equalsIgnoreCase(requestURI.getHost()) ||
62+
!uri.getScheme().equalsIgnoreCase(requestURI.getScheme()))
6263
request.removeHeaders("Authorization");
63-
}
6464
return RequestBuilder.copy(request).setUri(uri).build();
6565
}
6666
catch (final URISyntaxException ex) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.microsoft.graph.content;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.apache.http.HttpRequest;
9+
import org.apache.http.client.methods.HttpGet;
10+
import org.junit.After;
11+
import org.junit.AfterClass;
12+
import org.junit.Before;
13+
import org.junit.BeforeClass;
14+
import org.junit.Test;
15+
16+
public class MSBatchRequestContentTest {
17+
18+
@BeforeClass
19+
public static void setUpBeforeClass() throws Exception {
20+
}
21+
22+
@AfterClass
23+
public static void tearDownAfterClass() throws Exception {
24+
}
25+
26+
@Before
27+
public void setUp() throws Exception {
28+
}
29+
30+
@After
31+
public void tearDown() throws Exception {
32+
}
33+
34+
@Test
35+
public void testMSBatchRequestContentCreation() {
36+
37+
List<MSBatchRequestStep> requestStepArray = new ArrayList<>();
38+
for(int i=0;i<5;i++) {
39+
HttpRequest request = new HttpGet("http://graph.microsoft.com");
40+
List<String> arrayOfDependsOnIds = new ArrayList();
41+
MSBatchRequestStep requestStep = new MSBatchRequestStep("" + i, request, arrayOfDependsOnIds);
42+
requestStepArray.add(requestStep);
43+
}
44+
MSBatchRequestContent requestContent = new MSBatchRequestContent(requestStepArray);
45+
assertTrue(requestContent.getBatchRequestContent() != null);
46+
}
47+
48+
@Test
49+
public void testGetBatchRequestContent() {
50+
HttpRequest request = new HttpGet("http://graph.microsoft.com");
51+
List<String> arrayOfDependsOnIds = new ArrayList();
52+
MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds);
53+
MSBatchRequestContent requestContent = new MSBatchRequestContent();
54+
requestContent.addBatchRequestStep(requestStep);
55+
String content = requestContent.getBatchRequestContent();
56+
String expectedContent = "{\"requests\":[{\"method\":\"GET\",\"dependsOn\":\"[]\",\"id\":\"1\",\"url\":\"http:\\/\\/graph.microsoft.com\"}]}";
57+
assertTrue(content.compareTo(expectedContent) == 0);
58+
}
59+
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.microsoft.graph.content;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.apache.http.HttpRequest;
9+
import org.apache.http.client.methods.HttpGet;
10+
import org.junit.Test;
11+
12+
public class MSBatchRequestStepTest {
13+
14+
@Test
15+
public void testMSBatchRequestStepCreation() {
16+
HttpRequest request = new HttpGet("http://graph.microsoft.com");
17+
List<String> arrayOfDependsOnIds = new ArrayList();
18+
MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds);
19+
assertTrue("Test BatchRequestStep creation", requestStep != null);
20+
assertTrue("Test Request id", requestStep.getRequestId().compareTo("1") == 0);
21+
assertTrue("Test Request object", requestStep.getRequest() == request);
22+
assertTrue("Test Array of depends on Ids", requestStep.getArrayOfDependsOnIds() != null);
23+
}
24+
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.microsoft.graph.content;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.IOException;
6+
import java.util.Arrays;
7+
8+
import org.apache.http.HttpResponse;
9+
import org.apache.http.ParseException;
10+
import org.apache.http.util.EntityUtils;
11+
import org.junit.After;
12+
import org.junit.AfterClass;
13+
import org.junit.Before;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
public class MSBatchResponseContentTest {
18+
19+
@Test
20+
public void testNullMSBatchResponseContent() {
21+
String responsedata = null;
22+
MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata);
23+
assertTrue(batchresponse.getResponses() == null);
24+
}
25+
26+
@Test
27+
public void testValidMSBatchResponseContent() {
28+
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 } ] }";
29+
MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata);
30+
assertTrue(batchresponse.getResponses() != null);
31+
}
32+
33+
@Test
34+
public void testGetMSBatchResponseContentByID() {
35+
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 } ] }";
36+
MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata);
37+
HttpResponse response = batchresponse.getResponseById("1");
38+
assertTrue(response != null);
39+
}
40+
}

0 commit comments

Comments
 (0)