Merge "Add REST Apis for Tosca Node template operations"
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / provider / TestPolicyTypeProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2021 Nordix Foundation.
7  * Modifications Copyright (C) 2020-2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * SPDX-License-Identifier: Apache-2.0
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.policy.api.main.rest.provider;
26
27 import static org.assertj.core.api.Assertions.assertThatCode;
28 import static org.assertj.core.api.Assertions.assertThatThrownBy;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.onap.policy.api.main.PolicyApiApplication;
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.tosca.authorative.concepts.ToscaPolicyType;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.test.annotation.DirtiesContext;
45 import org.springframework.test.annotation.DirtiesContext.ClassMode;
46 import org.springframework.test.context.ActiveProfiles;
47 import org.springframework.test.context.junit4.SpringRunner;
48
49 /**
50  * This class performs unit test of {@link PolicyTypeProvider}.
51  *
52  * @author Chenfei Gao (cgao@research.att.com)
53  */
54 // Provider classes will be obsolete upon migration to Hibernate
55 @Ignore
56 @RunWith(SpringRunner.class)
57 @SpringBootTest(classes = PolicyApiApplication.class, properties = {"database.initialize=false"})
58 @ActiveProfiles("test")
59 @DirtiesContext(classMode = ClassMode.AFTER_CLASS)
60 public class TestPolicyTypeProvider {
61
62     private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
63
64     @Autowired
65     private PolicyProvider policyProvider;
66
67     @Autowired
68     private PolicyTypeProvider policyTypeProvider;
69
70     private static final String POLICY_TYPE_VERSION = "1.0.0";
71
72     private static final String POLICY_RESOURCE_MONITORING = "policies/vCPE.policy.monitoring.input.tosca.yaml";
73     private static final String POLICY_TYPE_RESOURCE_MONITORING = "policytypes/onap.policies.monitoring.tcagen2.yaml";
74     private static final String POLICY_TYPE_RESOURCE_WITH_NO_VERSION =
75             "policytypes/onap.policies.optimization.Resource.no.version.yaml";
76     private static final String POLICY_TYPE_NAME_MONITORING = "onap.policies.monitoring.tcagen2";
77
78     private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON =
79             "policytypes/onap.policies.controlloop.operational.Common.yaml";
80     private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS =
81             "policytypes/onap.policies.controlloop.operational.common.Drools.yaml";
82     private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_APEX =
83             "policytypes/onap.policies.controlloop.operational.common.Apex.yaml";
84     private static final String POLICY_TYPE_OPERATIONAL_COMMON = "onap.policies.controlloop.operational.Common";
85     private static final String POLICY_TYPE_OPERATIONAL_APEX = "onap.policies.controlloop.operational.common.Apex";
86     private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools";
87
88     @Test
89     public void testFetchPolicyTypes() throws Exception {
90
91         ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null);
92         assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
93
94         assertThatThrownBy(() -> {
95             policyTypeProvider.fetchPolicyTypes("dummy", null);
96         }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=null) do not exist");
97
98         assertThatThrownBy(() -> {
99             policyTypeProvider.fetchPolicyTypes("dummy", "dummy");
100         }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=dummy) do not exist");
101     }
102
103     @Test
104     public void testFetchLatestPolicyTypes() {
105
106         assertThatThrownBy(() -> {
107             policyTypeProvider.fetchLatestPolicyTypes("dummy");
108         }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=LATEST) do not exist");
109     }
110
111     @Test
112     public void testCreatePolicyType() throws Exception {
113
114         ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
115                 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
116         ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
117         assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
118
119         assertThatCode(() -> {
120             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
121         }).doesNotThrowAnyException();
122
123         ToscaPolicyType policyType = policyTypeServiceTemplate.getPolicyTypes().get("onap.policies.monitoring.tcagen2");
124         policyType.setDescription("Some other description");
125
126         assertThatThrownBy(() -> {
127             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
128         }).hasMessageContaining("item \"entity\" value \"onap.policies.monitoring.tcagen2:1.0.0\" INVALID, "
129                 + "does not equal existing entity");
130
131         assertThatThrownBy(() -> {
132             ToscaServiceTemplate badPolicyType =
133                     standardYamlCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION),
134                             ToscaServiceTemplate.class);
135             policyTypeProvider.createPolicyType(badPolicyType);
136         }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null");
137
138         policyTypeProvider.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION);
139     }
140
141     @Test
142     public void testCreateOperationalPolicyTypes() throws CoderException, PfModelException {
143         ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
144                 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
145         ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
146
147         assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_COMMON));
148
149         policyTypeServiceTemplate = standardYamlCoder.decode(
150                 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class);
151         serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
152         assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_DROOLS));
153
154         policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_DROOLS, POLICY_TYPE_VERSION);
155         policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_COMMON, POLICY_TYPE_VERSION);
156     }
157
158     @Test
159     public void testCreateApexOperationalPolicyTypes() throws CoderException, PfModelException {
160         ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
161                 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
162         ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
163         policyTypeServiceTemplate = standardYamlCoder.decode(
164                 ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_APEX), ToscaServiceTemplate.class);
165         serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
166         assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_APEX));
167         policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_APEX, POLICY_TYPE_VERSION);
168     }
169
170     @Test
171     public void testDeletePolicyType() throws Exception {
172
173         ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
174                 .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
175         ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
176         assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
177
178         ToscaServiceTemplate policyServiceTemplate = standardYamlCoder
179                 .decode(ResourceUtils.getResourceAsString(POLICY_RESOURCE_MONITORING), ToscaServiceTemplate.class);
180         policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
181
182         String exceptionMessage = "policy type onap.policies.monitoring.tcagen2:1.0.0 is in use, "
183                 + "it is referenced in policy onap.restart.tca:1.0.0";
184         assertThatThrownBy(() -> {
185             policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
186         }).hasMessage(exceptionMessage);
187
188         serviceTemplate =
189                 policyProvider.deletePolicy("onap.policies.monitoring.tcagen2", "1.0.0", "onap.restart.tca", "1.0.0");
190         assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
191
192         serviceTemplate = policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
193         assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
194
195         assertThatThrownBy(() -> {
196             policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
197         }).hasMessage("policy type onap.policies.monitoring.tcagen2:1.0.0 not found");
198     }
199 }