16 package org.openkilda.northbound.controller;
18 import static org.junit.Assert.assertEquals;
24 import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
25 import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
26 import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
27 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
30 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
40 import com.fasterxml.jackson.core.type.TypeReference;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.security.test.context.support.WithMockUser;
46 import org.springframework.test.context.ContextConfiguration;
47 import org.springframework.test.context.TestPropertySource;
48 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
49 import org.springframework.test.context.web.WebAppConfiguration;
50 import org.springframework.test.web.servlet.MockMvc;
51 import org.springframework.test.web.servlet.MvcResult;
52 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
53 import org.springframework.web.context.WebApplicationContext;
55 import java.util.Collections;
56 import java.util.List;
57 import java.util.UUID;
58 import java.util.concurrent.TimeUnit;
60 @RunWith(SpringJUnit4ClassRunner.class)
62 @ContextConfiguration(classes = TestConfig.class)
63 @TestPropertySource(
"classpath:northbound.properties")
65 private static final String USERNAME =
"kilda";
66 private static final String PASSWORD =
"kilda";
67 private static final String ROLE =
"ADMIN";
74 private MockMvc mockMvc;
77 private WebApplicationContext webApplicationContext;
80 public void setUp() throws Exception {
81 this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
86 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
87 public
void createFlow() throws Exception {
88 MvcResult
result = mockMvc.perform(put(
"/flows")
89 .header(CORRELATION_ID, testCorrelationId())
90 .contentType(APPLICATION_JSON_VALUE)
92 .andExpect(
status().isOk())
93 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
95 System.out.println(
"RESPONSE: " +
result.getResponse().getContentAsString());
101 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
102 public
void getFlow() throws Exception {
104 .header(CORRELATION_ID, testCorrelationId())
105 .contentType(APPLICATION_JSON_VALUE))
106 .andExpect(
status().isOk())
107 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
114 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
115 public
void deleteFlow() throws Exception {
117 .header(CORRELATION_ID, testCorrelationId())
118 .contentType(APPLICATION_JSON_VALUE))
119 .andExpect(
status().isOk())
120 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
127 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
128 public
void deleteFlows() throws Exception {
129 MvcResult
result = mockMvc.perform(
delete(
"/flows")
130 .header(CORRELATION_ID, testCorrelationId())
131 .header(EXTRA_AUTH, System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(119))
132 .contentType(APPLICATION_JSON_VALUE))
133 .andExpect(
status().isOk())
134 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
141 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
142 public
void shouldFailDeleteFlowsWithoutExtraAuth() throws Exception {
143 mockMvc.perform(
delete(
"/flows")
144 .header(CORRELATION_ID, testCorrelationId())
145 .contentType(APPLICATION_JSON_VALUE))
146 .andExpect(
status().isUnauthorized());
150 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
151 public
void updateFlow() throws Exception {
153 .header(CORRELATION_ID, testCorrelationId())
154 .contentType(APPLICATION_JSON_VALUE)
156 .andExpect(
status().isOk())
157 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
164 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
165 public
void getFlows() throws Exception {
167 .header(CORRELATION_ID, testCorrelationId())
168 .contentType(APPLICATION_JSON_VALUE))
169 .andExpect(
status().isOk())
170 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
172 List<FlowPayload> response = MAPPER.readValue(
173 result.getResponse().getContentAsString(),
174 new TypeReference<List<FlowPayload>>() {});
175 assertEquals(Collections.singletonList(
TestMessageMock.flow), response);
179 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
180 public
void statusFlow() throws Exception {
182 .header(CORRELATION_ID, testCorrelationId())
183 .contentType(APPLICATION_JSON_VALUE))
184 .andExpect(
status().isOk())
185 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
193 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
194 public
void pathFlow() throws Exception {
196 .header(CORRELATION_ID, testCorrelationId())
197 .contentType(APPLICATION_JSON_VALUE))
198 .andExpect(
status().isOk())
199 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
206 @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
207 public
void getNonExistingFlow() throws Exception {
208 MvcResult
result = mockMvc.perform(
get(
"/flows/{flow-id}", ERROR_FLOW_ID)
209 .header(CORRELATION_ID, DEFAULT_CORRELATION_ID)
210 .contentType(APPLICATION_JSON_VALUE))
211 .andExpect(
status().isNotFound())
212 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
216 assertEquals(NOT_FOUND_ERROR, response);
222 .header(CORRELATION_ID, DEFAULT_CORRELATION_ID)
223 .contentType(APPLICATION_JSON_VALUE))
224 .andExpect(
status().isUnauthorized())
225 .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
229 assertEquals(AUTH_ERROR, response);
232 private static String testCorrelationId() {
233 return UUID.randomUUID().toString();
static final ObjectMapper MAPPER
static RequestCorrelationClosable create(String correlationId)
static final String DEFAULT_CORRELATION_ID
static final String CORRELATION_ID
static final String EXTRA_AUTH