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