2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 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,
101 "jdbc:h2:mem:AuthorativeToscaProviderPolicyTypeTest");
104 daoParameters.setJdbcProperties(jdbcProperties);
106 pfDao = new PfDaoFactory().createPfDao(daoParameters);
107 pfDao.init(daoParameters);
114 public void setupGson() {
115 standardCoder = new StandardCoder();
119 public void teardown() {
124 public void testPolicyTypesGet() throws Exception {
125 assertThatThrownBy(() -> {
126 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
127 }).hasMessageMatching(DAO_IS_NULL);
129 assertThatThrownBy(() -> {
130 new AuthorativeToscaProvider().getPolicyList(null, null, null);
131 }).hasMessageMatching(DAO_IS_NULL);
133 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
135 assertNotNull(toscaServiceTemplate);
136 ToscaServiceTemplate createdServiceTemplate =
137 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
139 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
141 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
142 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
143 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
144 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
146 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
147 policyTypeKey.getName(), policyTypeKey.getVersion());
149 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
150 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
151 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
153 List<ToscaPolicyType> gotPolicyTypeList =
154 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
155 assertEquals(2, gotPolicyTypeList.size());
156 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
158 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
159 assertEquals(2, gotPolicyTypeList.size());
160 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
162 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
163 assertEquals(2, gotPolicyTypeList.size());
164 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
166 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
167 assertEquals(2, gotPolicyTypeList.size());
168 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
170 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
171 VERSION_001)).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,
184 ToscaEntityFilter.<ToscaPolicyType>builder().build());
185 }).hasMessageMatching(DAO_IS_NULL);
187 assertThatThrownBy(() -> {
188 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
189 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
191 assertThatThrownBy(() -> {
192 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
193 }).hasMessageMatching(DAO_IS_NULL);
195 assertThatThrownBy(() -> {
196 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
197 ToscaEntityFilter.<ToscaPolicyType>builder().build());
198 }).hasMessageMatching(DAO_IS_NULL);
200 assertThatThrownBy(() -> {
201 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
202 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
204 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
205 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
206 .hasMessageContaining("Policy Framework DAO has not been initialized");
208 assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
209 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
211 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
213 assertNotNull(toscaServiceTemplate);
214 ToscaServiceTemplate createdServiceTemplate =
215 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
217 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
219 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
220 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
221 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
222 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
224 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
225 ToscaEntityFilter.<ToscaPolicyType>builder().build());
227 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
228 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
230 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
231 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
233 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
234 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
236 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
237 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
239 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
240 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
242 List<ToscaPolicyType> gotPolicyTypeList =
243 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
244 assertEquals(2, gotPolicyTypeList.size());
245 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
247 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
248 ToscaEntityFilter.<ToscaPolicyType>builder().build());
249 assertEquals(2, gotPolicyTypeList.size());
250 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
252 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
253 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
254 assertEquals(1, gotPolicyTypeList.size());
255 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
257 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
258 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
259 assertEquals(1, gotPolicyTypeList.size());
260 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
262 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
263 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
264 assertEquals(1, gotPolicyTypeList.size());
265 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
268 private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
269 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
270 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
274 public void testPolicyTypesCreate() throws Exception {
275 assertThatThrownBy(() -> {
276 new AuthorativeToscaProvider().createPolicyTypes(null, null);
277 }).hasMessageMatching(DAO_IS_NULL);
279 assertThatThrownBy(() -> {
280 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
281 }).hasMessageMatching(DAO_IS_NULL);
283 assertThatThrownBy(() -> {
284 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
285 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
287 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
288 assertThatThrownBy(() -> {
289 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
290 }).hasMessage(MISSING_POLICY_TYPES);
292 testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
293 assertThatThrownBy(() -> {
294 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
295 }).hasMessage("An incoming list of concepts must have at least one entry");
297 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
299 assertNotNull(toscaServiceTemplate);
300 ToscaServiceTemplate createdServiceTemplate =
301 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
303 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
305 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
306 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
307 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
308 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
312 public void testPolicyTypesUpdate() throws Exception {
313 assertThatThrownBy(() -> {
314 new AuthorativeToscaProvider().createPolicyTypes(null, null);
315 }).hasMessageMatching(DAO_IS_NULL);
317 assertThatThrownBy(() -> {
318 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
319 }).hasMessageMatching(DAO_IS_NULL);
321 assertThatThrownBy(() -> {
322 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
323 }).hasMessageMatching(DAO_IS_NULL);
325 assertThatThrownBy(() -> {
326 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
327 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
329 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
331 assertNotNull(toscaServiceTemplate);
332 ToscaServiceTemplate createdServiceTemplate =
333 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
335 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
337 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
338 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
339 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
340 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
342 ToscaServiceTemplate updatedServiceTemplate =
343 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
345 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
346 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
347 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
351 public void testPolicyTypesDelete() throws Exception {
352 assertThatThrownBy(() -> {
353 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
354 }).hasMessageMatching(DAO_IS_NULL);
356 assertThatThrownBy(() -> {
357 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
358 }).hasMessageMatching(DAO_IS_NULL);
360 assertThatThrownBy(() -> {
361 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
362 }).hasMessageMatching(DAO_IS_NULL);
364 assertThatThrownBy(() -> {
365 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
366 }).hasMessageMatching(DAO_IS_NULL);
368 assertThatThrownBy(() -> {
369 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
370 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
372 assertThatThrownBy(() -> {
373 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
374 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
376 assertThatThrownBy(() -> {
377 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
378 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
380 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
382 assertNotNull(toscaServiceTemplate);
383 ToscaServiceTemplate createdServiceTemplate =
384 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
386 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
388 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
389 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
390 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
391 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
393 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
394 policyTypeKey.getName(), policyTypeKey.getVersion());
396 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
397 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
398 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
400 assertThatThrownBy(() -> {
401 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
402 }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
406 public void testAssertPoliciesExist() {
407 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
409 assertThatThrownBy(() -> {
410 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
411 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
413 assertThatThrownBy(() -> {
414 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
415 }).hasMessage(MISSING_POLICY_TYPES);
417 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
418 assertThatThrownBy(() -> {
419 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
420 }).hasMessage(MISSING_POLICY_TYPES);
422 testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
423 assertThatThrownBy(() -> {
424 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
425 }).hasMessage("An incoming list of concepts must have at least one entry");
429 public void testNullParameters() throws Exception {
430 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
431 .hasMessageMatching("^dao is marked .*on.*ull but is null$");