Add junit tests for endpoints
[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.setDatabaseDriver("org.h2.Driver");
71         providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
72         providerParams.setDatabaseUser("policy");
73         providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
74         providerParams.setPersistenceUnit("ToscaConceptTest");
75         apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
76         ParameterService.register(apiParamGroup, true);
77         guardPolicyProvider = new LegacyGuardPolicyProvider();
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         guardPolicyProvider.close();
89         ParameterService.deregister(apiParamGroup);
90     }
91
92
93     @Test
94     public void testFetchGuardPolicy() {
95
96         assertThatThrownBy(() -> {
97             guardPolicyProvider.fetchGuardPolicy("dummy", null);
98         }).hasMessage("no policy found for policy ID: dummy");
99
100         assertThatThrownBy(() -> {
101             guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
102         }).hasMessage("no policy found for policy ID: dummy");
103     }
104
105     @Test
106     public void testCreateGuardPolicy() {
107
108         assertThatCode(() -> {
109             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
110             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
111             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
112             assertNotNull(createdPolicy);
113             assertFalse(createdPolicy.isEmpty());
114             assertTrue(createdPolicy.containsKey("guard.frequency.scaleout"));
115             assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
116                     createdPolicy.get("guard.frequency.scaleout").getType());
117             assertEquals("1.0.0", createdPolicy.get("guard.frequency.scaleout").getVersion());
118         }).doesNotThrowAnyException();
119     }
120
121     @Test
122     public void testDeleteGuardPolicy() {
123
124         assertThatThrownBy(() -> {
125             guardPolicyProvider.deleteGuardPolicy("dummy", null);
126         }).hasMessage("version is marked @NonNull but is null");
127
128         assertThatThrownBy(() -> {
129             guardPolicyProvider.deleteGuardPolicy("dummy", "dummy");
130         }).hasMessage("no policy found for policy ID: dummy");
131
132         assertThatCode(() -> {
133             String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
134             LegacyGuardPolicyInput policyToCreate = standardCoder.decode(policyString, LegacyGuardPolicyInput.class);
135             Map<String, LegacyGuardPolicyOutput> createdPolicy = guardPolicyProvider.createGuardPolicy(policyToCreate);
136             assertNotNull(createdPolicy);
137             assertFalse(createdPolicy.isEmpty());
138         }).doesNotThrowAnyException();
139
140         assertThatCode(() -> {
141             Map<String, LegacyGuardPolicyOutput> deletedPolicy = guardPolicyProvider
142                     .deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
143             assertNotNull(deletedPolicy);
144             assertFalse(deletedPolicy.isEmpty());
145             assertTrue(deletedPolicy.containsKey("guard.frequency.scaleout"));
146             assertEquals("onap.policies.controlloop.guard.FrequencyLimiter",
147                     deletedPolicy.get("guard.frequency.scaleout").getType());
148             assertEquals("1.0.0", deletedPolicy.get("guard.frequency.scaleout").getVersion());
149         }).doesNotThrowAnyException();
150
151         assertThatThrownBy(() -> {
152             guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
153         }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
154     }
155 }