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 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
225 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
226 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
228 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
229 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
231 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
232 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
234 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
235 checkEqualsNameDescription(beforePolicyType, gotPolicyType);
237 List<ToscaPolicyType> gotPolicyTypeList =
238 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
239 assertEquals(2, gotPolicyTypeList.size());
240 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
242 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
243 ToscaEntityFilter.<ToscaPolicyType>builder().build());
244 assertEquals(2, gotPolicyTypeList.size());
245 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
247 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
248 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
249 assertEquals(1, gotPolicyTypeList.size());
250 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
252 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
253 .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
254 assertEquals(1, gotPolicyTypeList.size());
255 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
257 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
258 ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
259 assertEquals(1, gotPolicyTypeList.size());
260 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
263 private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
264 assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
265 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
269 public void testPolicyTypesCreate() throws Exception {
270 assertThatThrownBy(() -> {
271 new AuthorativeToscaProvider().createPolicyTypes(null, null);
272 }).hasMessageMatching(DAO_IS_NULL);
274 assertThatThrownBy(() -> {
275 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
276 }).hasMessageMatching(DAO_IS_NULL);
278 assertThatThrownBy(() -> {
279 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
280 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
282 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
283 assertThatThrownBy(() -> {
284 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
285 }).hasMessage(MISSING_POLICY_TYPES);
287 testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
288 assertThatThrownBy(() -> {
289 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
290 }).hasMessage("An incoming list of concepts must have at least one entry");
292 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
294 assertNotNull(toscaServiceTemplate);
295 ToscaServiceTemplate createdServiceTemplate =
296 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
298 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
300 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
301 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
302 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
303 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
307 public void testPolicyTypesUpdate() throws Exception {
308 assertThatThrownBy(() -> {
309 new AuthorativeToscaProvider().createPolicyTypes(null, null);
310 }).hasMessageMatching(DAO_IS_NULL);
312 assertThatThrownBy(() -> {
313 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
314 }).hasMessageMatching(DAO_IS_NULL);
316 assertThatThrownBy(() -> {
317 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
318 }).hasMessageMatching(DAO_IS_NULL);
320 assertThatThrownBy(() -> {
321 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
322 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
324 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
326 assertNotNull(toscaServiceTemplate);
327 ToscaServiceTemplate createdServiceTemplate =
328 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
330 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
332 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
333 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
334 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
335 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
337 ToscaServiceTemplate updatedServiceTemplate =
338 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
340 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
341 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
342 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
346 public void testPolicyTypesDelete() throws Exception {
347 assertThatThrownBy(() -> {
348 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
349 }).hasMessageMatching(DAO_IS_NULL);
351 assertThatThrownBy(() -> {
352 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
353 }).hasMessageMatching(DAO_IS_NULL);
355 assertThatThrownBy(() -> {
356 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
357 }).hasMessageMatching(DAO_IS_NULL);
359 assertThatThrownBy(() -> {
360 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
361 }).hasMessageMatching(DAO_IS_NULL);
363 assertThatThrownBy(() -> {
364 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
365 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
367 assertThatThrownBy(() -> {
368 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
369 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
371 assertThatThrownBy(() -> {
372 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
373 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
375 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
377 assertNotNull(toscaServiceTemplate);
378 ToscaServiceTemplate createdServiceTemplate =
379 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
381 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
383 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
384 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
385 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
386 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
388 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
389 policyTypeKey.getName(), policyTypeKey.getVersion());
391 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
392 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
393 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
395 assertThatThrownBy(() -> {
396 new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
397 }).hasMessage("policy types for onap.policies.NoVersion:0.0.1 do not exist");
401 public void testAssertPoliciesExist() {
402 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
404 assertThatThrownBy(() -> {
405 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
406 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
408 assertThatThrownBy(() -> {
409 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
410 }).hasMessage(MISSING_POLICY_TYPES);
412 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
413 assertThatThrownBy(() -> {
414 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
415 }).hasMessage(MISSING_POLICY_TYPES);
417 testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
418 assertThatThrownBy(() -> {
419 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
420 }).hasMessage("An incoming list of concepts must have at least one entry");
424 public void testNullParameters() throws Exception {
425 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
426 .hasMessageMatching("^dao is marked .*on.*ull but is null$");