|
| 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 | +import static org.junit.Assert.fail; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 12 | + |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import com.google.gson.JsonObject; |
| 16 | +import com.google.gson.JsonPrimitive; |
| 17 | +import com.microsoft.graph.authentication.MockAuthenticationProvider; |
| 18 | +import com.microsoft.graph.concurrency.IProgressCallback; |
| 19 | +import com.microsoft.graph.concurrency.MockExecutors; |
| 20 | +import com.microsoft.graph.core.ClientException; |
| 21 | +import com.microsoft.graph.core.GraphErrorCodes; |
| 22 | +import com.microsoft.graph.logger.LoggerLevel; |
| 23 | +import com.microsoft.graph.logger.MockLogger; |
| 24 | +import com.microsoft.graph.models.extensions.Drive; |
| 25 | +import com.microsoft.graph.models.extensions.DriveItem; |
| 26 | +import com.microsoft.graph.options.HeaderOption; |
| 27 | +import com.microsoft.graph.serializer.MockSerializer; |
| 28 | + |
| 29 | +public class CoreHttpProviderTests { |
| 30 | + |
| 31 | + private MockAuthenticationProvider mAuthenticationProvider; |
| 32 | + private CoreHttpProvider mProvider; |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testNoContentType() throws Exception { |
| 36 | + final ITestConnectionData data = new ITestConnectionData() { |
| 37 | + @Override |
| 38 | + public int getRequestCode() { |
| 39 | + return 200; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getJsonResponse() { |
| 44 | + return "{ \"id\": \"zzz\" }"; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Map<String, String> getHeaders() { |
| 49 | + return new HashMap<>(); |
| 50 | + } |
| 51 | + }; |
| 52 | + setDefaultHttpProvider(null); |
| 53 | + |
| 54 | + try { |
| 55 | + mProvider.send(new MockHttpRequest(), Drive.class, null); |
| 56 | + fail("Expected exception"); |
| 57 | + } catch (final ClientException ce) { |
| 58 | + if (!(ce.getCause() instanceof NullPointerException)) { |
| 59 | + fail("Wrong inner exception!"); |
| 60 | + } |
| 61 | + } |
| 62 | + assertEquals(1, mAuthenticationProvider.getInterceptionCount()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testPostByte() throws Exception { |
| 67 | + final String itemId = "itemId"; |
| 68 | + final ITestConnectionData data = new ITestConnectionData() { |
| 69 | + @Override |
| 70 | + public int getRequestCode() { |
| 71 | + return 200; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String getJsonResponse() { |
| 76 | + return "{ \"id\": \"zzz\" }"; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Map<String, String> getHeaders() { |
| 81 | + final HashMap<String, String> map = new HashMap<>(); |
| 82 | + map.put("Content-Type", "application/json"); |
| 83 | + return map; |
| 84 | + } |
| 85 | + }; |
| 86 | + final DriveItem expectedItem = new DriveItem(); |
| 87 | + expectedItem.id = itemId; |
| 88 | + setDefaultHttpProvider(expectedItem); |
| 89 | + |
| 90 | + final AtomicBoolean progress = new AtomicBoolean(false); |
| 91 | + final AtomicBoolean success = new AtomicBoolean(false); |
| 92 | + final AtomicBoolean failure = new AtomicBoolean(false); |
| 93 | + final IProgressCallback<DriveItem> progressCallback = new IProgressCallback<DriveItem>() { |
| 94 | + @Override |
| 95 | + public void progress(final long current, final long max) { |
| 96 | + progress.set(true); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void success(final DriveItem item) { |
| 101 | + success.set(true); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void failure(final ClientException ex) { |
| 106 | + failure.set(true); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + mProvider.send(new MockHttpRequest(), progressCallback, DriveItem.class, new byte[]{1, 2, 3, 4}); |
| 111 | + |
| 112 | + assertTrue(progress.get()); |
| 113 | + assertTrue(success.get()); |
| 114 | + assertEquals(1, mAuthenticationProvider.getInterceptionCount()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testErrorResponse() throws Exception { |
| 119 | + final GraphErrorCodes expectedErrorCode = GraphErrorCodes.INVALID_REQUEST; |
| 120 | + final String expectedMessage = "Test error!"; |
| 121 | + final GraphErrorResponse toSerialize = new GraphErrorResponse(); |
| 122 | + toSerialize.error = new GraphError(); |
| 123 | + toSerialize.error.code = expectedErrorCode.toString(); |
| 124 | + toSerialize.error.message = expectedMessage; |
| 125 | + toSerialize.error.innererror = null; |
| 126 | + |
| 127 | + setDefaultHttpProvider(toSerialize); |
| 128 | + final ITestConnectionData data = new ITestConnectionData() { |
| 129 | + @Override |
| 130 | + public int getRequestCode() { |
| 131 | + return 415; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String getJsonResponse() { |
| 136 | + return "{}"; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Map<String, String> getHeaders() { |
| 141 | + return new HashMap<>(); |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + try { |
| 146 | + mProvider.send(new MockHttpRequest(), DriveItem.class, null); |
| 147 | + fail("Expected exception in previous statement"); |
| 148 | + } catch (final GraphServiceException e) { |
| 149 | + assertTrue(e.getMessage().indexOf("truncated") > 0); |
| 150 | + assertEquals(expectedMessage, e.getServiceError().message); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testVerboseErrorResponse() throws Exception { |
| 156 | + final GraphErrorCodes expectedErrorCode = GraphErrorCodes.INVALID_REQUEST; |
| 157 | + final String expectedMessage = "Test error!"; |
| 158 | + final GraphErrorResponse toSerialize = new GraphErrorResponse(); |
| 159 | + toSerialize.error = new GraphError(); |
| 160 | + toSerialize.error.code = expectedErrorCode.toString(); |
| 161 | + toSerialize.error.message = expectedMessage; |
| 162 | + toSerialize.error.innererror = null; |
| 163 | + JsonObject raw = new JsonObject(); |
| 164 | + raw.add("response", new JsonPrimitive("The raw request was invalid")); |
| 165 | + toSerialize.rawObject = raw; |
| 166 | + |
| 167 | + MockLogger logger = new MockLogger(); |
| 168 | + logger.setLoggingLevel(LoggerLevel.DEBUG); |
| 169 | + |
| 170 | + mProvider = new CoreHttpProvider(new MockSerializer(toSerialize, ""), |
| 171 | + mAuthenticationProvider = new MockAuthenticationProvider(), |
| 172 | + new MockExecutors(), |
| 173 | + logger); |
| 174 | + |
| 175 | + final ITestConnectionData data = new ITestConnectionData() { |
| 176 | + @Override |
| 177 | + public int getRequestCode() { |
| 178 | + return 415; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public String getJsonResponse() { |
| 183 | + return "{}"; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public Map<String, String> getHeaders() { |
| 188 | + return new HashMap<>(); |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + try { |
| 193 | + mProvider.send(new MockHttpRequest(), DriveItem.class, null); |
| 194 | + fail("Expected exception in previous statement"); |
| 195 | + } catch (final GraphServiceException e) { |
| 196 | + assertFalse(e.getMessage().indexOf("truncated") > 0); |
| 197 | + assertTrue(e.getMessage().indexOf("The raw request was invalid") > 0); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void testHasHeaderReturnsTrue() { |
| 203 | + HeaderOption h = new HeaderOption("name", "value"); |
| 204 | + assertTrue(DefaultHttpProvider.hasHeader(Arrays.asList(h), "name")); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void testHasHeaderReturnsTrueWhenDifferentCase() { |
| 209 | + HeaderOption h = new HeaderOption("name", "value"); |
| 210 | + assertTrue(DefaultHttpProvider.hasHeader(Arrays.asList(h), "NAME")); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void testHasHeaderReturnsFalse() { |
| 215 | + HeaderOption h = new HeaderOption("name", "value"); |
| 216 | + assertFalse(DefaultHttpProvider.hasHeader(Arrays.asList(h), "blah")); |
| 217 | + } |
| 218 | + |
| 219 | + |
| 220 | + /** |
| 221 | + * Configures the http provider for test cases |
| 222 | + * @param toSerialize The object to serialize |
| 223 | + */ |
| 224 | + private void setDefaultHttpProvider(final Object toSerialize) { |
| 225 | + mProvider = new CoreHttpProvider(new MockSerializer(toSerialize, ""), |
| 226 | + mAuthenticationProvider = new MockAuthenticationProvider(), |
| 227 | + new MockExecutors(), |
| 228 | + new MockLogger()); |
| 229 | + } |
| 230 | + |
| 231 | +} |
0 commit comments