Modify legacy API and policy provider to support version enabled features in legacy...
[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_RESOURCE_VER1 = "policies/vDNS.policy.guard.frequency.input.ver1.json";
62     private static final String POLICY_RESOURCE_VER2 = "policies/vDNS.policy.guard.frequency.input.ver2.json";
63     private static final String POLICY_TYPE_RESOURCE =
64             "policytypes/onap.policies.controlloop.guard.FrequencyLimiter.json";
65     private static final String POLICY_TYPE_ID = "onap.policies.controlloop.guard.FrequencyLimiter:1.0.0";
66     private static final String POLICY_ID = "guard.frequency.scaleout:1.0.0";
67
68     /**
69      * Initializes parameters.
70      *
71      * @throws PfModelException the PfModel parsing exception
72      */
73     @BeforeClass
74     public static void setupParameters() throws PfModelException {
75
76         standardCoder = new StandardCoder();
77         providerParams = new PolicyModelsProviderParameters();
78         providerParams.setDatabaseDriver("org.h2.Driver");
79         providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
80         providerParams.setDatabaseUser("policy");
81         providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
82         providerParams.setPersistenceUnit("ToscaConceptTest");
83         apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
84         ParameterService.register(apiParamGroup, true);
85         guardPolicyProvider = new LegacyGuardPolicyProvider();
86         policyTypeProvider = new PolicyTypeProvider();
87     }
88
89     /**
90      * Closes up DB connections and deregisters API parameter group.
91      *
92      * @throws PfModelException the PfModel parsing exception
93      */
94     @AfterClass
95     public static void tearDown() throws PfModelException {
96
97         guardPolicyProvider.close();
98         policyTypeProvider.close();
99         ParameterService.deregister(apiParamGroup);
100     }
101
102
103     @Test
104     public void testFetchGuardPolicy() {
105
106         assertThatThrownBy(() -> {
107             guardPolicyProvider.fetchGuardPolicy("dummy", null);
108         }).hasMessage("no policy found for policy: dummy:null");
109
110         assertThatThrownBy(() -> {
111             guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
112         }).hasMessage("legacy policy version is not an integer");
113
114         assertThatCode(() -> {
115             String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
116             ToscaServiceTemplate policyTypeServiceTemplate =
117                     standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
118             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
119
120             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_VER1);
121             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
122             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
123             assertNotNull(createdPolicy);
124             assertFalse(createdPolicy.isEmpty());
125
126             policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_VER2);
127             policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
128             createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
129             assertNotNull(createdPolicy);
130             assertFalse(createdPolicy.isEmpty());
131
132             Map<String, LegacyGuardPolicyOutput> firstVersion =
133                     guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "1");
134             assertNotNull(firstVersion);
135             assertEquals("1",
136                     firstVersion.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
137
138             Map<String, LegacyGuardPolicyOutput> latestVersion =
139                     guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", null);
140             assertNotNull(latestVersion);
141             assertEquals("2",
142                     latestVersion.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
143         }).doesNotThrowAnyException();
144
145         assertThatThrownBy(() -> {
146             guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "1.0.0");
147         }).hasMessage("legacy policy version is not an integer");
148
149         assertThatThrownBy(() -> {
150             guardPolicyProvider.fetchGuardPolicy("guard.frequency.scaleout", "latest");
151         }).hasMessage("legacy policy version is not an integer");
152
153         assertThatCode(() -> {
154             guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
155             guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "2");
156             policyTypeProvider.deletePolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0");
157         }).doesNotThrowAnyException();
158     }
159
160     @Test
161     public void testCreateGuardPolicy() {
162
163         assertThatThrownBy(() -> {
164             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
165             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
166             guardPolicyProvider.createGuardPolicy(policyToCreate);
167         }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
168
169         assertThatCode(() -> {
170             String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
171             ToscaServiceTemplate policyTypeServiceTemplate =
172                     standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
173             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
174
175             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
176             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
177             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
178             assertNotNull(createdPolicy);
179             assertFalse(createdPolicy.isEmpty());
180             assertTrue(createdPolicy.containsKey("guard.frequency.scaleout"));
181             assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
182                     createdPolicy.get("guard.frequency.scaleout").getType());
183             assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
184         }).doesNotThrowAnyException();
185     }
186
187     @Test
188     public void testDeleteGuardPolicy() {
189
190         assertThatThrownBy(() -> {
191             guardPolicyProvider.deleteGuardPolicy("dummy", null);
192         }).hasMessage("version is marked @NonNull but is null");
193
194         assertThatThrownBy(() -> {
195             guardPolicyProvider.deleteGuardPolicy("dummy", "1.0.0");
196         }).hasMessage("legacy policy version is not an integer");
197
198         assertThatCode(() -> {
199             String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
200             ToscaServiceTemplate policyTypeServiceTemplate =
201                     standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
202             policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
203
204             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
205             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
206             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
207             assertNotNull(createdPolicy);
208             assertFalse(createdPolicy.isEmpty());
209
210             Map<String, LegacyGuardPolicyOutput> deletedPolicy = guardPolicyProvider
211                     .deleteGuardPolicy("guard.frequency.scaleout", "1");
212             assertNotNull(deletedPolicy);
213             assertFalse(deletedPolicy.isEmpty());
214             assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));
215             assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
216                     deletedPolicy.get("guard.frequency.scaleout").getType());
217             assertEquals("1",
218                     deletedPolicy.get("guard.frequency.scaleout").getMetadata().get("policy-version").toString());
219         }).doesNotThrowAnyException();
220
221         assertThatThrownBy(() -> {
222             guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1");
223         }).hasMessage("no policy found for policy: guard.frequency.scaleout:1");
224
225         assertThatCode(() -> {
226             policyTypeProvider.deletePolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0");
227         }).doesNotThrowAnyException();
228     }
229 }