catalog-be servlets refactoring
[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 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;
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     @Test
76     public void testCreatePropertyOnService_success() {
77         PropertyDefinition property = new PropertyDefinition();
78         property.setName(VALID_PROPERTY_NAME);
79         property.setType(STRING_TYPE);
80
81         EntryData<String, PropertyDefinition> propertyEntry = new EntryData<>(VALID_PROPERTY_NAME, property);
82         when(propertyBl.addPropertyToComponent(eq(SERVICE_ID), any(), any(), any())).thenReturn(Either.left(propertyEntry));
83
84         Response propertyInService =
85                 componentPropertyServlet.createPropertyInService(SERVICE_ID, getValidProperty(), request, USER_ID);
86
87         Assert.assertEquals(HttpStatus.OK_200.getStatusCode(), propertyInService.getStatus());
88     }
89
90     @Test
91     public void testCreatePropertyInvalidName_failure() {
92         PropertyDefinition property = new PropertyDefinition();
93         property.setName(INVALID_PROPERTY_NAME);
94         property.setType(STRING_TYPE);
95
96         ResponseFormat responseFormat = new ResponseFormat();
97         responseFormat.setStatus(HttpStatus.BAD_REQUEST_400.getStatusCode());
98
99         when(componentsUtils.getResponseFormat(eq(ActionStatus.INVALID_PROPERTY_NAME))).thenReturn(responseFormat);
100
101
102         Response propertyInService =
103                 componentPropertyServlet.createPropertyInService(SERVICE_ID, getInvalidProperty(), request, USER_ID);
104
105         Assert.assertEquals(HttpStatus.BAD_REQUEST_400.getStatusCode(), propertyInService.getStatus());
106     }
107
108     private String getValidProperty() {
109         return "{\n"
110                        + "  \"valid_name_123\": {\n"
111                        + "    \"schema\": {\n"
112                        + "      \"property\": {\n"
113                        + "        \"type\": \"\"\n"
114                        + "      }\n" + "    },\n"
115                        + "    \"type\": \"string\",\n"
116                        + "    \"name\": \"valid_name_123\"\n"
117                        + "  }\n"
118                        + "}";
119     }
120
121     private String getInvalidProperty() {
122         return "{\n"
123                        + "  \"invalid_name_$.&\": {\n"
124                        + "    \"schema\": {\n"
125                        + "      \"property\": {\n"
126                        + "        \"type\": \"\"\n"
127                        + "      }\n" + "    },\n"
128                        + "    \"type\": \"string\",\n"
129                        + "    \"name\": \"invalid_name_$.&\"\n"
130                        + "  }\n"
131                        + "}";
132     }
133
134 }