|
@@ -2,26 +2,25 @@ package com.stockr.server.news;
|
2
|
2
|
|
3
|
3
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
4
|
4
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
5
|
+import org.hamcrest.Matchers;
|
|
6
|
+import org.junit.Assert;
|
5
|
7
|
import org.junit.Before;
|
6
|
8
|
import org.junit.Ignore;
|
7
|
9
|
import org.junit.Test;
|
8
|
10
|
import org.junit.runner.RunWith;
|
9
|
|
-import org.mockito.InjectMocks;
|
10
|
|
-import org.mockito.Mock;
|
11
|
|
-import org.mockito.Mockito;
|
12
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
12
|
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
|
14
|
13
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
|
14
|
+import org.springframework.http.HttpMethod;
|
15
|
15
|
import org.springframework.http.MediaType;
|
16
|
16
|
import org.springframework.test.context.junit4.SpringRunner;
|
17
|
17
|
import org.springframework.test.web.client.MockRestServiceServer;
|
18
|
|
-import org.springframework.web.client.RestTemplate;
|
19
|
18
|
|
20
|
|
-import javax.print.attribute.standard.Media;
|
21
|
|
-import java.awt.*;
|
22
|
19
|
import java.util.ArrayList;
|
23
|
20
|
|
24
|
21
|
import static org.junit.Assert.*;
|
|
22
|
+import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
|
|
23
|
+import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
|
25
|
24
|
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
26
|
25
|
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
27
|
26
|
|
|
@@ -37,36 +36,64 @@ public class NewsServiceTest {
|
37
|
36
|
@Autowired
|
38
|
37
|
private ObjectMapper om;
|
39
|
38
|
|
|
39
|
+ private String JSON;
|
|
40
|
+
|
40
|
41
|
@Before
|
41
|
42
|
public void setUp() throws JsonProcessingException {
|
|
43
|
+ setUpJsonResponse();
|
|
44
|
+ setUpMrss();
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ private void setUpMrss() {
|
|
48
|
+ mrss.expect(requestTo(ns.insertQueryInUrl("test")))
|
|
49
|
+ .andExpect(method(HttpMethod.GET))
|
|
50
|
+ .andExpect(header("X-Api-Key", Matchers.containsString("")))
|
|
51
|
+ .andRespond(withSuccess(JSON, MediaType.APPLICATION_JSON));
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ private void setUpJsonResponse() throws JsonProcessingException {
|
42
|
55
|
ArrayList<Article> al = new ArrayList<>();
|
43
|
56
|
Article a = new Article();
|
44
|
57
|
a.setAuthor("Author Name");
|
45
|
58
|
al.add(a);
|
46
|
59
|
News n = new News();
|
47
|
60
|
n.setArticles(al);
|
|
61
|
+ JSON = om.writeValueAsString(n);
|
|
62
|
+ }
|
48
|
63
|
|
49
|
|
- String nstr = om.writeValueAsString(n);
|
50
|
|
- String url = "https://newsapi.org/v2/everything?q=bitcoin&apiKey=";
|
51
|
|
- String key = "471b872d96ca40c4a1d0f21de4bb061e";
|
|
64
|
+ @Test
|
|
65
|
+ public void getNews_Should_Hit_The_Proper_Endpoint() {
|
|
66
|
+ ns.getNews("test");
|
|
67
|
+ mrss.verify();
|
|
68
|
+ }
|
52
|
69
|
|
53
|
|
- this.mrss.expect(requestTo(url + key))
|
54
|
|
- .andRespond(withSuccess(nstr, MediaType.APPLICATION_JSON));
|
|
70
|
+ @Test
|
|
71
|
+ public void getNews_Should_Send_XApiKey_Header() {
|
|
72
|
+ ns.getNews("test");
|
|
73
|
+ mrss.verify();
|
55
|
74
|
}
|
|
75
|
+
|
56
|
76
|
@Test
|
57
|
|
- public void getNewsTest() {
|
58
|
|
- News ne = this.ns.getNews();
|
|
77
|
+ public void getNews_Should_Parse_The_JSON_Properly() {
|
|
78
|
+ News ne = ns.getNews("test");
|
59
|
79
|
assertEquals("Author Name", ne.getArticles().get(0).getAuthor());
|
60
|
80
|
}
|
61
|
81
|
|
62
|
82
|
@Test
|
63
|
|
- @Ignore
|
64
|
|
- public void test() {
|
65
|
|
- RestTemplateBuilder rt = new RestTemplateBuilder();
|
66
|
|
- NewsService ns = new NewsService(rt);
|
|
83
|
+ public void getAllArticles_Should_Return_An_ArrayList() {
|
|
84
|
+ ArrayList ne = ns.getAllArticles("test");
|
|
85
|
+ assertNotNull(ne);
|
|
86
|
+ }
|
67
|
87
|
|
68
|
|
- ArrayList<Article> ne = ns.getAllArticles();
|
69
|
|
- System.out.println(ne);
|
70
|
88
|
|
|
89
|
+ @Test
|
|
90
|
+ public void insertQueryInUrl_Given_Query_Should_Insert_In_Url() {
|
|
91
|
+ String query = "tesla";
|
|
92
|
+ String expected = "https://newsapi.org/v2/everything?q=" + query +
|
|
93
|
+ "&language=en" +
|
|
94
|
+ "&sources=bloomberg,business-insider,cnbc,the-wall-street-journal" +
|
|
95
|
+ "&pageSize=100";
|
|
96
|
+ String actual = ns.insertQueryInUrl(query);
|
|
97
|
+ assertEquals(expected, actual);
|
71
|
98
|
}
|
72
|
99
|
}
|