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;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Properties;
35 import org.apache.commons.lang3.ObjectUtils;
36 import org.eclipse.persistence.config.PersistenceUnitProperties;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.dao.DaoParameters;
45 import org.onap.policy.models.dao.PfDao;
46 import org.onap.policy.models.dao.PfDaoFactory;
47 import org.onap.policy.models.dao.impl.DefaultPfDao;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
52 import org.yaml.snakeyaml.Yaml;
55 * Test of the {@link AuthorativeToscaProvider} class.
57 * @author Liam Fallon (liam.fallon@est.tech)
59 public class AuthorativeToscaProviderPolicyTypeTest {
60 private static final String VERSION = "version";
61 private static final String POLICY_AFFINITY_VERSION0 = "onap.policies.NoVersion:0.0.0";
62 private static final String POLICY_AFFINITY = "onap.policies.NoVersion";
63 private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
64 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
65 private static final String VERSION_000 = "0.0.0";
66 private static String yamlAsJsonString;
68 private StandardCoder standardCoder;
71 * Read the policy type definition.
73 * @throws Exception on errors
76 public static void readPolicyDefinition() {
77 String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
79 Object yamlObject = new Yaml().load(yamlString);
80 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
84 * Set up the DAO towards the database.
86 * @throws Exception on database errors
89 public void setupDao() throws Exception {
90 final DaoParameters daoParameters = new DaoParameters();
91 daoParameters.setPluginClass(DefaultPfDao.class.getName());
93 daoParameters.setPersistenceUnit("ToscaConceptTest");
95 Properties jdbcProperties = new Properties();
96 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
97 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
99 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
100 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
101 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_AFFINITY_VERSION0);
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_AFFINITY, VERSION_000);
154 assertEquals(1, gotPolicyTypeList.size());
155 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
157 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_AFFINITY, null);
158 assertEquals(1, gotPolicyTypeList.size());
159 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
161 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
162 assertEquals(1, gotPolicyTypeList.size());
163 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
165 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_000);
166 assertEquals(1, gotPolicyTypeList.size());
167 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
171 public void testPolicyTypesGetFiltered() throws Exception {
172 assertThatThrownBy(() -> {
173 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
174 }).hasMessageMatching(DAO_IS_NULL);
176 assertThatThrownBy(() -> {
177 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
178 }).hasMessageMatching(DAO_IS_NULL);
180 assertThatThrownBy(() -> {
181 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
182 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
184 assertThatThrownBy(() -> {
185 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
186 }).hasMessageMatching(DAO_IS_NULL);
188 assertThatThrownBy(() -> {
189 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
190 }).hasMessageMatching(DAO_IS_NULL);
192 assertThatThrownBy(() -> {
193 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
194 }).hasMessageMatching("^filter is marked .*on.*ull but is null$");
196 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
198 assertNotNull(toscaServiceTemplate);
199 ToscaServiceTemplate createdServiceTemplate =
200 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
202 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
204 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
205 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
206 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
207 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
209 ToscaServiceTemplate gotServiceTemplate =
210 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
212 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
213 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
214 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
216 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
217 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
219 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
220 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
221 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
223 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
224 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build());
226 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
227 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
228 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
230 List<ToscaPolicyType> gotPolicyTypeList =
231 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_AFFINITY, VERSION_000);
232 assertEquals(1, gotPolicyTypeList.size());
233 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
235 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
236 ToscaPolicyTypeFilter.builder().build());
237 assertEquals(1, gotPolicyTypeList.size());
238 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
240 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
241 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
242 assertEquals(1, gotPolicyTypeList.size());
243 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
245 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
246 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build());
247 assertEquals(1, gotPolicyTypeList.size());
248 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
250 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
251 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
252 assertEquals(0, gotPolicyTypeList.size());
253 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
257 public void testPolicyTypesCreate() throws Exception {
258 assertThatThrownBy(() -> {
259 new AuthorativeToscaProvider().createPolicyTypes(null, null);
260 }).hasMessageMatching(DAO_IS_NULL);
262 assertThatThrownBy(() -> {
263 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
264 }).hasMessageMatching(DAO_IS_NULL);
266 assertThatThrownBy(() -> {
267 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
268 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
270 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
271 assertThatThrownBy(() -> {
272 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
273 }).hasMessage(MISSING_POLICY_TYPES);
275 testToscaServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
276 assertThatThrownBy(() -> {
277 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
278 }).hasMessage("An incoming list of concepts must have at least one entry");
280 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
282 assertNotNull(toscaServiceTemplate);
283 ToscaServiceTemplate createdServiceTemplate =
284 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
286 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
288 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
289 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
290 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
291 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
295 public void testPolicyTypesUpdate() throws Exception {
296 assertThatThrownBy(() -> {
297 new AuthorativeToscaProvider().createPolicyTypes(null, null);
298 }).hasMessageMatching(DAO_IS_NULL);
300 assertThatThrownBy(() -> {
301 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
302 }).hasMessageMatching(DAO_IS_NULL);
304 assertThatThrownBy(() -> {
305 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
306 }).hasMessageMatching(DAO_IS_NULL);
308 assertThatThrownBy(() -> {
309 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
310 }).hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
312 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
314 assertNotNull(toscaServiceTemplate);
315 ToscaServiceTemplate createdServiceTemplate =
316 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
318 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
320 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
321 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
322 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
323 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
325 ToscaServiceTemplate updatedServiceTemplate =
326 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
328 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
329 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
330 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
334 public void testPolicyTypesDelete() throws Exception {
335 assertThatThrownBy(() -> {
336 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
337 }).hasMessageMatching(DAO_IS_NULL);
339 assertThatThrownBy(() -> {
340 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
341 }).hasMessageMatching(DAO_IS_NULL);
343 assertThatThrownBy(() -> {
344 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
345 }).hasMessageMatching(DAO_IS_NULL);
347 assertThatThrownBy(() -> {
348 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
349 }).hasMessageMatching(DAO_IS_NULL);
351 assertThatThrownBy(() -> {
352 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
353 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
355 assertThatThrownBy(() -> {
356 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
357 }).hasMessageMatching("^name is marked .*on.*ull but is null$");
359 assertThatThrownBy(() -> {
360 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
361 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
363 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
365 assertNotNull(toscaServiceTemplate);
366 ToscaServiceTemplate createdServiceTemplate =
367 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
369 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
371 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
372 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
373 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
374 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
376 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
377 policyTypeKey.getName(), policyTypeKey.getVersion());
379 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
380 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
381 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
383 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
384 policyTypeKey.getName(), policyTypeKey.getVersion());
386 assertTrue(gotServiceTemplate.getPolicyTypes().isEmpty());
390 public void testAssertPoliciesExist() {
391 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
393 assertThatThrownBy(() -> {
394 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
395 }).hasMessageMatching("^version is marked .*on.*ull but is null$");
397 assertThatThrownBy(() -> {
398 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
399 }).hasMessage(MISSING_POLICY_TYPES);
401 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
402 assertThatThrownBy(() -> {
403 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
404 }).hasMessage(MISSING_POLICY_TYPES);
406 testServiceTemplate.setPolicyTypes(new LinkedHashMap<>());
407 assertThatThrownBy(() -> {
408 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
409 }).hasMessage("An incoming list of concepts must have at least one entry");