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