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 com.google.gson.Gson;
28 import fj.data.Either;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpSession;
31 import javax.ws.rs.core.Response;
32 import org.glassfish.grizzly.http.util.HttpStatus;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Spy;
40 import org.mockito.junit.MockitoJUnitRunner;
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 @RunWith(MockitoJUnitRunner.class)
52 public class ComponentPropertyServletTest extends JerseySpringBaseTest {
54 private static HttpSession session;
56 private static ServletContext context;
58 private static WebAppContextWrapper wrapper;
60 private static WebApplicationContext webAppContext;
62 private static PropertyBusinessLogic propertyBl;
64 private static ComponentsUtils componentsUtils;
67 private ComponentPropertyServlet componentPropertyServlet;
69 private static final String SERVICE_ID = "service1";
70 private final static String USER_ID = "jh0003";
71 private static final String VALID_PROPERTY_NAME = "valid_name_123";
72 private static final String INVALID_PROPERTY_NAME = "invalid_name_$.&";
73 private static final String STRING_TYPE = "string";
76 public void initClass() {
77 initMockitoStubbings();
81 public void testCreatePropertyOnService_success() {
82 PropertyDefinition property = new PropertyDefinition();
83 property.setName(VALID_PROPERTY_NAME);
84 property.setType(STRING_TYPE);
86 EntryData<String, PropertyDefinition> propertyEntry = new EntryData<>(VALID_PROPERTY_NAME, property);
87 when(propertyBl.addPropertyToComponent(eq(SERVICE_ID), any(), any(), any())).thenReturn(Either.left(propertyEntry));
89 Response propertyInService =
90 componentPropertyServlet.createPropertyInService(SERVICE_ID, getValidProperty(), request, USER_ID);
92 Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), propertyInService.getStatus());
96 public void testCreatePropertyInvalidName_failure() {
97 PropertyDefinition property = new PropertyDefinition();
98 property.setName(INVALID_PROPERTY_NAME);
99 property.setType(STRING_TYPE);
101 ResponseFormat responseFormat = new ResponseFormat();
102 responseFormat.setStatus(HttpStatus.BAD_REQUEST_400.getStatusCode());
104 when(componentsUtils.getResponseFormat(eq(ActionStatus.INVALID_PROPERTY_NAME))).thenReturn(responseFormat);
107 Response propertyInService =
108 componentPropertyServlet.createPropertyInService(SERVICE_ID, getInvalidProperty(), request, USER_ID);
110 Assert.assertEquals(HttpStatus.BAD_REQUEST_400.getStatusCode(), propertyInService.getStatus());
113 private static void initMockitoStubbings() {
114 when(request.getSession()).thenReturn(session);
115 when(session.getServletContext()).thenReturn(context);
116 when(context.getAttribute(eq(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))).thenReturn(wrapper);
117 when(wrapper.getWebAppContext(any())).thenReturn(webAppContext);
118 when(webAppContext.getBean(eq(PropertyBusinessLogic.class))).thenReturn(propertyBl);
119 when(webAppContext.getBean(eq(ComponentsUtils.class))).thenReturn(componentsUtils);
122 private String getValidProperty() {
124 + " \"valid_name_123\": {\n"
126 + " \"property\": {\n"
127 + " \"type\": \"\"\n"
129 + " \"type\": \"string\",\n"
130 + " \"name\": \"valid_name_123\"\n"
135 private String getInvalidProperty() {
137 + " \"invalid_name_$.&\": {\n"
139 + " \"property\": {\n"
140 + " \"type\": \"\"\n"
142 + " \"type\": \"string\",\n"
143 + " \"name\": \"invalid_name_$.&\"\n"