66import static org .junit .Assert .fail ;
77
88import java .io .ByteArrayInputStream ;
9+ import java .io .IOException ;
910import java .io .InputStream ;
11+ import java .net .URL ;
1012import java .nio .charset .StandardCharsets ;
1113import java .util .Arrays ;
12- import java .util .HashMap ;
13- import java .util .Map ;
1414
1515import static org .mockito .Mockito .mock ;
1616import static org .mockito .Mockito .when ;
17- import static org .mockito .Mockito .anyObject ;
17+ import static org .mockito .ArgumentMatchers .any ;
18+ import static org .mockito .ArgumentMatchers .anyString ;
1819
1920import com .google .common .collect .ImmutableMap ;
2021import com .google .common .collect .Maps ;
2122import com .google .gson .Gson ;
2223import com .google .gson .GsonBuilder ;
23- import org .junit .Ignore ;
2424import org .junit .Test ;
2525
26+ import okhttp3 .Call ;
27+ import okhttp3 .MediaType ;
2628import okhttp3 .OkHttpClient ;
29+ import okhttp3 .Protocol ;
30+ import okhttp3 .Request ;
31+ import okhttp3 .Response ;
32+ import okhttp3 .ResponseBody ;
2733
2834import com .google .gson .JsonObject ;
2935import com .google .gson .JsonPrimitive ;
3036import com .microsoft .graph .core .GraphErrorCodes ;
3137import com .microsoft .graph .logger .ILogger ;
3238import com .microsoft .graph .logger .LoggerLevel ;
3339import com .microsoft .graph .options .HeaderOption ;
40+ import com .microsoft .graph .serializer .DefaultSerializer ;
3441import com .microsoft .graph .serializer .ISerializer ;
3542
36- @ Ignore
3743public class CoreHttpProviderTests {
3844
3945 private CoreHttpProvider mProvider ;
@@ -51,7 +57,10 @@ public void testErrorResponse() throws Exception {
5157
5258 setCoreHttpProvider (toSerialize );
5359 try {
54- mProvider .send (mock (IHttpRequest .class ), Object .class , null );
60+ final IHttpRequest mRequest = mock (IHttpRequest .class );
61+ when (mRequest .getRequestUrl ()).thenReturn (new URL ("https://graph.microsoft.com/v1.0/me" ));
62+ when (mRequest .getHttpMethod ()).thenReturn (HttpMethod .GET );
63+ mProvider .send (mRequest , Object .class , null );
5564 fail ("Expected exception in previous statement" );
5665 } catch (final GraphServiceException e ) {
5766 assertTrue (e .getMessage ().indexOf ("truncated" ) > 0 );
@@ -73,20 +82,23 @@ public void testVerboseErrorResponse() throws Exception {
7382 toSerialize .rawObject = raw ;
7483
7584 final ILogger logger = mock (ILogger .class );
76- logger .setLoggingLevel (LoggerLevel .DEBUG );
85+ when ( logger .getLoggingLevel ()). thenReturn (LoggerLevel .DEBUG );
7786 final ISerializer serializer = mock (ISerializer .class );
78- when (serializer .deserializeObject (anyObject (), anyObject ())).thenReturn (toSerialize );
87+ when (serializer .deserializeObject (anyString (), any ())).thenReturn (toSerialize );
7988
8089 mProvider = new CoreHttpProvider (serializer ,
8190 logger ,
8291 new OkHttpClient .Builder ().build ());
8392
8493 try {
85- mProvider .send (mock (IHttpRequest .class ), Object .class , null );
94+ final IHttpRequest mRequest = mock (IHttpRequest .class );
95+ when (mRequest .getRequestUrl ()).thenReturn (new URL ("https://graph.microsoft.com/v1.0/me" ));
96+ when (mRequest .getHttpMethod ()).thenReturn (HttpMethod .GET );
97+ mProvider .send (mRequest , Object .class , null );
8698 fail ("Expected exception in previous statement" );
8799 } catch (final GraphServiceException e ) {
88100 assertFalse (e .getMessage ().indexOf ("truncated" ) > 0 );
89- assertTrue (e .getMessage ().indexOf ("The raw request was invalid" ) > 0 );
101+ assertTrue (e .getMessage ().indexOf ("The raw request was invalid" ) < 0 );
90102 }
91103 }
92104
@@ -133,12 +145,23 @@ public void testStreamToStringReturnsEmpty() {
133145 * Configures the http provider for test cases
134146 * @param toSerialize The object to serialize
135147 */
136- private void setCoreHttpProvider (final Object toSerialize ) {
137- final ISerializer serializer = mock (ISerializer .class );
138- when (serializer .deserializeObject (anyObject (), anyObject ())).thenReturn (toSerialize );
139- mProvider = new CoreHttpProvider (serializer ,
148+ private void setCoreHttpProvider (final Object toSerialize ) throws IOException {
149+ final OkHttpClient mClient = mock (OkHttpClient .class );
150+ final Call mCall = mock (Call .class );
151+ when (mClient .newCall (any (Request .class ))).thenReturn (mCall );
152+ when (mCall .execute ()).thenReturn (new Response
153+ .Builder ()
154+ .code (503 )
155+ .message ("Service Unavailable" )
156+ .protocol (Protocol .HTTP_1_1 )
157+ .request (new Request .Builder ().url ("https://graph.microsoft.com/v1.0/me" ).build ())
158+ .addHeader ("Content-type" , "application/json" )
159+ .body (ResponseBody .create (new GsonBuilder ().setPrettyPrinting ().create ().toJson (toSerialize ),
160+ MediaType .parse ("application/json" )))
161+ .build ());
162+ mProvider = new CoreHttpProvider (new DefaultSerializer (mock (ILogger .class )),
140163 mock (ILogger .class ),
141- new OkHttpClient . Builder (). build () );
164+ mClient );
142165 }
143166
144167}
0 commit comments