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.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import java.util.Base64;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.api.main.parameters.ApiParameterGroup;
36 import org.onap.policy.common.parameters.ParameterService;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
42 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
45 * This class performs unit test of {@link LegacyOperationalPolicyProvider}
47 * @author Chenfei Gao (cgao@research.att.com)
49 public class TestLegacyOperationalPolicyProvider {
51 private static LegacyOperationalPolicyProvider operationalPolicyProvider;
52 private static PolicyTypeProvider policyTypeProvider;
53 private static PolicyModelsProviderParameters providerParams;
54 private static ApiParameterGroup apiParamGroup;
55 private static StandardCoder standardCoder;
57 private static final String POLICY_RESOURCE = "policies/vCPE.policy.operational.input.json";
58 private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.controlloop.Operational.json";
59 private static final String POLICY_TYPE_ID = "onap.policies.controlloop.Operational:1.0.0";
60 private static final String POLICY_ID = "operational.restart:1.0.0";
63 * Initializes parameters.
65 * @throws PfModelException the PfModel parsing exception
68 public static void setupParameters() throws PfModelException {
70 standardCoder = new StandardCoder();
71 providerParams = new PolicyModelsProviderParameters();
72 providerParams.setDatabaseDriver("org.h2.Driver");
73 providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
74 providerParams.setDatabaseUser("policy");
75 providerParams.setDatabasePassword(Base64.getEncoder().encodeToString("P01icY".getBytes()));
76 providerParams.setPersistenceUnit("ToscaConceptTest");
77 apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams);
78 ParameterService.register(apiParamGroup, true);
79 operationalPolicyProvider = new LegacyOperationalPolicyProvider();
80 policyTypeProvider = new PolicyTypeProvider();
84 * Closes up DB connections and deregisters API parameter group.
86 * @throws PfModelException the PfModel parsing exception
89 public static void tearDown() throws PfModelException {
91 operationalPolicyProvider.close();
92 policyTypeProvider.close();
93 ParameterService.deregister(apiParamGroup);
97 public void testFetchOperationalPolicy() {
99 assertThatThrownBy(() -> {
100 operationalPolicyProvider.fetchOperationalPolicy("dummy", null);
101 }).hasMessage("no policy found for policy ID: dummy");
103 assertThatThrownBy(() -> {
104 operationalPolicyProvider.fetchOperationalPolicy("dummy", "dummy");
105 }).hasMessage("no policy found for policy ID: dummy");
109 public void testCreateOperationalPolicy() {
111 assertThatThrownBy(() -> {
112 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
113 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
114 operationalPolicyProvider.createOperationalPolicy(policyToCreate);
115 }).hasMessage("policy type " + POLICY_TYPE_ID + " for policy " + POLICY_ID + " does not exist");
117 assertThatCode(() -> {
118 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
119 ToscaServiceTemplate policyTypeServiceTemplate =
120 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
121 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
122 }).doesNotThrowAnyException();
124 assertThatCode(() -> {
125 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
126 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
127 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
128 assertNotNull(createdPolicy);
129 assertEquals("operational.restart", createdPolicy.getPolicyId());
130 assertTrue(createdPolicy.getContent()
131 .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
132 }).doesNotThrowAnyException();
136 public void testDeleteOperationalPolicy() {
138 assertThatThrownBy(() -> {
139 operationalPolicyProvider.deleteOperationalPolicy("dummy", null);
140 }).hasMessage("version is marked @NonNull but is null");
142 assertThatThrownBy(() -> {
143 operationalPolicyProvider.deleteOperationalPolicy("dummy", "dummy");
144 }).hasMessage("no policy found for policy ID: dummy");
146 assertThatCode(() -> {
147 String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE);
148 ToscaServiceTemplate policyTypeServiceTemplate =
149 standardCoder.decode(policyTypeString, ToscaServiceTemplate.class);
150 policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
151 }).doesNotThrowAnyException();
153 assertThatCode(() -> {
154 String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
155 LegacyOperationalPolicy policyToCreate = standardCoder.decode(policyString, LegacyOperationalPolicy.class);
156 LegacyOperationalPolicy createdPolicy = operationalPolicyProvider.createOperationalPolicy(policyToCreate);
157 assertNotNull(createdPolicy);
158 }).doesNotThrowAnyException();
160 assertThatCode(() -> {
161 LegacyOperationalPolicy deletedPolicy = operationalPolicyProvider
162 .deleteOperationalPolicy("operational.restart", "1.0.0");
163 assertNotNull(deletedPolicy);
164 assertEquals("operational.restart", deletedPolicy.getPolicyId());
165 assertTrue(deletedPolicy.getContent()
166 .startsWith("controlLoop%3A%0A%20%20version%3A%202.0.0%0A%20%20"));
167 }).doesNotThrowAnyException();
169 assertThatThrownBy(() -> {
170 operationalPolicyProvider.deleteOperationalPolicy("operational.restart", "1.0.0");
171 }).hasMessage("no policy found for policy ID: operational.restart");
173 assertThatCode(() -> {
174 policyTypeProvider.deletePolicyType("onap.policies.controlloop.Operational", "1.0.0");
175 }).doesNotThrowAnyException();