2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.ArrayList;
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.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
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 * @author Liam Fallon (liam.fallon@est.tech)
57 public class AuthorativeToscaProviderPolicyTypeTest {
58 private static final String VERSION = "version";
59 private static final String POLICY_AFFINITY_VERSION0 = "onap.policies.optimization.AffinityPolicy:0.0.0";
60 private static final String POLICY_AFFINITY = "onap.policies.optimization.AffinityPolicy";
61 private static final String MISSING_POLICY_TYPES = "no policy types specified on service template";
62 private static final String DAO_IS_NULL = "dao is marked @NonNull but is null";
63 private static final String VERSION_000 = "0.0.0";
64 private static String yamlAsJsonString;
66 private StandardCoder standardCoder;
70 * Read the policy type definition.
72 * @throws Exception on errors
75 public static void readPolicyDefinition() {
77 ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.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.getCanonicalName());
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 }).hasMessage(DAO_IS_NULL);
128 assertThatThrownBy(() -> {
129 new AuthorativeToscaProvider().getPolicyList(null, null, null);
130 }).hasMessage(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(1).get(policyTypeKey.getName());
141 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).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(0).get(policyTypeKey.getName());
149 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
150 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
152 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
153 POLICY_AFFINITY, VERSION_000);
154 assertEquals(1, gotPolicyTypeList.size());
155 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
157 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
158 POLICY_AFFINITY, null);
159 assertEquals(1, 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_000);
167 assertEquals(2, gotPolicyTypeList.size());
168 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
173 public void testPolicyTypesGetFiltered() throws Exception {
174 assertThatThrownBy(() -> {
175 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
176 }).hasMessage(DAO_IS_NULL);
178 assertThatThrownBy(() -> {
179 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
180 }).hasMessage(DAO_IS_NULL);
182 assertThatThrownBy(() -> {
183 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
184 }).hasMessage("filter is marked @NonNull but is null");
186 assertThatThrownBy(() -> {
187 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
188 }).hasMessage(DAO_IS_NULL);
190 assertThatThrownBy(() -> {
191 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
192 }).hasMessage(DAO_IS_NULL);
194 assertThatThrownBy(() -> {
195 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
196 }).hasMessage("filter is marked @NonNull but is null");
198 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
200 assertNotNull(toscaServiceTemplate);
201 ToscaServiceTemplate createdServiceTemplate =
202 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
204 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
206 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
207 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
208 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
209 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
211 ToscaServiceTemplate gotServiceTemplate =
212 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
214 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
215 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
216 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
218 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
219 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
221 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
222 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
223 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
225 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
226 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build());
228 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
229 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
230 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
232 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
233 POLICY_AFFINITY, VERSION_000);
234 assertEquals(1, gotPolicyTypeList.size());
235 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
237 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
238 ToscaPolicyTypeFilter.builder().build());
239 assertEquals(2, gotPolicyTypeList.size());
240 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
242 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
243 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
244 assertEquals(1, gotPolicyTypeList.size());
245 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
247 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
248 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_000).build());
249 assertEquals(1, gotPolicyTypeList.size());
250 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
252 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
253 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
254 assertEquals(1, gotPolicyTypeList.size());
255 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
259 public void testPolicyTypesCreate() throws Exception {
260 assertThatThrownBy(() -> {
261 new AuthorativeToscaProvider().createPolicyTypes(null, null);
262 }).hasMessage(DAO_IS_NULL);
264 assertThatThrownBy(() -> {
265 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
266 }).hasMessage(DAO_IS_NULL);
268 assertThatThrownBy(() -> {
269 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
270 }).hasMessage("serviceTemplate is marked @NonNull but is null");
272 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
273 assertThatThrownBy(() -> {
274 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
275 }).hasMessage(MISSING_POLICY_TYPES);
277 testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
278 assertThatThrownBy(() -> {
279 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
280 }).hasMessage("An incoming list of concepts must have at least one entry");
282 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
284 assertNotNull(toscaServiceTemplate);
285 ToscaServiceTemplate createdServiceTemplate =
286 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
288 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
290 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
291 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
292 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
293 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
297 public void testPolicyTypesUpdate() throws Exception {
298 assertThatThrownBy(() -> {
299 new AuthorativeToscaProvider().createPolicyTypes(null, null);
300 }).hasMessage(DAO_IS_NULL);
302 assertThatThrownBy(() -> {
303 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
304 }).hasMessage(DAO_IS_NULL);
306 assertThatThrownBy(() -> {
307 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
308 }).hasMessage(DAO_IS_NULL);
310 assertThatThrownBy(() -> {
311 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
312 }).hasMessage("serviceTemplate is marked @NonNull but is null");
314 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
316 assertNotNull(toscaServiceTemplate);
317 ToscaServiceTemplate createdServiceTemplate =
318 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
320 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
322 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
323 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
324 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
325 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
327 ToscaServiceTemplate updatedServiceTemplate =
328 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
330 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
331 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
332 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
336 public void testPolicyTypesDelete() throws Exception {
337 assertThatThrownBy(() -> {
338 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
339 }).hasMessage(DAO_IS_NULL);
341 assertThatThrownBy(() -> {
342 new AuthorativeToscaProvider().deletePolicyType(null, null, VERSION);
343 }).hasMessage(DAO_IS_NULL);
345 assertThatThrownBy(() -> {
346 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
347 }).hasMessage(DAO_IS_NULL);
349 assertThatThrownBy(() -> {
350 new AuthorativeToscaProvider().deletePolicyType(null, "name", VERSION);
351 }).hasMessage(DAO_IS_NULL);
353 assertThatThrownBy(() -> {
354 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
355 }).hasMessage("name is marked @NonNull but is null");
357 assertThatThrownBy(() -> {
358 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, VERSION);
359 }).hasMessage("name is marked @NonNull but is null");
361 assertThatThrownBy(() -> {
362 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
363 }).hasMessage("version is marked @NonNull but is null");
365 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
367 assertNotNull(toscaServiceTemplate);
368 ToscaServiceTemplate createdServiceTemplate =
369 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
371 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_AFFINITY_VERSION0);
373 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
374 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
375 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
376 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
378 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
379 policyTypeKey.getName(), policyTypeKey.getVersion());
381 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
382 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
383 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
385 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
386 policyTypeKey.getName(), policyTypeKey.getVersion());
388 assertTrue(gotServiceTemplate.getPolicyTypes().isEmpty());
392 public void testAssertPoliciesExist() {
393 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
395 assertThatThrownBy(() -> {
396 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
397 }).hasMessage("version is marked @NonNull but is null");
399 assertThatThrownBy(() -> {
400 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
401 }).hasMessage(MISSING_POLICY_TYPES);
403 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
404 assertThatThrownBy(() -> {
405 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
406 }).hasMessage(MISSING_POLICY_TYPES);
408 testServiceTemplate.setPolicyTypes(new ArrayList<>());
409 assertThatThrownBy(() -> {
410 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
411 }).hasMessage("An incoming list of concepts must have at least one entry");