2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest.provider;
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;
32 import java.util.Base64;
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;
48 * This class performs unit test of {@link LegacyGuardPolicyProvider}
50 * @author Chenfei Gao (cgao@research.att.com)
52 public class TestLegacyGuardPolicyProvider {
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;
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";
67 * Initializes parameters.
69 * @throws PfModelException the PfModel parsing exception
72 public static void setupParameters() throws PfModelException {
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();
88 * Closes up DB connections and deregisters API parameter group.
90 * @throws PfModelException the PfModel parsing exception
93 public static void tearDown() throws PfModelException {
95 guardPolicyProvider.close();
96 policyTypeProvider.close();
97 ParameterService.deregister(apiParamGroup);
102 public void testFetchGuardPolicy() {
104 assertThatThrownBy(() -> {
105 guardPolicyProvider.fetchGuardPolicy("dummy", null);
106 }).hasMessage("no policy found for policy ID: dummy");
108 assertThatThrownBy(() -> {
109 guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
110 }).hasMessage("no policy found for policy ID: dummy");
114 public void testCreateGuardPolicy() {
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");
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();
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();
143 public void testDeleteGuardPolicy() {
145 assertThatThrownBy(() -> {
146 guardPolicyProvider.deleteGuardPolicy("dummy", null);
147 }).hasMessage("version is marked @NonNull but is null");
149 assertThatThrownBy(() -> {
150 guardPolicyProvider.deleteGuardPolicy("dummy", "dummy");
151 }).hasMessage("no policy found for policy ID: dummy");
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();
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();
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();
179 assertThatThrownBy(() -> {
180 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
181 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");
183 assertThatCode(() -> {
184 policyTypeProvider.deletePolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0");
185 }).doesNotThrowAnyException();