1717import java .util .HashSet ;
1818import java .util .concurrent .ThreadLocalRandom ;
1919
20+ import com .google .gson .JsonObject ;
2021import com .google .gson .JsonParser ;
2122import com .microsoft .graph .authentication .IAuthenticationProvider ;
2223import com .microsoft .graph .core .BaseClient ;
2627import com .microsoft .graph .http .IHttpRequest ;
2728import com .microsoft .graph .logger .ILogger ;
2829import com .microsoft .graph .options .HeaderOption ;
30+ import com .microsoft .graph .serializer .AdditionalDataManager ;
2931import com .microsoft .graph .serializer .DefaultSerializer ;
32+ import com .microsoft .graph .serializer .IJsonBackedObject ;
33+ import com .microsoft .graph .serializer .ISerializer ;
3034
3135import org .junit .jupiter .api .Test ;
3236import org .mockito .invocation .InvocationOnMock ;
@@ -62,7 +66,8 @@ public void testGetBatchRequestContent() throws MalformedURLException {
6266 BatchRequestContent requestContent = new BatchRequestContent ();
6367 String stepId = requestContent .addBatchRequestStep (requestStep );
6468 String content = new DefaultSerializer (mock (ILogger .class )).serializeObject (requestContent );
65- String expectedContent = "{\" requests\" :[{\" url\" :\" http://graph.microsoft.com/me\" ,\" method\" :\" get\" ,\" id\" :\" " +stepId +"\" }]}" ;
69+ String expectedContent = "{\" requests\" :[{\" url\" :\" http://graph.microsoft.com/me\" ,\" method\" :\" GET\" ,\" id\" :\" "
70+ + stepId + "\" }]}" ;
6671 assertEquals (expectedContent , content );
6772 }
6873
@@ -74,7 +79,8 @@ public void testGetBatchRequestContentWithHeader() throws MalformedURLException
7479 BatchRequestContent requestContent = new BatchRequestContent ();
7580 String stepId = requestContent .addBatchRequestStep (requestStep );
7681 String content = new DefaultSerializer (mock (ILogger .class )).serializeObject (requestContent );
77- String expectedContent = "{\" requests\" :[{\" url\" :\" http://graph.microsoft.com/me\" ,\" method\" :\" get\" ,\" id\" :\" " +stepId +"\" ,\" headers\" :{\" testkey\" :\" testvalue\" }}]}" ;
82+ String expectedContent = "{\" requests\" :[{\" url\" :\" http://graph.microsoft.com/me\" ,\" method\" :\" GET\" ,\" id\" :\" "
83+ + stepId + "\" ,\" headers\" :{\" testkey\" :\" testvalue\" }}]}" ;
7884 assertEquals (expectedContent , content );
7985 }
8086
@@ -104,7 +110,8 @@ public void testRemoveBatchRequesStepWithIdByAddingMultipleBatchSteps() throws M
104110
105111 requestContent .removeBatchRequestStepWithId (stepId );
106112 String content = new DefaultSerializer (mock (ILogger .class )).serializeObject (requestContent );
107- String expectedContent = "{\" requests\" :[{\" url\" :\" http://graph.microsoft.com/me\" ,\" method\" :\" get\" ,\" id\" :\" " +step1Id +"\" }]}" ;
113+ String expectedContent = "{\" requests\" :[{\" url\" :\" http://graph.microsoft.com/me\" ,\" method\" :\" GET\" ,\" id\" :\" "
114+ + step1Id + "\" }]}" ;
108115 assertEquals (expectedContent , content );
109116 }
110117
@@ -122,7 +129,11 @@ public void defensiveProgrammingTests() {
122129 }, "get step by id with null id throws" );
123130
124131 assertThrows (NullPointerException .class , () -> {
125- new BatchRequestContent () {{ requests = new ArrayList <>(); }}.removeBatchRequestStepWithId ((String )null );
132+ new BatchRequestContent () {
133+ {
134+ requests = new ArrayList <>();
135+ }
136+ }.removeBatchRequestStepWithId ((String ) null );
126137 }, "remove step by id with null id throws" );
127138
128139 assertThrows (IllegalArgumentException .class , () -> {
@@ -141,49 +152,42 @@ public void executeBatchTest() throws Throwable {
141152 final Call mCall = mock (Call .class );
142153 when (mHttpClient .newCall (any (Request .class ))).thenReturn (mCall );
143154
144- final CoreHttpProvider mHttpProvider = new CoreHttpProvider (new DefaultSerializer (mock (ILogger .class )), mock (ILogger .class ), mHttpClient );
145- final IBaseClient mClient = BaseClient .builder ()
146- .authenticationProvider (mock (IAuthenticationProvider .class ))
147- .httpProvider (mHttpProvider )
148- .buildClient ();
149- final Response mResponse = new Response
150- .Builder ()
151- .request (new Request
152- .Builder ()
153- .url ("https://graph.microsoft.com/v1.0/$batch" )
154- .build ())
155- .code (200 )
156- .protocol (Protocol .HTTP_1_1 )
157- .message ("OK" )
158- .addHeader ("Content-type" , "application/json" )
159- .body (ResponseBody .create ("{\" responses\" : [{\" id\" : \" " +stepId +"\" ,\" status\" : 200,\" body\" : null}]}" ,
160- MediaType .parse ("application/json" )))
161- .build ();
155+ final CoreHttpProvider mHttpProvider = new CoreHttpProvider (new DefaultSerializer (mock (ILogger .class )),
156+ mock (ILogger .class ), mHttpClient );
157+ final IBaseClient mClient = BaseClient .builder ().authenticationProvider (mock (IAuthenticationProvider .class ))
158+ .httpProvider (mHttpProvider ).buildClient ();
159+ final Response mResponse = new Response .Builder ()
160+ .request (new Request .Builder ().url ("https://graph.microsoft.com/v1.0/$batch" ).build ()).code (200 )
161+ .protocol (Protocol .HTTP_1_1 ).message ("OK" ).addHeader ("Content-type" , "application/json" )
162+ .body (ResponseBody .create (
163+ "{\" responses\" : [{\" id\" : \" " + stepId + "\" ,\" status\" : 200,\" body\" : null}]}" ,
164+ MediaType .parse ("application/json" )))
165+ .build ();
162166 when (mCall .execute ()).thenReturn (mResponse );
163167 final BatchResponseContent batchResponse = mClient .batch ().buildRequest ().post (content );
164168 assertNotNull (mClient .batch ().buildRequest ().postAsync (content ));
165169 final BatchResponseStep <?> response = batchResponse .getResponseById (stepId );
166170 assertNotNull (response );
167171 }
172+
168173 @ Test
169- public void usesHttpMethodFromRequestIfAlreadySet () throws MalformedURLException
170- {
174+ public void usesHttpMethodFromRequestIfAlreadySet () throws MalformedURLException {
171175 IHttpRequest requestStep = mock (IHttpRequest .class );
172176 when (requestStep .getRequestUrl ()).thenReturn (new URL (testurl ));
173177 when (requestStep .getHttpMethod ()).thenReturn (HttpMethod .DELETE );
174178 final BatchRequestContent batchRequest = new BatchRequestContent ();
175179 final String stepId = batchRequest .addBatchRequestStep (requestStep );
176- assertEquals (HttpMethod . DELETE , batchRequest .getStepById (stepId ).method );
180+ assertEquals (" DELETE" , batchRequest .getStepById (stepId ).method );
177181 }
182+
178183 @ Test
179- public void doesntThrowWhenTryingToRemoveRequestFromNull ()
180- {
184+ public void doesntThrowWhenTryingToRemoveRequestFromNull () {
181185 final BatchRequestContent batchRequest = new BatchRequestContent ();
182186 batchRequest .removeBatchRequestStepWithId ("id" );
183187 }
188+
184189 @ Test
185- public void doesntRemoveDependsOnWhenNotEmpty () throws MalformedURLException
186- {
190+ public void doesntRemoveDependsOnWhenNotEmpty () throws MalformedURLException {
187191 IHttpRequest requestStep = mock (IHttpRequest .class );
188192 when (requestStep .getRequestUrl ()).thenReturn (new URL (testurl ));
189193 final BatchRequestContent batchRequest = new BatchRequestContent ();
@@ -195,4 +199,30 @@ public void doesntRemoveDependsOnWhenNotEmpty() throws MalformedURLException
195199
196200 assertNotNull (batchRequest .getStepById (stepId3 ).dependsOn );
197201 }
202+
203+ @ Test
204+ public void addsContentTypeForBodies () throws MalformedURLException {
205+ IHttpRequest requestStep = mock (IHttpRequest .class );
206+ when (requestStep .getRequestUrl ()).thenReturn (new URL (testurl ));
207+ final BatchRequestContent batchRequest = new BatchRequestContent ();
208+ final IJsonBackedObject body = new IJsonBackedObject () {
209+
210+ @ Override
211+ public void setRawObject (ISerializer serializer , JsonObject json ) {
212+ // TODO Auto-generated method stub
213+
214+ }
215+
216+ @ Override
217+ public AdditionalDataManager additionalDataManager () {
218+ // TODO Auto-generated method stub
219+ return null ;
220+ }
221+
222+ };
223+ final String stepId = batchRequest .addBatchRequestStep (requestStep , HttpMethod .POST , body );
224+ final BatchRequestStep <?> step = batchRequest .getStepById (stepId );
225+ assertNotNull (step .headers );
226+ assertEquals ("application/json" , step .headers .get ("content-type" ));
227+ }
198228}
0 commit comments