2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.authorative.provider;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
29 import com.google.gson.GsonBuilder;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Properties;
35 import org.apache.commons.lang3.ObjectUtils;
36 import org.eclipse.persistence.config.PersistenceUnitProperties;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.dao.DaoParameters;
45 import org.onap.policy.models.dao.PfDao;
46 import org.onap.policy.models.dao.PfDaoFactory;
47 import org.onap.policy.models.dao.impl.DefaultPfDao;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
52 import org.yaml.snakeyaml.Yaml;
55 * Test of the {@link AuthorativeToscaProvider} class.
57 * @author Liam Fallon (liam.fallon@est.tech)
59 public class AuthorativeToscaProviderPolicyTypeTest {
60 private static final String VERSION = "version";
61 private static final String POLICY_NO_VERSION_VERSION1 = "onap.policies.NoVersion:0.0.1";
62 private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
63 private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
64 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
65 private static final String VERSION_001 = "0.0.1";
66 private static String yamlAsJsonString;
68 private StandardCoder standardCoder;
71 * Read the policy type definition.
73 * @throws Exception on errors
76 public static void readPolicyDefinition() {
77 String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
79 Object yamlObject = new Yaml().load(yamlString);
80 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
84 * Set up the DAO towards the database.
86 * @throws Exception on database errors
89 public void setupDao() throws Exception {
90 final DaoParameters daoParameters = new DaoParameters();
91 daoParameters.setPluginClass(DefaultPfDao.class.getName());
93 daoParameters.setPersistenceUnit("ToscaConceptTest");
95 Properties jdbcProperties = new Properties();
96 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
97 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
99 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
100 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
101 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
103 daoParameters.setJdbcProperties(jdbcProperties);
105 pfDao = new PfDaoFactory().createPfDao(daoParameters);
106 pfDao.init(daoParameters);
113 public void setupGson() {
114 standardCoder = new StandardCoder();
118 public void teardown() {
123 public void testPolicyTypesGet() throws Exception {
124 assertThatThrownBy(() -> {
125 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
126 }).hasMessageMatching(DAO_IS_NULL);
128 assertThatThrownBy(() -> {
129 new AuthorativeToscaProvider().getPolicyList(null, null, null);
130 }).hasMessageMatching(DAO_IS_NULL);
132 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
134 assertNotNull(toscaServiceTemplate);
135 ToscaServiceTemplate createdServiceTemplate =
136 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
138 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
140 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
141 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
142 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
143 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
145 ToscaServiceTemplate gotServiceTemplate =
146 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
148 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
149 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
150 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
152 List<ToscaPolicyType> gotPolicyTypeList =
153 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
154 assertEquals(2, gotPolicyTypeList.size());
155 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
157 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
158 assertEquals(2, gotPolicyTypeList.size());
159 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
161 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
162 assertEquals(2, gotPolicyTypeList.size());
163 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
165 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
166 assertEquals(2, gotPolicyTypeList.size());
167 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
170 () -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION, VERSION_001))
171 .hasMessageContaining("Policy Framework DAO has not been initialized");
173 assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
177 public void testPolicyTypesGetFiltered() throws Exception {
178 assertThatThrownBy(() -> {
179 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
180 }).hasMessageMatching(DAO_IS_NULL);
182 assertThatThrownBy(() -> {
183 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
184 }).hasMessageMatching(DAO_IS_NULL);
186 assertThatThrownBy(() -> {
187 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
188 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
190 assertThatThrownBy(() -> {
191 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
192 }).hasMessageMatching(DAO_IS_NULL);
194 assertThatThrownBy(() -> {
195 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
196 }).hasMessageMatching(DAO_IS_NULL);
198 assertThatThrownBy(() -> {
199 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
200 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
202 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
203 ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()))
204 .hasMessageContaining("Policy Framework DAO has not been initialized");
206 assertTrue(new AuthorativeToscaProvider()
207 .getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()).isEmpty());
209 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
211 assertNotNull(toscaServiceTemplate);
212 ToscaServiceTemplate createdServiceTemplate =
213 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
215 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
217 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
218 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
219 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
220 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
222 ToscaServiceTemplate gotServiceTemplate =
223 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
225 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
226 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
227 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
229 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
230 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
232 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
233 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
234 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
236 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
237 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build());
239 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
240 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
241 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
243 List<ToscaPolicyType> gotPolicyTypeList =
244 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
245 assertEquals(2, gotPolicyTypeList.size());
246 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
249 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().build());
250 assertEquals(2, gotPolicyTypeList.size());
251 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
253 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
254 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
255 assertEquals(1, gotPolicyTypeList.size());
256 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
258 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
259 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build());
260 assertEquals(1, gotPolicyTypeList.size());
261 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
263 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
264 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
265 assertEquals(1, gotPolicyTypeList.size());
266 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
270 public void testPolicyTypesCreate() throws Exception {
271 assertThatThrownBy(() -> {
272 new AuthorativeToscaProvider().createPolicyTypes(null, null);
273 }).hasMessageMatching(DAO_IS_NULL);
275 assertThatThrownBy(() -> {
276 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
277 }).hasMessageMatching(DAO_IS_NULL);
279 assertThatThrownBy(() -> {
280 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
281 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
283 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
284 assertThatThrownBy(() -> {
285 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
286 }).hasMessage(MISSING_POLICY_TYPES);
288 testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
289 assertThatThrownBy(() -> {
290 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
291 }).hasMessage("An incoming list of concepts must have at least one entry");
293 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
295 assertNotNull(toscaServiceTemplate);
296 ToscaServiceTemplate createdServiceTemplate =
297 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
299 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
301 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
302 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
303 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
304 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
308 public void testPolicyTypesUpdate() throws Exception {
309 assertThatThrownBy(() -> {
310 new AuthorativeToscaProvider().createPolicyTypes(null, null);
311 }).hasMessageMatching(DAO_IS_NULL);
313 assertThatThrownBy(() -> {
314 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
315 }).hasMessageMatching(DAO_IS_NULL);
317 assertThatThrownBy(() -> {
318 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
319 }).hasMessageMatching(DAO_IS_NULL);
321 assertThatThrownBy(() -> {
322 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
323 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
325 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
327 assertNotNull(toscaServiceTemplate);
328 ToscaServiceTemplate createdServiceTemplate =
329 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
331 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
333 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
334 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
335 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
336 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
338 ToscaServiceTemplate updatedServiceTemplate =
339 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
341 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
342 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
343 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
347 public void testPolicyTypesDelete() throws Exception {
348 assertThatThrownBy(() -> {
349 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
350 }).hasMessageMatching(DAO_IS_NULL);
352 assertThatThrownBy(() -> {
353 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
354 }).hasMessageMatching(DAO_IS_NULL);
356 assertThatThrownBy(() -> {
357 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
358 }).hasMessageMatching(DAO_IS_NULL);
360 assertThatThrownBy(() -> {
361 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
362 }).hasMessageMatching(DAO_IS_NULL);
364 assertThatThrownBy(() -> {
365 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
366 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
368 assertThatThrownBy(() -> {
369 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
370 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
372 assertThatThrownBy(() -> {
373 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
374 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
376 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
378 assertNotNull(toscaServiceTemplate);
379 ToscaServiceTemplate createdServiceTemplate =
380 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
382 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
384 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
385 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
386 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
387 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
389 ToscaServiceTemplate deletedServiceTemplate =
390 new AuthorativeToscaProvider().deletePolicyType(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
392 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
393 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
394 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
396 assertThatThrownBy(() -> {
397 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
398 }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
402 public void testAssertPoliciesExist() {
403 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
405 assertThatThrownBy(() -> {
406 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
407 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
409 assertThatThrownBy(() -> {
410 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
411 }).hasMessage(MISSING_POLICY_TYPES);
413 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
414 assertThatThrownBy(() -> {
415 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
416 }).hasMessage(MISSING_POLICY_TYPES);
418 testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
419 assertThatThrownBy(() -> {
420 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
421 }).hasMessage("An incoming list of concepts must have at least one entry");