2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================;
22 package org.onap.policy.api.main.service;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
29 import jakarta.ws.rs.core.Response;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardYamlCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 * This class performs unit test of Policy Type CRUD operations as implemented in {@link ToscaServiceTemplateService}.
46 * @author Chenfei Gao (cgao@research.att.com)
48 @RunWith(MockitoJUnitRunner.class)
49 class TestToscaServiceTemplateServiceForPolicyTypeCrud extends TestCommonToscaServiceTemplateService {
51 private static final StandardYamlCoder coder = new StandardYamlCoder();
52 private static final String POLICY_TYPE_VERSION = "1.0.0";
54 private static final String POLICY_RESOURCE_MONITORING = "policies/vCPE.policy.monitoring.input.tosca.yaml";
55 private static final String POLICY_TYPE_RESOURCE_MONITORING = "policytypes/onap.policies.monitoring.tcagen2.yaml";
56 private static final String POLICY_TYPE_RESOURCE_WITH_NO_VERSION =
57 "policytypes/onap.policies.optimization.Resource.no.version.yaml";
58 private static final String POLICY_TYPE_NAME_MONITORING = "onap.policies.monitoring.tcagen2";
60 private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON =
61 "policytypes/onap.policies.controlloop.operational.Common.yaml";
62 private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS =
63 "policytypes/onap.policies.controlloop.operational.common.Drools.yaml";
64 private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_APEX =
65 "policytypes/onap.policies.controlloop.operational.common.Apex.yaml";
66 private static final String POLICY_TYPE_OPERATIONAL_COMMON = "onap.policies.controlloop.operational.Common";
67 private static final String POLICY_TYPE_OPERATIONAL_APEX = "onap.policies.controlloop.operational.common.Apex";
68 private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools";
71 private PdpGroupService pdpGroupService;
74 private ToscaServiceTemplateService toscaServiceTemplateService;
85 void testFetchPolicyTypes() {
86 assertThatThrownBy(() -> toscaServiceTemplateService.fetchPolicyTypes("dummy", null))
87 .hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=null) do not exist");
89 assertThatThrownBy(() -> toscaServiceTemplateService.fetchPolicyTypes("dummy", "dummy"))
90 .hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=dummy) do not exist");
93 // ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(null, null);
94 // assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
98 void testFetchLatestPolicyTypes() {
99 assertThatThrownBy(() -> toscaServiceTemplateService.fetchLatestPolicyTypes("dummy"))
100 .hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=LATEST) do not exist");
104 void testCreatePolicyType() throws CoderException {
105 var policyTypeServiceTemplate = coder
106 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
107 var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
108 assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
109 assertEquals(2, serviceTemplate.getPolicyTypes().size());
110 mockDbServiceTemplate(serviceTemplate, null, null);
112 policyTypeServiceTemplate.getPolicyTypes().get("onap.policies.monitoring.tcagen2")
113 .setDescription("Some other description");
115 assertThatThrownBy(() -> toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate))
116 .hasMessageContaining("item \"entity\" value \"onap.policies.monitoring.tcagen2:1.0.0\" INVALID, "
117 + "does not equal existing entity");
119 assertThatThrownBy(() -> {
120 ToscaServiceTemplate badPolicyType =
121 coder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION),
122 ToscaServiceTemplate.class);
123 toscaServiceTemplateService.createPolicyType(badPolicyType);
124 }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null");
126 toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION);
130 void testCreateOperationalPolicyTypes() throws CoderException {
131 ToscaServiceTemplate policyTypeServiceTemplate = coder.decode(
132 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
133 ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
134 assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_COMMON));
135 mockDbServiceTemplate(serviceTemplate, null, null);
137 policyTypeServiceTemplate = coder.decode(
138 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class);
139 var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
140 assertNotNull(createPolicyTypeResponseFragment.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_DROOLS));
141 mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
143 var deletePolicyTypeResponseFragment = toscaServiceTemplateService
144 .deletePolicyType(POLICY_TYPE_OPERATIONAL_DROOLS, POLICY_TYPE_VERSION);
145 mockDbServiceTemplate(serviceTemplate, deletePolicyTypeResponseFragment, Operation.DELETE_POLICY_TYPE);
146 toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_OPERATIONAL_COMMON, POLICY_TYPE_VERSION);
150 void testCreateApexOperationalPolicyTypes() throws CoderException {
151 var policyTypeServiceTemplate = coder.decode(
152 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
153 var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
155 mockDbServiceTemplate(serviceTemplate, null, null);
156 policyTypeServiceTemplate = coder.decode(
157 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_APEX), ToscaServiceTemplate.class);
158 var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
159 assertNotNull(createPolicyTypeResponseFragment.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_APEX));
161 mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
162 toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_OPERATIONAL_APEX, POLICY_TYPE_VERSION);
166 void testDeletePolicyType() throws CoderException {
167 var policyTypeServiceTemplate = coder
168 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
169 var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
170 assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
172 var policyServiceTemplate = coder
173 .decode(ResourceUtils.getResourceAsString(POLICY_RESOURCE_MONITORING), ToscaServiceTemplate.class);
174 mockDbServiceTemplate(serviceTemplate, null, null);
175 var createPolicyResponseFragment = toscaServiceTemplateService.createPolicy("onap.policies.monitoring.tcagen2",
176 "1.0.0", policyServiceTemplate);
178 mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
179 var exceptionMessage = "policy type onap.policies.monitoring.tcagen2:1.0.0 is in use, "
180 + "it is referenced in policy onap.restart.tca:1.0.0";
181 assertThatThrownBy(() -> toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2",
182 "1.0.0")).hasMessage(exceptionMessage);
184 var deletePolicyResponseFragment = toscaServiceTemplateService
185 .deletePolicy("onap.policies.monitoring.tcagen2", "1.0.0", "onap.restart.tca", "1.0.0");
186 assertFalse(deletePolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
187 mockDbServiceTemplate(serviceTemplate, deletePolicyResponseFragment, Operation.DELETE_POLICY);
189 exceptionMessage = "policy type is in use, it is referenced in PDP group dummy subgroup dummy";
190 Mockito.doThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, exceptionMessage))
191 .when(pdpGroupService).assertPolicyTypeNotSupportedInPdpGroup("onap.policies.monitoring.tcagen2", "1.0.0");
192 assertThatThrownBy(() -> toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2",
193 "1.0.0")).hasMessage(exceptionMessage);
195 Mockito.doNothing().when(pdpGroupService)
196 .assertPolicyTypeNotSupportedInPdpGroup("onap.policies.monitoring.tcagen2", "1.0.0");
197 var deletePolicyTypeResponseFragment = toscaServiceTemplateService
198 .deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
199 assertFalse(deletePolicyTypeResponseFragment.getPolicyTypes().isEmpty());
201 mockDbServiceTemplate(serviceTemplate, deletePolicyTypeResponseFragment, Operation.DELETE_POLICY_TYPE);
202 assertThatThrownBy(() -> toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2",
203 "1.0.0")).hasMessage("policy type onap.policies.monitoring.tcagen2:1.0.0 not found");