Selaa lähdekoodia

news controller advice

Ahmad Rusdi 6 vuotta sitten
vanhempi
commit
c548f7c17d

+ 0
- 9
server/src/main/java/com/stockr/server/exception/MissingQueryParameters.java Näytä tiedosto

@@ -1,9 +0,0 @@
1
-package com.stockr.server.exception;
2
-
3
-import org.springframework.http.HttpStatus;
4
-import org.springframework.web.bind.annotation.ResponseStatus;
5
-
6
-@ResponseStatus(value = HttpStatus.BAD_REQUEST)
7
-public class MissingQueryParameters extends RuntimeException{
8
-
9
-}

+ 1
- 2
server/src/main/java/com/stockr/server/news/NewsController.java Näytä tiedosto

@@ -1,6 +1,5 @@
1 1
 package com.stockr.server.news;
2 2
 
3
-import com.stockr.server.exception.MissingQueryParameters;
4 3
 import org.springframework.beans.factory.annotation.Autowired;
5 4
 import org.springframework.web.bind.annotation.*;
6 5
 
@@ -13,7 +12,7 @@ public class NewsController {
13 12
     NewsService ns;
14 13
 
15 14
     @GetMapping// root
16
-    public ArrayList<Article> getAllArticles(@RequestParam("query") String query) throws MissingQueryParameters{
15
+    public ArrayList<Article> getAllArticles(@RequestParam("query") String query){
17 16
         return ns.getAllArticles(query);
18 17
     }
19 18
 }

+ 29
- 0
server/src/main/java/com/stockr/server/news/NewsControllerAdvice.java Näytä tiedosto

@@ -0,0 +1,29 @@
1
+package com.stockr.server.news;
2
+
3
+import com.fasterxml.jackson.databind.ObjectMapper;
4
+import com.fasterxml.jackson.databind.node.ObjectNode;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.MissingServletRequestParameterException;
7
+import org.springframework.web.bind.annotation.ControllerAdvice;
8
+import org.springframework.web.bind.annotation.ExceptionHandler;
9
+
10
+import java.io.IOException;
11
+
12
+@ControllerAdvice(basePackageClasses = NewsControllerAdvice.class)
13
+public class NewsControllerAdvice {
14
+    private ObjectMapper om = new ObjectMapper();
15
+
16
+    @ExceptionHandler(MissingServletRequestParameterException.class)
17
+    public ResponseEntity<Object> handleMissingParams(MissingServletRequestParameterException ex) throws IOException {
18
+        return ResponseEntity
19
+                .badRequest()
20
+                .body(removeStackTrace(ex));
21
+    }
22
+
23
+    ObjectNode removeStackTrace(Exception ex) throws IOException {
24
+        String exAsString = om.writeValueAsString(ex);
25
+        ObjectNode o = om.readValue(exAsString, ObjectNode.class);
26
+        o.remove("stackTrace");
27
+        return o;
28
+    }
29
+}

+ 2
- 2
server/src/test/java/com/stockr/server/ServerApplicationTests.java Näytä tiedosto

@@ -6,11 +6,11 @@ import org.springframework.boot.test.context.SpringBootTest;
6 6
 import org.springframework.test.context.junit4.SpringRunner;
7 7
 
8 8
 @RunWith(SpringRunner.class)
9
-@SpringBootTest
10 9
 public class ServerApplicationTests {
11 10
 
12 11
 	@Test
13
-	public void contextLoads() {
12
+	public void ServerApplication_Should_Start() {
13
+		ServerApplication.main(new String[] {});
14 14
 	}
15 15
 
16 16
 }

+ 1
- 1
server/src/test/java/com/stockr/server/news/ArticleTest.java Näytä tiedosto

@@ -9,7 +9,7 @@ import static org.junit.Assert.*;
9 9
 
10 10
 public class ArticleTest {
11 11
     @Test
12
-    public void testUnpackNestedSource() throws IOException {
12
+    public void unpackSource_ShouldGetNestedValues() throws IOException {
13 13
         ObjectMapper om = new ObjectMapper();
14 14
         String JSON = "{\"source\":" +
15 15
                         " {\"id\": \"ID\"," +

+ 21
- 0
server/src/test/java/com/stockr/server/news/NewsControllerAdviceTest.java Näytä tiedosto

@@ -0,0 +1,21 @@
1
+package com.stockr.server.news;
2
+
3
+import com.fasterxml.jackson.databind.node.ObjectNode;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+import java.io.IOException;
8
+
9
+import static org.junit.Assert.*;
10
+
11
+public class NewsControllerAdviceTest {
12
+    NewsControllerAdvice nca = new NewsControllerAdvice();
13
+
14
+    @Test
15
+    public void removeStackTrace_ShouldRemoveStackTrace() throws IOException {
16
+        Exception ex = new Exception();
17
+        ex.fillInStackTrace();
18
+
19
+        assertNull(nca.removeStackTrace(ex).get("stackTrace"));
20
+    }
21
+}

+ 8
- 4
server/src/test/java/com/stockr/server/news/NewsControllerTest.java Näytä tiedosto

@@ -6,12 +6,16 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
6 6
 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
7 7
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
8 8
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.handler;
9
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
9 10
 
11
+import org.junit.Ignore;
10 12
 import org.junit.Test;
11 13
 import org.junit.runner.RunWith;
12 14
 import org.springframework.beans.factory.annotation.Autowired;
13 15
 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
14 16
 import org.springframework.boot.test.mock.mockito.MockBean;
17
+import org.springframework.http.HttpHeaders;
18
+import org.springframework.http.HttpStatus;
15 19
 import org.springframework.test.context.junit4.SpringRunner;
16 20
 import org.springframework.test.web.servlet.MockMvc;
17 21
 
@@ -26,7 +30,7 @@ public class NewsControllerTest {
26 30
     private NewsService service;
27 31
 
28 32
     @Test
29
-    public void news_Should_Use_Method_getAllArticles() throws Exception {
33
+    public void news_ShouldUseMethod_getAllArticles() throws Exception {
30 34
         when(service.getNews("test")).thenReturn(new News());
31 35
         this.mockMvc.perform(get("/news?query=fb"))
32 36
                 .andDo(print())
@@ -34,7 +38,7 @@ public class NewsControllerTest {
34 38
     }
35 39
 
36 40
     @Test
37
-    public void news_Should_Return_A_Json_Array() throws Exception {
41
+    public void news_ShouldReturnJsonArray() throws Exception {
38 42
         when(service.getNews("test")).thenReturn(new News());
39 43
         this.mockMvc.perform(get("/news?query=fb"))
40 44
                 .andDo(print())
@@ -42,10 +46,10 @@ public class NewsControllerTest {
42 46
     }
43 47
 
44 48
     @Test
45
-    public void news_When_No_Queries_Throw_Exception() throws Exception {
49
+    public void news_ShouldHaveBadRequest_WhenNoQueries() throws Exception {
46 50
         when(service.getNews("test")).thenReturn(new News());
47 51
         this.mockMvc.perform(get("/news"))
48 52
                 .andDo(print())
49
-                .andExpect(content().string("[]"));
53
+                .andExpect(status().isBadRequest());
50 54
     }
51 55
 }

+ 5
- 8
server/src/test/java/com/stockr/server/news/NewsServiceTest.java Näytä tiedosto

@@ -3,14 +3,11 @@ package com.stockr.server.news;
3 3
 import com.fasterxml.jackson.core.JsonProcessingException;
4 4
 import com.fasterxml.jackson.databind.ObjectMapper;
5 5
 import org.hamcrest.Matchers;
6
-import org.junit.Assert;
7 6
 import org.junit.Before;
8
-import org.junit.Ignore;
9 7
 import org.junit.Test;
10 8
 import org.junit.runner.RunWith;
11 9
 import org.springframework.beans.factory.annotation.Autowired;
12 10
 import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
13
-import org.springframework.boot.web.client.RestTemplateBuilder;
14 11
 import org.springframework.http.HttpMethod;
15 12
 import org.springframework.http.MediaType;
16 13
 import org.springframework.test.context.junit4.SpringRunner;
@@ -62,32 +59,32 @@ public class NewsServiceTest {
62 59
     }
63 60
 
64 61
     @Test
65
-    public void getNews_Should_Hit_The_Proper_Endpoint() {
62
+    public void getNews_ShouldHitTheProperEndpoint() {
66 63
         ns.getNews("test");
67 64
         mrss.verify();
68 65
     }
69 66
 
70 67
     @Test
71
-    public void getNews_Should_Send_XApiKey_Header() {
68
+    public void getNewsShouldSendXApiKeyHeader() {
72 69
         ns.getNews("test");
73 70
         mrss.verify();
74 71
     }
75 72
 
76 73
     @Test
77
-    public void getNews_Should_Parse_The_JSON_Properly() {
74
+    public void getNews_ShouldParseJsonProperly() {
78 75
         News ne = ns.getNews("test");
79 76
         assertEquals("Author Name", ne.getArticles().get(0).getAuthor());
80 77
     }
81 78
 
82 79
     @Test
83
-    public void getAllArticles_Should_Return_An_ArrayList() {
80
+    public void getAllArticles_ShouldReturnArrayList() {
84 81
         ArrayList ne = ns.getAllArticles("test");
85 82
         assertNotNull(ne);
86 83
     }
87 84
 
88 85
 
89 86
     @Test
90
-    public void insertQueryInUrl_Given_Query_Should_Insert_In_Url() {
87
+    public void insertQueryInUrl_ShouldPassQueryIntoUrl() {
91 88
         String query = "tesla";
92 89
         String expected = "https://newsapi.org/v2/everything?q=" + query +
93 90
                 "&language=en" +