|
| 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.assertNotNull; |
| 6 | +import static org.junit.Assert.assertNull; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | + |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.concurrent.ExecutionException; |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 14 | + |
| 15 | +import static org.mockito.Mockito.mock; |
| 16 | + |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import okhttp3.Call; |
| 21 | +import okhttp3.MediaType; |
| 22 | +import okhttp3.OkHttpClient; |
| 23 | +import okhttp3.Protocol; |
| 24 | +import okhttp3.Request; |
| 25 | +import okhttp3.Response; |
| 26 | +import okhttp3.ResponseBody; |
| 27 | + |
| 28 | +import com.google.gson.JsonObject; |
| 29 | +import com.google.gson.JsonPrimitive; |
| 30 | +import com.microsoft.graph.core.ClientException; |
| 31 | +import com.microsoft.graph.core.IBaseGraphServiceClient; |
| 32 | +import com.microsoft.graph.options.FunctionOption; |
| 33 | +import com.microsoft.graph.options.Option; |
| 34 | +import com.microsoft.graph.options.QueryOption; |
| 35 | +import com.microsoft.graph.core.BaseGraphServiceClient; |
| 36 | + |
| 37 | +/** |
| 38 | + * Test cases for {@see BaseRequest} |
| 39 | + */ |
| 40 | +public class BaseRequestTests { |
| 41 | + private IBaseGraphServiceClient mBaseClient; |
| 42 | + private BaseRequest<JsonObject> mRequest; |
| 43 | + private JsonObject callbackJsonObject; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() throws Exception { |
| 47 | + final Response response = new Response.Builder() |
| 48 | + .request(new Request.Builder().url("https://a.b.c").build()) |
| 49 | + .protocol(Protocol.HTTP_1_1) |
| 50 | + .code(200).message("OK").body( |
| 51 | + ResponseBody.create(MediaType.parse("application/json"), |
| 52 | + "{ \"id\": \"zzz\" }" |
| 53 | + )) |
| 54 | + .addHeader("Content-Type", "application/json") |
| 55 | + .build(); |
| 56 | + final OkHttpClient mockClient = BaseStreamRequestTests.getMockClient(response); |
| 57 | + final JsonObject result = new JsonObject(); |
| 58 | + result.add("id", new JsonPrimitive("zzz")); |
| 59 | + mBaseClient = BaseGraphServiceClient.builder() |
| 60 | + .httpClient(mockClient) |
| 61 | + .buildClient(); |
| 62 | + mRequest = new BaseRequest<JsonObject>("https://a.b.c/", mBaseClient, null,JsonObject.class){}; |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testSend() { |
| 67 | + final JsonObject result = mRequest.send(HttpMethod.GET, null); |
| 68 | + assertNotNull(result); |
| 69 | + assertEquals("zzz", result.get("id").getAsString()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testSendWithCallback() throws InterruptedException, ExecutionException { |
| 74 | + final java.util.concurrent.CompletableFuture<JsonObject> result = mRequest.futureSend(HttpMethod.GET, null); |
| 75 | + assertNotNull(result.get()); |
| 76 | + assertTrue(result.isDone()); |
| 77 | + assertFalse(result.isCancelled()); |
| 78 | + assertEquals("zzz", result.get().get("id").getAsString()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testFunctionParameters() { |
| 83 | + final Option fo1 = new FunctionOption("1", "one"); |
| 84 | + final Option fo2 = new FunctionOption("2", null); |
| 85 | + final BaseRequest<Void> request = new BaseRequest<Void>("https://a.b.c/", null, Arrays.asList(fo1, fo2), null){}; |
| 86 | + assertEquals("https://a.b.c/(1='one',2=null)", request.getRequestUrl().toString()); |
| 87 | + request.addFunctionOption(new FunctionOption("3","two"));; |
| 88 | + assertEquals("https://a.b.c/(1='one',2=null,3='two')", request.getRequestUrl().toString()); |
| 89 | + assertEquals(4, request.getOptions().size()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testQueryParameters() { |
| 94 | + final Option q1 = new QueryOption("q1","option1 "); |
| 95 | + final Option q2 = new QueryOption("q2","option2"); |
| 96 | + final BaseRequest<Void> request = new BaseRequest<Void>("https://a.b.c/", null, Arrays.asList(q1, q2), null){}; |
| 97 | + assertEquals("https://a.b.c/?q1=option1%20&q2=option2", request.getRequestUrl().toString()); |
| 98 | + request.addQueryOption(new QueryOption("q3","option3")); |
| 99 | + assertEquals("https://a.b.c/?q1=option1%20&q2=option2&q3=option3", request.getRequestUrl().toString()); |
| 100 | + assertEquals(4,request.getOptions().size()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testFunctionAndQueryParameters() { |
| 105 | + final Option f1 = new FunctionOption("f1", "fun1"); |
| 106 | + final Option f2 = new FunctionOption("f2", null); |
| 107 | + final Option q1 = new QueryOption("q1","option1 "); |
| 108 | + final Option q2 = new QueryOption("q2","option2"); |
| 109 | + final BaseRequest<Void> request = new BaseRequest<Void>("https://a.b.c/", null, Arrays.asList(f1, f2, q1, q2), null){}; |
| 110 | + assertEquals("https://a.b.c/(f1='fun1',f2=null)?q1=option1%20&q2=option2", request.getRequestUrl().toString()); |
| 111 | + assertEquals(5, request.getOptions().size()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testHttpMethod() { |
| 116 | + final BaseRequest<Void> request = new BaseRequest<Void>("https://a.b.c/", null, null, null){}; |
| 117 | + assertNull(request.getHttpMethod()); |
| 118 | + request.setHttpMethod(HttpMethod.GET); |
| 119 | + assertEquals(HttpMethod.GET, request.getHttpMethod()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testHeader() { |
| 124 | + final String expectedHeader = "header key"; |
| 125 | + final String expectedValue = "header value"; |
| 126 | + final BaseRequest<Void> request = new BaseRequest<Void>("https://a.b.c/", null, null, null){}; |
| 127 | + assertEquals(1, request.getHeaders().size()); |
| 128 | + assertEquals("SdkVersion", request.getHeaders().get(0).getName()); |
| 129 | + //assertEquals(String.format("graph-android-v%s", BuildConfig.VERSION_NAME), request.getHeaders().get(0).getValue()); |
| 130 | + request.addHeader(expectedHeader,expectedValue); |
| 131 | + assertEquals(2,request.getHeaders().size()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testProtectedProperties() { |
| 136 | + assertEquals(0, mRequest.functionOptions.size()); |
| 137 | + assertEquals(0, mRequest.queryOptions.size()); |
| 138 | + final Option q1 = new QueryOption("q1","option1 "); |
| 139 | + final Option f1 = new FunctionOption("f1","option2"); |
| 140 | + final BaseRequest<Void> request = new BaseRequest<Void>("https://a.b.c/", null, Arrays.asList(q1,f1), null){}; |
| 141 | + assertEquals(1, request.functionOptions.size()); |
| 142 | + assertEquals(1, request.queryOptions.size()); |
| 143 | + assertEquals("q1", request.queryOptions.get(0).getName()); |
| 144 | + assertEquals("option1 ", request.queryOptions.get(0).getValue()); |
| 145 | + assertEquals("f1", request.functionOptions.get(0).getName()); |
| 146 | + assertEquals("option2", request.functionOptions.get(0).getValue()); |
| 147 | + } |
| 148 | +} |
0 commit comments