Java tutorial
/** * Copyright 2014 Andy Godwin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package spring.travel.site.services.news; import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.google.common.cache.CacheBuilder; import com.ning.http.client.AsyncHttpClient; import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.springframework.test.util.ReflectionTestUtils; import spring.travel.site.model.NewsItem; import spring.travel.site.services.HttpClient; import spring.travel.site.utils.HandOff; import java.io.InputStream; import java.util.List; import java.util.Optional; import java.util.Scanner; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.client.WireMock.verify; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static spring.travel.site.controllers.WireMockSupport.stubGet; public class NewsServiceTest { @Rule public WireMockRule wireMockRule = new WireMockRule(9101); private NewsService newsService; private AsyncHttpClient asyncHttpClient; @Before public void before() { newsService = new NewsService(); asyncHttpClient = new AsyncHttpClient(); HttpClient client = new HttpClient(null, asyncHttpClient); ReflectionTestUtils.setField(newsService, "client", client); ReflectionTestUtils.setField(newsService, "newsCache", CacheBuilder.newBuilder().build()); ReflectionTestUtils.setField(newsService, "newsDigester", new NewsDigester()); ReflectionTestUtils.setField(newsService, "newsServiceUrl", "http://localhost:9101/news"); } @After public void after() { asyncHttpClient.close(); } @Test @Ignore public void shouldReadTheNews() throws Exception { stubNewsData("/news", "/grauniad-travel-news.xml"); HandOff<List<NewsItem>> handOff = new HandOff<>(); newsService.news().whenComplete((items, t) -> handOff.put(items.get())); List<NewsItem> newsItems = handOff.get(1); assertEquals(2, newsItems.size()); NewsItem first = newsItems.get(0); assertEquals("Londons first board game cafe to open in Hackney", first.getHeadline()); assertEquals( "Draughts, the capitals latest concept cafe, hopes to capitalise on a new trend for beer" + " and board games, with over 500 different tabletop games on offer", first.getStandFirst()); assertEquals( "http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/" + "2014/7/16/1405502998989/e83ad4cc-ec6b-45c7-9270-79b37d905742-460x276.jpeg", first.getImage()); NewsItem second = newsItems.get(1); assertEquals("Travel tips: Oslos trendy new suburb Grnerlkka, and the weeks best deals", second.getHeadline()); assertEquals( "Edgy and urban, this once gritty corner of the Norwegian capital has been given a new lease of" + " life. Plus, beach huts in Devon and cheap villas in Ibiza", second.getStandFirst()); assertEquals( "http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/" + "2014/7/9/1404922550268/09c1c517-bde3-4edc-adf6-0f0deb01b419-460x276.jpeg", second.getImage()); } @Test @Ignore public void shouldReturnNewsFromTheCacheIfPresent() throws Exception { stubNewsData("/news", "/grauniad-travel-news.xml"); HandOff<List<NewsItem>> handOff = new HandOff<>(3); newsService.news().whenComplete((items1, t1) -> { handOff.put(items1.get()); newsService.news().whenComplete((items2, t2) -> handOff.put(items2.get())); newsService.news().whenComplete((items3, t3) -> handOff.put(items3.get())); }); List<List<NewsItem>> items = handOff.getAll(1); assertEquals(3, items.size()); assertEquals(2, items.get(0).size()); assertEquals(2, items.get(1).size()); assertEquals(2, items.get(2).size()); verify(1, getRequestedFor(urlEqualTo("/news"))); } // TODO: Like the test below, need to refactor news service to return empty list instead of optional @Test public void shouldReturnNoneIfNewsDigesterBarfsReadingTheNews() throws Exception { stubGet("/news", ""); HandOff<Optional<List<NewsItem>>> handOff = new HandOff<>(); newsService.news().whenComplete((items, t) -> handOff.put(items)); Optional<List<NewsItem>> newsItems = handOff.get(2); assertThat(newsItems.isPresent(), is(false)); } @Ignore("Needs to return an empty list on failure instead of Optional.empty") @Test public void shouldReturnEmptyListIfNewsServiceReturns404() throws Exception { stubFor(WireMock.get(urlEqualTo("/news")).willReturn(aResponse().withStatus(404))); HandOff<List<NewsItem>> handOff = new HandOff<>(); newsService.news().whenComplete((items, t) -> items.ifPresent(it -> handOff.put(it))); List<NewsItem> newsItems = handOff.get(1); assertEquals(0, newsItems.size()); } private void stubNewsData(String url, String filename) throws Exception { InputStream inputStream = getClass().getResourceAsStream(filename); String stubData = new Scanner(inputStream).useDelimiter("\\A").next(); stubGet(url, stubData); } }