Implement Attributes/Outputs BE (part 3)
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / OutputsServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021, Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.servlets;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum.RESOURCE;
31
32 import fj.data.Either;
33 import java.util.ArrayList;
34 import java.util.List;
35 import javax.servlet.ServletContext;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpSession;
38 import javax.ws.rs.core.Application;
39 import javax.ws.rs.core.Response;
40 import org.assertj.core.api.Assertions;
41 import org.glassfish.grizzly.http.util.HttpStatus;
42 import org.glassfish.hk2.utilities.binding.AbstractBinder;
43 import org.glassfish.jersey.server.ResourceConfig;
44 import org.glassfish.jersey.test.JerseyTest;
45 import org.junit.jupiter.api.AfterEach;
46 import org.junit.jupiter.api.BeforeEach;
47 import org.junit.jupiter.api.Test;
48 import org.junit.jupiter.api.TestInstance;
49 import org.junit.jupiter.api.TestInstance.Lifecycle;
50 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.OutputsBusinessLogic;
52 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
53 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
54 import org.openecomp.sdc.be.config.SpringConfig;
55 import org.openecomp.sdc.be.dao.api.ActionStatus;
56 import org.openecomp.sdc.be.impl.ComponentsUtils;
57 import org.openecomp.sdc.be.impl.ServletUtils;
58 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
59 import org.openecomp.sdc.be.model.ComponentInstOutputsMap;
60 import org.openecomp.sdc.be.model.OutputDefinition;
61 import org.openecomp.sdc.be.model.User;
62 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
63 import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper;
64 import org.openecomp.sdc.be.user.UserBusinessLogic;
65 import org.openecomp.sdc.common.api.Constants;
66 import org.openecomp.sdc.exception.ResponseFormat;
67 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
68 import org.springframework.web.context.WebApplicationContext;
69
70 @TestInstance(Lifecycle.PER_CLASS)
71 class OutputsServletTest extends JerseyTest {
72
73     private static final String USER_ID = "jh0003";
74     private static final String COMPONENT_ID = "componentId";
75     private static final String RESOURCES = "resources";
76     private static final String COMPONENT_INST_OUTPUTS_MAP_OBJ = "componentInstOutputsMapObj";
77     private static final String OUTPUT_ID = "outputId";
78     private static final String INSTANCE_ID = "instanceId";
79     private static final String COMPONENT_UID = "originComponentUid";
80
81     private final UserBusinessLogic userBusinessLogic = mock(UserBusinessLogic.class);
82     private final OutputsBusinessLogic outputsBusinessLogic = mock(OutputsBusinessLogic.class);
83     private final ComponentInstanceBusinessLogic componentInstanceBL = mock(ComponentInstanceBusinessLogic.class);
84     private final HttpSession httpSession = mock(HttpSession.class);
85     private final ServletContext servletContext = mock(ServletContext.class);
86     private final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class);
87     private final ComponentsUtils componentsUtils = mock(ComponentsUtils.class);
88     private final ServletUtils servletUtils = mock(ServletUtils.class);
89     private final ResourceImportManager resourceImportManager = mock(ResourceImportManager.class);
90     private final HttpServletRequest request = mock(HttpServletRequest.class);
91     private final WebAppContextWrapper wrapper = mock(WebAppContextWrapper.class);
92
93     @Override
94     @BeforeEach
95     public void setUp() throws Exception {
96         super.setUp();
97         when(request.getSession()).thenReturn(httpSession);
98         when(httpSession.getServletContext()).thenReturn(servletContext);
99         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(wrapper);
100         when(wrapper.getWebAppContext(any())).thenReturn(webApplicationContext);
101         when(webApplicationContext.getBean(OutputsBusinessLogic.class)).thenReturn(outputsBusinessLogic);
102         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
103     }
104
105     @Override
106     @AfterEach
107     public void tearDown() throws Exception {
108         super.tearDown();
109     }
110
111     @Test
112     void test_getComponentInstanceOutputs_success() {
113         final Either<List<OutputDefinition>, ResponseFormat> listResponseFormatEither = Either.left(new ArrayList<>());
114         doReturn(listResponseFormatEither).when(outputsBusinessLogic).getComponentInstanceOutputs(USER_ID, COMPONENT_ID, INSTANCE_ID);
115
116         final ResponseFormat responseFormat = new ResponseFormat(HttpStatus.OK_200.getStatusCode());
117         doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.OK);
118
119         final OutputsServlet outputsServlet = createTestObject();
120         Assertions.assertThat(outputsServlet).isNotNull();
121
122         final Response response = outputsServlet.getComponentInstanceOutputs(RESOURCES, COMPONENT_ID, INSTANCE_ID, COMPONENT_UID, request, USER_ID);
123         Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200.getStatusCode());
124         verify(componentsUtils, times(2)).getResponseFormat(ActionStatus.OK);
125     }
126
127     @Test
128     void test_getComponentInstanceOutputs_isRight() {
129         final ResponseFormat responseFormat = new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode());
130         final Either<Object, ResponseFormat> right = Either.right(responseFormat);
131         doReturn(right).when(outputsBusinessLogic).getComponentInstanceOutputs(USER_ID, COMPONENT_ID, INSTANCE_ID);
132
133         doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.GENERAL_ERROR);
134
135         final OutputsServlet outputsServlet = createTestObject();
136         Assertions.assertThat(outputsServlet).isNotNull();
137
138         final Response response = outputsServlet.getComponentInstanceOutputs(RESOURCES, COMPONENT_ID, INSTANCE_ID, COMPONENT_UID, request, USER_ID);
139         Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
140         verify(componentsUtils, times(0)).getResponseFormat(ActionStatus.GENERAL_ERROR);
141     }
142
143     @Test
144     void test_getComponentInstanceOutputs_fail() {
145         final ResponseFormat responseFormat = new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode());
146         final Either<Object, ResponseFormat> right = Either.right(responseFormat);
147         doReturn(right).when(outputsBusinessLogic).getComponentInstanceOutputs(USER_ID, COMPONENT_ID, INSTANCE_ID);
148
149         doThrow(new RuntimeException()).when(componentsUtils).getResponseFormat(ActionStatus.GENERAL_ERROR);
150
151         final OutputsServlet outputsServlet = createTestObject();
152         Assertions.assertThat(outputsServlet).isNotNull();
153
154         final Response response = outputsServlet.getComponentInstanceOutputs(RESOURCES, COMPONENT_ID, INSTANCE_ID, COMPONENT_UID, request, USER_ID);
155         Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
156         verify(componentsUtils, times(0)).getResponseFormat(ActionStatus.GENERAL_ERROR);
157     }
158
159     @Test
160     void test_createMultipleOutputs_success() {
161         final ComponentInstOutputsMap componentInstOutputsMap = new ComponentInstOutputsMap();
162         final Either<ComponentInstOutputsMap, ResponseFormat> left = Either.left(componentInstOutputsMap);
163
164         doReturn(left).when(componentsUtils)
165             .convertJsonToObjectUsingObjectMapper(any(String.class), any(User.class), eq(ComponentInstOutputsMap.class),
166                 eq(AuditingActionEnum.CREATE_RESOURCE), eq(RESOURCE));
167         final Either<List<OutputDefinition>, ResponseFormat> listResponseFormatEither = Either.left(new ArrayList<>());
168         doReturn(listResponseFormatEither).when(outputsBusinessLogic).declareAttributes(USER_ID, COMPONENT_ID, RESOURCE, componentInstOutputsMap);
169
170         final ResponseFormat responseFormat = new ResponseFormat(HttpStatus.OK_200.getStatusCode());
171         doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.OK);
172
173         final OutputsServlet outputsServlet = createTestObject();
174         Assertions.assertThat(outputsServlet).isNotNull();
175
176         final Response response = outputsServlet.createMultipleOutputs(RESOURCES, COMPONENT_ID, request, USER_ID, COMPONENT_INST_OUTPUTS_MAP_OBJ);
177         Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200.getStatusCode());
178         verify(componentsUtils, times(3)).getResponseFormat(ActionStatus.OK);
179     }
180
181     @Test
182     void test_createMultipleOutputs_isRight() {
183         final ResponseFormat responseFormat = new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode());
184         final Either<Object, ResponseFormat> right = Either.right(responseFormat);
185         doReturn(right).when(outputsBusinessLogic).declareAttributes(USER_ID, COMPONENT_ID, RESOURCE, new ComponentInstOutputsMap());
186
187         doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.GENERAL_ERROR);
188
189         final OutputsServlet outputsServlet = createTestObject();
190         Assertions.assertThat(outputsServlet).isNotNull();
191
192         final Response response = outputsServlet.createMultipleOutputs(RESOURCES, COMPONENT_ID, request, USER_ID, COMPONENT_INST_OUTPUTS_MAP_OBJ);
193         Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
194         verify(componentsUtils, times(3)).getResponseFormat(ActionStatus.GENERAL_ERROR);
195     }
196
197     @Test
198     void test_createMultipleOutputs_fail() {
199         final ResponseFormat responseFormat = new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode());
200         doThrow(new RuntimeException()).when(outputsBusinessLogic).declareAttributes(USER_ID, COMPONENT_ID, RESOURCE, new ComponentInstOutputsMap());
201
202         doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.GENERAL_ERROR);
203
204         final OutputsServlet outputsServlet = createTestObject();
205         Assertions.assertThat(outputsServlet).isNotNull();
206
207         final Response response = outputsServlet.createMultipleOutputs(RESOURCES, COMPONENT_ID, request, USER_ID, COMPONENT_INST_OUTPUTS_MAP_OBJ);
208         Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
209         verify(componentsUtils, times(2)).getResponseFormat(ActionStatus.GENERAL_ERROR);
210     }
211
212     @Test
213     void test_deleteOutput_success() {
214         doReturn(new OutputDefinition()).when(outputsBusinessLogic).deleteOutput(COMPONENT_ID, USER_ID, OUTPUT_ID);
215
216         final ResponseFormat responseFormat = new ResponseFormat(HttpStatus.OK_200.getStatusCode());
217         doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.OK);
218
219         final OutputsServlet outputsServlet = createTestObject();
220         Assertions.assertThat(outputsServlet).isNotNull();
221
222         final Response response = outputsServlet.deleteOutput(RESOURCES, COMPONENT_ID, OUTPUT_ID, request, USER_ID, COMPONENT_INST_OUTPUTS_MAP_OBJ);
223         Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200.getStatusCode());
224         verify(componentsUtils, times(1)).getResponseFormat(ActionStatus.OK);
225     }
226
227     @Test
228     void test_deleteOutput_fail() {
229         final ResponseFormat responseFormat = new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode());
230         doThrow(new ComponentException(responseFormat)).when(outputsBusinessLogic).deleteOutput(COMPONENT_ID, USER_ID, OUTPUT_ID);
231
232         doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.GENERAL_ERROR);
233
234         final OutputsServlet outputsServlet = createTestObject();
235         Assertions.assertThat(outputsServlet).isNotNull();
236
237         final Response response = outputsServlet.deleteOutput(RESOURCES, COMPONENT_ID, OUTPUT_ID, request, USER_ID, COMPONENT_INST_OUTPUTS_MAP_OBJ);
238         Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());
239         verify(componentsUtils, times(1)).getResponseFormat(ActionStatus.GENERAL_ERROR);
240     }
241
242     private OutputsServlet createTestObject() {
243         return new OutputsServlet(userBusinessLogic, outputsBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
244     }
245
246     @Override
247     protected Application configure() {
248         final OutputsServlet outputsServlet = createTestObject();
249         final ResourceConfig resourceConfig = new ResourceConfig()
250             .register(outputsServlet)
251             .register(new ComponentExceptionMapper(componentsUtils))
252             .register(new AbstractBinder() {
253                 @Override
254                 protected void configure() {
255                     bind(request).to(HttpServletRequest.class);
256                 }
257             });
258
259         resourceConfig.property("contextConfig", new AnnotationConfigApplicationContext(SpringConfig.class));
260         return resourceConfig;
261     }
262
263 }