1 package org.onap.sdc.workflow.api;
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;
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.api.types.VersionRequestDto;
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;
37 @RunWith(MockitoJUnitRunner.class)
38 public class WorkflowVersionControllerTest {
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 private List<Version> versionList;
46 private static final Gson GSON = new Gson();
48 private MockMvc mockMvc;
51 private WorkflowVersionManager workflowVersionManagerMock;
54 private WorkflowVersionController workflowVersionController;
58 versionList = Arrays.asList( new Version(VERSION1_ID),new Version(VERSION2_ID));
59 mockMvc = MockMvcBuilders.standaloneSetup(workflowVersionController).build();
63 public void shouldReturnWorkflowVersionListWhenCallingVersionGetREST() throws Exception {
65 doReturn(versionList).when(workflowVersionManagerMock).list(ITEM1_ID);
66 mockMvc.perform(get(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
67 .contentType(APPLICATION_JSON)).andExpect(status().isOk())
68 .andExpect(jsonPath("$.results", hasSize(2)))
69 .andExpect(jsonPath("$.results[0].id", equalTo(VERSION1_ID)))
70 .andExpect(jsonPath("$.results[1].id", equalTo(VERSION2_ID)));
72 verify(workflowVersionManagerMock, times(1)).list(ITEM1_ID);
77 public void shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception {
79 VersionRequestDto version = new VersionRequestDto();
80 version.setDescription("VersionDescription");
81 mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
82 .contentType(APPLICATION_JSON)
83 .content(GSON.toJson(version)))
84 .andExpect(status().isCreated());
86 verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, version);
91 public void shouldReturnWorkflowVersionWhenExists() throws Exception {
92 WorkflowVersion version = new WorkflowVersion(VERSION1_ID);
93 doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID);
95 get(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
96 .contentType(APPLICATION_JSON)).andDo(print())
97 .andExpect(status().isOk()).andExpect(jsonPath("$.id", is(version.getId())));
98 verify(workflowVersionManagerMock, times(1)).get(ITEM1_ID, VERSION1_ID);
102 public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception {
103 WorkflowVersion version = new WorkflowVersion();
104 version.setDescription("Updated");
106 MockHttpServletResponse result = mockMvc.perform(
107 put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID)
108 .contentType(APPLICATION_JSON)
109 .content(GSON.toJson(version))).andReturn()
112 assertEquals(HttpStatus.OK.value(), result.getStatus());
113 version.setId(VERSION1_ID);
114 verify(workflowVersionManagerMock, times(1)).update(ITEM1_ID, version);