2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
28 import java.util.Optional;
29 import javax.servlet.ServletContext;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import org.apache.http.HttpStatus;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.glassfish.jersey.test.TestProperties;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mockito;
41 import org.mockito.MockitoAnnotations;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
47 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
48 import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation;
49 import org.openecomp.sdc.common.api.Constants;
50 import org.openecomp.sdc.exception.ResponseFormat;
51 import org.springframework.web.context.WebApplicationContext;
53 @ExtendWith(MockitoExtension.class)
54 class DataTypeServletTest extends JerseySpringBaseTest {
56 private static final String USER_ID = "cs0008";
57 private static final String DATA_TYPE_UID = "ETSI SOL001 v2.5.1.tosca.datatypes.nfv.L3AddressData.datatype";
58 private static final String PATH = "/v1/catalog/data-types/" + DATA_TYPE_UID;
61 private DataTypeServlet dataTypeServlet;
62 private ComponentsUtils componentsUtils;
63 private DataTypeOperation dataTypeOperation;
64 private ServletContext servletContext;
65 private WebApplicationContext webApplicationContext;
66 private WebAppContextWrapper webAppContextWrapper;
69 protected ResourceConfig configure() {
71 MockitoAnnotations.openMocks(this);
72 forceSet(TestProperties.CONTAINER_PORT, "0");
73 return super.configure().register(dataTypeServlet);
76 private void initMocks() {
77 componentsUtils = mock(ComponentsUtils.class);
78 dataTypeOperation = mock(DataTypeOperation.class);
79 servletContext = Mockito.mock(ServletContext.class);
80 webApplicationContext = Mockito.mock(WebApplicationContext.class);
81 webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
85 void before() throws Exception {
87 when(request.getSession()).thenReturn(session);
88 when(session.getServletContext()).thenReturn(servletContext);
89 when(webApplicationContext.getBean(ComponentsUtils.class)).thenReturn(componentsUtils);
90 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
91 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
95 void after() throws Exception {
100 void fetchDataTypeTest_Success() {
101 final DataTypeDataDefinition expectedDataType = new DataTypeDataDefinition();
102 expectedDataType.setUniqueId(DATA_TYPE_UID);
103 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.SC_OK));
104 when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenReturn(Optional.of(expectedDataType));
106 final Response response = target()
108 .request(MediaType.APPLICATION_JSON)
109 .header("USER_ID", USER_ID)
110 .get(Response.class);
111 assertNotNull(response);
112 assertEquals(HttpStatus.SC_OK, response.getStatus());
113 final DataTypeDataDefinition actualDataType = response.readEntity(DataTypeDataDefinition.class);
114 assertEquals(expectedDataType.getUniqueId(), actualDataType.getUniqueId());
118 void fetchDataTypeTest_Fail_OperationException() {
119 when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenThrow(OperationException.class);
121 final Response response = target()
123 .request(MediaType.APPLICATION_JSON)
124 .header("USER_ID", USER_ID)
125 .get(Response.class);
126 assertNotNull(response);
127 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatus());
131 void fetchDataTypeTest_Fail_EmptyOptional() {
132 when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenReturn(Optional.empty());
134 final Response response = target()
136 .request(MediaType.APPLICATION_JSON)
137 .header("USER_ID", USER_ID)
138 .get(Response.class);
139 assertNotNull(response);
140 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatus());
144 void fetchDataTypeTest_Fail_RuntimeException() {
145 when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenThrow(RuntimeException.class);
147 final Response response = target()
149 .request(MediaType.APPLICATION_JSON)
150 .header("USER_ID", USER_ID)
151 .get(Response.class);
152 assertNotNull(response);
153 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatus());