aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/PolicyServletTest.java
blob: bb949253b2c30a4b114fd3507e02d7913f335b23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package org.openecomp.sdc.be.servlets;

import com.fasterxml.jackson.databind.DeserializationFeature;
import fj.data.Either;
import org.glassfish.grizzly.http.util.HttpStatus;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.openecomp.sdc.be.components.impl.PolicyBusinessLogic;
import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.impl.ServletUtils;
import org.openecomp.sdc.be.model.PolicyDefinition;
import org.openecomp.sdc.be.model.PolicyTargetDTO;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.exception.ResponseFormat;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class PolicyServletTest extends JerseySpringBaseTest{

    private final static String USER_ID = "jh0003";
    private static final String COMPONENT_ID = "componentId";
    private static PolicyBusinessLogic businessLogic;
    private static ComponentsUtils componentsUtils;
    private static ServletUtils servletUtils;
    private static ResponseFormat responseFormat;

    private static String validComponentType = "resources";
    private static String unsupportedComponentType = "unsupported";
    private static String componentId = "componentId";
    private static String policyTypeName = "policyTypeName";

    private static final String PROPS_URL = "/v1/catalog/{componentType}/{serviceId}/policies/{policyId}/properties";
    private static final String SERVICE_ID = "serviceId";
    private static final String POLICY_ID = "policyId";

    private static final String UPDATE_TARGETS_URL = "/v1/catalog/{componentType}/{componentId}/policies/{policyId}/targets";

    @BeforeClass
    public static void initClass() {
        createMocks();
        when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
    }
    
    @Before
    public void beforeMethod() {
        final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        setClient(ClientBuilder.newClient(new ClientConfig(jacksonJsonProvider)));
    }

    @Test
    public void testGetPolicySuccess(){
        String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
        Either<PolicyDefinition, ResponseFormat> successResponse = Either.left(new PolicyDefinition());
        when(businessLogic.getPolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(POLICY_ID), eq(USER_ID))).thenReturn(successResponse);
        when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200.getStatusCode());
        when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
        Response response = target()
                .path(path)
                .request(MediaType.APPLICATION_JSON)
                .header("USER_ID", USER_ID)
                .get(Response.class);

        assertTrue(response.getStatus() == HttpStatus.OK_200.getStatusCode());
    }

    @Test
    public void testGetPolicyFailure(){
        String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
        when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400.getStatusCode());
        when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(unsupportedComponentType))).thenReturn(responseFormat);
        Response response = target()
                .path(path)
                .request(MediaType.APPLICATION_JSON)
                .header("USER_ID", USER_ID)
                .get(Response.class);

        assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400.getStatusCode());
    }
    
    @Test
    public void testPostPolicySuccess(){
        String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + policyTypeName;
        PolicyDefinition policy = new PolicyDefinition();
        Either<PolicyDefinition, ResponseFormat> successResponse = Either.left(policy);
        when(businessLogic.createPolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(policyTypeName), eq(USER_ID), eq(true))).thenReturn(successResponse);
        when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201.getStatusCode());
        when(componentsUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
        Response response = target()
                .path(path)
                .request(MediaType.APPLICATION_JSON)
                .header("USER_ID", USER_ID)
                .post(Entity.entity(policy, MediaType.APPLICATION_JSON),Response.class);

        assertTrue(response.getStatus() == HttpStatus.CREATED_201.getStatusCode());
    }
    
    @Test
    public void testPostPolicyFailure(){
        String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + policyTypeName;
        PolicyDefinition policy = new PolicyDefinition();
        when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400.getStatusCode());
        when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(unsupportedComponentType))).thenReturn(responseFormat);
        Response response = target()
                .path(path)
                .request(MediaType.APPLICATION_JSON)
                .header("USER_ID", USER_ID)
                .post(Entity.entity(policy, MediaType.APPLICATION_JSON),Response.class);

        assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400.getStatusCode());
    }
    
    @Test
    public void testPutPolicySuccess(){
        String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
        PolicyDefinition policy = new PolicyDefinition();
        policy.setUniqueId(POLICY_ID);
        Either<PolicyDefinition, ResponseFormat> successResponse = Either.left(policy);
        when(businessLogic.updatePolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), any(PolicyDefinition.class), eq(USER_ID), eq(true))).thenReturn(successResponse);
        when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200.getStatusCode());
        when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
        Response response = target()
                .path(path)
                .request(MediaType.APPLICATION_JSON)
                .header("USER_ID", USER_ID)
                .put(Entity.entity(policy, MediaType.APPLICATION_JSON),Response.class);

        assertTrue(response.getStatus() == HttpStatus.OK_200.getStatusCode());
    }
    
    @Test
    public void testPutPolicyFailure(){
        String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
        PolicyDefinition policy = new PolicyDefinition();
        when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400.getStatusCode());
        when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(unsupportedComponentType))).thenReturn(responseFormat);
        Response response = target()
                .path(path)
                .request(MediaType.APPLICATION_JSON)
                .header("USER_ID", USER_ID)
                .put(Entity.entity(policy, MediaType.APPLICATION_JSON),Response.class);

        assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400.getStatusCode());
    }
    
    @Test
    public void testDeletePolicySuccess(){
        String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
        Either<PolicyDefinition, ResponseFormat> successResponse = Either.left(new PolicyDefinition());
        when(businessLogic.deletePolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(POLICY_ID), eq(USER_ID), eq(true))).thenReturn(successResponse);
        when(responseFormat.getStatus()).thenReturn(HttpStatus.NO_CONTENT_204.getStatusCode());
        when(componentsUtils.getResponseFormat(ActionStatus.NO_CONTENT)).thenReturn(responseFormat);
        Response response = target()
                .path(path)
                .request(MediaType.APPLICATION_JSON)
                .header("USER_ID", USER_ID)
                .delete(Response.class);

        assertTrue(response.getStatus() == HttpStatus.NO_CONTENT_204.getStatusCode());
    }

    @Test
    public void testDeletePolicyFailure(){
        String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
        when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400.getStatusCode());
        when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(unsupportedComponentType))).thenReturn(responseFormat);
        Response response = target()
                .path(path)
                .request(MediaType.APPLICATION_JSON)
                .header("USER_ID", USER_ID)
                .delete(Response.class);

        assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400.getStatusCode());
    }

    @Test
    public void getPolicyProperties_operationForbidden() {
        when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID)).thenReturn(Either.right(new ResponseFormat(Response.Status.FORBIDDEN.getStatusCode())));
        Response response = buildGetPropertiesRequest().get();
        assertThat(response.getStatus()).isEqualTo(Response.Status.FORBIDDEN.getStatusCode());
    }

    @Test
    public void getPolicyProperties_unHandledError_returnGeneralError() {
        when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID)).thenThrow(new RuntimeException());
        Response response = buildGetPropertiesRequest().get();
        assertThat(response.getStatus()).isEqualTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }

    @Test
    @Ignore
    public void getPolicyProperties_wrongComponentType() {
        Response response = buildGetPropertiesRequest("unknownType").get();
        assertThat(response.getStatus()).isEqualTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        verifyZeroInteractions(businessLogic);
    }

    @Test
    public void getPolicyProperties() {
        List<PropertyDataDefinition> properties = getPropertiesList();
        when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID)).thenReturn(Either.left(properties));
        List<PropertyDataDefinition> policyProps = buildGetPropertiesRequest().get(new GenericType<List<PropertyDataDefinition>>() {});
        assertThat(policyProps)
                .usingElementComparatorOnFields("uniqueId")
                .containsExactlyInAnyOrder(properties.get(0), properties.get(1));
    }
    
    @Test
    public void updatePolicyPropertiesSuccess() {
        List<PropertyDataDefinition> properties = getPropertiesList();
        when(businessLogic.updatePolicyProperties(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID), eq(POLICY_ID), any(PropertyDataDefinition[].class), eq(USER_ID), eq(true))).thenReturn(Either.left(properties));
        List<PropertyDataDefinition> policyProps = buildUpdatePropertiesRequest(ComponentTypeEnum.SERVICE_PARAM_NAME, properties).invoke(new GenericType<List<PropertyDataDefinition>>() {});
        assertThat(policyProps)
                .usingElementComparatorOnFields("uniqueId")
                .containsExactlyInAnyOrder(properties.get(0), properties.get(1));
    }

    @Test
    public void updatePolicyTargetsSuccess() {
        List<PolicyTargetDTO> targets = getTargetDTOList();
        when(businessLogic.updatePolicyTargets(eq(ComponentTypeEnum.RESOURCE), eq(COMPONENT_ID), eq(POLICY_ID), anyMap(), eq(USER_ID))).thenReturn(Either.left(new PolicyDefinition()));
        Response policyTargets = buildUpdateTargetsRequest(ComponentTypeEnum.RESOURCE_PARAM_NAME, targets).invoke();
        assertThat(policyTargets.getStatus()).isEqualTo(200);
    }

    @Test
    public void updatePolicyPropertiesFailure() {
        List<PropertyDataDefinition> properties = getPropertiesList();
        ResponseFormat notFoundResponse = new ResponseFormat(HttpStatus.NOT_FOUND_404.getStatusCode());
        when(businessLogic.updatePolicyProperties(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID), eq(POLICY_ID), any(PropertyDataDefinition[].class), eq(USER_ID), eq(true))).thenReturn(Either.right(notFoundResponse));
        Response policyProps = buildUpdatePropertiesRequest(ComponentTypeEnum.SERVICE_PARAM_NAME, properties).invoke();
        assertEquals(HttpStatus.NOT_FOUND_404.getStatusCode(), policyProps.getStatus());
    }

    private List<PropertyDataDefinition> getPropertiesList() {
        PropertyDefinition prop1 = new PropertyDataDefinitionBuilder()
                .setUniqueId("prop1")
                .build();

        PropertyDefinition prop2 = new PropertyDataDefinitionBuilder()
                .setUniqueId("prop2")
                .build();
        return Arrays.asList(prop1, prop2);
    }

    private List<PolicyTargetDTO> getTargetDTOList() {
        PolicyTargetDTO target1 = new PolicyTargetDTO();
        target1.setUniqueIds(Collections.singletonList("uniqueId"));
        target1.setType("GROUPS");

        PolicyTargetDTO target2 = new PolicyTargetDTO();
        target2.setUniqueIds(Collections.singletonList("uniqueId"));
        target2.setType("component_Instances");

        return Arrays.asList(target1, target2);
    }

    private Invocation.Builder buildGetPropertiesRequest(String componentType) {
        return target(PROPS_URL)
                .resolveTemplate("componentType", componentType)
                .resolveTemplate("serviceId", SERVICE_ID)
                .resolveTemplate("policyId", POLICY_ID)
                .request(MediaType.APPLICATION_JSON)
                .header(Constants.USER_ID_HEADER, USER_ID);

    }
    
    private Invocation buildUpdatePropertiesRequest(String componentType, List<PropertyDataDefinition> properties) {
        return target(PROPS_URL)
                .resolveTemplate("componentType", componentType)
                .resolveTemplate("serviceId", SERVICE_ID)
                .resolveTemplate("policyId", POLICY_ID)
                .request(MediaType.APPLICATION_JSON)
                .header(Constants.USER_ID_HEADER, USER_ID)
                .buildPut(Entity.entity(properties, MediaType.APPLICATION_JSON));
    }

    private Invocation buildUpdateTargetsRequest(String componentType, List<PolicyTargetDTO> targets) {
        return target(UPDATE_TARGETS_URL)
                .resolveTemplate("componentType", componentType)
                .resolveTemplate("componentId", COMPONENT_ID)
                .resolveTemplate("policyId", POLICY_ID)
                .request(MediaType.APPLICATION_JSON)
                .header(Constants.USER_ID_HEADER, USER_ID)
                .buildPut(Entity.entity(targets, MediaType.APPLICATION_JSON));
    }

    private Invocation.Builder buildGetPropertiesRequest() {
        return target(PROPS_URL)
                .resolveTemplate("componentType", "services")
                .resolveTemplate("serviceId", SERVICE_ID)
                .resolveTemplate("policyId", POLICY_ID)
                .request(MediaType.APPLICATION_JSON)
                .header(Constants.USER_ID_HEADER, USER_ID);
    }
    
    @Override
    protected ResourceConfig configure() {
        return super.configure()
                .register(new PolicyServlet(businessLogic, servletUtils, null, componentsUtils));
    }

    private static void createMocks() {
        businessLogic = Mockito.mock(PolicyBusinessLogic.class);
        componentsUtils = Mockito.mock(ComponentsUtils.class);
        servletUtils = Mockito.mock(ServletUtils.class);
        responseFormat = Mockito.mock(ResponseFormat.class);
    }
    
}