Modify delete safety net and DB lab setup
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / provider / TestPolicyProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest.provider;
24
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertFalse;
28
29 import java.util.Base64;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.api.main.parameters.ApiParameterGroup;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40
41 /**
42  * This class performs unit test of {@link PolicyProvider}
43  *
44  * @author Chenfei Gao (cgao@research.att.com)
45  */
46 public class TestPolicyProvider {
47
48     private static PolicyProvider policyProvider;
49     private static PolicyTypeProvider policyTypeProvider;
50     private static PolicyModelsProviderParameters providerParams;
51     private static ApiParameterGroup apiParamGroup;
52     private static StandardCoder standardCoder;
53
54     private static final String POLICY_RESOURCE = "policies/vCPE.policy.monitoring.input.tosca.json";
55     private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.json";
56     private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID = "policies/vCPE.policy.bad.policytypeid.json";
57     private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION =
58             "policies/vCPE.policy.bad.policytypeversion.json";
59
60     /**
61      * Initializes parameters.
62      *
63      * @throws PfModelException the PfModel parsing exception
64      */
65     @BeforeClass
66     public static void setupParameters() throws PfModelException {
67
68         standardCoder = new StandardCoder();
69         providerParams = new PolicyModelsProviderParameters();
70         providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
71         providerParams.setDatabaseUser("policy");
72         providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
73         providerParams.setPersistenceUnit("ToscaConceptTest");
74         apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
75         ParameterService.register(apiParamGroup, true);
76         policyTypeProvider = new PolicyTypeProvider();
77         policyProvider = new PolicyProvider();
78     }
79
80     /**
81      * Closes up DB connections and deregisters API parameter group.
82      *
83      * @throws PfModelException the PfModel parsing exception
84      */
85     @AfterClass
86     public static void tearDown() throws PfModelException {
87
88         policyTypeProvider.close();
89         policyProvider.close();
90         ParameterService.deregister(apiParamGroup);
91     }
92
93     @Test
94     public void testFetchPolicies() {
95
96         assertThatThrownBy(() -> {
97             policyProvider.fetchPolicies("dummy", "dummy", null, null);
98         }).hasMessage("policy with ID null:null and type dummy:dummy does not exist");
99
100         assertThatThrownBy(() -> {
101             policyProvider.fetchPolicies("dummy", "dummy", "dummy", null);
102         }).hasMessage("policy with ID dummy:null and type dummy:dummy does not exist");
103
104         assertThatThrownBy(() -> {
105             policyProvider.fetchPolicies("dummy", "dummy", "dummy", "dummy");
106         }).hasMessage("policy with ID dummy:dummy and type dummy:dummy does not exist");
107     }
108
109     @Test
110     public void testFetchLatestPolicies() {
111
112         assertThatThrownBy(() -> {
113             policyProvider.fetchLatestPolicies("dummy", "dummy", "dummy");
114         }).hasMessage("policy with ID dummy:null and type dummy:dummy does not exist");
115     }
116
117     @Test
118     public void testFetchDeployedPolicies() {
119
120         assertThatThrownBy(() -> {
121             policyProvider.fetchDeployedPolicies("dummy", "dummy", "dummy");
122         }).hasMessage("could not find policy with ID dummy and type dummy:dummy deployed in any pdp group");
123     }
124
125     @Test
126     public void testCreatePolicy() {
127
128         assertThatThrownBy(() -> {
129             policyProvider.createPolicy("dummy", "dummy", new ToscaServiceTemplate());
130         }).hasMessage("policy type with ID dummy:dummy does not exist");
131
132         assertThatCode(() -> {
133             String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
134             ToscaServiceTemplate policyTypeServiceTemplate =
135                     standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
136             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
137         }).doesNotThrowAnyException();
138
139         assertThatThrownBy(() -> {
140             String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID);
141             ToscaServiceTemplate badPolicyServiceTemplate =
142                     standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
143             policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
144                     badPolicyServiceTemplate);
145         }).hasMessage("policy type id does not match");
146
147         assertThatThrownBy(() -> {
148             String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION);
149             ToscaServiceTemplate badPolicyServiceTemplate =
150                     standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
151             policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
152                     badPolicyServiceTemplate);
153         }).hasMessage("policy type version does not match");
154
155         assertThatCode(() -> {
156             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
157             ToscaServiceTemplate policyServiceTemplate =
158                     standardCoder.decode(policyString, ToscaServiceTemplate.class);
159             ToscaServiceTemplate serviceTemplate = policyProvider
160                     .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
161             assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
162         }).doesNotThrowAnyException();
163     }
164
165     @Test
166     public void testDeletePolicy() {
167
168         assertThatThrownBy(() -> {
169             policyProvider.deletePolicy("dummy", "dummy", "dummy", "dummy");
170         }).hasMessage("policy with ID dummy:dummy and type dummy:dummy does not exist");
171
172         assertThatCode(() -> {
173             ToscaServiceTemplate serviceTemplate = policyProvider.deletePolicy(
174                     "onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0");
175             assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
176         }).doesNotThrowAnyException();
177
178         String exceptionMessage = "policy with ID onap.restart.tca:1.0.0 and type "
179             + "onap.policies.monitoring.cdap.tca.hi.lo.app:1.0.0 does not exist";
180         assertThatThrownBy(() -> {
181             policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
182                     "onap.restart.tca", "1.0.0");
183         }).hasMessage(exceptionMessage);
184     }
185 }