Add activity spec code
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / test / java / org / onap / sdc / workflow / api / WorkflowVersionControllerTest.java
1 package org.onap.sdc.workflow.api;
2
3 import static org.hamcrest.Matchers.hasSize;
4 import static org.hamcrest.Matchers.is;
5 import static org.junit.Assert.assertEquals;
6 import static org.mockito.Mockito.doReturn;
7 import static org.mockito.Mockito.times;
8 import static org.mockito.Mockito.verify;
9 import static org.springframework.http.MediaType.APPLICATION_JSON;
10 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
11 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
12 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
13 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
14 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
15 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
16
17 import com.google.gson.Gson;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.onap.sdc.workflow.RestPath;
27 import org.onap.sdc.workflow.persistence.types.ParameterEntity;
28 import org.onap.sdc.workflow.persistence.types.ParameterType;
29 import org.onap.sdc.workflow.services.WorkflowVersionManager;
30 import org.onap.sdc.workflow.services.types.WorkflowVersion;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.mock.web.MockHttpServletResponse;
34 import org.springframework.test.web.servlet.MockMvc;
35 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
36
37 @RunWith(MockitoJUnitRunner.class)
38 public class WorkflowVersionControllerTest {
39
40     private static final String USER_ID = "cs0008";
41     private static final String ITEM1_ID = "item_id_1";
42     private static final String VERSION1_ID = "version_id_1";
43     private static final String VERSION2_ID = "version_id_2";
44
45     private static final Gson GSON = new Gson();
46
47     private MockMvc mockMvc;
48
49     @Mock
50     private WorkflowVersionManager workflowVersionManagerMock;
51
52     @InjectMocks
53     private WorkflowVersionController workflowVersionController;
54
55     @Before
56     public void setUp() {
57         mockMvc = MockMvcBuilders.standaloneSetup(workflowVersionController).build();
58     }
59
60     @Test
61     public void shouldReturnWorkflowVersionListWhenCallingVersionGetREST() throws Exception {
62         doReturn(Arrays.asList(new Version(VERSION1_ID), new Version(VERSION2_ID))).when(workflowVersionManagerMock)
63                 .list(ITEM1_ID, null);
64         mockMvc.perform(get(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
65                                 .contentType(APPLICATION_JSON)).andExpect(status().isOk())
66                 .andExpect(jsonPath("$.items", hasSize(2))).andExpect(jsonPath("$.items[0].id", is(VERSION1_ID)))
67                 .andExpect(jsonPath("$.items[1].id", is(VERSION2_ID)));
68
69         verify(workflowVersionManagerMock, times(1)).list(ITEM1_ID, null);
70     }
71
72
73     @Test
74     public void shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception {
75
76         WorkflowVersion version = new WorkflowVersion();
77         version.setDescription("VersionDescription");
78         mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
79                                 .contentType(APPLICATION_JSON).content(GSON.toJson(version)))
80                 .andExpect(status().isCreated());
81
82         verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, null, version);
83     }
84
85     @Test
86     public void shouldFailCreateWorkflowVersionWhenCallingVersionsPostRESTWithDuplicateInput() throws Exception {
87
88         WorkflowVersion version = new WorkflowVersion();
89         Collection<ParameterEntity> inputs =
90                 Arrays.asList(createParameterEntity("name1"), createParameterEntity("name1"));
91         version.setInputs(inputs);
92         version.setDescription("VersionDescription");
93         mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
94                                 .contentType(APPLICATION_JSON).content(GSON.toJson(version)))
95                 .andExpect(status().isBadRequest());
96
97     }
98
99
100     @Test
101     public void shouldReturnWorkflowVersionWhenExists() throws Exception {
102         WorkflowVersion version = new WorkflowVersion(VERSION1_ID);
103         doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID);
104         mockMvc.perform(
105                 get(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
106                         .contentType(APPLICATION_JSON)).andDo(print()).andExpect(status().isOk())
107                 .andExpect(jsonPath("$.id", is(version.getId())));
108         verify(workflowVersionManagerMock, times(1)).get(ITEM1_ID, VERSION1_ID);
109     }
110
111     @Test
112     public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception {
113         WorkflowVersion version = new WorkflowVersion();
114         version.setDescription("Updated");
115
116         MockHttpServletResponse result = mockMvc.perform(
117                 put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
118                         .contentType(APPLICATION_JSON).content(GSON.toJson(version))).andReturn().getResponse();
119
120         assertEquals(HttpStatus.OK.value(), result.getStatus());
121         version.setId(VERSION1_ID);
122         verify(workflowVersionManagerMock, times(1)).update(ITEM1_ID, version);
123
124     }
125
126     private ParameterEntity createParameterEntity(String name) {
127         ParameterEntity parameterEntity = new ParameterEntity();
128         parameterEntity.setName(name);
129         parameterEntity.setMandatory(false);
130         parameterEntity.setType(ParameterType.STRING);
131         return parameterEntity;
132     }
133
134 }