22
33import com .google .gson .Gson ;
44import com .google .gson .GsonBuilder ;
5+ import com .mapbox .api .directions .v5 .models .BannerComponents ;
6+ import com .mapbox .api .directions .v5 .models .BannerInstructions ;
57import com .mapbox .api .directions .v5 .models .DirectionsResponse ;
68import com .mapbox .api .directions .v5 .models .DirectionsRoute ;
79import com .mapbox .api .directions .v5 .models .LegAnnotation ;
2729import okhttp3 .mockwebserver .RecordedRequest ;
2830import retrofit2 .Response ;
2931
32+ import static com .mapbox .api .directions .v5 .DirectionsCriteria .GEOMETRY_POLYLINE ;
33+ import static com .mapbox .api .directions .v5 .DirectionsCriteria .PROFILE_DRIVING ;
3034import static org .hamcrest .CoreMatchers .containsString ;
3135import static org .hamcrest .junit .MatcherAssert .assertThat ;
3236import static org .junit .Assert .assertEquals ;
@@ -42,6 +46,7 @@ public class MapboxDirectionsTest extends TestUtils {
4246 private static final String DIRECTIONS_V5_ANNOTATIONS_FIXTURE = "directions_annotations_v5.json" ;
4347 private static final String DIRECTIONS_V5_NO_ROUTE = "directions_v5_no_route.json" ;
4448 private static final String DIRECTIONS_V5_MAX_SPEED_ANNOTATION = "directions_v5_max_speed_annotation.json" ;
49+ private static final String DIRECTIONS_V5_BANNER_INSTRUCTIONS = "directions_v5_banner_instructions.json" ;
4550
4651 private MockWebServer server ;
4752 private HttpUrl mockUrl ;
@@ -56,21 +61,20 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
5661
5762 // Switch response on geometry parameter (only false supported, so nice and simple)
5863 String resource = DIRECTIONS_V5_FIXTURE ;
59- if (request .getPath ().contains ("geometries=polyline6" )) {
60- resource = DIRECTIONS_V5_PRECISION6_FIXTURE ;
61- }
62- if (request .getPath ().contains ("driving-traffic" )) {
63- resource = DIRECTIONS_TRAFFIC_FIXTURE ;
64- }
6564 if (request .getPath ().contains ("-77.04430" )) {
6665 resource = DIRECTIONS_ROTARY_FIXTURE ;
67- }
68- if (request .getPath ().contains ("annotations" )) {
66+ } else if (request .getPath ().contains ("annotations" )) {
6967 resource = DIRECTIONS_V5_ANNOTATIONS_FIXTURE ;
70- }
71- if (request .getPath ().contains ("151.2302,-33.9283" )) {
68+ } else if (request .getPath ().contains ("-151.2302" )) {
7269 resource = DIRECTIONS_V5_NO_ROUTE ;
70+ } else if (request .getPath ().contains ("-122.403561,37.777689" )) {
71+ resource = DIRECTIONS_V5_BANNER_INSTRUCTIONS ;
72+ } else if (request .getPath ().contains ("driving-traffic" )) {
73+ resource = DIRECTIONS_TRAFFIC_FIXTURE ;
74+ } else if (request .getPath ().contains ("geometries=polyline6" )) {
75+ resource = DIRECTIONS_V5_PRECISION6_FIXTURE ;
7376 }
77+
7478 try {
7579 String body = loadJsonFixture (resource );
7680 return new MockResponse ().setBody (body );
@@ -360,7 +364,7 @@ public void voiceUnits_doesGetFormattedInUrlCorrectly() throws Exception {
360364 .destination (Point .fromLngLat (13.4930 , 9.958 ))
361365 .origin (Point .fromLngLat (1.234 , 2.345 ))
362366 .accessToken (ACCESS_TOKEN )
363- .baseUrl (mockUrl . toString () )
367+ .baseUrl ("https://foobar.com" )
364368 .voiceUnits (DirectionsCriteria .METRIC )
365369 .build ();
366370
@@ -374,7 +378,7 @@ public void bannerInstructions_doesGetFormattedInUrlCorrectly() throws Exception
374378 .destination (Point .fromLngLat (13.4930 , 9.958 ))
375379 .origin (Point .fromLngLat (1.234 , 2.345 ))
376380 .accessToken (ACCESS_TOKEN )
377- .baseUrl (mockUrl . toString () )
381+ .baseUrl ("https://foobar.com" )
378382 .bannerInstructions (true )
379383 .build ();
380384
@@ -388,7 +392,7 @@ public void voiceInstructions_doesGetFormattedInUrlCorrectly() throws Exception
388392 .destination (Point .fromLngLat (13.4930 , 9.958 ))
389393 .origin (Point .fromLngLat (1.234 , 2.345 ))
390394 .accessToken (ACCESS_TOKEN )
391- .baseUrl (mockUrl . toString () )
395+ .baseUrl ("https://foobar.com" )
392396 .voiceInstructions (true )
393397 .build ();
394398
@@ -402,7 +406,7 @@ public void exclude_doesGetFormattedInUrlCorrectly() throws Exception {
402406 .destination (Point .fromLngLat (13.4930 , 9.958 ))
403407 .origin (Point .fromLngLat (1.234 , 2.345 ))
404408 .accessToken (ACCESS_TOKEN )
405- .baseUrl (mockUrl . toString () )
409+ .baseUrl ("https://foobar.com" )
406410 .exclude (DirectionsCriteria .EXCLUDE_MOTORWAY )
407411 .build ();
408412
@@ -439,7 +443,7 @@ public void testRadiusWithUnlimitedDistance() throws IOException {
439443 .origin (coordinates .get (0 ))
440444 .addWaypoint (coordinates .get (1 ))
441445 .destination (coordinates .get (2 ))
442- .baseUrl (mockUrl . toString () )
446+ .baseUrl ("https://foobar.com" )
443447 .radiuses (100 , Double .POSITIVE_INFINITY , 100 )
444448 .build ();
445449
@@ -450,8 +454,8 @@ public void testRadiusWithUnlimitedDistance() throws IOException {
450454 @ Test
451455 public void noValidRouteTest () throws Exception {
452456 MapboxDirections mapboxDirections = MapboxDirections .builder ()
453- .origin (Point .fromLngLat (151.2302 , -33.9283 ))
454- .destination (Point .fromLngLat (174.7654 , -36.8641 ))
457+ .origin (Point .fromLngLat (- 151.2302 , -33.9283 ))
458+ .destination (Point .fromLngLat (- 174.7654 , -36.8641 ))
455459 .baseUrl (mockUrl .toString ())
456460 .accessToken (ACCESS_TOKEN )
457461 .steps (true )
@@ -501,4 +505,51 @@ public void maxSpeedAnnotation_doesGetCreatedInResponse() throws IOException {
501505
502506 assertNotNull (maxSpeedAnnotation .maxspeed ());
503507 }
508+
509+ @ Test
510+ public void subBannerInstructions () throws Exception {
511+ Gson gson = new GsonBuilder ()
512+ .registerTypeAdapterFactory (DirectionsAdapterFactory .create ()).create ();
513+ String body = loadJsonFixture (DIRECTIONS_V5_BANNER_INSTRUCTIONS );
514+ DirectionsResponse response = gson .fromJson (body , DirectionsResponse .class );
515+
516+ BannerInstructions bannerInstructions =
517+ response .routes ().get (0 ).legs ().get (0 ).steps ().get (0 ).bannerInstructions ().get (1 );
518+
519+ assertNotNull (bannerInstructions .sub ());
520+ assertNotNull (bannerInstructions .sub ().components ());
521+
522+ BannerComponents component = bannerInstructions .sub ().components ().get (1 );
523+ assertNotNull (component .active ());
524+ assertNotNull (component .directions ());
525+ assertEquals (2 , component .directions ().size ());
526+ }
527+
528+ @ Test
529+ public void subBannerInstructionsFromJson () throws Exception {
530+
531+ MapboxDirections mapboxDirections = MapboxDirections .builder ()
532+ .origin (Point .fromLngLat (-122.403561 ,37.777689 ))
533+ .destination (Point .fromLngLat (-122.405786 ,37.770369 ))
534+ .accessToken (ACCESS_TOKEN )
535+ .profile (PROFILE_DRIVING )
536+ .steps (true )
537+ .geometries (GEOMETRY_POLYLINE )
538+ .bannerInstructions (true )
539+ .baseUrl (mockUrl .toString ())
540+ .build ();
541+
542+ Response <DirectionsResponse > response = mapboxDirections .executeCall ();
543+
544+ BannerInstructions bannerInstructions =
545+ response .body ().routes ().get (0 ).legs ().get (0 ).steps ().get (0 ).bannerInstructions ().get (1 );
546+
547+ assertNotNull (bannerInstructions .sub ());
548+ assertNotNull (bannerInstructions .sub ().components ());
549+
550+ BannerComponents component = bannerInstructions .sub ().components ().get (1 );
551+ assertNotNull (component .active ());
552+ assertNotNull (component .directions ());
553+ assertEquals (2 , component .directions ().size ());
554+ }
504555}
0 commit comments