Preload legacy operational policy type and guard policy types
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / provider / TestLegacyGuardPolicyProvider.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.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.Base64;
33 import java.util.Map;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.api.main.parameters.ApiParameterGroup;
38 import org.onap.policy.common.parameters.ParameterService;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.base.PfModelException;
42 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
45 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
46
47 /**
48  * This class performs unit test of {@link LegacyGuardPolicyProvider}
49  *
50  * @author Chenfei Gao (cgao@research.att.com)
51  */
52 public class TestLegacyGuardPolicyProvider {
53
54     private static LegacyGuardPolicyProvider guardPolicyProvider;
55     private static PolicyTypeProvider policyTypeProvider;
56     private static PolicyModelsProviderParameters providerParams;
57     private static ApiParameterGroup apiParamGroup;
58     private static StandardCoder standardCoder;
59
60     private static final String POLICY_RESOURCE = "policies/vDNS.policy.guard.frequency.input.json";
61     private static final String POLICY_TYPE_RESOURCE =
62             "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.json";
63     private static final String POLICY_TYPE_ID = "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0";
64     private static final String POLICY_ID = "guard.frequency.scaleout:1.0.0";
65
66     /**
67      * Initializes parameters.
68      *
69      * @throws PfModelException the PfModel parsing exception
70      */
71     @BeforeClass
72     public static void setupParameters() throws PfModelException {
73
74         standardCoder = new StandardCoder();
75         providerParams = new PolicyModelsProviderParameters();
76         providerParams.setDatabaseDriver("org.h2.Driver");
77         providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
78         providerParams.setDatabaseUser("policy");
79         providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
80         providerParams.setPersistenceUnit("ToscaConceptTest");
81         apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
82         ParameterService.register(apiParamGroup, true);
83         guardPolicyProvider = new LegacyGuardPolicyProvider();
84         policyTypeProvider = new PolicyTypeProvider();
85     }
86
87     /**
88      * Closes up DB connections and deregisters API parameter group.
89      *
90      * @throws PfModelException the PfModel parsing exception
91      */
92     @AfterClass
93     public static void tearDown() throws PfModelException {
94
95         guardPolicyProvider.close();
96         policyTypeProvider.close();
97         ParameterService.deregister(apiParamGroup);
98     }
99
100
101     @Test
102     public void testFetchGuardPolicy() {
103
104         assertThatThrownBy(() -> {
105             guardPolicyProvider.fetchGuardPolicy("dummy", null);
106         }).hasMessage("no policy found for policy ID: dummy");
107
108         assertThatThrownBy(() -> {
109             guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
110         }).hasMessage("no policy found for policy ID: dummy");
111     }
112
113     @Test
114     public void testCreateGuardPolicy() {
115
116         assertThatThrownBy(() -> {
117             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
118             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
119             guardPolicyProvider.createGuardPolicy(policyToCreate);
120         }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
121
122         assertThatCode(() -> {
123             String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
124             ToscaServiceTemplate policyTypeServiceTemplate =
125                     standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
126             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
127         }).doesNotThrowAnyException();
128
129         assertThatCode(() -> {
130             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
131             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
132             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
133             assertNotNull(createdPolicy);
134             assertFalse(createdPolicy.isEmpty());
135             assertTrue(createdPolicy.containsKey("guard.frequency.scaleout"));
136             assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
137                     createdPolicy.get("guard.frequency.scaleout").getType());
138             assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
139         }).doesNotThrowAnyException();
140     }
141
142     @Test
143     public void testDeleteGuardPolicy() {
144
145         assertThatThrownBy(() -> {
146             guardPolicyProvider.deleteGuardPolicy("dummy", null);
147         }).hasMessage("version is marked @NonNull but is null");
148
149         assertThatThrownBy(() -> {
150             guardPolicyProvider.deleteGuardPolicy("dummy", "dummy");
151         }).hasMessage("no policy found for policy ID: dummy");
152
153         assertThatCode(() -> {
154             String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
155             ToscaServiceTemplate policyTypeServiceTemplate =
156                     standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
157             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
158         }).doesNotThrowAnyException();
159
160         assertThatCode(() -> {
161             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
162             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
163             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
164             assertNotNull(createdPolicy);
165             assertFalse(createdPolicy.isEmpty());
166         }).doesNotThrowAnyException();
167
168         assertThatCode(() -> {
169             Map<String, LegacyGuardPolicyOutput> deletedPolicy = guardPolicyProvider
170                     .deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
171             assertNotNull(deletedPolicy);
172             assertFalse(deletedPolicy.isEmpty());
173             assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));
174             assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
175                     deletedPolicy.get("guard.frequency.scaleout").getType());
176             assertEquals("1.0.0", deletedPolicy.get("guard.frequency.scaleout").getVersion());
177         }).doesNotThrowAnyException();
178
179         assertThatThrownBy(() -> {
180             guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
181         }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
182
183         assertThatCode(() -> {
184             policyTypeProvider.deletePolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0");
185         }).doesNotThrowAnyException();
186
187     }
188 }