Skip to content

Commit 05ab9f4

Browse files
committed
Add more test cases for Content classes and Redirect Handler to increase code coverage to more than 90%
1 parent 7535525 commit 05ab9f4

File tree

4 files changed

+149
-5
lines changed

4 files changed

+149
-5
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
// Apply the java-library plugin to add support for Java Library
1010
apply plugin: 'java-library'
11+
apply plugin: 'jacoco'
1112

1213
// In this section you declare where to find the dependencies of your project
1314
repositories {

src/test/java/com/microsoft/graph/content/MSBatchRequestContentTest.java

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
public class MSBatchRequestContentTest {
1717

18+
String testurl = "http://graph.microsoft.com";
19+
1820
@Test
1921
public void testMSBatchRequestContentCreation() {
20-
2122
List<MSBatchRequestStep> requestStepArray = new ArrayList<>();
2223
for(int i=0;i<5;i++) {
23-
HttpRequest request = new HttpGet("http://graph.microsoft.com");
24-
List<String> arrayOfDependsOnIds = new ArrayList();
24+
HttpRequest request = new HttpGet(testurl);
25+
List<String> arrayOfDependsOnIds = new ArrayList<>();
2526
MSBatchRequestStep requestStep = new MSBatchRequestStep("" + i, request, arrayOfDependsOnIds);
2627
requestStepArray.add(requestStep);
2728
}
@@ -31,14 +32,61 @@ public void testMSBatchRequestContentCreation() {
3132

3233
@Test
3334
public void testGetBatchRequestContent() {
34-
HttpRequest request = new HttpGet("http://graph.microsoft.com");
35-
List<String> arrayOfDependsOnIds = new ArrayList();
35+
HttpRequest request = new HttpGet(testurl);
36+
List<String> arrayOfDependsOnIds = new ArrayList<>();
3637
MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds);
3738
MSBatchRequestContent requestContent = new MSBatchRequestContent();
3839
requestContent.addBatchRequestStep(requestStep);
3940
String content = requestContent.getBatchRequestContent();
4041
String expectedContent = "{\"requests\":[{\"method\":\"GET\",\"dependsOn\":\"[]\",\"id\":\"1\",\"url\":\"http:\\/\\/graph.microsoft.com\"}]}";
4142
assertTrue(content.compareTo(expectedContent) == 0);
4243
}
44+
45+
@Test
46+
public void testGetBatchRequestContentWithHeader() {
47+
HttpRequest request = new HttpGet(testurl);
48+
request.setHeader("testkey", "testvalue");
49+
List<String> arrayOfDependsOnIds = new ArrayList<>();
50+
MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds);
51+
MSBatchRequestContent requestContent = new MSBatchRequestContent();
52+
requestContent.addBatchRequestStep(requestStep);
53+
String content = requestContent.getBatchRequestContent();
54+
String expectedContent = "{\"requests\":[{\"headers\":\"{\\\"testkey\\\":\\\"testvalue\\\"}\",\"method\":\"GET\",\"dependsOn\":\"[]\",\"id\":\"1\",\"url\":\"http:\\/\\/graph.microsoft.com\"}]}";
55+
assertTrue(content.compareTo(expectedContent) == 0);
56+
}
57+
58+
@Test
59+
public void testRemoveBatchRequesStepWithId() {
60+
HttpRequest request = new HttpGet(testurl);
61+
List<String> arrayOfDependsOnIds = new ArrayList<>();
62+
MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds);
63+
MSBatchRequestContent requestContent = new MSBatchRequestContent();
64+
requestContent.addBatchRequestStep(requestStep);
65+
requestContent.removeBatchRequesStepWithId("1");
66+
String content = requestContent.getBatchRequestContent();
67+
String expectedContent = "{\"requests\":[]}";
68+
assertTrue(content.compareTo(expectedContent) == 0);
69+
}
70+
71+
@Test
72+
public void testRemoveBatchRequesStepWithId1() {
73+
HttpRequest request = new HttpGet(testurl);
74+
List<String> arrayOfDependsOnIds = new ArrayList<>();
75+
MSBatchRequestStep requestStep = new MSBatchRequestStep("1", request, arrayOfDependsOnIds);
76+
77+
HttpRequest request1 = new HttpGet(testurl);
78+
List<String> arrayOfDependsOnIds1 = new ArrayList<>();
79+
arrayOfDependsOnIds1.add("1");
80+
MSBatchRequestStep requestStep1 = new MSBatchRequestStep("2", request1, arrayOfDependsOnIds1);
81+
82+
MSBatchRequestContent requestContent = new MSBatchRequestContent();
83+
requestContent.addBatchRequestStep(requestStep);
84+
requestContent.addBatchRequestStep(requestStep1);
85+
86+
requestContent.removeBatchRequesStepWithId("1");
87+
String content = requestContent.getBatchRequestContent();
88+
String expectedContent = "{\"requests\":[{\"method\":\"GET\",\"dependsOn\":\"[]\",\"id\":\"2\",\"url\":\"http:\\/\\/graph.microsoft.com\"}]}";
89+
assertTrue(content.compareTo(expectedContent) == 0);
90+
}
4391

4492
}

src/test/java/com/microsoft/graph/content/MSBatchResponseContentTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ public void testValidMSBatchResponseContent() {
3030
assertTrue(batchresponse.getResponses() != null);
3131
}
3232

33+
@Test
34+
public void testInvalidMSBatchResponseContent() {
35+
//passing empty responses
36+
String responsedata = "{\"responses\": [] }";
37+
MSBatchResponseContent batchresponse = new MSBatchResponseContent(responsedata);
38+
assertTrue(batchresponse.getResponseById("1") == null);
39+
}
40+
41+
@Test
42+
public void testInvalidMSBatchResponseContent1() {
43+
//passing null response json string
44+
MSBatchResponseContent batchresponse = new MSBatchResponseContent(null);
45+
assertTrue(batchresponse.getResponseById("1") == null);
46+
}
47+
48+
@Test
49+
public void testInvalidMSBatchResponseContent2() {
50+
//passing malformed json response
51+
String invalidResponsedata = "{responses: [] }";
52+
MSBatchResponseContent batchresponse = new MSBatchResponseContent(invalidResponsedata);
53+
assertTrue(batchresponse.getResponses() == null);
54+
}
55+
3356
@Test
3457
public void testGetMSBatchResponseContentByID() {
3558
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 } ] }";

src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertTrue;
44
import static org.junit.Assert.fail;
55

6+
import org.apache.http.Header;
67
import org.apache.http.HttpRequest;
78
import org.apache.http.HttpResponse;
89
import org.apache.http.HttpStatus;
@@ -19,6 +20,7 @@ public class RedirectHandlerTest {
1920

2021
String testmeurl = "https://graph.microsoft.com/v1.0/me/";
2122
String testurl = "https://graph.microsoft.com/v1.0/";
23+
String differenthosturl = "https://graph.abc.com/v1.0/";
2224

2325
@Test
2426
public void testIsRedirectedFailure() {
@@ -40,6 +42,7 @@ public void testIsRedirectedFailure1() {
4042
RedirectHandler redirectHandler = RedirectHandler.INSTANCE;
4143
HttpGet httpget = new HttpGet(testmeurl);
4244
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad Request");
45+
response.setHeader("location", testmeurl);
4346
HttpClientContext localContext = HttpClientContext.create();
4447
try {
4548
boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext);
@@ -66,6 +69,54 @@ public void testIsRedirectedSuccess() {
6669
}
6770
}
6871

72+
@Test
73+
public void testIsRedirectedSuccess1() {
74+
RedirectHandler redirectHandler = RedirectHandler.INSTANCE;
75+
HttpGet httpget = new HttpGet(testmeurl);
76+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_PERMANENTLY, "Moved Permanently");
77+
response.setHeader("location", testmeurl);
78+
HttpClientContext localContext = HttpClientContext.create();
79+
try {
80+
boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext);
81+
assertTrue(isRedirected);
82+
} catch (ProtocolException e) {
83+
e.printStackTrace();
84+
fail("Redirect handler testIsRedirectedSuccess1 failure");
85+
}
86+
}
87+
88+
@Test
89+
public void testIsRedirectedSuccess2() {
90+
RedirectHandler redirectHandler = RedirectHandler.INSTANCE;
91+
HttpGet httpget = new HttpGet(testmeurl);
92+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_TEMPORARY_REDIRECT, "Temporary Redirect");
93+
response.setHeader("location", testmeurl);
94+
HttpClientContext localContext = HttpClientContext.create();
95+
try {
96+
boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext);
97+
assertTrue(isRedirected);
98+
} catch (ProtocolException e) {
99+
e.printStackTrace();
100+
fail("Redirect handler testIsRedirectedSuccess2 failure");
101+
}
102+
}
103+
104+
@Test
105+
public void testIsRedirectedSuccess3() {
106+
RedirectHandler redirectHandler = RedirectHandler.INSTANCE;
107+
HttpGet httpget = new HttpGet(testmeurl);
108+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_SEE_OTHER, "See Other");
109+
response.setHeader("location", testmeurl);
110+
HttpClientContext localContext = HttpClientContext.create();
111+
try {
112+
boolean isRedirected = redirectHandler.isRedirected(httpget, response, localContext);
113+
assertTrue(isRedirected);
114+
} catch (ProtocolException e) {
115+
e.printStackTrace();
116+
fail("Redirect handler testIsRedirectedSuccess3 failure");
117+
}
118+
}
119+
69120
@Test
70121
public void testGetRedirectForGetMethod() {
71122
RedirectHandler redirectHandler = RedirectHandler.INSTANCE;
@@ -83,6 +134,27 @@ public void testGetRedirectForGetMethod() {
83134
fail("Redirect handler testGetRedirectForGetMethod failure");
84135
}
85136
}
137+
138+
@Test
139+
public void testGetRedirectForGetMethodForAuthHeader() {
140+
RedirectHandler redirectHandler = RedirectHandler.INSTANCE;
141+
HttpGet httpget = new HttpGet(testurl);
142+
httpget.addHeader("Authorization", "TOKEN");
143+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_MOVED_TEMPORARILY, "Moved Temporarily");
144+
response.setHeader("location", differenthosturl);
145+
HttpClientContext localContext = HttpClientContext.create();
146+
try {
147+
HttpRequest request = redirectHandler.getRedirect(httpget, response, localContext);
148+
assertTrue(request != null);
149+
final String method = request.getRequestLine().getMethod();
150+
assertTrue(method.equalsIgnoreCase(HttpGet.METHOD_NAME));
151+
Header header = request.getFirstHeader("Authorization");
152+
assertTrue(header == null);
153+
} catch (ProtocolException e) {
154+
e.printStackTrace();
155+
fail("Redirect handler testGetRedirectForGetMethodForAuthHeader failure");
156+
}
157+
}
86158

87159
@Test
88160
public void testGetRedirectForHeadMethod() {

0 commit comments

Comments
 (0)