|
1 | | -// Copyright (c) 2019, 2021, Oracle and/or its affiliates. |
| 1 | +// Copyright (c) 2019, 2022, Oracle and/or its affiliates. |
2 | 2 | // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
3 | 3 |
|
4 | 4 | package com.oracle.wls.exporter.webapp; |
|
9 | 9 | import javax.servlet.annotation.WebServlet; |
10 | 10 | import javax.servlet.http.HttpServlet; |
11 | 11 |
|
| 12 | +import com.oracle.wls.exporter.WebClientFactoryStub; |
12 | 13 | import com.oracle.wls.exporter.WlsRestExchanges; |
13 | 14 | import org.hamcrest.Matcher; |
14 | 15 | import org.junit.jupiter.api.BeforeEach; |
|
27 | 28 | import static org.hamcrest.Matchers.is; |
28 | 29 | import static org.hamcrest.Matchers.notNullValue; |
29 | 30 |
|
30 | | -public class MessagesServletTest { |
31 | | - private static final int EXCESS_EXCHANGES = 3; |
32 | | - private static final String URL = "http://localhost:7001"; |
33 | | - private final MessagesServlet servlet = new MessagesServlet(); |
34 | | - private final HttpServletRequestStub request = createGetRequest(); |
35 | | - private final HttpServletResponseStub response = createServletResponse(); |
| 31 | +class MessagesServletTest { |
36 | 32 |
|
37 | | - @BeforeEach |
38 | | - public void setUp() { |
39 | | - WlsRestExchanges.clear(); |
40 | | - } |
| 33 | + private static final int EXCESS_EXCHANGES = 3; |
| 34 | + private static final String URL = "http://localhost:7001"; |
| 35 | + private final WebClientFactoryStub factory = new WebClientFactoryStub(); |
| 36 | + private final MessagesServlet servlet = new MessagesServlet(factory); |
| 37 | + private final HttpServletRequestStub request = createGetRequest(); |
| 38 | + private final HttpServletResponseStub response = createServletResponse(); |
41 | 39 |
|
42 | | - @Test |
43 | | - public void diagnostics_isHttpServlet() { |
44 | | - assertThat(servlet, instanceOf(HttpServlet.class)); |
45 | | - } |
| 40 | + @BeforeEach |
| 41 | + public void setUp() { |
| 42 | + WlsRestExchanges.clear(); |
| 43 | + } |
46 | 44 |
|
47 | | - @Test |
48 | | - public void servlet_isPublic() { |
49 | | - assertThat(Modifier.isPublic(MessagesServlet.class.getModifiers()), is(true)); |
50 | | - } |
| 45 | + @Test |
| 46 | + void diagnostics_isHttpServlet() { |
| 47 | + assertThat(servlet, instanceOf(HttpServlet.class)); |
| 48 | + } |
51 | 49 |
|
52 | | - @Test |
53 | | - public void servlet_hasWebServletAnnotation() { |
54 | | - assertThat(MessagesServlet.class.getAnnotation(WebServlet.class), notNullValue()); |
55 | | - } |
| 50 | + @Test |
| 51 | + void servlet_isPublic() { |
| 52 | + assertThat(Modifier.isPublic(MessagesServlet.class.getModifiers()), is(true)); |
| 53 | + } |
56 | 54 |
|
57 | | - @Test |
58 | | - public void servletAnnotationIndicatesMetricsPage() { |
59 | | - WebServlet annotation = MessagesServlet.class.getAnnotation(WebServlet.class); |
| 55 | + @Test |
| 56 | + void servlet_hasWebServletAnnotation() { |
| 57 | + assertThat(MessagesServlet.class.getAnnotation(WebServlet.class), notNullValue()); |
| 58 | + } |
60 | 59 |
|
61 | | - assertThat(annotation.value(), arrayContaining("/messages")); |
62 | | - } |
| 60 | + @Test |
| 61 | + void servletAnnotationIndicatesMetricsPage() { |
| 62 | + WebServlet annotation = MessagesServlet.class.getAnnotation(WebServlet.class); |
63 | 63 |
|
64 | | - @Test |
65 | | - public void afterExchangeAdded_retrieveExchange() { |
66 | | - WlsRestExchanges.addExchange(URL, "request 1", "response1"); |
| 64 | + assertThat(annotation.value(), arrayContaining("/messages")); |
| 65 | + } |
67 | 66 |
|
68 | | - assertThat(WlsRestExchanges.getExchanges(), hasItem(both(containsString("request 1")).and(containsString("response1")))); |
69 | | - } |
| 67 | + @Test |
| 68 | + void afterExchangeAdded_retrieveExchange() { |
| 69 | + WlsRestExchanges.addExchange(URL, "request 1", "response1"); |
70 | 70 |
|
71 | | - @Test |
72 | | - public void afterMaximumExchangesAdded_retrieveAllExchanges() { |
73 | | - IntStream.rangeClosed(1, MAX_EXCHANGES).forEach(this::addTestExchange); |
| 71 | + assertThat(WlsRestExchanges.getExchanges(), |
| 72 | + hasItem(both(containsString("request 1")).and(containsString("response1")))); |
| 73 | + } |
74 | 74 |
|
75 | | - assertThat(WlsRestExchanges.getExchanges(), contains(getExchangeMatchers(1, MAX_EXCHANGES))); |
76 | | - } |
| 75 | + @Test |
| 76 | + void afterMaximumExchangesAdded_retrieveAllExchanges() { |
| 77 | + IntStream.rangeClosed(1, MAX_EXCHANGES).forEach(this::addTestExchange); |
77 | 78 |
|
78 | | - private void addTestExchange(int i) { |
79 | | - WlsRestExchanges.addExchange(URL, "request " + i, "response " + i); |
80 | | - } |
| 79 | + assertThat(WlsRestExchanges.getExchanges(), contains(getExchangeMatchers(1, MAX_EXCHANGES))); |
| 80 | + } |
81 | 81 |
|
82 | | - @SuppressWarnings("unchecked") |
83 | | - private Matcher<String>[] getExchangeMatchers(int first, int last) { |
84 | | - return IntStream.rangeClosed(first, last).mapToObj(this::getExchangeMatcher).toArray(Matcher[]::new); |
85 | | - } |
| 82 | + private void addTestExchange(int i) { |
| 83 | + WlsRestExchanges.addExchange(URL, "request " + i, "response " + i); |
| 84 | + } |
86 | 85 |
|
87 | | - private Matcher<String> getExchangeMatcher(int i) { |
88 | | - return containsString("request " + i); |
89 | | - } |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + private Matcher<String>[] getExchangeMatchers(int first, int last) { |
| 88 | + return IntStream.rangeClosed(first, last).mapToObj(this::getExchangeMatcher).toArray(Matcher[]::new); |
| 89 | + } |
90 | 90 |
|
91 | | - @Test |
92 | | - public void afterMoreThanMaximumExchangesAdded_retrieveLastMExchanges() { |
93 | | - IntStream.rangeClosed(1, MAX_EXCHANGES+EXCESS_EXCHANGES).forEach(this::addTestExchange); |
| 91 | + private Matcher<String> getExchangeMatcher(int i) { |
| 92 | + return containsString("request " + i); |
| 93 | + } |
94 | 94 |
|
95 | | - assertThat(WlsRestExchanges.getExchanges(), contains(getExchangeMatchers(EXCESS_EXCHANGES+1, MAX_EXCHANGES+EXCESS_EXCHANGES))); |
96 | | - } |
| 95 | + @Test |
| 96 | + void afterMoreThanMaximumExchangesAdded_retrieveLastMExchanges() { |
| 97 | + IntStream.rangeClosed(1, MAX_EXCHANGES + EXCESS_EXCHANGES).forEach(this::addTestExchange); |
97 | 98 |
|
98 | | - @Test |
99 | | - public void whenServletInvoked_responseDisplaysRecentExchanges() throws IOException { |
100 | | - IntStream.rangeClosed(1, MAX_EXCHANGES).forEach(this::addTestExchange); |
| 99 | + assertThat(WlsRestExchanges.getExchanges(), |
| 100 | + contains(getExchangeMatchers(EXCESS_EXCHANGES + 1, MAX_EXCHANGES + EXCESS_EXCHANGES))); |
| 101 | + } |
101 | 102 |
|
102 | | - servlet.doGet(request, response); |
| 103 | + @Test |
| 104 | + void whenServletInvoked_responseDisplaysRecentExchanges() throws IOException { |
| 105 | + IntStream.rangeClosed(1, MAX_EXCHANGES).forEach(this::addTestExchange); |
103 | 106 |
|
104 | | - assertThat(response.getHtml(), containsString("request 4")); |
105 | | - } |
| 107 | + servlet.doGet(request, response); |
106 | 108 |
|
107 | | - // todo report last commit |
108 | | - // todo diagnose classpath issues (all should be in the WAR) |
| 109 | + assertThat(response.getHtml(), containsString("request 4")); |
| 110 | + } |
| 111 | + |
| 112 | + // todo report last commit |
| 113 | + // todo diagnose classpath issues (all should be in the WAR) |
109 | 114 | } |
0 commit comments