Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / PolicyServletTest.java
1 package org.openecomp.sdc.be.servlets;
2
3 import com.fasterxml.jackson.databind.DeserializationFeature;
4 import fj.data.Either;
5 import org.glassfish.grizzly.http.util.HttpStatus;
6 import org.glassfish.jersey.client.ClientConfig;
7 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
8 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
9 import org.glassfish.jersey.server.ResourceConfig;
10 import org.junit.Before;
11 import org.junit.BeforeClass;
12 import org.junit.Ignore;
13 import org.junit.Test;
14 import org.mockito.Mockito;
15 import org.openecomp.sdc.be.components.impl.PolicyBusinessLogic;
16 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
17 import org.openecomp.sdc.be.dao.api.ActionStatus;
18 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
19 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
20 import org.openecomp.sdc.be.impl.ComponentsUtils;
21 import org.openecomp.sdc.be.impl.ServletUtils;
22 import org.openecomp.sdc.be.model.PolicyDefinition;
23 import org.openecomp.sdc.be.model.PolicyTargetDTO;
24 import org.openecomp.sdc.be.model.PropertyDefinition;
25 import org.openecomp.sdc.common.api.Constants;
26 import org.openecomp.sdc.exception.ResponseFormat;
27
28 import javax.ws.rs.client.ClientBuilder;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.client.Invocation;
31 import javax.ws.rs.core.GenericType;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.junit.Assert.assertEquals;
40 import static org.junit.Assert.assertTrue;
41 import static org.mockito.ArgumentMatchers.any;
42 import static org.mockito.ArgumentMatchers.anyMap;
43 import static org.mockito.ArgumentMatchers.eq;
44 import static org.mockito.Mockito.verifyZeroInteractions;
45 import static org.mockito.Mockito.when;
46
47 public class PolicyServletTest extends JerseySpringBaseTest{
48
49     private final static String USER_ID = "jh0003";
50     private static final String COMPONENT_ID = "componentId";
51     private static PolicyBusinessLogic businessLogic;
52     private static ComponentsUtils componentsUtils;
53     private static ServletUtils servletUtils;
54     private static ResponseFormat responseFormat;
55
56     private static String validComponentType = "resources";
57     private static String unsupportedComponentType = "unsupported";
58     private static String componentId = "componentId";
59     private static String policyTypeName = "policyTypeName";
60
61     private static final String PROPS_URL = "/v1/catalog/{componentType}/{serviceId}/policies/{policyId}/properties";
62     private static final String SERVICE_ID = "serviceId";
63     private static final String POLICY_ID = "policyId";
64
65     private static final String UPDATE_TARGETS_URL = "/v1/catalog/{componentType}/{componentId}/policies/{policyId}/targets";
66
67     @BeforeClass
68     public static void initClass() {
69         createMocks();
70         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
71     }
72     
73     @Before
74     public void beforeMethod() {
75         final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
76         setClient(ClientBuilder.newClient(new ClientConfig(jacksonJsonProvider)));
77     }
78
79     @Test
80     public void testGetPolicySuccess(){
81         String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
82         Either<PolicyDefinition, ResponseFormat> successResponse = Either.left(new PolicyDefinition());
83         when(businessLogic.getPolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(POLICY_ID), eq(USER_ID))).thenReturn(successResponse);
84         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200.getStatusCode());
85         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
86         Response response = target()
87                 .path(path)
88                 .request(MediaType.APPLICATION_JSON)
89                 .header("USER_ID", USER_ID)
90                 .get(Response.class);
91
92         assertTrue(response.getStatus() == HttpStatus.OK_200.getStatusCode());
93     }
94
95     @Test
96     public void testGetPolicyFailure(){
97         String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
98         when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400.getStatusCode());
99         when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(unsupportedComponentType))).thenReturn(responseFormat);
100         Response response = target()
101                 .path(path)
102                 .request(MediaType.APPLICATION_JSON)
103                 .header("USER_ID", USER_ID)
104                 .get(Response.class);
105
106         assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400.getStatusCode());
107     }
108     
109     @Test
110     public void testPostPolicySuccess(){
111         String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + policyTypeName;
112         PolicyDefinition policy = new PolicyDefinition();
113         Either<PolicyDefinition, ResponseFormat> successResponse = Either.left(policy);
114         when(businessLogic.createPolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(policyTypeName), eq(USER_ID), eq(true))).thenReturn(successResponse);
115         when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201.getStatusCode());
116         when(componentsUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
117         Response response = target()
118                 .path(path)
119                 .request(MediaType.APPLICATION_JSON)
120                 .header("USER_ID", USER_ID)
121                 .post(Entity.entity(policy, MediaType.APPLICATION_JSON),Response.class);
122
123         assertTrue(response.getStatus() == HttpStatus.CREATED_201.getStatusCode());
124     }
125     
126     @Test
127     public void testPostPolicyFailure(){
128         String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + policyTypeName;
129         PolicyDefinition policy = new PolicyDefinition();
130         when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400.getStatusCode());
131         when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(unsupportedComponentType))).thenReturn(responseFormat);
132         Response response = target()
133                 .path(path)
134                 .request(MediaType.APPLICATION_JSON)
135                 .header("USER_ID", USER_ID)
136                 .post(Entity.entity(policy, MediaType.APPLICATION_JSON),Response.class);
137
138         assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400.getStatusCode());
139     }
140     
141     @Test
142     public void testPutPolicySuccess(){
143         String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
144         PolicyDefinition policy = new PolicyDefinition();
145         policy.setUniqueId(POLICY_ID);
146         Either<PolicyDefinition, ResponseFormat> successResponse = Either.left(policy);
147         when(businessLogic.updatePolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), any(PolicyDefinition.class), eq(USER_ID), eq(true))).thenReturn(successResponse);
148         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200.getStatusCode());
149         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
150         Response response = target()
151                 .path(path)
152                 .request(MediaType.APPLICATION_JSON)
153                 .header("USER_ID", USER_ID)
154                 .put(Entity.entity(policy, MediaType.APPLICATION_JSON),Response.class);
155
156         assertTrue(response.getStatus() == HttpStatus.OK_200.getStatusCode());
157     }
158     
159     @Test
160     public void testPutPolicyFailure(){
161         String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
162         PolicyDefinition policy = new PolicyDefinition();
163         when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400.getStatusCode());
164         when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(unsupportedComponentType))).thenReturn(responseFormat);
165         Response response = target()
166                 .path(path)
167                 .request(MediaType.APPLICATION_JSON)
168                 .header("USER_ID", USER_ID)
169                 .put(Entity.entity(policy, MediaType.APPLICATION_JSON),Response.class);
170
171         assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400.getStatusCode());
172     }
173     
174     @Test
175     public void testDeletePolicySuccess(){
176         String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
177         Either<PolicyDefinition, ResponseFormat> successResponse = Either.left(new PolicyDefinition());
178         when(businessLogic.deletePolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(POLICY_ID), eq(USER_ID), eq(true))).thenReturn(successResponse);
179         when(responseFormat.getStatus()).thenReturn(HttpStatus.NO_CONTENT_204.getStatusCode());
180         when(componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT)).thenReturn(responseFormat);
181         Response response = target()
182                 .path(path)
183                 .request(MediaType.APPLICATION_JSON)
184                 .header("USER_ID", USER_ID)
185                 .delete(Response.class);
186
187         assertTrue(response.getStatus() == HttpStatus.NO_CONTENT_204.getStatusCode());
188     }
189
190     @Test
191     public void testDeletePolicyFailure(){
192         String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
193         when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400.getStatusCode());
194         when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(unsupportedComponentType))).thenReturn(responseFormat);
195         Response response = target()
196                 .path(path)
197                 .request(MediaType.APPLICATION_JSON)
198                 .header("USER_ID", USER_ID)
199                 .delete(Response.class);
200
201         assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400.getStatusCode());
202     }
203
204     @Test
205     public void getPolicyProperties_operationForbidden() {
206         when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID)).thenReturn(Either.right(new ResponseFormat(Response.Status.FORBIDDEN.getStatusCode())));
207         Response response = buildGetPropertiesRequest().get();
208         assertThat(response.getStatus()).isEqualTo(Response.Status.FORBIDDEN.getStatusCode());
209     }
210
211     @Test
212     public void getPolicyProperties_unHandledError_returnGeneralError() {
213         when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID)).thenThrow(new RuntimeException());
214         Response response = buildGetPropertiesRequest().get();
215         assertThat(response.getStatus()).isEqualTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
216     }
217
218     @Test
219     @Ignore
220     public void getPolicyProperties_wrongComponentType() {
221         Response response = buildGetPropertiesRequest("unknownType").get();
222         assertThat(response.getStatus()).isEqualTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
223         verifyZeroInteractions(businessLogic);
224     }
225
226     @Test
227     public void getPolicyProperties() {
228         List<PropertyDataDefinition> properties = getPropertiesList();
229         when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID)).thenReturn(Either.left(properties));
230         List<PropertyDataDefinition> policyProps = buildGetPropertiesRequest().get(new GenericType<List<PropertyDataDefinition>>() {});
231         assertThat(policyProps)
232                 .usingElementComparatorOnFields("uniqueId")
233                 .containsExactlyInAnyOrder(properties.get(0), properties.get(1));
234     }
235     
236     @Test
237     public void updatePolicyPropertiesSuccess() {
238         List<PropertyDataDefinition> properties = getPropertiesList();
239         when(businessLogic.updatePolicyProperties(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID), eq(POLICY_ID), any(PropertyDataDefinition[].class), eq(USER_ID), eq(true))).thenReturn(Either.left(properties));
240         List<PropertyDataDefinition> policyProps = buildUpdatePropertiesRequest(ComponentTypeEnum.SERVICE_PARAM_NAME, properties).invoke(new GenericType<List<PropertyDataDefinition>>() {});
241         assertThat(policyProps)
242                 .usingElementComparatorOnFields("uniqueId")
243                 .containsExactlyInAnyOrder(properties.get(0), properties.get(1));
244     }
245
246     @Test
247     public void updatePolicyTargetsSuccess() {
248         List<PolicyTargetDTO> targets = getTargetDTOList();
249         when(businessLogic.updatePolicyTargets(eq(ComponentTypeEnum.RESOURCE), eq(COMPONENT_ID), eq(POLICY_ID), anyMap(), eq(USER_ID))).thenReturn(Either.left(new PolicyDefinition()));
250         Response policyTargets = buildUpdateTargetsRequest(ComponentTypeEnum.RESOURCE_PARAM_NAME, targets).invoke();
251         assertThat(policyTargets.getStatus()).isEqualTo(200);
252     }
253
254     @Test
255     public void updatePolicyPropertiesFailure() {
256         List<PropertyDataDefinition> properties = getPropertiesList();
257         ResponseFormat notFoundResponse = new ResponseFormat(HttpStatus.NOT_FOUND_404.getStatusCode());
258         when(businessLogic.updatePolicyProperties(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID), eq(POLICY_ID), any(PropertyDataDefinition[].class), eq(USER_ID), eq(true))).thenReturn(Either.right(notFoundResponse));
259         Response policyProps = buildUpdatePropertiesRequest(ComponentTypeEnum.SERVICE_PARAM_NAME, properties).invoke();
260         assertEquals(HttpStatus.NOT_FOUND_404.getStatusCode(), policyProps.getStatus());
261     }
262
263     private List<PropertyDataDefinition> getPropertiesList() {
264         PropertyDefinition prop1 = new PropertyDataDefinitionBuilder()
265                 .setUniqueId("prop1")
266                 .build();
267
268         PropertyDefinition prop2 = new PropertyDataDefinitionBuilder()
269                 .setUniqueId("prop2")
270                 .build();
271         return Arrays.asList(prop1, prop2);
272     }
273
274     private List<PolicyTargetDTO> getTargetDTOList() {
275         PolicyTargetDTO target1 = new PolicyTargetDTO();
276         target1.setUniqueIds(Collections.singletonList("uniqueId"));
277         target1.setType("GROUPS");
278
279         PolicyTargetDTO target2 = new PolicyTargetDTO();
280         target2.setUniqueIds(Collections.singletonList("uniqueId"));
281         target2.setType("component_Instances");
282
283         return Arrays.asList(target1, target2);
284     }
285
286     private Invocation.Builder buildGetPropertiesRequest(String componentType) {
287         return target(PROPS_URL)
288                 .resolveTemplate("componentType", componentType)
289                 .resolveTemplate("serviceId", SERVICE_ID)
290                 .resolveTemplate("policyId", POLICY_ID)
291                 .request(MediaType.APPLICATION_JSON)
292                 .header(Constants.USER_ID_HEADER, USER_ID);
293
294     }
295     
296     private Invocation buildUpdatePropertiesRequest(String componentType, List<PropertyDataDefinition> properties) {
297         return target(PROPS_URL)
298                 .resolveTemplate("componentType", componentType)
299                 .resolveTemplate("serviceId", SERVICE_ID)
300                 .resolveTemplate("policyId", POLICY_ID)
301                 .request(MediaType.APPLICATION_JSON)
302                 .header(Constants.USER_ID_HEADER, USER_ID)
303                 .buildPut(Entity.entity(properties, MediaType.APPLICATION_JSON));
304     }
305
306     private Invocation buildUpdateTargetsRequest(String componentType, List<PolicyTargetDTO> targets) {
307         return target(UPDATE_TARGETS_URL)
308                 .resolveTemplate("componentType", componentType)
309                 .resolveTemplate("componentId", COMPONENT_ID)
310                 .resolveTemplate("policyId", POLICY_ID)
311                 .request(MediaType.APPLICATION_JSON)
312                 .header(Constants.USER_ID_HEADER, USER_ID)
313                 .buildPut(Entity.entity(targets, MediaType.APPLICATION_JSON));
314     }
315
316     private Invocation.Builder buildGetPropertiesRequest() {
317         return target(PROPS_URL)
318                 .resolveTemplate("componentType", "services")
319                 .resolveTemplate("serviceId", SERVICE_ID)
320                 .resolveTemplate("policyId", POLICY_ID)
321                 .request(MediaType.APPLICATION_JSON)
322                 .header(Constants.USER_ID_HEADER, USER_ID);
323     }
324     
325     @Override
326     protected ResourceConfig configure() {
327         return super.configure()
328                 .register(new PolicyServlet(businessLogic, servletUtils, null, componentsUtils));
329     }
330
331     private static void createMocks() {
332         businessLogic = Mockito.mock(PolicyBusinessLogic.class);
333         componentsUtils = Mockito.mock(ComponentsUtils.class);
334         servletUtils = Mockito.mock(ServletUtils.class);
335         responseFormat = Mockito.mock(ResponseFormat.class);
336     }
337     
338 }