1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package org.onap.sdc.workflow.api;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.onap.sdc.workflow.TestUtil.createWorkflow;
import org.onap.sdc.workflow.api.impl.WorkflowControllerImpl;
import org.onap.sdc.workflow.persistence.types.Workflow;
import org.onap.sdc.workflow.services.WorkflowManager;
import org.openecomp.sdc.versioning.types.Item;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@RunWith(MockitoJUnitRunner.class)
public class WorkflowControllerTest {
private static final String MISSING_REQUEST_HEADER_ERRROR_FORMAT =
"Missing request header '%s' for method parameter of type String";
private static final String USER_ID = "userId";
private static final String WORKFLOWS_URL = "/workflows";
private static final Gson GSON = new Gson();
private MockMvc mockMvc;
@InjectMocks
private WorkflowControllerImpl workflowController;
@Mock
private WorkflowManager workflowManagerMock;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(workflowController).build();
}
@Test
public void shouldReturnErrorWhenMissingUserIdInGetReqHeader() throws Exception {
Workflow workflowMock = createWorkflow(1, true);
MockHttpServletResponse response =
mockMvc.perform(get(WORKFLOWS_URL + "/" + workflowMock.getId()).contentType(APPLICATION_JSON))
.andDo(print()).andExpect(status().isBadRequest()).andExpect(status().is(400)).andReturn()
.getResponse();
assertEquals(String.format(MISSING_REQUEST_HEADER_ERRROR_FORMAT, "USER_ID"), response.getErrorMessage());
}
@Test
public void shouldReturnWorkflowDataWhenRequestPathIsOk() throws Exception {
Workflow workflowMock = createWorkflow(1, true);
doReturn(workflowMock).when(workflowManagerMock).get(any(Workflow.class));
mockMvc.perform(
get(WORKFLOWS_URL + "/" + workflowMock.getId()).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
.contentType(APPLICATION_JSON)).andDo(print())
.andExpect(status().isOk()).andExpect(jsonPath("$.id", is(workflowMock.getId())))
.andExpect(jsonPath("$.name", is(workflowMock.getName())));
}
@Test
public void shouldReturnErrorWhenMissingUserIdInListReqHeader() throws Exception {
MockHttpServletResponse response =
mockMvc.perform(get(WORKFLOWS_URL).contentType(APPLICATION_JSON)).andDo(print())
.andExpect(status().isBadRequest()).andExpect(status().is(400)).andReturn().getResponse();
assertEquals(String.format(MISSING_REQUEST_HEADER_ERRROR_FORMAT, "USER_ID"), response.getErrorMessage());
}
@Test
public void shouldReturnListOfWorkflows() throws Exception {
int numOfWorkflows = 5;
List<Workflow> workflowMocks = createWorkflows(numOfWorkflows);
doReturn(workflowMocks).when(workflowManagerMock).list();
mockMvc.perform(
get(WORKFLOWS_URL).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON))
.andDo(print()).andExpect(status().isOk()).andExpect(jsonPath("$.results", hasSize(numOfWorkflows)));
}
@Test
public void create() throws Exception {
Item item = new Item();
item.setId("abc");
Workflow reqWorkflow = createWorkflow(1, false);
mockMvc.perform(
post(WORKFLOWS_URL).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON)
.content(GSON.toJson(reqWorkflow))).andDo(print()).andExpect(status().isCreated())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
verify(workflowManagerMock, times(1)).create(reqWorkflow);
}
private List<Workflow> createWorkflows(int numOfWorkflows) {
List<Workflow> workflowList = new ArrayList<>(numOfWorkflows);
for (int i = 0; i < numOfWorkflows; i++) {
workflowList.add(createWorkflow(i, true));
}
return workflowList;
}
}
|