Modify delete safety net and DB lab setup
[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.legacy.concepts.LegacyGuardPolicyInput;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
45
46 /**
47  * This class performs unit test of {@link LegacyGuardPolicyProvider}
48  *
49  * @author Chenfei Gao (cgao@research.att.com)
50  */
51 public class TestLegacyGuardPolicyProvider {
52
53     private static LegacyGuardPolicyProvider guardPolicyProvider;
54     private static PolicyModelsProviderParameters providerParams;
55     private static ApiParameterGroup apiParamGroup;
56     private static StandardCoder standardCoder;
57
58     private static final String POLICY_RESOURCE = "policies/vDNS.policy.guard.frequency.input.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         guardPolicyProvider = new LegacyGuardPolicyProvider();
77     }
78
79     /**
80      * Closes up DB connections and deregisters API parameter group.
81      *
82      * @throws PfModelException the PfModel parsing exception
83      */
84     @AfterClass
85     public static void tearDown() throws PfModelException {
86
87         guardPolicyProvider.close();
88         ParameterService.deregister(apiParamGroup);
89     }
90
91
92     @Test
93     public void testFetchGuardPolicy() {
94
95         assertThatThrownBy(() -> {
96             guardPolicyProvider.fetchGuardPolicy("dummy", null);
97         }).hasMessage("no policy found for policy ID: dummy");
98
99         assertThatThrownBy(() -> {
100             guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
101         }).hasMessage("no policy found for policy ID: dummy");
102     }
103
104     @Test
105     public void testCreateGuardPolicy() {
106
107         assertThatCode(() -> {
108             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
109             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
110             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
111             assertNotNull(createdPolicy);
112             assertFalse(createdPolicy.isEmpty());
113             assertTrue(createdPolicy.containsKey("guard.frequency.scaleout"));
114             assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
115                     createdPolicy.get("guard.frequency.scaleout").getType());
116             assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
117         }).doesNotThrowAnyException();
118     }
119
120     @Test
121     public void testDeleteGuardPolicy() {
122
123         assertThatThrownBy(() -> {
124             guardPolicyProvider.deleteGuardPolicy("dummy", null);
125         }).hasMessage("version is marked @NonNull but is null");
126
127         assertThatThrownBy(() -> {
128             guardPolicyProvider.deleteGuardPolicy("dummy", "dummy");
129         }).hasMessage("no policy found for policy ID: dummy");
130
131         assertThatCode(() -> {
132             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
133             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
134             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
135             assertNotNull(createdPolicy);
136             assertFalse(createdPolicy.isEmpty());
137         }).doesNotThrowAnyException();
138
139         assertThatCode(() -> {
140             Map<String, LegacyGuardPolicyOutput> deletedPolicy = guardPolicyProvider
141                     .deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
142             assertNotNull(deletedPolicy);
143             assertFalse(deletedPolicy.isEmpty());
144             assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));
145             assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
146                     deletedPolicy.get("guard.frequency.scaleout").getType());
147             assertEquals("1.0.0", deletedPolicy.get("guard.frequency.scaleout").getVersion());
148         }).doesNotThrowAnyException();
149
150         assertThatThrownBy(() -> {
151             guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
152         }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
153     }
154 }