-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/java/it/garambo/retrosearch/news/client/GNewsApiClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/it/garambo/retrosearch/news/repository/InMemoryNewsRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |