Skip to content

Commit ad4f746

Browse files
author
Caitlin Bales
committed
Test debug/error logger reporting
1 parent 461068e commit ad4f746

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/test/java/com/microsoft/graph/http/DefaultHttpProviderTests.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222

2323
package com.microsoft.graph.http;
2424

25+
import com.google.gson.JsonObject;
26+
import com.google.gson.JsonPrimitive;
2527
import com.microsoft.graph.authentication.MockAuthenticationProvider;
2628
import com.microsoft.graph.concurrency.IProgressCallback;
2729
import com.microsoft.graph.concurrency.MockExecutors;
2830
import com.microsoft.graph.core.ClientException;
2931
import com.microsoft.graph.core.GraphErrorCodes;
3032
import com.microsoft.graph.models.extensions.Drive;
3133
import com.microsoft.graph.models.extensions.DriveItem;
34+
import com.microsoft.graph.logger.LoggerLevel;
3235
import com.microsoft.graph.logger.MockLogger;
3336
import com.microsoft.graph.serializer.MockSerializer;
3437

@@ -259,9 +262,58 @@ public Map<String, String> getHeaders() {
259262
mProvider.send(new MockHttpRequest(), DriveItem.class, null);
260263
fail("Expected exception in previous statement");
261264
} catch (final GraphServiceException e) {
265+
assertTrue(e.getMessage().indexOf("truncated") > 0);
262266
assertEquals(expectedMessage, e.getServiceError().message);
263267
}
264268
}
269+
270+
@Test
271+
public void testVerboseErrorResponse() throws Exception {
272+
final GraphErrorCodes expectedErrorCode = GraphErrorCodes.INVALID_REQUEST;
273+
final String expectedMessage = "Test error!";
274+
final GraphErrorResponse toSerialize = new GraphErrorResponse();
275+
toSerialize.error = new GraphError();
276+
toSerialize.error.code = expectedErrorCode.toString();
277+
toSerialize.error.message = expectedMessage;
278+
toSerialize.error.innererror = null;
279+
JsonObject raw = new JsonObject();
280+
raw.add("response", new JsonPrimitive("The raw request was invalid"));
281+
toSerialize.rawObject = raw;
282+
283+
MockLogger logger = new MockLogger();
284+
logger.setLoggingLevel(LoggerLevel.DEBUG);
285+
286+
mProvider = new DefaultHttpProvider(new MockSerializer(toSerialize, ""),
287+
mAuthenticationProvider = new MockAuthenticationProvider(),
288+
new MockExecutors(),
289+
logger);
290+
291+
final ITestConnectionData data = new ITestConnectionData() {
292+
@Override
293+
public int getRequestCode() {
294+
return 415;
295+
}
296+
297+
@Override
298+
public String getJsonResponse() {
299+
return "{}";
300+
}
301+
302+
@Override
303+
public Map<String, String> getHeaders() {
304+
return new HashMap<>();
305+
}
306+
};
307+
mProvider.setConnectionFactory(new MockConnectionFactory(new MockConnection(data)));
308+
309+
try {
310+
mProvider.send(new MockHttpRequest(), DriveItem.class, null);
311+
fail("Expected exception in previous statement");
312+
} catch (final GraphServiceException e) {
313+
assertFalse(e.getMessage().indexOf("truncated") > 0);
314+
assertTrue(e.getMessage().indexOf("The raw request was invalid") > 0);
315+
}
316+
}
265317

266318
@Test
267319
public void testBodyLessResponse() throws Exception {

src/test/java/com/microsoft/graph/http/GraphServiceExceptionTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,23 @@ public void testError() {
2525
String message = exception.getMessage();
2626
assertTrue(message.indexOf("Error code: UNAUTHENTICATED") == 0);
2727
assertTrue(message.indexOf("401 : Unauthorized") > 0);
28+
assertTrue(message.indexOf("truncated") > 0);
2829
assertEquals(error,exception.getServiceError());
2930
}
31+
32+
@Test
33+
public void testVerboseError() {
34+
GraphErrorResponse errorResponse = new GraphErrorResponse();
35+
GraphError error = new GraphError();
36+
error.code = GraphErrorCodes.UNAUTHENTICATED.toString();
37+
errorResponse.error = error;
38+
GraphServiceException exception = new GraphServiceException(null,null,new ArrayList<String>(),null,401,"Unauthorized",new ArrayList<String>(),errorResponse, true);
39+
String message = exception.getMessage();
40+
assertTrue(message.indexOf("Error code: UNAUTHENTICATED") == 0);
41+
assertTrue(message.indexOf("401 : Unauthorized") > 0);
42+
assertFalse(message.indexOf("truncated") > 0);
43+
assertEquals(error,exception.getServiceError());
44+
}
3045

3146
@Test
3247
public void testCreateFromConnection() {

0 commit comments

Comments
 (0)