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