ee9a56c80af6e9342521ee476ebe8688bb11133f
[sdc/sdc-workflow-designer.git] /
1 package org.onap.sdc.workflow.api;
2
3 import static org.hamcrest.Matchers.equalTo;
4 import static org.hamcrest.Matchers.is;
5 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
6 import static org.junit.Assert.assertEquals;
7 import static org.mockito.Mockito.doReturn;
8 import static org.mockito.Mockito.times;
9 import static org.mockito.Mockito.verify;
10 import static org.springframework.http.MediaType.APPLICATION_JSON;
11 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
12 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
13 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
14 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
15 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
16 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17
18 import com.google.gson.Gson;
19 import java.util.Arrays;
20 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.onap.sdc.workflow.RestPath;
28 import org.onap.sdc.workflow.persistence.types.WorkflowVersion;
29 import org.onap.sdc.workflow.services.WorkflowVersionManager;
30 import org.openecomp.sdc.versioning.dao.types.Version;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.mock.web.MockHttpServletResponse;
33 import org.springframework.test.web.servlet.MockMvc;
34 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class WorkflowVersionControllerTest {
38
39     private static final String USER_ID = "cs0008";
40     private static final String ITEM1_ID = "item_id_1";
41     private static final String VERSION1_ID = "version_id_1";
42     private static final String VERSION2_ID = "version_id_2";
43     private List<Version> versionList;
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         versionList = Arrays.asList( new Version(VERSION1_ID),new Version(VERSION2_ID));
58         mockMvc = MockMvcBuilders.standaloneSetup(workflowVersionController).build();
59     }
60
61     @Test
62     public void shouldReturnWorkflowVersionListWhenCallingVersionGetREST() throws Exception {
63
64         doReturn(versionList).when(workflowVersionManagerMock).list(ITEM1_ID, null);
65         mockMvc.perform(get(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
66                                                                    .contentType(APPLICATION_JSON)).andExpect(status().isOk())
67                .andExpect(jsonPath("$.results", hasSize(2)))
68                .andExpect(jsonPath("$.results[0].id", equalTo(VERSION1_ID)))
69                .andExpect(jsonPath("$.results[1].id", equalTo(VERSION2_ID)));
70
71         verify(workflowVersionManagerMock, times(1)).list(ITEM1_ID, null);
72     }
73
74
75     @Test
76     public void shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception {
77
78         WorkflowVersion version = new WorkflowVersion();
79         version.setDescription("VersionDescription");
80         mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
81                                                                     .contentType(APPLICATION_JSON)
82                                                                     .content(GSON.toJson(version)))
83                .andExpect(status().isCreated());
84
85         verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, null, version);
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(RestConstants.USER_ID_HEADER_PARAM, 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(RestConstants.USER_ID_HEADER_PARAM, 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 }