Fix locally failing TCs in catalog-be
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / ComponentPropertyServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.when;
26
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;
50
51 @ExtendWith(MockitoExtension.class)
52 class ComponentPropertyServletTest extends JerseySpringBaseTest {
53
54     @Mock
55     private static HttpSession session;
56     @Mock
57     private static ServletContext context;
58     @Mock
59     private static WebAppContextWrapper wrapper;
60     @Mock
61     private static WebApplicationContext webAppContext;
62     @Mock
63     private static PropertyBusinessLogic propertyBl;
64     @Mock
65     private static ComponentsUtils componentsUtils;
66     @InjectMocks
67     @Spy
68     private ComponentPropertyServlet componentPropertyServlet;
69
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";
75
76     @BeforeEach
77     public void before() throws Exception {
78         super.setUp();
79         when(request.getSession()).thenReturn(session);
80     }
81
82     @AfterEach
83     void after() throws Exception {
84         super.tearDown();
85     }
86
87     @Test
88     void testCreatePropertyOnService_success() {
89         PropertyDefinition property = new PropertyDefinition();
90         property.setName(VALID_PROPERTY_NAME);
91         property.setType(STRING_TYPE);
92
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));
96
97         Response propertyInService =
98             componentPropertyServlet.createPropertyInService(SERVICE_ID, getValidProperty(), request, USER_ID);
99
100         Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), propertyInService.getStatus());
101     }
102
103     @Test
104     void testCreatePropertyInvalidName_failure() {
105         PropertyDefinition property = new PropertyDefinition();
106         property.setName(INVALID_PROPERTY_NAME);
107         property.setType(STRING_TYPE);
108
109         ResponseFormat responseFormat = new ResponseFormat();
110         responseFormat.setStatus(HttpStatus.BAD_REQUEST_400.getStatusCode());
111
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);
117
118         Response propertyInService =
119             componentPropertyServlet.createPropertyInService(SERVICE_ID, getInvalidProperty(), request, USER_ID);
120
121         Assert.assertEquals(HttpStatus.BAD_REQUEST_400.getStatusCode(), propertyInService.getStatus());
122     }
123
124     private String getValidProperty() {
125         return "{\n"
126             + "  \"valid_name_123\": {\n"
127             + "    \"schema\": {\n"
128             + "      \"property\": {\n"
129             + "        \"type\": \"\"\n"
130             + "      }\n" + "    },\n"
131             + "    \"type\": \"string\",\n"
132             + "    \"name\": \"valid_name_123\"\n"
133             + "  }\n"
134             + "}";
135     }
136
137     private String getInvalidProperty() {
138         return "{\n"
139             + "  \"invalid_name_$.&\": {\n"
140             + "    \"schema\": {\n"
141             + "      \"property\": {\n"
142             + "        \"type\": \"\"\n"
143             + "      }\n" + "    },\n"
144             + "    \"type\": \"string\",\n"
145             + "    \"name\": \"invalid_name_$.&\"\n"
146             + "  }\n"
147             + "}";
148     }
149
150 }