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.legacy.concepts.LegacyGuardPolicyInput;
44 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
47 * This class performs unit test of {@link LegacyGuardPolicyProvider}
49 * @author Chenfei Gao (cgao@research.att.com)
51 public class TestLegacyGuardPolicyProvider {
53 private static LegacyGuardPolicyProvider guardPolicyProvider;
54 private static PolicyModelsProviderParameters providerParams;
55 private static ApiParameterGroup apiParamGroup;
56 private static StandardCoder standardCoder;
58 private static final String POLICY_RESOURCE = "policies/vDNS.policy.guard.frequency.input.json";
61 * Initializes parameters.
63 * @throws PfModelException the PfModel parsing exception
66 public static void setupParameters() throws PfModelException {
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();
80 * Closes up DB connections and deregisters API parameter group.
82 * @throws PfModelException the PfModel parsing exception
85 public static void tearDown() throws PfModelException {
87 guardPolicyProvider.close();
88 ParameterService.deregister(apiParamGroup);
93 public void testFetchGuardPolicy() {
95 assertThatThrownBy(() -> {
96 guardPolicyProvider.fetchGuardPolicy("dummy", null);
97 }).hasMessage("no policy found for policy ID: dummy");
99 assertThatThrownBy(() -> {
100 guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
101 }).hasMessage("no policy found for policy ID: dummy");
105 public void testCreateGuardPolicy() {
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();
121 public void testDeleteGuardPolicy() {
123 assertThatThrownBy(() -> {
124 guardPolicyProvider.deleteGuardPolicy("dummy", null);
125 }).hasMessage("version is marked @NonNull but is null");
127 assertThatThrownBy(() -> {
128 guardPolicyProvider.deleteGuardPolicy("dummy", "dummy");
129 }).hasMessage("no policy found for policy ID: dummy");
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();
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();
150 assertThatThrownBy(() -> {
151 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
152 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");