aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowVersionControllerTest.java
blob: a8813c05ce87bd2636714b7e2fdda3d32afcbc7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.sdc.workflow.api;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.sdc.workflow.RestPath;
import org.onap.sdc.workflow.api.mappers.WorkflowVersionDtoMapper;
import org.onap.sdc.workflow.api.types.Parameter;
import org.onap.sdc.workflow.api.types.WorkflowVersionRequest;
import org.onap.sdc.workflow.api.types.WorkflowVersionResponse;
import org.onap.sdc.workflow.persistence.types.ParameterType;
import org.onap.sdc.workflow.services.WorkflowVersionManager;
import org.onap.sdc.workflow.services.types.WorkflowVersion;
import org.onap.sdc.workflow.services.utilities.JsonUtil;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(MockitoJUnitRunner.class)
public class WorkflowVersionControllerTest {

    private static final String USER_ID = "cs0008";
    private static final String ITEM1_ID = "item_id_1";
    private static final String VERSION1_ID = "version_id_1";
    private static final String VERSION2_ID = "version_id_2";

    private MockMvc mockMvc;

    @Mock
    private WorkflowVersionManager workflowVersionManagerMock;
    @Mock
    private WorkflowVersionDtoMapper versionDtoMapperMock;
    @InjectMocks
    private WorkflowVersionController workflowVersionController;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.standaloneSetup(workflowVersionController).build();
    }

    @Test
    public void shouldReturnWorkflowVersionListWhenCallingVersionGetREST() throws Exception {
        WorkflowVersion version1 = new WorkflowVersion(VERSION1_ID);
        WorkflowVersion version2 = new WorkflowVersion(VERSION2_ID);
        doReturn(Arrays.asList(version1, version2)).when(workflowVersionManagerMock).list(ITEM1_ID, null);

        WorkflowVersionResponse response1 = new WorkflowVersionResponse();
        response1.setId(VERSION1_ID);
        doReturn(response1).when(versionDtoMapperMock).workflowVersionToResponse(version1);
        WorkflowVersionResponse response2 = new WorkflowVersionResponse();
        response2.setId(VERSION2_ID);
        doReturn(response2).when(versionDtoMapperMock).workflowVersionToResponse(version2);

        mockMvc.perform(get(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
                                .contentType(APPLICATION_JSON)).andExpect(status().isOk())
                .andExpect(jsonPath("$.items", hasSize(2))).andExpect(jsonPath("$.items[0].id", is(VERSION1_ID)))
                .andExpect(jsonPath("$.items[1].id", is(VERSION2_ID)));

        verify(workflowVersionManagerMock).list(ITEM1_ID, null);
    }

    @Test
    public void shouldCreateWorkflowVersionWhenCallingVersionsPostREST() throws Exception {

        WorkflowVersionRequest request = new WorkflowVersionRequest();
        request.setDescription("Updated");
        WorkflowVersion version = new WorkflowVersion();
        version.setDescription("Updated");
        doReturn(version).when(versionDtoMapperMock).requestToWorkflowVersion(request);

        mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
                                .contentType(APPLICATION_JSON).content(JsonUtil.object2Json(request)))
                .andExpect(status().isCreated());

        verify(workflowVersionManagerMock).create(ITEM1_ID, null, version);
    }

    @Test
    public void shouldFailCreateWorkflowVersionWhenCallingVersionsPostRESTWithDuplicateInput() throws Exception {

        WorkflowVersionRequest version = new WorkflowVersionRequest();
        Collection<Parameter> inputs = Arrays.asList(createParameter("name1"), createParameter("name1"));
        version.setInputs(inputs);
        version.setDescription("VersionDescription");
        mockMvc.perform(post(RestPath.getWorkflowVersions(ITEM1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
                                .contentType(APPLICATION_JSON).content(JsonUtil.object2Json(version)))
                .andExpect(status().isBadRequest());
    }

    @Test
    public void shouldReturnWorkflowVersionWhenExists() throws Exception {
        WorkflowVersion version = new WorkflowVersion(VERSION1_ID);
        doReturn(version).when(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID);
        WorkflowVersionResponse response = new WorkflowVersionResponse();
        response.setId(VERSION1_ID);
        doReturn(response).when(versionDtoMapperMock).workflowVersionToResponse(version);

        mockMvc.perform(
                get(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
                        .contentType(APPLICATION_JSON)).andExpect(status().isOk())
                .andExpect(jsonPath("$.id", is(VERSION1_ID)));
        verify(workflowVersionManagerMock).get(ITEM1_ID, VERSION1_ID);
    }

    @Test
    public void shouldUpdateWorkflowVersionWhenCallingPutREST() throws Exception {
        WorkflowVersionRequest request = new WorkflowVersionRequest();
        request.setDescription("Updated");
        WorkflowVersion version = new WorkflowVersion();
        version.setDescription("Updated");
        doReturn(version).when(versionDtoMapperMock).requestToWorkflowVersion(request);

        MockHttpServletResponse result = mockMvc.perform(
                put(RestPath.getWorkflowVersion(ITEM1_ID, VERSION1_ID)).header(RestParams.USER_ID_HEADER, USER_ID)
                        .contentType(APPLICATION_JSON).content(JsonUtil.object2Json(request))).andReturn()
                                                 .getResponse();

        assertEquals(HttpStatus.OK.value(), result.getStatus());

        verify(workflowVersionManagerMock).update(ITEM1_ID, version);
    }

    private Parameter createParameter(String name) {
        Parameter parameter = new Parameter();
        parameter.setName(name);
        parameter.setMandatory(false);
        parameter.setType(ParameterType.STRING);
        return parameter;
    }
}