|
| 1 | +package com.microsoft.graph.http; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.net.URL; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static org.mockito.Mockito.mock; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import okhttp3.MediaType; |
| 21 | +import okhttp3.Protocol; |
| 22 | +import okhttp3.Request; |
| 23 | +import okhttp3.Response; |
| 24 | +import okhttp3.ResponseBody; |
| 25 | + |
| 26 | +import com.microsoft.graph.core.GraphErrorCodes; |
| 27 | +import com.microsoft.graph.logger.DefaultLogger; |
| 28 | + |
| 29 | +public class GraphServiceExceptionTests { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testError() { |
| 33 | + GraphErrorResponse errorResponse = new GraphErrorResponse(); |
| 34 | + GraphError error = new GraphError(); |
| 35 | + error.code = GraphErrorCodes.UNAUTHENTICATED.toString(); |
| 36 | + errorResponse.error = error; |
| 37 | + GraphServiceException exception = new GraphServiceException(null,null,new ArrayList<String>(),null,401,"Unauthorized",new ArrayList<String>(),errorResponse, false); |
| 38 | + String message = exception.getMessage(); |
| 39 | + assertTrue(message.indexOf("Error code: UNAUTHENTICATED") == 0); |
| 40 | + assertTrue(message.indexOf("401 : Unauthorized") > 0); |
| 41 | + assertTrue(message.indexOf("truncated") > 0); |
| 42 | + assertEquals(error,exception.getServiceError()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testVerboseError() { |
| 47 | + GraphErrorResponse errorResponse = new GraphErrorResponse(); |
| 48 | + GraphError error = new GraphError(); |
| 49 | + error.code = GraphErrorCodes.UNAUTHENTICATED.toString(); |
| 50 | + errorResponse.error = error; |
| 51 | + GraphServiceException exception = new GraphServiceException(null,null,new ArrayList<String>(),null,401,"Unauthorized",new ArrayList<String>(),errorResponse, true); |
| 52 | + String message = exception.getMessage(); |
| 53 | + assertTrue(message.indexOf("Error code: UNAUTHENTICATED") == 0); |
| 54 | + assertTrue(message.indexOf("401 : Unauthorized") > 0); |
| 55 | + assertFalse(message.indexOf("truncated") > 0); |
| 56 | + assertEquals(error,exception.getServiceError()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testcreateFromResponse() { |
| 61 | + DefaultLogger logger = new DefaultLogger(); |
| 62 | + GraphServiceException exception = null; |
| 63 | + Boolean success = false; |
| 64 | + Boolean failure = false; |
| 65 | + final Response response = new Response.Builder() |
| 66 | + .request(new Request.Builder().url("https://a.b.c").build()) |
| 67 | + .protocol(Protocol.HTTP_1_1) |
| 68 | + .code(401).message("Unauthorized").body( |
| 69 | + ResponseBody.create( |
| 70 | + "{}", |
| 71 | + MediaType.parse("application/json") |
| 72 | + )) |
| 73 | + .build(); |
| 74 | + try{ |
| 75 | + IHttpRequest mRequest = mock(IHttpRequest.class); |
| 76 | + when(mRequest.getRequestUrl()).thenReturn(new URL("http://localhost")); |
| 77 | + exception = GraphServiceException.createFromResponse(mRequest ,null,null,response,logger); |
| 78 | + success = true; |
| 79 | + }catch (IOException ex){ |
| 80 | + failure = true; |
| 81 | + } |
| 82 | + |
| 83 | + assertTrue(success); |
| 84 | + assertFalse(failure); |
| 85 | + |
| 86 | + String message = exception.getMessage(); |
| 87 | + assertTrue(message.indexOf("Error code: Unable to parse error response message") == 0); |
| 88 | + assertTrue(message.indexOf("http://localhost") > 0); |
| 89 | + } |
| 90 | +} |
0 commit comments