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 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;
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;
47 class PolicyTypesEndpointTest extends JerseySpringBaseTest {
49 public static final String USER_ID = "userId";
50 public static final String COMPONENT_TYPE = "VF";
51 private PolicyTypeBusinessLogic policyTypeBusinessLogic;
52 private ComponentsUtils componentUtils;
55 public void before() throws Exception {
60 void after() throws Exception {
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));
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>>() {
83 verifyPolicyTypesList(policyTypes, fetchedPolicyTypes);
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>>() {
94 verifyPolicyTypesList(policyTypes, fetchedPolicyTypes);
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);
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);
115 private Invocation.Builder buildGetPolicyTypesCallNoInternalComponent() {
116 return target("/v1/catalog/policyTypes")
117 .request(MediaType.APPLICATION_JSON)
118 .header(Constants.USER_ID_HEADER, USER_ID);
121 private List<PolicyTypeDefinition> buildPolicyTypesList() {
122 PolicyTypeDefinition type1 = new PolicyTypeBuilder()
123 .setDerivedFrom("root")
127 .setName("type1name")
128 .setIcon("type1Icon")
130 PolicyTypeDefinition type2 = new PolicyTypeBuilder()
131 .setDerivedFrom("type1")
135 .setName("type2name")
138 PolicyTypeDefinition type3 = new PolicyTypeBuilder()
139 .setDerivedFrom("root")
143 .setName("type3name")
146 return asList(type1, type2, type3);