|
15 | 15 | */ |
16 | 16 | package de.codecentric.boot.admin.registry.web; |
17 | 17 |
|
| 18 | +import static org.mockito.Matchers.matches; |
18 | 19 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; |
19 | 20 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
20 | 21 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
21 | 23 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
22 | 24 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
23 | 25 |
|
24 | 26 | import java.io.UnsupportedEncodingException; |
25 | 27 |
|
| 28 | +import org.hamcrest.CoreMatchers; |
26 | 29 | import org.json.JSONException; |
27 | 30 | import org.json.JSONObject; |
28 | 31 | import org.junit.Before; |
29 | 32 | import org.junit.Test; |
30 | 33 | import org.mockito.Mockito; |
| 34 | +import org.mockito.internal.matchers.Matches; |
31 | 35 | import org.springframework.context.ApplicationEventPublisher; |
| 36 | +import org.springframework.http.HttpHeaders; |
32 | 37 | import org.springframework.http.MediaType; |
33 | 38 | import org.springframework.test.web.servlet.MockMvc; |
34 | 39 | import org.springframework.test.web.servlet.MvcResult; |
|
42 | 47 |
|
43 | 48 | public class RegistryControllerTest { |
44 | 49 |
|
45 | | - private static final String APPLICATION_TEST_JSON; |
46 | | - private static final String APPLICATION_TWICE_JSON; |
47 | | - |
48 | | - static { |
49 | | - try { |
50 | | - APPLICATION_TEST_JSON = new JSONObject().put("name", "test") |
51 | | - .put("healthUrl", "http://localhost/mgmt/health").toString(); |
52 | | - APPLICATION_TWICE_JSON = new JSONObject().put("name", "twice") |
53 | | - .put("healthUrl", "http://localhost/mgmt/health").toString(); |
54 | | - } catch (JSONException ex) { |
55 | | - throw new ExceptionInInitializerError(ex); |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - private MockMvc mvc; |
60 | | - |
61 | | - @Before |
62 | | - public void setup() { |
63 | | - ApplicationRegistry registry = new ApplicationRegistry(new SimpleApplicationStore(), |
64 | | - new HashingApplicationUrlIdGenerator()); |
65 | | - registry.setApplicationEventPublisher(Mockito.mock(ApplicationEventPublisher.class)); |
66 | | - mvc = MockMvcBuilders.standaloneSetup(new RegistryController(registry)).build(); |
67 | | - } |
68 | | - |
69 | | - @Test |
70 | | - public void test_register_twice_get_and_remove() throws Exception { |
71 | | - MvcResult result = mvc |
72 | | - .perform(post("/api/applications").contentType(MediaType.APPLICATION_JSON) |
73 | | - .content(APPLICATION_TEST_JSON)) |
74 | | - .andExpect(status().isCreated()).andExpect(jsonPath("$.name").value("test")) |
75 | | - .andExpect(jsonPath("$.healthUrl").value("http://localhost/mgmt/health")) |
76 | | - .andExpect(jsonPath("$.id").isNotEmpty()).andReturn(); |
77 | | - |
78 | | - String id = extractId(result); |
79 | | - |
80 | | - mvc.perform(post("/api/applications").contentType(MediaType.APPLICATION_JSON) |
81 | | - .content(APPLICATION_TWICE_JSON)).andExpect(status().isCreated()) |
82 | | - .andExpect(jsonPath("$.name").value("twice")) |
83 | | - .andExpect(jsonPath("$.healthUrl").value("http://localhost/mgmt/health")) |
84 | | - .andExpect(jsonPath("$.id").value(id)); |
85 | | - |
86 | | - mvc.perform(get("/api/applications")).andExpect(status().isOk()) |
87 | | - .andExpect(jsonPath("$[0].id").value(id)); |
88 | | - |
89 | | - mvc.perform(get("/api/applications?name=twice")).andExpect(status().isOk()) |
90 | | - .andExpect(jsonPath("$[0].id").value(id)); |
91 | | - |
92 | | - mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isOk()) |
93 | | - .andExpect(jsonPath("$.id").value(id)); |
94 | | - |
95 | | - mvc.perform(delete("/api/applications/{id}", id)).andExpect(status().isOk()) |
96 | | - .andExpect(jsonPath("$.id").value(id)); |
97 | | - |
98 | | - mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isNotFound()); |
99 | | - } |
100 | | - |
101 | | - private String extractId(MvcResult result) throws UnsupportedEncodingException { |
102 | | - return JsonPath.compile("$.id").read(result.getResponse().getContentAsString()); |
103 | | - } |
104 | | - |
105 | | - @Test |
106 | | - public void test_get_notFound() throws Exception { |
107 | | - mvc.perform(get("/api/applications/unknown")).andExpect(status().isNotFound()); |
108 | | - mvc.perform(get("/api/applications?name=unknown")).andExpect(status().isOk()) |
109 | | - .andExpect(jsonPath("$").isEmpty()); |
110 | | - } |
111 | | - |
112 | | - @Test |
113 | | - public void test_delete_notFound() throws Exception { |
114 | | - mvc.perform(delete("/api/applications/unknown")).andExpect(status().isNotFound()); |
115 | | - } |
| 50 | + private static final String APPLICATION_TEST_JSON; |
| 51 | + private static final String APPLICATION_TWICE_JSON; |
| 52 | + |
| 53 | + static { |
| 54 | + try { |
| 55 | + APPLICATION_TEST_JSON = new JSONObject().put("name", "test") |
| 56 | + .put("healthUrl", "http://localhost/mgmt/health") |
| 57 | + .toString(); |
| 58 | + APPLICATION_TWICE_JSON = new JSONObject().put("name", "twice") |
| 59 | + .put("healthUrl", "http://localhost/mgmt/health") |
| 60 | + .toString(); |
| 61 | + } catch (JSONException ex) { |
| 62 | + throw new ExceptionInInitializerError(ex); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private MockMvc mvc; |
| 67 | + |
| 68 | + @Before |
| 69 | + public void setup() { |
| 70 | + ApplicationRegistry registry = new ApplicationRegistry(new SimpleApplicationStore(), |
| 71 | + new HashingApplicationUrlIdGenerator()); |
| 72 | + registry.setApplicationEventPublisher(Mockito.mock(ApplicationEventPublisher.class)); |
| 73 | + mvc = MockMvcBuilders.standaloneSetup(new RegistryController(registry)).build(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void test_register_twice_get_and_remove() throws Exception { |
| 78 | + MvcResult result = mvc.perform( |
| 79 | + post("/api/applications").contentType(MediaType.APPLICATION_JSON).content(APPLICATION_TEST_JSON)) |
| 80 | + .andExpect(status().isCreated()) |
| 81 | + .andExpect(header().string(HttpHeaders.LOCATION, new Matches("http://localhost/[0-9a-f]+"))) |
| 82 | + .andExpect(jsonPath("$.id").isNotEmpty()) |
| 83 | + .andReturn(); |
| 84 | + |
| 85 | + String id = extractId(result); |
| 86 | + |
| 87 | + mvc.perform(post("/api/applications").contentType(MediaType.APPLICATION_JSON).content(APPLICATION_TWICE_JSON)) |
| 88 | + .andExpect(status().isCreated()) |
| 89 | + .andExpect(header().string(HttpHeaders.LOCATION, new Matches("http://localhost/[0-9a-f]+"))) |
| 90 | + .andExpect(jsonPath("$.id").value(id)); |
| 91 | + |
| 92 | + mvc.perform(get("/api/applications")).andExpect(status().isOk()).andExpect(jsonPath("$[0].id").value(id)); |
| 93 | + |
| 94 | + mvc.perform(get("/api/applications?name=twice")) |
| 95 | + .andExpect(status().isOk()) |
| 96 | + .andExpect(jsonPath("$[0].id").value(id)); |
| 97 | + |
| 98 | + mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isOk()).andExpect(jsonPath("$.id").value(id)); |
| 99 | + |
| 100 | + mvc.perform(delete("/api/applications/{id}", id)) |
| 101 | + .andExpect(status().isOk()) |
| 102 | + .andExpect(jsonPath("$.id").value(id)); |
| 103 | + |
| 104 | + mvc.perform(get("/api/applications/{id}", id)).andExpect(status().isNotFound()); |
| 105 | + } |
| 106 | + |
| 107 | + private String extractId(MvcResult result) throws UnsupportedEncodingException { |
| 108 | + return JsonPath.compile("$.id").read(result.getResponse().getContentAsString()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void test_get_notFound() throws Exception { |
| 113 | + mvc.perform(get("/api/applications/unknown")).andExpect(status().isNotFound()); |
| 114 | + mvc.perform(get("/api/applications?name=unknown")) |
| 115 | + .andExpect(status().isOk()) |
| 116 | + .andExpect(jsonPath("$").isEmpty()); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void test_delete_notFound() throws Exception { |
| 121 | + mvc.perform(delete("/api/applications/unknown")).andExpect(status().isNotFound()); |
| 122 | + } |
116 | 123 |
|
117 | 124 | } |
0 commit comments