catalog-be servlets refactoring
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / ComponentServletTest.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.jersey.server.ResourceConfig;
25 import org.json.JSONArray;
26 import org.json.JSONObject;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogicProvider;
30 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
31 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
33 import org.openecomp.sdc.be.components.utils.PolicyDefinitionBuilder;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.PolicyDefinition;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
38 import org.openecomp.sdc.be.user.UserBusinessLogic;
39 import org.openecomp.sdc.common.api.Constants;
40
41 import javax.ws.rs.client.Invocation;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import java.util.Collections;
45
46 import static java.util.Arrays.asList;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.mockito.ArgumentMatchers.any;
49 import static org.mockito.ArgumentMatchers.eq;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.when;
52
53 public class ComponentServletTest extends JerseySpringBaseTest{
54
55     private static final String USER_ID = "userId";
56     private static final String RESOURCE_ID = "resourceId";
57     private ResourceBusinessLogic resourceBusinessLogic;
58     private UserBusinessLogic userBusinessLogic;
59     private ComponentsUtils componentsUtils;
60     private PolicyDefinition policy1, policy2;
61
62     @Override
63     @Before
64     public void setUp() throws Exception {
65         super.setUp();
66         policy1 = buildPolicy("p1");
67         policy2 = buildPolicy("p2");
68     }
69
70     @Override
71     protected ResourceConfig configure() {
72         resourceBusinessLogic = mock(ResourceBusinessLogic.class);
73         userBusinessLogic = mock(UserBusinessLogic.class);
74         componentsUtils = mock(ComponentsUtils.class);
75         ComponentServlet componentServlet = new ComponentServlet(userBusinessLogic, componentsUtils,
76             new ComponentBusinessLogicProvider(resourceBusinessLogic, null, null));
77         return super.configure().register(componentServlet);
78     }
79
80     @Test
81     public void filterDataByParam_getPolicies_returnOnlyNameTargetsAndIdFields() {
82         UiComponentDataTransfer dataTransfer = buildDataTransferWithPolicies();
83         when(resourceBusinessLogic.getComponentDataFilteredByParams(eq(RESOURCE_ID.toLowerCase()), any(User.class), eq(Collections.singletonList("policies")))).thenReturn(Either.left(dataTransfer));
84         UiComponentDataTransfer uiComponentDataTransfer = buildGetPolicyTypesCall().get(UiComponentDataTransfer.class);
85         assertThat(uiComponentDataTransfer.getPolicies())
86                 .usingElementComparatorOnFields("name", "uniqueId", "targets")
87                 .containsExactlyInAnyOrder(policy1, policy2)
88                 .extracting("properties")//properties is not returned in the response
89                 .containsExactly(null, null);
90     }
91
92     @Test
93     public void filterDataByParam_getPolicies_policyTypeNameFieldShouldReturnAsType() {
94         UiComponentDataTransfer dataTransfer = buildDataTransferWithPolicies();
95         when(resourceBusinessLogic.getComponentDataFilteredByParams(eq(RESOURCE_ID.toLowerCase()), any(User.class), eq(Collections.singletonList("policies")))).thenReturn(Either.left(dataTransfer));
96         Response uiComponentDataTransfer = buildGetPolicyTypesCall().get();
97         verifyPolicyTypeFieldUsingJsonResponse(uiComponentDataTransfer);
98     }
99
100     private void verifyPolicyTypeFieldUsingJsonResponse(Response uiComponentDataTransfer) {
101         JSONObject json = new JSONObject(uiComponentDataTransfer.readEntity(String.class));
102         JSONArray policies = json.getJSONArray("policies");
103         for (int i = 0; i < policies.length(); i++) {
104             JSONObject policy = policies.getJSONObject(i);
105             String policyId = policy.get("uniqueId").toString();
106             assertThat(policy.get("type")).isEqualTo("type" + policyId);
107         }
108     }
109
110     private UiComponentDataTransfer buildDataTransferWithPolicies() {
111         UiComponentDataTransfer res = new UiComponentDataTransfer();
112         res.setPolicies(asList(policy1, policy2));
113         return res;
114     }
115
116     private PolicyDefinition buildPolicy(String id) {
117         return PolicyDefinitionBuilder.create()
118                 .setUniqueId(id)
119                 .setName("name" + id)
120                 .setType("type" + id)
121                 .addGroupTarget("group1")
122                 .addGroupTarget("group2")
123                 .addComponentInstanceTarget("inst1")
124                 .addComponentInstanceTarget("inst2")
125                 .addProperty("prop1")
126                 .build();
127     }
128
129     private Invocation.Builder buildGetPolicyTypesCall() {
130         return target("/v1/catalog/resources/{id}/filteredDataByParams")
131                 .queryParam("include", "policies")
132                 .resolveTemplate("id", RESOURCE_ID)
133                 .request(MediaType.APPLICATION_JSON)
134                 .header(Constants.USER_ID_HEADER, USER_ID);
135     }
136 }