Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garamb1 committed Apr 10, 2024
1 parent da58c8f commit 626223c
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package it.garambo.retrosearch;

import it.garambo.retrosearch.news.repository.InMemoryNewsRepository;
import it.garambo.retrosearch.news.repository.NewsRepository;
import it.garambo.retrosearch.sports.football.repository.FootballRepository;
import it.garambo.retrosearch.sports.football.repository.InMemoryFootballRepository;
import org.springframework.boot.test.context.TestConfiguration;
Expand All @@ -11,7 +13,13 @@ public class PrimaryTestConfiguration {

@Bean
@Primary
public FootballRepository repository() {
public FootballRepository footballRepository() {
return new InMemoryFootballRepository();
}

@Bean
@Primary
public NewsRepository newsRepository() {
return new InMemoryNewsRepository();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package it.garambo.retrosearch.news.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import it.garambo.retrosearch.PrimaryTestConfiguration;
import it.garambo.retrosearch.http.HttpService;
import it.garambo.retrosearch.news.model.GNewsApiResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.test.util.ReflectionTestUtils;

@ExtendWith(MockitoExtension.class)
@SpringBootTest(classes = PrimaryTestConfiguration.class)
class GNewsApiClientTest {

@Mock private HttpService httpService;

@InjectMocks GNewsApiClient client;

@BeforeEach
void setup() {
ReflectionTestUtils.setField(client, "apiKey", "testKey");
}

@Test
void testFetchNews(@Value("classpath:news/response.json") Resource responseJson)
throws IOException, URISyntaxException {
URI uri = new URI("https://gnews.io/api/v4/top-headlines");

Map<String, String> params =
Map.of(
"category", "general",
"max", "10",
"lang", "it",
"country", "it",
"apikey", "testKey");

when(httpService.get(eq(uri), eq(params)))
.thenReturn(responseJson.getContentAsString(StandardCharsets.UTF_8));

GNewsApiResponse response = client.fetchNews("it", "it");
assertNotNull(response);
assertEquals(54904, response.totalArticles());
assertNotNull(response.articles());
assertEquals(1, response.articles().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package it.garambo.retrosearch.news.repository;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.ObjectMapper;
import it.garambo.retrosearch.PrimaryTestConfiguration;
import it.garambo.retrosearch.news.model.Article;
import it.garambo.retrosearch.news.model.GNewsApiResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;

@SpringBootTest(classes = PrimaryTestConfiguration.class)
@ExtendWith(MockitoExtension.class)
class InMemoryNewsRepositoryTest {

@Autowired NewsRepository newsRepository;

@Test
void testUpdate(@Value("classpath:news/response.json") Resource responseJson)
throws IOException, URISyntaxException {

String content = responseJson.getContentAsString(StandardCharsets.UTF_8);
GNewsApiResponse response = new ObjectMapper().readValue(content, GNewsApiResponse.class);
assertNotNull(response);

Map<String, List<Article>> articles = Map.of("Example", response.articles());

newsRepository.updateAll(articles);

assertNotNull(newsRepository.getUpdatedAt());
assertTrue(newsRepository.isCountrySupported("Example"));
assertNotNull(newsRepository.getArticlesByCountry("Example"));
assertEquals(1, newsRepository.getArticlesByCountry("Example").size());
assertFalse(newsRepository.isCountrySupported("Wrong"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@ExtendWith(MockitoExtension.class)
class InMemoryFootballRepositoryTest {

@Autowired FootballRepository repository;
@Autowired FootballRepository footballRepository;

@Test
void testUpdate(@Value("classpath:sports/football/response.json") Resource responseJson)
Expand All @@ -31,14 +31,14 @@ void testUpdate(@Value("classpath:sports/football/response.json") Resource respo
FootballDataResponse response =
new ObjectMapper().readValue(content, FootballDataResponse.class);
assertNotNull(response);
repository.updateAll(response.matches());
assertNotNull(repository.getUpdatedAt());
footballRepository.updateAll(response.matches());
assertNotNull(footballRepository.getUpdatedAt());

Map<String, Set<Match>> matches = repository.getAllMatches();
Map<String, Set<Match>> matches = footballRepository.getAllMatches();
assertEquals(7, matches.keySet().size());
assertTrue(matches.containsKey("Italy"));

Set<Match> italianMatches = repository.getAllMatchesByArea("Italy");
Set<Match> italianMatches = footballRepository.getAllMatchesByArea("Italy");
assertEquals(5, italianMatches.size());
}
}
17 changes: 17 additions & 0 deletions src/test/resources/news/response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"totalArticles": 54904,
"articles": [
{
"title": "Google's Pixel 7 and 7 Pro’s design gets revealed even more with fresh crisp renders",
"description": "Now we have a complete image of what the next Google flagship phones will look like. All that's left now is to welcome them during their October announcement!",
"content": "Google’s highly anticipated upcoming Pixel 7 series is just around the corner, scheduled to be announced on October 6, 2022, at 10 am EDT during the Made by Google event. Well, not that there is any lack of images showing the two new Google phones, b... [1419 chars]",
"url": "https://www.phonearena.com/news/google-pixel-7-and-pro-design-revealed-even-more-fresh-renders_id142800",
"image": "https://m-cdn.phonearena.com/images/article/142800-wide-two_1200/Googles-Pixel-7-and-7-Pros-design-gets-revealed-even-more-with-fresh-crisp-renders.jpg",
"publishedAt": "2022-09-28T08:14:24Z",
"source": {
"name": "PhoneArena",
"url": "https://www.phonearena.com"
}
}
]
}

0 comments on commit 626223c

Please sign in to comment.