Catalog alignment
[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 fj.data.Either;
24 import org.glassfish.grizzly.http.util.HttpStatus;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.Spy;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
37 import org.openecomp.sdc.be.model.PropertyDefinition;
38 import org.openecomp.sdc.be.resources.data.EntryData;
39 import org.openecomp.sdc.common.api.Constants;
40 import org.openecomp.sdc.exception.ResponseFormat;
41 import org.springframework.web.context.WebApplicationContext;
42
43 import javax.servlet.ServletContext;
44 import javax.servlet.http.HttpSession;
45 import javax.ws.rs.core.Response;
46
47 import static org.mockito.ArgumentMatchers.any;
48 import static org.mockito.ArgumentMatchers.eq;
49 import static org.mockito.Mockito.when;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class ComponentPropertyServletTest extends JerseySpringBaseTest {
53     @Mock
54     private static HttpSession session;
55     @Mock
56     private static ServletContext context;
57     @Mock
58     private static WebAppContextWrapper wrapper;
59     @Mock
60     private static WebApplicationContext webAppContext;
61     @Mock
62     private static PropertyBusinessLogic propertyBl;
63     @Mock
64     private static ComponentsUtils componentsUtils;
65     @InjectMocks
66     @Spy
67     private ComponentPropertyServlet componentPropertyServlet;
68
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";
74
75     @Before
76     public void initClass() {
77         initMockitoStubbings();
78     }
79
80     @Test
81     public void testCreatePropertyOnService_success() {
82         PropertyDefinition property = new PropertyDefinition();
83         property.setName(VALID_PROPERTY_NAME);
84         property.setType(STRING_TYPE);
85
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));
88
89         Response propertyInService =
90                 componentPropertyServlet.createPropertyInService(SERVICE_ID, getValidProperty(), request, USER_ID);
91
92         Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), propertyInService.getStatus());
93     }
94
95     @Test
96     public void testCreatePropertyInvalidName_failure() {
97         PropertyDefinition property = new PropertyDefinition();
98         property.setName(INVALID_PROPERTY_NAME);
99         property.setType(STRING_TYPE);
100
101         ResponseFormat responseFormat = new ResponseFormat();
102         responseFormat.setStatus(HttpStatus.BAD_REQUEST_400.getStatusCode());
103
104         when(componentsUtils.getResponseFormat(eq(ActionStatus.INVALID_PROPERTY_NAME))).thenReturn(responseFormat);
105
106
107         Response propertyInService =
108                 componentPropertyServlet.createPropertyInService(SERVICE_ID, getInvalidProperty(), request, USER_ID);
109
110         Assert.assertEquals(HttpStatus.BAD_REQUEST_400.getStatusCode(), propertyInService.getStatus());
111     }
112
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(ComponentsUtils.class))).thenReturn(componentsUtils);
119     }
120
121     private String getValidProperty() {
122         return "{\n"
123                        + "  \"valid_name_123\": {\n"
124                        + "    \"schema\": {\n"
125                        + "      \"property\": {\n"
126                        + "        \"type\": \"\"\n"
127                        + "      }\n" + "    },\n"
128                        + "    \"type\": \"string\",\n"
129                        + "    \"name\": \"valid_name_123\"\n"
130                        + "  }\n"
131                        + "}";
132     }
133
134     private String getInvalidProperty() {
135         return "{\n"
136                        + "  \"invalid_name_$.&\": {\n"
137                        + "    \"schema\": {\n"
138                        + "      \"property\": {\n"
139                        + "        \"type\": \"\"\n"
140                        + "      }\n" + "    },\n"
141                        + "    \"type\": \"string\",\n"
142                        + "    \"name\": \"invalid_name_$.&\"\n"
143                        + "  }\n"
144                        + "}";
145     }
146
147 }