Skip to content

Commit 7788912

Browse files
committed
- moves another set of http tests to the core library
1 parent 0673a8f commit 7788912

5 files changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.microsoft.graph.http;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.google.gson.JsonObject;
8+
9+
public class GraphErrorResponseTests {
10+
11+
@Test
12+
public void testSetRawObject() {
13+
JsonObject expectedJson = new JsonObject();
14+
GraphErrorResponse errorResponse = new GraphErrorResponse();
15+
errorResponse.setRawObject(null,expectedJson);
16+
assertEquals(expectedJson, errorResponse.rawObject);
17+
}
18+
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 org.junit.Test;
8+
9+
import com.microsoft.graph.core.GraphErrorCodes;
10+
11+
public class GraphErrorTests {
12+
13+
14+
@Test
15+
public void testIsError(){
16+
String expectedMessage = "test error message";
17+
GraphError error = new GraphError();
18+
error.code = GraphErrorCodes.ACCESS_DENIED.toString();
19+
error.message = expectedMessage;
20+
assertTrue(error.isError(GraphErrorCodes.ACCESS_DENIED));
21+
assertEquals(expectedMessage, error.message);
22+
}
23+
24+
@Test
25+
public void testIsNotError() {
26+
GraphError error = new GraphError();
27+
error.code = GraphErrorCodes.ACCESS_DENIED.toString();
28+
assertFalse(error.isError(GraphErrorCodes.UNAUTHENTICATED));
29+
}
30+
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.microsoft.graph.http;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
7+
import org.junit.Test;
8+
9+
public class GraphFatalServiceExceptionTests {
10+
11+
@Test
12+
public void testGraphFatalServiceException() {
13+
GraphFatalServiceException exception = new GraphFatalServiceException(null,null,new ArrayList<String>(),null,401,"Unauthenticated",new ArrayList<String>(),null, false);
14+
String expectedMessage = "Unexpected exception returned from the service." +
15+
"null null\n" +
16+
"\n" +
17+
"\n" +
18+
"401 : Unauthenticated\n" +
19+
"[...]\n" +
20+
"\n" +
21+
"[Some information was truncated for brevity, enable debug logging for more details]";
22+
String message = exception.getMessage();
23+
assertEquals(expectedMessage, message);
24+
}
25+
26+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.microsoft.graph.http;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import static org.mockito.Mockito.mock;
6+
7+
import org.junit.Test;
8+
9+
import com.google.gson.JsonObject;
10+
import com.microsoft.graph.serializer.ISerializer;
11+
12+
public class ReferenceRequestBodyTests {
13+
14+
@Test
15+
public void testRawObject() {
16+
ReferenceRequestBody body = new ReferenceRequestBody(null);
17+
ISerializer serializer = mock(ISerializer.class);
18+
JsonObject jsonObject = new JsonObject();
19+
body.setRawObject(serializer,jsonObject);
20+
assertEquals(serializer,body.getSerializer());
21+
assertEquals(jsonObject,body.getRawObject());
22+
}
23+
24+
}

0 commit comments

Comments
 (0)