|
1 | 1 | package com.mapbox.api.optimization.v1; |
2 | 2 |
|
3 | | -import static org.hamcrest.CoreMatchers.startsWith; |
4 | | -import static org.junit.Assert.*; |
5 | | -import static org.junit.Assert.assertTrue; |
6 | | - |
| 3 | +import com.mapbox.api.optimization.v1.models.OptimizationResponse; |
7 | 4 | import com.mapbox.core.TestUtils; |
8 | | -import com.mapbox.geojson.Point; |
9 | 5 | import com.mapbox.core.exceptions.ServicesException; |
10 | | - |
| 6 | +import com.mapbox.geojson.Point; |
11 | 7 | import okhttp3.HttpUrl; |
12 | 8 | import okhttp3.mockwebserver.MockResponse; |
13 | 9 | import okhttp3.mockwebserver.MockWebServer; |
|
17 | 13 | import org.junit.Before; |
18 | 14 | import org.junit.Rule; |
19 | 15 | import org.junit.Test; |
| 16 | +import org.mockito.MockedConstruction; |
| 17 | +import org.mockito.Mockito; |
| 18 | +import retrofit2.Call; |
| 19 | +import retrofit2.Callback; |
| 20 | +import retrofit2.Response; |
20 | 21 |
|
21 | 22 | import java.io.IOException; |
22 | 23 | import java.util.ArrayList; |
23 | 24 | import java.util.List; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +import static org.hamcrest.CoreMatchers.startsWith; |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertNotNull; |
| 31 | +import static org.junit.Assert.assertTrue; |
| 32 | +import static org.mockito.ArgumentMatchers.any; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.when; |
24 | 35 |
|
25 | 36 | public class MapboxOptimizationTest extends TestUtils { |
26 | 37 |
|
27 | 38 | private static final String OPTIMIZATION_DISTRIBUTION = "optimized_trip_distributions.json"; |
28 | 39 | private static final String OPTIMIZATION_FIXTURE = "optimization.json"; |
29 | | - private static final String OPTIMIZATION_STEPS= "optimized_trip_steps.json"; |
| 40 | + private static final String OPTIMIZATION_STEPS = "optimized_trip_steps.json"; |
30 | 41 |
|
31 | 42 | private MockWebServer server; |
32 | 43 | private HttpUrl mockUrl; |
@@ -120,4 +131,82 @@ public void testUserAgent() throws ServicesException, IOException { |
120 | 131 | .build(); |
121 | 132 | assertTrue(client.executeCall().raw().request().header("User-Agent").contains("APP")); |
122 | 133 | } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void executeCall_modifiesResponse() throws IOException { |
| 137 | + final Response<OptimizationResponse> modifiedResponse = mock(Response.class); |
| 138 | + final MockedConstruction.MockInitializer<OptimizationResponseFactory> mockInitializer = (mock, context) -> |
| 139 | + when(mock.generate(any())).thenReturn(modifiedResponse); |
| 140 | + try (MockedConstruction ignored = Mockito.mockConstruction(OptimizationResponseFactory.class, mockInitializer)) { |
| 141 | + MapboxOptimization client = MapboxOptimization.builder() |
| 142 | + .clientAppName("APP") |
| 143 | + .accessToken(ACCESS_TOKEN) |
| 144 | + .coordinate(Point.fromLngLat(1.0, 1.0)) |
| 145 | + .coordinate(Point.fromLngLat(1.0, 1.0)) |
| 146 | + .baseUrl(mockUrl.url().toString()) |
| 147 | + .build(); |
| 148 | + assertEquals(modifiedResponse, client.executeCall()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void enqueueCall_onSuccess_modifiesResponse() throws Exception { |
| 154 | + final CountDownLatch latch = new CountDownLatch(1); |
| 155 | + MapboxOptimization client = MapboxOptimization.builder() |
| 156 | + .clientAppName("APP") |
| 157 | + .accessToken(ACCESS_TOKEN) |
| 158 | + .coordinate(Point.fromLngLat(1.0, 1.0)) |
| 159 | + .coordinate(Point.fromLngLat(1.0, 1.0)) |
| 160 | + .baseUrl(mockUrl.url().toString()) |
| 161 | + .build(); |
| 162 | + List<Response<OptimizationResponse>> actualResponses = new ArrayList<>(); |
| 163 | + client.enqueueCall(new Callback<OptimizationResponse>() { |
| 164 | + @Override |
| 165 | + public void onResponse(Call<OptimizationResponse> call, Response<OptimizationResponse> response) { |
| 166 | + actualResponses.add(response); |
| 167 | + latch.countDown(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onFailure(Call<OptimizationResponse> call, Throwable t) { |
| 172 | + latch.countDown(); |
| 173 | + } |
| 174 | + }); |
| 175 | + latch.await(5, TimeUnit.SECONDS); |
| 176 | + assertEquals(1, actualResponses.size()); |
| 177 | + assertEquals("0", actualResponses.get(0).body().trips().get(0).routeIndex()); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void enqueueCall_onFailure() throws InterruptedException { |
| 182 | + final CountDownLatch latch = new CountDownLatch(1); |
| 183 | + MapboxOptimization client = MapboxOptimization.builder() |
| 184 | + .clientAppName("APP") |
| 185 | + .accessToken(ACCESS_TOKEN) |
| 186 | + .coordinate(Point.fromLngLat(1.0, 1.0)) |
| 187 | + .coordinate(Point.fromLngLat(1.0, 1.0)) |
| 188 | + .baseUrl(mockUrl.url().toString()) |
| 189 | + .build(); |
| 190 | + server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() { |
| 191 | + @Override |
| 192 | + public MockResponse dispatch(RecordedRequest request) { |
| 193 | + return new MockResponse().setResponseCode(500).setStatus("Error"); |
| 194 | + } |
| 195 | + }); |
| 196 | + List<Throwable> actualExceptions = new ArrayList<>(); |
| 197 | + client.enqueueCall(new Callback<OptimizationResponse>() { |
| 198 | + @Override |
| 199 | + public void onResponse(Call<OptimizationResponse> call, Response<OptimizationResponse> response) { |
| 200 | + latch.countDown(); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public void onFailure(Call<OptimizationResponse> call, Throwable t) { |
| 205 | + actualExceptions.add(t); |
| 206 | + latch.countDown(); |
| 207 | + } |
| 208 | + }); |
| 209 | + latch.await(5, TimeUnit.SECONDS); |
| 210 | + assertEquals(1, actualExceptions.size()); |
| 211 | + } |
123 | 212 | } |
0 commit comments