cea2bcf0f31bd6f38d43f4f31d69c7de24998783
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / service / TestToscaServiceTemplateServiceForPolicyTypeCrud.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *   Copyright (C) 2022 Bell Canada. All rights reserved.
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================;
19  */
20
21 package org.onap.policy.api.main.service;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27
28 import javax.ws.rs.core.Response;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardYamlCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42
43 /**
44  * This class performs unit test of Policy Type CRUD operations as implemented in {@link ToscaServiceTemplateService}.
45  *
46  * @author Chenfei Gao (cgao@research.att.com)
47  */
48 @RunWith(MockitoJUnitRunner.class)
49 public class TestToscaServiceTemplateServiceForPolicyTypeCrud extends TestCommonToscaServiceTemplateService {
50
51     private static StandardYamlCoder coder = new StandardYamlCoder();
52     private static final String POLICY_TYPE_VERSION = "1.0.0";
53
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";
59
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";
69
70     @Mock
71     private PdpGroupService pdpGroupService;
72
73     @InjectMocks
74     private ToscaServiceTemplateService toscaServiceTemplateService;
75
76     /**
77      * Test setup.
78      */
79     @Before
80     public void setUp() {
81         super.setUp();
82     }
83
84     @Test
85     public void testFetchPolicyTypes() throws PfModelException {
86         assertThatThrownBy(() -> {
87             toscaServiceTemplateService.fetchPolicyTypes("dummy", null);
88         }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=null) do not exist");
89
90         assertThatThrownBy(() -> {
91             toscaServiceTemplateService.fetchPolicyTypes("dummy", "dummy");
92         }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=dummy) do not exist");
93
94         // FIXME
95         // ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(null, null);
96         // assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
97     }
98
99     @Test
100     public void testFetchLatestPolicyTypes() {
101
102         assertThatThrownBy(() -> {
103             toscaServiceTemplateService.fetchLatestPolicyTypes("dummy");
104         }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=LATEST) do not exist");
105     }
106
107     @Test
108     public void testCreatePolicyType() throws CoderException {
109         var policyTypeServiceTemplate = coder
110             .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
111         var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
112         assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
113         assertEquals(2, serviceTemplate.getPolicyTypes().size());
114         mockDbServiceTemplate(serviceTemplate, null, null);
115
116         policyTypeServiceTemplate.getPolicyTypes().get("onap.policies.monitoring.tcagen2")
117             .setDescription("Some other description");
118
119         assertThatThrownBy(() -> {
120             toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
121         }).hasMessageContaining("item \"entity\" value \"onap.policies.monitoring.tcagen2:1.0.0\" INVALID, "
122             + "does not equal existing entity");
123
124         assertThatThrownBy(() -> {
125             ToscaServiceTemplate badPolicyType =
126                 coder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION),
127                     ToscaServiceTemplate.class);
128             toscaServiceTemplateService.createPolicyType(badPolicyType);
129         }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null");
130
131         toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION);
132     }
133
134     @Test
135     public void testCreateOperationalPolicyTypes() throws CoderException {
136         ToscaServiceTemplate policyTypeServiceTemplate = coder.decode(
137             ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
138         ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
139         assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_COMMON));
140         mockDbServiceTemplate(serviceTemplate, null, null);
141
142         policyTypeServiceTemplate = coder.decode(
143             ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class);
144         var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
145         assertNotNull(createPolicyTypeResponseFragment.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_DROOLS));
146         mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
147
148         var deletePolicyTypeResponseFragment = toscaServiceTemplateService
149             .deletePolicyType(POLICY_TYPE_OPERATIONAL_DROOLS, POLICY_TYPE_VERSION);
150         mockDbServiceTemplate(serviceTemplate, deletePolicyTypeResponseFragment, Operation.DELETE_POLICY_TYPE);
151         toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_OPERATIONAL_COMMON, POLICY_TYPE_VERSION);
152     }
153
154     @Test
155     public void testCreateApexOperationalPolicyTypes() throws CoderException {
156         var policyTypeServiceTemplate = coder.decode(
157             ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
158         var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
159
160         mockDbServiceTemplate(serviceTemplate, null, null);
161         policyTypeServiceTemplate = coder.decode(
162             ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_APEX), ToscaServiceTemplate.class);
163         var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
164         assertNotNull(createPolicyTypeResponseFragment.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_APEX));
165
166         mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
167         toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_OPERATIONAL_APEX, POLICY_TYPE_VERSION);
168     }
169
170     @Test
171     public void testDeletePolicyType() throws CoderException {
172         var policyTypeServiceTemplate = coder
173             .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
174         var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
175         assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
176
177         var policyServiceTemplate = coder
178             .decode(ResourceUtils.getResourceAsString(POLICY_RESOURCE_MONITORING), ToscaServiceTemplate.class);
179         mockDbServiceTemplate(serviceTemplate, null, null);
180         var createPolicyResponseFragment = toscaServiceTemplateService.createPolicy("onap.policies.monitoring.tcagen2",
181             "1.0.0", policyServiceTemplate);
182
183         mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
184         var exceptionMessage = "policy type onap.policies.monitoring.tcagen2:1.0.0 is in use, "
185             + "it is referenced in policy onap.restart.tca:1.0.0";
186         assertThatThrownBy(() -> {
187             toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
188         }).hasMessage(exceptionMessage);
189
190         var deletePolicyResponseFragment = toscaServiceTemplateService
191             .deletePolicy("onap.policies.monitoring.tcagen2", "1.0.0", "onap.restart.tca", "1.0.0");
192         assertFalse(deletePolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
193         mockDbServiceTemplate(serviceTemplate, deletePolicyResponseFragment, Operation.DELETE_POLICY);
194
195         exceptionMessage = "policy type is in use, it is referenced in PDP group dummy subgroup dummy";
196         Mockito.doThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, exceptionMessage))
197             .when(pdpGroupService).assertPolicyTypeNotSupportedInPdpGroup("onap.policies.monitoring.tcagen2", "1.0.0");
198         assertThatThrownBy(() -> {
199             toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
200         }).hasMessage(exceptionMessage);
201
202         Mockito.doNothing().when(pdpGroupService)
203             .assertPolicyTypeNotSupportedInPdpGroup("onap.policies.monitoring.tcagen2", "1.0.0");
204         var deletePolicyTypeResponseFragment = toscaServiceTemplateService
205             .deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
206         assertFalse(deletePolicyTypeResponseFragment.getPolicyTypes().isEmpty());
207
208         mockDbServiceTemplate(serviceTemplate, deletePolicyTypeResponseFragment, Operation.DELETE_POLICY_TYPE);
209         assertThatThrownBy(() -> {
210             toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
211         }).hasMessage("policy type onap.policies.monitoring.tcagen2:1.0.0 not found");
212     }
213 }