65dae72653b68790e1ef661e51ad128b9ebfbd50
[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.is;
4 import static org.junit.Assert.assertEquals;
5 import static org.mockito.Mockito.doReturn;
6 import static org.mockito.Mockito.times;
7 import static org.mockito.Mockito.verify;
8 import static org.springframework.http.MediaType.APPLICATION_JSON;
9 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
10 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
11 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
12 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
13 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
14 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
15
16 import com.google.gson.Gson;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.List;
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.persistence.types.WorkflowVersion;
30 import org.onap.sdc.workflow.services.WorkflowVersionManager;
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 shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception {
62
63         WorkflowVersion version = new WorkflowVersion();
64         version.setDescription("VersionDescription");
65         mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
66                                                                     .contentType(APPLICATION_JSON)
67                                                                     .content(GSON.toJson(version)))
68                .andExpect(status().isCreated());
69
70         verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, null, version);
71     }
72
73     @Test
74     public void shouldFailCreateWorkflowVersionWhenCallingVersionsPostRESTWithDuplicateInput() throws Exception {
75
76         WorkflowVersion version = new WorkflowVersion();
77         Collection<ParameterEntity> inputs =
78                 Arrays.asList(createParameterEntity("name1"), createParameterEntity("name1"));
79         version.setInputs(inputs);
80         version.setDescription("VersionDescription");
81         mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
82                                                                     .contentType(APPLICATION_JSON)
83                                                                     .content(GSON.toJson(version)))
84                .andExpect(status().isBadRequest());
85
86     }
87
88
89     @Test
90     public void shouldReturnWorkflowVersionWhenExists() throws Exception {
91         WorkflowVersion version = new WorkflowVersion(VERSION1_ID);
92         doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID);
93         mockMvc.perform(
94                 get(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
95                                                                        .contentType(APPLICATION_JSON)).andDo(print())
96                .andExpect(status().isOk()).andExpect(jsonPath("$.id", is(version.getId())));
97         verify(workflowVersionManagerMock, times(1)).get(ITEM1_ID, VERSION1_ID);
98     }
99
100     @Test
101     public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception {
102         WorkflowVersion version = new WorkflowVersion();
103         version.setDescription("Updated");
104
105         MockHttpServletResponse result = mockMvc.perform(
106                 put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
107                                                                        .contentType(APPLICATION_JSON)
108                                                                        .content(GSON.toJson(version))).andReturn()
109                                                 .getResponse();
110
111         assertEquals(HttpStatus.OK.value(), result.getStatus());
112         version.setId(VERSION1_ID);
113         verify(workflowVersionManagerMock, times(1)).update(ITEM1_ID, version);
114
115     }
116
117     private ParameterEntity createParameterEntity(String name) {
118         ParameterEntity parameterEntity = new ParameterEntity();
119         parameterEntity.setName(name);
120         parameterEntity.setMandatory(false);
121         parameterEntity.setType(ParameterType.STRING);
122         return parameterEntity;
123     }
124
125 }