2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. 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.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.when;
27 import fj.data.Either;
28 import javax.servlet.ServletContext;
29 import javax.servlet.http.HttpSession;
30 import javax.ws.rs.core.Response;
31 import org.glassfish.grizzly.http.util.HttpStatus;
32 import org.junit.Assert;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Spy;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
42 import org.openecomp.sdc.be.dao.api.ActionStatus;
43 import org.openecomp.sdc.be.impl.ComponentsUtils;
44 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
45 import org.openecomp.sdc.be.model.PropertyDefinition;
46 import org.openecomp.sdc.be.resources.data.EntryData;
47 import org.openecomp.sdc.common.api.Constants;
48 import org.openecomp.sdc.exception.ResponseFormat;
49 import org.springframework.web.context.WebApplicationContext;
51 @ExtendWith(MockitoExtension.class)
52 class ComponentPropertyServletTest extends JerseySpringBaseTest {
55 private static HttpSession session;
57 private static ServletContext context;
59 private static WebAppContextWrapper wrapper;
61 private static WebApplicationContext webAppContext;
63 private static PropertyBusinessLogic propertyBl;
65 private static ComponentsUtils componentsUtils;
68 private ComponentPropertyServlet componentPropertyServlet;
70 private static final String SERVICE_ID = "service1";
71 private static final String USER_ID = "jh0003";
72 private static final String VALID_PROPERTY_NAME = "valid_name_123";
73 private static final String INVALID_PROPERTY_NAME = "invalid_name_$.&";
74 private static final String STRING_TYPE = "string";
77 public void before() throws Exception {
79 when(request.getSession()).thenReturn(session);
83 void after() throws Exception {
88 void testCreatePropertyOnService_success() {
89 PropertyDefinition property = new PropertyDefinition();
90 property.setName(VALID_PROPERTY_NAME);
91 property.setType(STRING_TYPE);
93 EntryData<String, PropertyDefinition> propertyEntry = new EntryData<>(VALID_PROPERTY_NAME, property);
94 when(propertyBl.addPropertyToComponent(eq(SERVICE_ID), any(), any(), any()))
95 .thenReturn(Either.left(propertyEntry));
97 Response propertyInService =
98 componentPropertyServlet.createPropertyInService(SERVICE_ID, getValidProperty(), request, USER_ID);
100 Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), propertyInService.getStatus());
104 void testCreatePropertyInvalidName_failure() {
105 PropertyDefinition property = new PropertyDefinition();
106 property.setName(INVALID_PROPERTY_NAME);
107 property.setType(STRING_TYPE);
109 ResponseFormat responseFormat = new ResponseFormat();
110 responseFormat.setStatus(HttpStatus.BAD_REQUEST_400.getStatusCode());
112 when(componentsUtils.getResponseFormat(eq(ActionStatus.INVALID_PROPERTY_NAME))).thenReturn(responseFormat);
113 when(session.getServletContext()).thenReturn(context);
114 when(context.getAttribute(eq(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))).thenReturn(wrapper);
115 when(wrapper.getWebAppContext(any())).thenReturn(webAppContext);
116 when(webAppContext.getBean(eq(ComponentsUtils.class))).thenReturn(componentsUtils);
118 Response propertyInService =
119 componentPropertyServlet.createPropertyInService(SERVICE_ID, getInvalidProperty(), request, USER_ID);
121 Assert.assertEquals(HttpStatus.BAD_REQUEST_400.getStatusCode(), propertyInService.getStatus());
124 private String getValidProperty() {
126 + " \"valid_name_123\": {\n"
128 + " \"property\": {\n"
129 + " \"type\": \"\"\n"
131 + " \"type\": \"string\",\n"
132 + " \"name\": \"valid_name_123\"\n"
137 private String getInvalidProperty() {
139 + " \"invalid_name_$.&\": {\n"
141 + " \"property\": {\n"
142 + " \"type\": \"\"\n"
144 + " \"type\": \"string\",\n"
145 + " \"name\": \"invalid_name_$.&\"\n"