2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.authorative.provider;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
27 import com.google.gson.GsonBuilder;
29 import java.util.ArrayList;
30 import java.util.List;
31 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.base.PfModelException;
43 import org.onap.policy.models.dao.DaoParameters;
44 import org.onap.policy.models.dao.PfDao;
45 import org.onap.policy.models.dao.PfDaoFactory;
46 import org.onap.policy.models.dao.impl.DefaultPfDao;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
51 import org.yaml.snakeyaml.Yaml;
54 * Test of the {@link AuthorativeToscaProvider} class.
56 * @author Liam Fallon (liam.fallon@est.tech)
58 public class AuthorativeToscaProviderPolicyTypeTest {
59 private static String yamlAsJsonString;
61 private StandardCoder standardCoder;
65 * Read the policy type definition.
67 * @throws Exception on errors
70 public static void readPolicyDefinition() {
72 ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.yaml");
74 Object yamlObject = new Yaml().load(yamlString);
75 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
79 * Set up the DAO towards the database.
81 * @throws Exception on database errors
84 public void setupDao() throws Exception {
85 final DaoParameters daoParameters = new DaoParameters();
86 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
88 daoParameters.setPersistenceUnit("ToscaConceptTest");
90 Properties jdbcProperties = new Properties();
91 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
92 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
94 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
95 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
96 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
98 daoParameters.setJdbcProperties(jdbcProperties );
100 pfDao = new PfDaoFactory().createPfDao(daoParameters);
101 pfDao.init(daoParameters);
108 public void setupGson() {
109 standardCoder = new StandardCoder();
113 public void teardown() throws Exception {
118 public void testPolicyTypesGet() throws Exception {
119 assertThatThrownBy(() -> {
120 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
121 }).hasMessage("dao is marked @NonNull but is null");
123 assertThatThrownBy(() -> {
124 new AuthorativeToscaProvider().getPolicyList(null, null, null);
125 }).hasMessage("dao is marked @NonNull but is null");
127 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
129 assertNotNull(toscaServiceTemplate);
130 ToscaServiceTemplate createdServiceTemplate =
131 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
133 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
135 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
136 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
137 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
138 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
140 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
141 policyTypeKey.getName(), policyTypeKey.getVersion());
143 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
144 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
145 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
147 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
148 "onap.policies.optimization.AffinityPolicy", "0.0.0");
149 assertEquals(1, gotPolicyTypeList.size());
150 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
152 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
153 "onap.policies.optimization.AffinityPolicy", null);
154 assertEquals(1, 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, "0.0.0");
162 assertEquals(2, gotPolicyTypeList.size());
163 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
168 public void testPolicyTypesGetFiltered() throws Exception {
169 assertThatThrownBy(() -> {
170 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
171 }).hasMessage("dao is marked @NonNull but is null");
173 assertThatThrownBy(() -> {
174 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
175 }).hasMessage("dao is marked @NonNull but is null");
177 assertThatThrownBy(() -> {
178 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
179 }).hasMessage("filter is marked @NonNull but is null");
181 assertThatThrownBy(() -> {
182 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
183 }).hasMessage("dao is marked @NonNull but is null");
185 assertThatThrownBy(() -> {
186 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
187 }).hasMessage("dao is marked @NonNull but is null");
189 assertThatThrownBy(() -> {
190 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
191 }).hasMessage("filter is marked @NonNull but is null");
193 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
195 assertNotNull(toscaServiceTemplate);
196 ToscaServiceTemplate createdServiceTemplate =
197 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
199 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
201 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
202 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
203 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
204 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
206 ToscaServiceTemplate gotServiceTemplate =
207 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
209 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
210 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
211 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
213 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
214 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
216 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
217 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
218 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
220 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
221 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build());
223 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
224 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
225 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
227 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
228 "onap.policies.optimization.AffinityPolicy", "0.0.0");
229 assertEquals(1, gotPolicyTypeList.size());
230 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
232 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
233 ToscaPolicyTypeFilter.builder().build());
234 assertEquals(2, gotPolicyTypeList.size());
235 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
237 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
238 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
239 assertEquals(1, gotPolicyTypeList.size());
240 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
242 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
243 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build());
244 assertEquals(1, gotPolicyTypeList.size());
245 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
247 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
248 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
249 assertEquals(1, gotPolicyTypeList.size());
250 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
254 public void testPolicyTypesCreate() throws Exception {
255 assertThatThrownBy(() -> {
256 new AuthorativeToscaProvider().createPolicyTypes(null, null);
257 }).hasMessage("dao is marked @NonNull but is null");
259 assertThatThrownBy(() -> {
260 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
261 }).hasMessage("dao is marked @NonNull but is null");
263 assertThatThrownBy(() -> {
264 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
265 }).hasMessage("serviceTemplate is marked @NonNull but is null");
267 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
268 assertThatThrownBy(() -> {
269 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
270 }).hasMessage("no policy types specified on service template");
272 testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
273 assertThatThrownBy(() -> {
274 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
275 }).hasMessage("An incoming list of concepts must have at least one entry");
277 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
279 assertNotNull(toscaServiceTemplate);
280 ToscaServiceTemplate createdServiceTemplate =
281 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
283 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
285 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
286 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
287 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
288 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
292 public void testPolicyTypesUpdate() throws Exception {
293 assertThatThrownBy(() -> {
294 new AuthorativeToscaProvider().createPolicyTypes(null, null);
295 }).hasMessage("dao is marked @NonNull but is null");
297 assertThatThrownBy(() -> {
298 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
299 }).hasMessage("dao is marked @NonNull but is null");
301 assertThatThrownBy(() -> {
302 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
303 }).hasMessage("dao is marked @NonNull but is null");
305 assertThatThrownBy(() -> {
306 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
307 }).hasMessage("serviceTemplate is marked @NonNull but is null");
309 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
311 assertNotNull(toscaServiceTemplate);
312 ToscaServiceTemplate createdServiceTemplate =
313 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
315 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
317 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
318 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
319 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
320 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
322 ToscaServiceTemplate updatedServiceTemplate =
323 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
325 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
326 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
327 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
331 public void testPolicyTypesDelete() throws Exception {
332 assertThatThrownBy(() -> {
333 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
334 }).hasMessage("dao is marked @NonNull but is null");
336 assertThatThrownBy(() -> {
337 new AuthorativeToscaProvider().deletePolicyType(null, null, "version");
338 }).hasMessage("dao is marked @NonNull but is null");
340 assertThatThrownBy(() -> {
341 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
342 }).hasMessage("dao is marked @NonNull but is null");
344 assertThatThrownBy(() -> {
345 new AuthorativeToscaProvider().deletePolicyType(null, "name", "version");
346 }).hasMessage("dao is marked @NonNull but is null");
348 assertThatThrownBy(() -> {
349 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
350 }).hasMessage("name is marked @NonNull but is null");
352 assertThatThrownBy(() -> {
353 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version");
354 }).hasMessage("name is marked @NonNull but is null");
356 assertThatThrownBy(() -> {
357 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
358 }).hasMessage("version is marked @NonNull but is null");
360 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
362 assertNotNull(toscaServiceTemplate);
363 ToscaServiceTemplate createdServiceTemplate =
364 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
366 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
368 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
369 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
370 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
371 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
373 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
374 policyTypeKey.getName(), policyTypeKey.getVersion());
376 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
377 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
378 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
380 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
381 policyTypeKey.getName(), policyTypeKey.getVersion());
383 assertEquals(0, gotServiceTemplate.getPolicyTypes().get(0).size());
387 public void testAssertPoliciesExist() throws PfModelException {
388 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
390 assertThatThrownBy(() -> {
391 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
392 }).hasMessage("version is marked @NonNull but is null");
394 assertThatThrownBy(() -> {
395 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
396 }).hasMessage("no policy types specified on service template");
398 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
399 assertThatThrownBy(() -> {
400 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
401 }).hasMessage("no policy types specified on service template");
403 testServiceTemplate.setPolicyTypes(new ArrayList<>());
404 assertThatThrownBy(() -> {
405 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
406 }).hasMessage("An incoming list of concepts must have at least one entry");