2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.servlets;
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;
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;
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;
44 public class PolicyTypesEndpointTest extends JerseySpringBaseTest {
46 public static final String USER_ID = "userId";
47 public static final String COMPONENT_TYPE = "VF";
48 private PolicyTypeBusinessLogic policyTypeBusinessLogic;
49 private ComponentsUtils componentUtils;
52 protected ResourceConfig configure() {
53 policyTypeBusinessLogic = mock(PolicyTypeBusinessLogic.class);
54 componentUtils = mock(ComponentsUtils.class);
55 return super.configure()
56 .register(new PolicyTypesEndpoint(policyTypeBusinessLogic));
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);
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);
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);
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);
93 private Invocation.Builder buildGetPolicyTypesCallNoInternalComponent() {
94 return target("/v1/catalog/policyTypes")
95 .request(MediaType.APPLICATION_JSON)
96 .header(Constants.USER_ID_HEADER, USER_ID);
99 private List<PolicyTypeDefinition> buildPolicyTypesList() {
100 PolicyTypeDefinition type1 = new PolicyTypeBuilder()
101 .setDerivedFrom("root")
105 .setName("type1name")
106 .setIcon("type1Icon")
108 PolicyTypeDefinition type2 = new PolicyTypeBuilder()
109 .setDerivedFrom("type1")
113 .setName("type2name")
116 PolicyTypeDefinition type3 = new PolicyTypeBuilder()
117 .setDerivedFrom("root")
121 .setName("type3name")
124 return asList(type1, type2, type3);