|
| 1 | +package com.mapbox.services.api.rx.directionsmatrix.v1; |
| 2 | + |
| 3 | +import com.mapbox.services.api.ServicesException; |
| 4 | +import com.mapbox.services.api.directions.v5.DirectionsCriteria; |
| 5 | +import com.mapbox.services.api.directionsmatrix.v1.models.DirectionsMatrixResponse; |
| 6 | +import com.mapbox.services.commons.models.Position; |
| 7 | + |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Rule; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.rules.ExpectedException; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.charset.Charset; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Paths; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import io.reactivex.observers.TestObserver; |
| 22 | +import okhttp3.HttpUrl; |
| 23 | +import okhttp3.mockwebserver.MockResponse; |
| 24 | +import okhttp3.mockwebserver.MockWebServer; |
| 25 | +import okhttp3.mockwebserver.RecordedRequest; |
| 26 | + |
| 27 | +import static org.junit.Assert.assertEquals; |
| 28 | + |
| 29 | +public class MapboxDirectionsMatrixRxTest { |
| 30 | + |
| 31 | + public static final String DIRECTIONS_MATRIX_3X3_FIXTURE |
| 32 | + = "../libjava-services/src/test/fixtures/directions_matrix_3x3.json"; |
| 33 | + |
| 34 | + private MockWebServer server; |
| 35 | + private HttpUrl mockUrl; |
| 36 | + |
| 37 | + private List<Position> positions; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() throws IOException { |
| 41 | + server = new MockWebServer(); |
| 42 | + |
| 43 | + server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() { |
| 44 | + @Override |
| 45 | + public MockResponse dispatch(RecordedRequest request) throws InterruptedException { |
| 46 | + try { |
| 47 | + String body = new String( |
| 48 | + Files.readAllBytes(Paths.get(DIRECTIONS_MATRIX_3X3_FIXTURE)), Charset.forName("utf-8") |
| 49 | + ); |
| 50 | + return new MockResponse().setBody(body); |
| 51 | + } catch (IOException ioException) { |
| 52 | + throw new RuntimeException(ioException); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + server.start(); |
| 59 | + |
| 60 | + mockUrl = server.url(""); |
| 61 | + |
| 62 | + positions = new ArrayList<>(); |
| 63 | + positions.add(Position.fromCoordinates(-122.42, 37.78)); |
| 64 | + positions.add(Position.fromCoordinates(-122.45, 37.91)); |
| 65 | + positions.add(Position.fromCoordinates(-122.48, 37.73)); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void tearDown() throws IOException { |
| 70 | + server.shutdown(); |
| 71 | + } |
| 72 | + |
| 73 | + @Rule |
| 74 | + public ExpectedException thrown = ExpectedException.none(); |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testSanityRX() throws ServicesException { |
| 78 | + MapboxDirectionsMatrixRx client = new MapboxDirectionsMatrixRx.Builder() |
| 79 | + .setAccessToken("pk.XXX") |
| 80 | + .setCoordinates(positions) |
| 81 | + .setProfile(DirectionsCriteria.PROFILE_DRIVING) |
| 82 | + .setBaseUrl(mockUrl.toString()) |
| 83 | + .build(); |
| 84 | + |
| 85 | + TestObserver<DirectionsMatrixResponse> testObserver = new TestObserver(); |
| 86 | + client.getObservable().subscribe(testObserver); |
| 87 | + testObserver.assertComplete(); |
| 88 | + testObserver.assertNoErrors(); |
| 89 | + testObserver.assertValueCount(1); |
| 90 | + |
| 91 | + List<List<Object>> events = testObserver.getEvents(); |
| 92 | + assertEquals(1, events.get(0).size()); |
| 93 | + |
| 94 | + DirectionsMatrixResponse response = (DirectionsMatrixResponse) events.get(0).get(0); |
| 95 | + assertEquals(response.getCode(), DirectionsCriteria.RESPONSE_OK); |
| 96 | + } |
| 97 | +} |
0 commit comments