d916fffc120f5500e7372355abb83f918276fd97
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / DataTypeServletTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * SDC
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
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
21 package org.openecomp.sdc.be.servlets;
22
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;
27
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;
52
53 @ExtendWith(MockitoExtension.class)
54 class DataTypeServletTest extends JerseySpringBaseTest {
55
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;
59
60     @InjectMocks
61     private DataTypeServlet dataTypeServlet;
62     private ComponentsUtils componentsUtils;
63     private DataTypeOperation dataTypeOperation;
64     private ServletContext servletContext;
65     private WebApplicationContext webApplicationContext;
66     private WebAppContextWrapper webAppContextWrapper;
67
68     @Override
69     protected ResourceConfig configure() {
70         initMocks();
71         MockitoAnnotations.openMocks(this);
72         forceSet(TestProperties.CONTAINER_PORT, "0");
73         return super.configure().register(dataTypeServlet);
74     }
75
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);
82     }
83
84     @BeforeEach
85     void before() throws Exception {
86         super.setUp();
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);
92     }
93
94     @AfterEach
95     void after() throws Exception {
96         super.tearDown();
97     }
98
99     @Test
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));
105
106         final Response response = target()
107             .path(PATH)
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());
115     }
116
117     @Test
118     void fetchDataTypeTest_Fail_OperationException() {
119         when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenThrow(OperationException.class);
120
121         final Response response = target()
122             .path(PATH)
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());
128     }
129
130     @Test
131     void fetchDataTypeTest_Fail_EmptyOptional() {
132         when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenReturn(Optional.empty());
133
134         final Response response = target()
135             .path(PATH)
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());
141     }
142
143     @Test
144     void fetchDataTypeTest_Fail_RuntimeException() {
145         when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenThrow(RuntimeException.class);
146
147         final Response response = target()
148             .path(PATH)
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());
154     }
155
156 }