2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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.ToscaEntityFilter;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
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 public class AuthorativeToscaProviderPolicyTypeTest {
56 private static final String VERSION = "version";
57 private static final String POLICY_NO_VERSION_VERSION1 = "onap.policies.NoVersion:0.0.1";
58 private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
59 private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
60 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
61 private static final String VERSION_001 = "0.0.1";
62 private static String yamlAsJsonString;
64 private StandardCoder standardCoder;
67 * Read the policy type definition.
69 * @throws Exception on errors
72 public static void readPolicyDefinition() {
73 String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
75 Object yamlObject = new Yaml().load(yamlString);
76 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
80 * Set up the DAO towards the database.
82 * @throws Exception on database errors
85 public void setupDao() throws Exception {
86 final DaoParameters daoParameters = new DaoParameters();
87 daoParameters.setPluginClass(DefaultPfDao.class.getName());
89 daoParameters.setPersistenceUnit("ToscaConceptTest");
91 Properties jdbcProperties = new Properties();
92 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
93 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
95 if (System.getProperty("USE-MARIADB") != null) {
96 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
97 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
99 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
100 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 = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
146 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()));
169 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
170 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
172 assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
176 public void testPolicyTypesGetFiltered() throws Exception {
177 assertThatThrownBy(() -> {
178 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
179 }).hasMessageMatching(DAO_IS_NULL);
181 assertThatThrownBy(() -> {
182 new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
183 ToscaEntityFilter.<ToscaPolicyType>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,
196 ToscaEntityFilter.<ToscaPolicyType>builder().build());
197 }).hasMessageMatching(DAO_IS_NULL);
199 assertThatThrownBy(() -> {
200 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
201 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
203 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
204 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
205 .hasMessageContaining("Policy Framework DAO has not been initialized");
207 assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
208 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
210 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
212 assertNotNull(toscaServiceTemplate);
213 ToscaServiceTemplate createdServiceTemplate =
214 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
216 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
218 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
219 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
220 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
221 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
223 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
224 ToscaEntityFilter.<ToscaPolicyType>builder().build());
226 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
227 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
229 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
230 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
232 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
233 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
235 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
236 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
238 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
239 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
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()));
246 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
247 ToscaEntityFilter.<ToscaPolicyType>builder().build());
248 assertEquals(2, gotPolicyTypeList.size());
249 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
251 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
252 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
253 assertEquals(1, gotPolicyTypeList.size());
254 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
256 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
257 .<ToscaPolicyType>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 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
263 assertEquals(1, gotPolicyTypeList.size());
264 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
267 private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
268 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
269 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
273 public void testPolicyTypesCreate() throws Exception {
274 assertThatThrownBy(() -> {
275 new AuthorativeToscaProvider().createPolicyTypes(null, null);
276 }).hasMessageMatching(DAO_IS_NULL);
278 assertThatThrownBy(() -> {
279 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
280 }).hasMessageMatching(DAO_IS_NULL);
282 assertThatThrownBy(() -> {
283 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
284 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
286 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
287 assertThatThrownBy(() -> {
288 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
289 }).hasMessage(MISSING_POLICY_TYPES);
291 testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
292 assertThatThrownBy(() -> {
293 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
294 }).hasMessage("An incoming list of concepts must have at least one entry");
296 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
298 assertNotNull(toscaServiceTemplate);
299 ToscaServiceTemplate createdServiceTemplate =
300 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
302 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
304 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
305 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
306 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
307 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
311 public void testPolicyTypesUpdate() throws Exception {
312 assertThatThrownBy(() -> {
313 new AuthorativeToscaProvider().createPolicyTypes(null, null);
314 }).hasMessageMatching(DAO_IS_NULL);
316 assertThatThrownBy(() -> {
317 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
318 }).hasMessageMatching(DAO_IS_NULL);
320 assertThatThrownBy(() -> {
321 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
322 }).hasMessageMatching(DAO_IS_NULL);
324 assertThatThrownBy(() -> {
325 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
326 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
328 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
330 assertNotNull(toscaServiceTemplate);
331 ToscaServiceTemplate createdServiceTemplate =
332 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
334 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
336 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
337 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
338 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
339 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
341 ToscaServiceTemplate updatedServiceTemplate =
342 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
344 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
345 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
346 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
350 public void testPolicyTypesDelete() throws Exception {
351 assertThatThrownBy(() -> {
352 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
353 }).hasMessageMatching(DAO_IS_NULL);
355 assertThatThrownBy(() -> {
356 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
357 }).hasMessageMatching(DAO_IS_NULL);
359 assertThatThrownBy(() -> {
360 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
361 }).hasMessageMatching(DAO_IS_NULL);
363 assertThatThrownBy(() -> {
364 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
365 }).hasMessageMatching(DAO_IS_NULL);
367 assertThatThrownBy(() -> {
368 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
369 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
371 assertThatThrownBy(() -> {
372 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
373 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
375 assertThatThrownBy(() -> {
376 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
377 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
379 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
381 assertNotNull(toscaServiceTemplate);
382 ToscaServiceTemplate createdServiceTemplate =
383 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
385 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
387 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
388 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
389 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
390 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
392 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
393 policyTypeKey.getName(), policyTypeKey.getVersion());
395 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
396 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
397 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
399 assertThatThrownBy(() -> {
400 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
401 }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
405 public void testAssertPoliciesExist() {
406 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
408 assertThatThrownBy(() -> {
409 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
410 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
412 assertThatThrownBy(() -> {
413 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
414 }).hasMessage(MISSING_POLICY_TYPES);
416 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
417 assertThatThrownBy(() -> {
418 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
419 }).hasMessage(MISSING_POLICY_TYPES);
421 testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
422 assertThatThrownBy(() -> {
423 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
424 }).hasMessage("An incoming list of concepts must have at least one entry");
428 public void testNullParameters() throws Exception {
429 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
430 .hasMessageMatching("^dao is marked .*on.*ull but is null$");