2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.openecomp.sdc.be.servlets;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.when;
26 import fj.data.Either;
27 import java.util.Arrays;
28 import java.util.List;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpSession;
31 import javax.ws.rs.core.Response;
32 import org.assertj.core.api.Assertions;
33 import org.glassfish.grizzly.http.util.HttpStatus;
34 import org.junit.Assert;
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.Mock;
41 import org.mockito.Spy;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic;
44 import org.openecomp.sdc.be.dao.api.ActionStatus;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
47 import org.openecomp.sdc.be.model.AttributeDefinition;
48 import org.openecomp.sdc.common.api.Constants;
49 import org.openecomp.sdc.exception.ResponseFormat;
50 import org.springframework.web.context.WebApplicationContext;
52 @ExtendWith(MockitoExtension.class)
53 class ComponentAttributeServletTest extends JerseySpringBaseTest {
56 private ServletContext context;
58 private WebAppContextWrapper wrapper;
60 private WebApplicationContext webAppContext;
62 private HttpSession session;
64 private AttributeBusinessLogic attributeBusinessLogic;
66 private ComponentsUtils componentsUtils;
69 private ComponentAttributeServlet componentAttributeServlet;
71 private static final String SERVICE_ID = "service1";
72 private static final String RESOURCE_ID = "resource1";
73 private static final String USER_ID = "jh0003";
74 private static final String VALID_PROPERTY_NAME = "valid_name_123";
75 private static final String INVALID_PROPERTY_NAME = "invalid_name_$.&";
76 private static final String STRING_TYPE = "string";
79 public void initClass() throws Exception {
81 when(request.getSession()).thenReturn(session);
85 public void tearDown() throws Exception {
90 void getAttributeListInService_success() {
91 AttributeDefinition attributeDefinition = new AttributeDefinition();
92 attributeDefinition.setName(VALID_PROPERTY_NAME);
93 attributeDefinition.setType(STRING_TYPE);
95 List<AttributeDefinition> attributeDefinitionEntryData = Arrays.asList(attributeDefinition);
96 when(attributeBusinessLogic.getAttributesList(any(), any())).thenReturn(Either.left(attributeDefinitionEntryData));
98 Response attributeInService = componentAttributeServlet.getAttributeListInService(SERVICE_ID, request, USER_ID);
100 Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), attributeInService.getStatus());
104 void getAttributeListInService_fail() {
105 when(attributeBusinessLogic.getAttributesList(any(), any()))
106 .thenReturn(Either.right(new ResponseFormat(Response.Status.NOT_FOUND.getStatusCode())));
108 Response attributeInService = componentAttributeServlet.getAttributeListInService(SERVICE_ID, request, USER_ID);
110 Assertions.assertThat(attributeInService).isNotNull();
111 Assertions.assertThat(attributeInService.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
115 void getAttributeListInResource_success() {
116 AttributeDefinition attributeDefinition = new AttributeDefinition();
117 attributeDefinition.setName(VALID_PROPERTY_NAME);
118 attributeDefinition.setType(STRING_TYPE);
120 List<AttributeDefinition> attributeDefinitionEntryData = Arrays.asList(attributeDefinition);
121 when(attributeBusinessLogic.getAttributesList(any(), any())).thenReturn(Either.left(attributeDefinitionEntryData));
122 when(attributeBusinessLogic.getAttributesList(any(), any())).thenReturn(Either.left(attributeDefinitionEntryData));
124 Response attributeInService =
125 componentAttributeServlet.getAttributeListInResource(RESOURCE_ID, request, USER_ID);
127 Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), attributeInService.getStatus());
131 void getAttributeListInResource_fail() {
132 when(session.getServletContext()).thenReturn(context);
133 when(context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(wrapper);
134 when(wrapper.getWebAppContext(any())).thenReturn(webAppContext);
135 when(webAppContext.getBean(ComponentsUtils.class)).thenReturn(componentsUtils);
136 when(attributeBusinessLogic.getAttributesList(any(), any())).thenThrow(new RuntimeException());
138 ResponseFormat responseFormat = new ResponseFormat(HttpStatus.BAD_REQUEST_400.getStatusCode());
140 when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
142 Response attributeInService = componentAttributeServlet.getAttributeListInResource(SERVICE_ID, request, USER_ID);
144 Assertions.assertThat(attributeInService).isNotNull();
145 Assertions.assertThat(attributeInService.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST_400.getStatusCode());