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.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();
81 * Closes up DB connections and deregisters API parameter group.
83 * @throws PfModelException the PfModel parsing exception
86 public static void tearDown() throws PfModelException {
88 guardPolicyProvider.close();
89 ParameterService.deregister(apiParamGroup);
94 public void testFetchGuardPolicy() {
96 assertThatThrownBy(() -> {
97 guardPolicyProvider.fetchGuardPolicy("dummy", null);
98 }).hasMessage("no policy found for policy ID: dummy");
100 assertThatThrownBy(() -> {
101 guardPolicyProvider.fetchGuardPolicy("dummy", "dummy");
102 }).hasMessage("no policy found for policy ID: dummy");
106 public void testCreateGuardPolicy() {
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();
122 public void testDeleteGuardPolicy() {
124 assertThatThrownBy(() -> {
125 guardPolicyProvider.deleteGuardPolicy("dummy", null);
126 }).hasMessage("version is marked @NonNull but is null");
128 assertThatThrownBy(() -> {
129 guardPolicyProvider.deleteGuardPolicy("dummy", "dummy");
130 }).hasMessage("no policy found for policy ID: dummy");
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();
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();
151 assertThatThrownBy(() -> {
152 guardPolicyProvider.deleteGuardPolicy("guard.frequency.scaleout", "1.0.0");
153 }).hasMessage("no policy found for policy ID: guard.frequency.scaleout");