Fix locally failing TCs in catalog-be
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / PolicyTypesEndpointTest.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 java.util.Arrays.asList;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.util.List;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.core.GenericType;
31 import javax.ws.rs.core.MediaType;
32 import org.apache.http.HttpStatus;
33 import org.glassfish.jersey.server.ResourceConfig;
34 import org.glassfish.jersey.test.TestProperties;
35 import org.junit.jupiter.api.AfterEach;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
39 import org.openecomp.sdc.be.components.utils.PolicyTypeBuilder;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.impl.ComponentsUtils;
42 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
43 import org.openecomp.sdc.be.user.UserBusinessLogic;
44 import org.openecomp.sdc.common.api.Constants;
45 import org.openecomp.sdc.exception.ResponseFormat;
46
47 class PolicyTypesEndpointTest extends JerseySpringBaseTest {
48
49     public static final String USER_ID = "userId";
50     public static final String COMPONENT_TYPE = "VF";
51     private PolicyTypeBusinessLogic policyTypeBusinessLogic;
52     private ComponentsUtils componentUtils;
53
54     @BeforeEach
55     public void before() throws Exception {
56         super.setUp();
57     }
58
59     @AfterEach
60     void after() throws Exception {
61         super.tearDown();
62     }
63
64     @Override
65     protected ResourceConfig configure() {
66         forceSet(TestProperties.CONTAINER_PORT, "0");
67         policyTypeBusinessLogic = mock(PolicyTypeBusinessLogic.class);
68         componentUtils = mock(ComponentsUtils.class);
69         UserBusinessLogic userBusinessLogic = mock(UserBusinessLogic.class);
70         ComponentsUtils componentsUtils = mock(ComponentsUtils.class);
71         return super.configure()
72             .register(new PolicyTypesEndpoint(userBusinessLogic, componentsUtils, policyTypeBusinessLogic));
73     }
74
75     @Test
76     void getPolicyTypes() {
77         List<PolicyTypeDefinition> policyTypes = buildPolicyTypesList();
78         when(policyTypeBusinessLogic.getAllPolicyTypes(USER_ID, COMPONENT_TYPE)).thenReturn(policyTypes);
79         when(componentUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.SC_OK));
80         List<PolicyTypeDefinition> fetchedPolicyTypes = buildGetPolicyTypesCall()
81             .get(new GenericType<List<PolicyTypeDefinition>>() {
82             });
83         verifyPolicyTypesList(policyTypes, fetchedPolicyTypes);
84     }
85
86     @Test
87     void getPolicyTypes_whenNoInternalComponent_passNullAsComponentType() {
88         List<PolicyTypeDefinition> policyTypes = buildPolicyTypesList();
89         when(policyTypeBusinessLogic.getAllPolicyTypes(USER_ID, null)).thenReturn(policyTypes);
90         when(componentUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.SC_OK));
91         List<PolicyTypeDefinition> fetchedPolicyTypes = buildGetPolicyTypesCallNoInternalComponent()
92             .get(new GenericType<List<PolicyTypeDefinition>>() {
93             });
94         verifyPolicyTypesList(policyTypes, fetchedPolicyTypes);
95     }
96
97
98     private void verifyPolicyTypesList(List<PolicyTypeDefinition> policyTypes,
99                                        List<PolicyTypeDefinition> fetchedPolicyTypes) {
100         assertThat(fetchedPolicyTypes)
101             .usingElementComparatorOnFields("version", "type", "uniqueId", "name", "icon")
102             .isEqualTo(policyTypes);
103         assertThat(fetchedPolicyTypes)
104             .extracting("derivedFrom")//derivedFrom is not on the PolicyTypeMixin and should not return in response
105             .containsOnly((String) null);
106     }
107
108     private Invocation.Builder buildGetPolicyTypesCall() {
109         return target("/v1/catalog/policyTypes")
110             .queryParam("internalComponentType", COMPONENT_TYPE)
111             .request(MediaType.APPLICATION_JSON)
112             .header(Constants.USER_ID_HEADER, USER_ID);
113     }
114
115     private Invocation.Builder buildGetPolicyTypesCallNoInternalComponent() {
116         return target("/v1/catalog/policyTypes")
117             .request(MediaType.APPLICATION_JSON)
118             .header(Constants.USER_ID_HEADER, USER_ID);
119     }
120
121     private List<PolicyTypeDefinition> buildPolicyTypesList() {
122         PolicyTypeDefinition type1 = new PolicyTypeBuilder()
123             .setDerivedFrom("root")
124             .setType("type1")
125             .setUniqueId("id1")
126             .setVersion("1.0")
127             .setName("type1name")
128             .setIcon("type1Icon")
129             .build();
130         PolicyTypeDefinition type2 = new PolicyTypeBuilder()
131             .setDerivedFrom("type1")
132             .setType("type2")
133             .setUniqueId("id2")
134             .setVersion("1.0")
135             .setName("type2name")
136             .setIcon("type2con")
137             .build();
138         PolicyTypeDefinition type3 = new PolicyTypeBuilder()
139             .setDerivedFrom("root")
140             .setType("type3")
141             .setUniqueId("id3")
142             .setVersion("1.0")
143             .setName("type3name")
144             .setIcon("type3con")
145             .build();
146         return asList(type1, type2, type3);
147     }
148
149
150 }