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 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
96 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
97 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
99 daoParameters.setJdbcProperties(jdbcProperties);
101 pfDao = new PfDaoFactory().createPfDao(daoParameters);
102 pfDao.init(daoParameters);
109 public void setupGson() {
110 standardCoder = new StandardCoder();
114 public void teardown() {
119 public void testPolicyTypesGet() throws Exception {
120 assertThatThrownBy(() -> {
121 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
122 }).hasMessageMatching(DAO_IS_NULL);
124 assertThatThrownBy(() -> {
125 new AuthorativeToscaProvider().getPolicyList(null, null, null);
126 }).hasMessageMatching(DAO_IS_NULL);
128 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
130 assertNotNull(toscaServiceTemplate);
131 ToscaServiceTemplate createdServiceTemplate =
132 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
134 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
136 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
137 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
138 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
139 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
141 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
142 policyTypeKey.getName(), policyTypeKey.getVersion());
144 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
145 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
146 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
148 List<ToscaPolicyType> gotPolicyTypeList =
149 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
150 assertEquals(2, gotPolicyTypeList.size());
151 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
153 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
154 assertEquals(2, gotPolicyTypeList.size());
155 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
157 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
158 assertEquals(2, gotPolicyTypeList.size());
159 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
161 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
162 assertEquals(2, gotPolicyTypeList.size());
163 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
165 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
166 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
168 assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
172 public void testPolicyTypesGetFiltered() throws Exception {
173 assertThatThrownBy(() -> {
174 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
175 }).hasMessageMatching(DAO_IS_NULL);
177 assertThatThrownBy(() -> {
178 new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
179 ToscaEntityFilter.<ToscaPolicyType>builder().build());
180 }).hasMessageMatching(DAO_IS_NULL);
182 assertThatThrownBy(() -> {
183 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
184 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
186 assertThatThrownBy(() -> {
187 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
188 }).hasMessageMatching(DAO_IS_NULL);
190 assertThatThrownBy(() -> {
191 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
192 ToscaEntityFilter.<ToscaPolicyType>builder().build());
193 }).hasMessageMatching(DAO_IS_NULL);
195 assertThatThrownBy(() -> {
196 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
197 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
199 assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
200 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
201 .hasMessageContaining("Policy Framework DAO has not been initialized");
203 assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
204 ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
206 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
208 assertNotNull(toscaServiceTemplate);
209 ToscaServiceTemplate createdServiceTemplate =
210 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
212 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
214 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
215 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
216 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
217 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
219 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
220 ToscaEntityFilter.<ToscaPolicyType>builder().build());
222 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
223 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
224 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
226 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
227 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
229 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
230 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
231 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
233 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
234 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
236 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
237 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
238 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
240 List<ToscaPolicyType> gotPolicyTypeList =
241 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
242 assertEquals(2, gotPolicyTypeList.size());
243 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
245 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
246 ToscaEntityFilter.<ToscaPolicyType>builder().build());
247 assertEquals(2, gotPolicyTypeList.size());
248 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
250 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
251 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
252 assertEquals(1, gotPolicyTypeList.size());
253 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
255 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
256 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
257 assertEquals(1, gotPolicyTypeList.size());
258 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
260 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
261 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
262 assertEquals(1, gotPolicyTypeList.size());
263 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
267 public void testPolicyTypesCreate() throws Exception {
268 assertThatThrownBy(() -> {
269 new AuthorativeToscaProvider().createPolicyTypes(null, null);
270 }).hasMessageMatching(DAO_IS_NULL);
272 assertThatThrownBy(() -> {
273 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
274 }).hasMessageMatching(DAO_IS_NULL);
276 assertThatThrownBy(() -> {
277 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
278 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
280 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
281 assertThatThrownBy(() -> {
282 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
283 }).hasMessage(MISSING_POLICY_TYPES);
285 testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
286 assertThatThrownBy(() -> {
287 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
288 }).hasMessage("An incoming list of concepts must have at least one entry");
290 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
292 assertNotNull(toscaServiceTemplate);
293 ToscaServiceTemplate createdServiceTemplate =
294 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
296 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
298 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
299 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
300 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
301 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
305 public void testPolicyTypesUpdate() throws Exception {
306 assertThatThrownBy(() -> {
307 new AuthorativeToscaProvider().createPolicyTypes(null, null);
308 }).hasMessageMatching(DAO_IS_NULL);
310 assertThatThrownBy(() -> {
311 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
312 }).hasMessageMatching(DAO_IS_NULL);
314 assertThatThrownBy(() -> {
315 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
316 }).hasMessageMatching(DAO_IS_NULL);
318 assertThatThrownBy(() -> {
319 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
320 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
322 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
324 assertNotNull(toscaServiceTemplate);
325 ToscaServiceTemplate createdServiceTemplate =
326 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
328 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
330 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
331 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
332 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
333 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
335 ToscaServiceTemplate updatedServiceTemplate =
336 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
338 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
339 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
340 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
344 public void testPolicyTypesDelete() throws Exception {
345 assertThatThrownBy(() -> {
346 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
347 }).hasMessageMatching(DAO_IS_NULL);
349 assertThatThrownBy(() -> {
350 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
351 }).hasMessageMatching(DAO_IS_NULL);
353 assertThatThrownBy(() -> {
354 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
355 }).hasMessageMatching(DAO_IS_NULL);
357 assertThatThrownBy(() -> {
358 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
359 }).hasMessageMatching(DAO_IS_NULL);
361 assertThatThrownBy(() -> {
362 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
363 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
365 assertThatThrownBy(() -> {
366 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
367 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
369 assertThatThrownBy(() -> {
370 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
371 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
373 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
375 assertNotNull(toscaServiceTemplate);
376 ToscaServiceTemplate createdServiceTemplate =
377 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
379 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
381 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
382 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
383 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
384 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
386 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
387 policyTypeKey.getName(), policyTypeKey.getVersion());
389 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
390 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
391 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
393 assertThatThrownBy(() -> {
394 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
395 }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
399 public void testAssertPoliciesExist() {
400 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
402 assertThatThrownBy(() -> {
403 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
404 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
406 assertThatThrownBy(() -> {
407 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
408 }).hasMessage(MISSING_POLICY_TYPES);
410 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
411 assertThatThrownBy(() -> {
412 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
413 }).hasMessage(MISSING_POLICY_TYPES);
415 testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
416 assertThatThrownBy(() -> {
417 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
418 }).hasMessage("An incoming list of concepts must have at least one entry");
422 public void testNullParameters() throws Exception {
423 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
424 .hasMessageMatching("^dao is marked .*on.*ull but is null$");