Open Kilda Java Documentation
FlowControllerTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.controller;
17 
18 import static org.junit.Assert.assertEquals;
21 import static org.openkilda.messaging.Utils.EXTRA_AUTH;
22 import static org.openkilda.messaging.Utils.MAPPER;
23 import static org.openkilda.northbound.controller.TestMessageMock.ERROR_FLOW_ID;
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;
32 
39 
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;
54 
55 import java.util.Collections;
56 import java.util.List;
57 import java.util.UUID;
58 import java.util.concurrent.TimeUnit;
59 
60 @RunWith(SpringJUnit4ClassRunner.class)
61 @WebAppConfiguration
62 @ContextConfiguration(classes = TestConfig.class)
63 @TestPropertySource("classpath:northbound.properties")
64 public class FlowControllerTest extends NorthboundBaseTest {
65  private static final String USERNAME = "kilda";
66  private static final String PASSWORD = "kilda";
67  private static final String ROLE = "ADMIN";
68 
69  private static final MessageError AUTH_ERROR = new MessageError(DEFAULT_CORRELATION_ID, 0,
70  ErrorType.AUTH_FAILED.toString(), "Kilda", "InsufficientAuthenticationException");
71  private static final MessageError NOT_FOUND_ERROR = new MessageError(DEFAULT_CORRELATION_ID, 0,
72  ErrorType.NOT_FOUND.toString(), "Flow was not found", TestMessageMock.ERROR_FLOW_ID);
73 
74  private MockMvc mockMvc;
75 
76  @Autowired
77  private WebApplicationContext webApplicationContext;
78 
79  @Before
80  public void setUp() throws Exception {
81  this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
82  RequestCorrelationId.create(DEFAULT_CORRELATION_ID);
83  }
84 
85  @Test
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)
91  .content(MAPPER.writeValueAsString(TestMessageMock.flow)))
92  .andExpect(status().isOk())
93  .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
94  .andReturn();
95  System.out.println("RESPONSE: " + result.getResponse().getContentAsString());
96  FlowPayload response = MAPPER.readValue(result.getResponse().getContentAsString(), FlowPayload.class);
97  assertEquals(TestMessageMock.flow, response);
98  }
99 
100  @Test
101  @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
102  public void getFlow() throws Exception {
103  MvcResult result = mockMvc.perform(get("/flows/{flow-id}", TestMessageMock.FLOW_ID)
104  .header(CORRELATION_ID, testCorrelationId())
105  .contentType(APPLICATION_JSON_VALUE))
106  .andExpect(status().isOk())
107  .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
108  .andReturn();
109  FlowPayload response = MAPPER.readValue(result.getResponse().getContentAsString(), FlowPayload.class);
110  assertEquals(TestMessageMock.flow, response);
111  }
112 
113  @Test
114  @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
115  public void deleteFlow() throws Exception {
116  MvcResult result = mockMvc.perform(delete("/flows/{flow-id}", TestMessageMock.FLOW_ID)
117  .header(CORRELATION_ID, testCorrelationId())
118  .contentType(APPLICATION_JSON_VALUE))
119  .andExpect(status().isOk())
120  .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
121  .andReturn();
122  FlowPayload response = MAPPER.readValue(result.getResponse().getContentAsString(), FlowPayload.class);
123  assertEquals(TestMessageMock.flow, response);
124  }
125 
126  @Test
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))
135  .andReturn();
136  FlowPayload[] response = MAPPER.readValue(result.getResponse().getContentAsString(), FlowPayload[].class);
137  assertEquals(TestMessageMock.flow, response[0]);
138  }
139 
140  @Test
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());
147  }
148 
149  @Test
150  @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
151  public void updateFlow() throws Exception {
152  MvcResult result = mockMvc.perform(put("/flows/{flow-id}", TestMessageMock.FLOW_ID)
153  .header(CORRELATION_ID, testCorrelationId())
154  .contentType(APPLICATION_JSON_VALUE)
155  .content(MAPPER.writeValueAsString(TestMessageMock.flow)))
156  .andExpect(status().isOk())
157  .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
158  .andReturn();
159  FlowPayload response = MAPPER.readValue(result.getResponse().getContentAsString(), FlowPayload.class);
160  assertEquals(TestMessageMock.flow, response);
161  }
162 
163  @Test
164  @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
165  public void getFlows() throws Exception {
166  MvcResult result = mockMvc.perform(get("/flows", TestMessageMock.FLOW_ID)
167  .header(CORRELATION_ID, testCorrelationId())
168  .contentType(APPLICATION_JSON_VALUE))
169  .andExpect(status().isOk())
170  .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
171  .andReturn();
172  List<FlowPayload> response = MAPPER.readValue(
173  result.getResponse().getContentAsString(),
174  new TypeReference<List<FlowPayload>>() {});
175  assertEquals(Collections.singletonList(TestMessageMock.flow), response);
176  }
177 
178  @Test
179  @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
180  public void statusFlow() throws Exception {
181  MvcResult result = mockMvc.perform(get("/flows/status/{flow-id}", TestMessageMock.FLOW_ID)
182  .header(CORRELATION_ID, testCorrelationId())
183  .contentType(APPLICATION_JSON_VALUE))
184  .andExpect(status().isOk())
185  .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
186  .andReturn();
187  FlowIdStatusPayload response =
188  MAPPER.readValue(result.getResponse().getContentAsString(), FlowIdStatusPayload.class);
189  assertEquals(TestMessageMock.flowStatus, response);
190  }
191 
192  @Test
193  @WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
194  public void pathFlow() throws Exception {
195  MvcResult result = mockMvc.perform(get("/flows/{flow-id}/path", TestMessageMock.FLOW_ID)
196  .header(CORRELATION_ID, testCorrelationId())
197  .contentType(APPLICATION_JSON_VALUE))
198  .andExpect(status().isOk())
199  .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
200  .andReturn();
201  FlowPathPayload response = MAPPER.readValue(result.getResponse().getContentAsString(), FlowPathPayload.class);
202  assertEquals(TestMessageMock.flowPath, response);
203  }
204 
205  @Test
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))
213  .andReturn();
214 
215  MessageError response = MAPPER.readValue(result.getResponse().getContentAsString(), MessageError.class);
216  assertEquals(NOT_FOUND_ERROR, response);
217  }
218 
219  @Test
220  public void emptyCredentials() throws Exception {
221  MvcResult result = mockMvc.perform(get("/flows/path/{flow-id}", TestMessageMock.FLOW_ID)
222  .header(CORRELATION_ID, DEFAULT_CORRELATION_ID)
223  .contentType(APPLICATION_JSON_VALUE))
224  .andExpect(status().isUnauthorized())
225  .andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE))
226  .andReturn();
227 
228  MessageError response = MAPPER.readValue(result.getResponse().getContentAsString(), MessageError.class);
229  assertEquals(AUTH_ERROR, response);
230  }
231 
232  private static String testCorrelationId() {
233  return UUID.randomUUID().toString();
234  }
235 }
static final ObjectMapper MAPPER
Definition: Utils.java:31
static RequestCorrelationClosable create(String correlationId)
def status()
Definition: rest.py:593
static final String DEFAULT_CORRELATION_ID
Definition: Utils.java:69
list result
Definition: plan-d.py:72
static final String CORRELATION_ID
Definition: Utils.java:43
static final String EXTRA_AUTH
Definition: Utils.java:47