PeliasCallbackHandler { void handleSuccess(String title, Response<Result> response, OnPlaceDetailsFetchedListener listener) { if (!isValidResponse(response)) { listener.onFetchFailure(); return; } for (Feature feature : response.body().getFeatures()) { if (feature.properties.name.equals(title)) { Place place = converter.getFetchedPlace(feature); String details = converter.getDetails(feature, title); listener.onFetchSuccess(place, details); } } } PeliasCallbackHandler();   }
@Test public void handleSuccess_titleFeatures_shouldCallListenerForMatchingTitle() throws Exception { String title = "Place"; Response response = getTestResponse("NotPlace", "Place"); TestDetailsListener listener = new TestDetailsListener(); callbackHandler.handleSuccess(title, response, listener); assertThat(listener.place.getName()).isEqualTo(title); }
@Test public void handleSuccess_titleFeatures_shouldCreateCorrectDetails() throws Exception { String title = "Place"; Response response = getTestResponse("NotPlace", "", "Place", "Place, Label"); TestDetailsListener listener = new TestDetailsListener(); callbackHandler.handleSuccess(title, response, listener); assertThat(listener.details).isEqualTo("Place\nLabel"); }
@Test public void handleSuccess_titleFeatures_shouldCallFetchFailure() { String title = "Place"; TestDetailsListener listener = new TestDetailsListener(); Place place = new PlaceImpl.Builder().build(); listener.place = place; callbackHandler.handleSuccess(title, Response.<Result>success(null), listener); assertThat(listener.place).isNull(); listener.place = place; Result result = new Result(); callbackHandler.handleSuccess(title, Response.success(result), listener); assertThat(listener.place).isNull(); listener.place = place; result.setFeatures(new ArrayList<Feature>()); callbackHandler.handleSuccess(title, Response.success(result), listener); assertThat(listener.place).isNull(); }
@Test public void handleSuccess_response_shouldCallFetchSuccess() { Response<Result> response = getTestResponse("Place", "OtherPlace"); TestDetailsListener listener = new TestDetailsListener(); callbackHandler.handleSuccess(response, listener); assertThat(listener.place.getName()).isEqualTo("Place"); }
@Test public void handleSuccess_response_shouldCallFetchSuccessWithCorrectDetails() { Response<Result> response = getTestResponse("Place", "Place, Label", "OtherPlace", ""); TestDetailsListener listener = new TestDetailsListener(); callbackHandler.handleSuccess(response, listener); assertThat(listener.details).isEqualTo("Place\nLabel"); }
@Test public void handleSuccess_response_shouldCallFetchFailure() { TestDetailsListener listener = new TestDetailsListener(); Place place = new PlaceImpl.Builder().build(); listener.place = place; callbackHandler.handleSuccess(Response.<Result>success(null), listener); assertThat(listener.place).isNull(); listener.place = place; Result result = new Result(); callbackHandler.handleSuccess(Response.success(result), listener); assertThat(listener.place).isNull(); listener.place = place; result.setFeatures(new ArrayList<Feature>()); callbackHandler.handleSuccess(Response.success(result), listener); assertThat(listener.place).isNull(); }