2929import okhttp3 .mockwebserver .RecordedRequest ;
3030import retrofit2 .Response ;
3131
32+ import static com .mapbox .api .directions .v5 .DirectionsCriteria .APPROACH_CURB ;
33+ import static com .mapbox .api .directions .v5 .DirectionsCriteria .APPROACH_UNRESTRICTED ;
3234import static com .mapbox .api .directions .v5 .DirectionsCriteria .GEOMETRY_POLYLINE ;
3335import static com .mapbox .api .directions .v5 .DirectionsCriteria .PROFILE_DRIVING ;
3436import static org .hamcrest .CoreMatchers .containsString ;
37+ import static org .hamcrest .Matchers .startsWith ;
3538import static org .hamcrest .junit .MatcherAssert .assertThat ;
3639import static org .junit .Assert .assertEquals ;
3740import static org .junit .Assert .assertNotNull ;
@@ -47,6 +50,7 @@ public class MapboxDirectionsTest extends TestUtils {
4750 private static final String DIRECTIONS_V5_NO_ROUTE = "directions_v5_no_route.json" ;
4851 private static final String DIRECTIONS_V5_MAX_SPEED_ANNOTATION = "directions_v5_max_speed_annotation.json" ;
4952 private static final String DIRECTIONS_V5_BANNER_INSTRUCTIONS = "directions_v5_banner_instructions.json" ;
53+ private static final String DIRECTIONS_V5_APPROACHES_REQUEST = "directions_v5_approaches.json" ;
5054
5155 private MockWebServer server ;
5256 private HttpUrl mockUrl ;
@@ -65,6 +69,8 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
6569 resource = DIRECTIONS_ROTARY_FIXTURE ;
6670 } else if (request .getPath ().contains ("annotations" )) {
6771 resource = DIRECTIONS_V5_ANNOTATIONS_FIXTURE ;
72+ } else if (request .getPath ().contains ("approaches" )) {
73+ resource = DIRECTIONS_V5_APPROACHES_REQUEST ;
6874 } else if (request .getPath ().contains ("-151.2302" )) {
6975 resource = DIRECTIONS_V5_NO_ROUTE ;
7076 } else if (request .getPath ().contains ("-122.403561,37.777689" )) {
@@ -552,4 +558,64 @@ public void subBannerInstructionsFromJson() throws Exception {
552558 assertNotNull (component .directions ());
553559 assertEquals (2 , component .directions ().size ());
554560 }
561+
562+ @ Test
563+ public void sanityApproachesInstructions () throws Exception {
564+ MapboxDirections mapboxDirections = MapboxDirections .builder ()
565+ .origin (Point .fromLngLat (1.0 , 1.0 ))
566+ .addWaypoint (Point .fromLngLat (2.0 , 2.0 ))
567+ .addWaypoint (Point .fromLngLat (3.0 , 3.0 ))
568+ .destination (Point .fromLngLat (4.0 , 4.0 ))
569+ .addApproaches (APPROACH_UNRESTRICTED , null , "" , APPROACH_CURB )
570+ .baseUrl ("https://foobar.com" )
571+ .accessToken (ACCESS_TOKEN )
572+ .build ();
573+ assertNotNull (mapboxDirections );
574+ assertTrue (mapboxDirections .cloneCall ().request ().url ().toString ()
575+ .contains ("approaches=unrestricted;;;curb" ));
576+ }
577+ @ Test
578+ public void build_exceptionThrownWhenNumApproachesDoesNotMatchCoordinates () throws Exception {
579+ thrown .expect (ServicesException .class );
580+ thrown .expectMessage (
581+ startsWith ("Number of approach elements must match" ));
582+ MapboxDirections mapboxDirections = MapboxDirections .builder ()
583+ .origin (Point .fromLngLat (2.0 , 2.0 ))
584+ .destination (Point .fromLngLat (4.0 , 4.0 ))
585+ .addApproaches (APPROACH_UNRESTRICTED )
586+ .baseUrl ("https://foobar.com" )
587+ .accessToken (ACCESS_TOKEN )
588+ .build ();
589+ }
590+
591+ @ Test
592+ public void build_exceptionThrownWhenInvalidApproaches () throws Exception {
593+ thrown .expect (ServicesException .class );
594+ thrown .expectMessage (
595+ startsWith ("All approaches values must be one of curb, unrestricted" ));
596+ MapboxDirections mapboxDirections = MapboxDirections .builder ()
597+ .origin (Point .fromLngLat (2.0 , 2.0 ))
598+ .destination (Point .fromLngLat (4.0 , 4.0 ))
599+ .addApproaches (APPROACH_UNRESTRICTED , "restricted" )
600+ .baseUrl ("https://foobar.com" )
601+ .accessToken (ACCESS_TOKEN )
602+ .build ();
603+ }
604+
605+ @ Test
606+ public void testApproaches () throws Exception {
607+ MapboxDirections mapboxDirections = MapboxDirections .builder ()
608+ .profile (PROFILE_DRIVING )
609+ .origin (Point .fromLngLat (13.4301 ,52.5109 ))
610+ .destination (Point .fromLngLat (13.432508 ,52.501725 ))
611+ .addApproaches (APPROACH_UNRESTRICTED , APPROACH_CURB )
612+ .accessToken (ACCESS_TOKEN )
613+ .baseUrl (mockUrl .toString ())
614+ .build ();
615+
616+ mapboxDirections .setCallFactory (null );
617+ Response <DirectionsResponse > response = mapboxDirections .executeCall ();
618+ assertEquals (200 , response .code ());
619+ assertEquals ("Ok" , response .body ().code ());
620+ }
555621}
0 commit comments