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.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;
36 @RunWith(MockitoJUnitRunner.class)
37 public class WorkflowVersionControllerTest {
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;
45 private static final Gson GSON = new Gson();
47 private MockMvc mockMvc;
50 private WorkflowVersionManager workflowVersionManagerMock;
53 private WorkflowVersionController workflowVersionController;
57 versionList = Arrays.asList( new Version(VERSION1_ID),new Version(VERSION2_ID));
58 mockMvc = MockMvcBuilders.standaloneSetup(workflowVersionController).build();
62 public void shouldReturnWorkflowVersionListWhenCallingVersionGetREST() throws Exception {
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)));
71 verify(workflowVersionManagerMock, times(1)).list(ITEM1_ID, null);
76 public void shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception {
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());
85 verify(workflowVersionManagerMock, times(1)).create(ITEM1_ID, null, version);
90 public void shouldReturnWorkflowVersionWhenExists() throws Exception {
91 WorkflowVersion version = new WorkflowVersion(VERSION1_ID);
92 doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID);
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);
101 public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception {
102 WorkflowVersion version = new WorkflowVersion();
103 version.setDescription("Updated");
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()
111 assertEquals(HttpStatus.OK.value(), result.getStatus());
112 version.setId(VERSION1_ID);
113 verify(workflowVersionManagerMock, times(1)).update(ITEM1_ID, version);