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.be.user.UserBusinessLogic;
32 import org.openecomp.sdc.common.api.Constants;
33 import org.openecomp.sdc.exception.ResponseFormat;
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;
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;
45 public class PolicyTypesEndpointTest extends JerseySpringBaseTest {
47 public static final String USER_ID = "userId";
48 public static final String COMPONENT_TYPE = "VF";
49 private PolicyTypeBusinessLogic policyTypeBusinessLogic;
50 private ComponentsUtils componentUtils;
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));
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);
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);
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);
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);
96 private Invocation.Builder buildGetPolicyTypesCallNoInternalComponent() {
97 return target("/v1/catalog/policyTypes")
98 .request(MediaType.APPLICATION_JSON)
99 .header(Constants.USER_ID_HEADER, USER_ID);
102 private List<PolicyTypeDefinition> buildPolicyTypesList() {
103 PolicyTypeDefinition type1 = new PolicyTypeBuilder()
104 .setDerivedFrom("root")
108 .setName("type1name")
109 .setIcon("type1Icon")
111 PolicyTypeDefinition type2 = new PolicyTypeBuilder()
112 .setDerivedFrom("type1")
116 .setName("type2name")
119 PolicyTypeDefinition type3 = new PolicyTypeBuilder()
120 .setDerivedFrom("root")
124 .setName("type3name")
127 return asList(type1, type2, type3);