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;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Properties;
33 import org.apache.commons.lang3.ObjectUtils;
34 import org.eclipse.persistence.config.PersistenceUnitProperties;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
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.PfConceptKey;
42 import org.onap.policy.models.dao.DaoParameters;
43 import org.onap.policy.models.dao.PfDao;
44 import org.onap.policy.models.dao.PfDaoFactory;
45 import org.onap.policy.models.dao.impl.DefaultPfDao;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
50 import org.yaml.snakeyaml.Yaml;
53 * Test of the {@link AuthorativeToscaProvider} class.
55 * @author Liam Fallon (liam.fallon@est.tech)
57 public class AuthorativeToscaProviderPolicyTypeTest {
58 private static final String VERSION = "version";
59 private static final String POLICY_NO_VERSION_VERSION1 = "onap.policies.NoVersion:0.0.1";
60 private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
61 private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
62 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
63 private static final String VERSION_001 = "0.0.1";
64 private static String yamlAsJsonString;
66 private StandardCoder standardCoder;
69 * Read the policy type definition.
71 * @throws Exception on errors
74 public static void readPolicyDefinition() {
75 String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
77 Object yamlObject = new Yaml().load(yamlString);
78 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
82 * Set up the DAO towards the database.
84 * @throws Exception on database errors
87 public void setupDao() throws Exception {
88 final DaoParameters daoParameters = new DaoParameters();
89 daoParameters.setPluginClass(DefaultPfDao.class.getName());
91 daoParameters.setPersistenceUnit("ToscaConceptTest");
93 Properties jdbcProperties = new Properties();
94 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
95 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
97 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
98 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
99 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
101 daoParameters.setJdbcProperties(jdbcProperties);
103 pfDao = new PfDaoFactory().createPfDao(daoParameters);
104 pfDao.init(daoParameters);
111 public void setupGson() {
112 standardCoder = new StandardCoder();
116 public void teardown() {
121 public void testPolicyTypesGet() throws Exception {
122 assertThatThrownBy(() -> {
123 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
124 }).hasMessageMatching(DAO_IS_NULL);
126 assertThatThrownBy(() -> {
127 new AuthorativeToscaProvider().getPolicyList(null, null, null);
128 }).hasMessageMatching(DAO_IS_NULL);
130 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
132 assertNotNull(toscaServiceTemplate);
133 ToscaServiceTemplate createdServiceTemplate =
134 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
136 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
138 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
139 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
140 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
141 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
143 ToscaServiceTemplate gotServiceTemplate =
144 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
146 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
147 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
148 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
150 List<ToscaPolicyType> gotPolicyTypeList =
151 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
152 assertEquals(2, gotPolicyTypeList.size());
153 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
155 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
156 assertEquals(2, gotPolicyTypeList.size());
157 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
159 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
160 assertEquals(2, gotPolicyTypeList.size());
161 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
163 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
164 assertEquals(2, gotPolicyTypeList.size());
165 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
168 () -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION, VERSION_001))
169 .hasMessageContaining("Policy Framework DAO has not been initialized");
171 assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
175 public void testPolicyTypesGetFiltered() throws Exception {
176 assertThatThrownBy(() -> {
177 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
178 }).hasMessageMatching(DAO_IS_NULL);
180 assertThatThrownBy(() -> {
181 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
182 }).hasMessageMatching(DAO_IS_NULL);
184 assertThatThrownBy(() -> {
185 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
186 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
188 assertThatThrownBy(() -> {
189 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
190 }).hasMessageMatching(DAO_IS_NULL);
192 assertThatThrownBy(() -> {
193 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
194 }).hasMessageMatching(DAO_IS_NULL);
196 assertThatThrownBy(() -> {
197 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
198 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
200 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
201 ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()))
202 .hasMessageContaining("Policy Framework DAO has not been initialized");
204 assertTrue(new AuthorativeToscaProvider()
205 .getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()).isEmpty());
207 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
209 assertNotNull(toscaServiceTemplate);
210 ToscaServiceTemplate createdServiceTemplate =
211 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
213 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
215 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
216 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
217 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
218 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
220 ToscaServiceTemplate gotServiceTemplate =
221 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
223 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
224 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
225 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
227 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
228 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
230 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
231 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
232 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
234 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
235 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build());
237 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
238 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
239 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
241 List<ToscaPolicyType> gotPolicyTypeList =
242 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
243 assertEquals(2, gotPolicyTypeList.size());
244 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
247 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().build());
248 assertEquals(2, gotPolicyTypeList.size());
249 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
251 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
252 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
253 assertEquals(1, gotPolicyTypeList.size());
254 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
256 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
257 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build());
258 assertEquals(1, gotPolicyTypeList.size());
259 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
261 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
262 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
263 assertEquals(1, gotPolicyTypeList.size());
264 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
268 public void testPolicyTypesCreate() throws Exception {
269 assertThatThrownBy(() -> {
270 new AuthorativeToscaProvider().createPolicyTypes(null, null);
271 }).hasMessageMatching(DAO_IS_NULL);
273 assertThatThrownBy(() -> {
274 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
275 }).hasMessageMatching(DAO_IS_NULL);
277 assertThatThrownBy(() -> {
278 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
279 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
281 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
282 assertThatThrownBy(() -> {
283 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
284 }).hasMessage(MISSING_POLICY_TYPES);
286 testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
287 assertThatThrownBy(() -> {
288 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
289 }).hasMessage("An incoming list of concepts must have at least one entry");
291 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
293 assertNotNull(toscaServiceTemplate);
294 ToscaServiceTemplate createdServiceTemplate =
295 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
297 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
299 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
300 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
301 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
302 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
306 public void testPolicyTypesUpdate() throws Exception {
307 assertThatThrownBy(() -> {
308 new AuthorativeToscaProvider().createPolicyTypes(null, null);
309 }).hasMessageMatching(DAO_IS_NULL);
311 assertThatThrownBy(() -> {
312 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
313 }).hasMessageMatching(DAO_IS_NULL);
315 assertThatThrownBy(() -> {
316 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
317 }).hasMessageMatching(DAO_IS_NULL);
319 assertThatThrownBy(() -> {
320 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
321 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
323 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
325 assertNotNull(toscaServiceTemplate);
326 ToscaServiceTemplate createdServiceTemplate =
327 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
329 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
331 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
332 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
333 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
334 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
336 ToscaServiceTemplate updatedServiceTemplate =
337 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
339 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
340 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
341 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
345 public void testPolicyTypesDelete() throws Exception {
346 assertThatThrownBy(() -> {
347 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
348 }).hasMessageMatching(DAO_IS_NULL);
350 assertThatThrownBy(() -> {
351 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
352 }).hasMessageMatching(DAO_IS_NULL);
354 assertThatThrownBy(() -> {
355 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
356 }).hasMessageMatching(DAO_IS_NULL);
358 assertThatThrownBy(() -> {
359 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
360 }).hasMessageMatching(DAO_IS_NULL);
362 assertThatThrownBy(() -> {
363 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
364 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
366 assertThatThrownBy(() -> {
367 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
368 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
370 assertThatThrownBy(() -> {
371 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
372 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
374 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
376 assertNotNull(toscaServiceTemplate);
377 ToscaServiceTemplate createdServiceTemplate =
378 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
380 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
382 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
383 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
384 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
385 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
387 ToscaServiceTemplate deletedServiceTemplate =
388 new AuthorativeToscaProvider().deletePolicyType(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
390 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
391 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
392 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
394 assertThatThrownBy(() -> {
395 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
396 }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
400 public void testAssertPoliciesExist() {
401 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
403 assertThatThrownBy(() -> {
404 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
405 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
407 assertThatThrownBy(() -> {
408 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
409 }).hasMessage(MISSING_POLICY_TYPES);
411 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
412 assertThatThrownBy(() -> {
413 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
414 }).hasMessage(MISSING_POLICY_TYPES);
416 testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
417 assertThatThrownBy(() -> {
418 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
419 }).hasMessage("An incoming list of concepts must have at least one entry");