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;
26 import static org.junit.Assert.assertTrue;
28 import com.google.gson.GsonBuilder;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Properties;
34 import org.apache.commons.lang3.ObjectUtils;
35 import org.eclipse.persistence.config.PersistenceUnitProperties;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.base.PfModelException;
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 String yamlAsJsonString;
62 private StandardCoder standardCoder;
66 * Read the policy type definition.
68 * @throws Exception on errors
71 public static void readPolicyDefinition() {
73 ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.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.getCanonicalName());
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() throws Exception {
119 public void testPolicyTypesGet() throws Exception {
120 assertThatThrownBy(() -> {
121 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
122 }).hasMessage("dao is marked @NonNull but is null");
124 assertThatThrownBy(() -> {
125 new AuthorativeToscaProvider().getPolicyList(null, null, null);
126 }).hasMessage("dao is marked @NonNull but 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("onap.policies.optimization.AffinityPolicy:0.0.0");
136 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
137 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).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(0).get(policyTypeKey.getName());
145 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
146 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
148 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
149 "onap.policies.optimization.AffinityPolicy", "0.0.0");
150 assertEquals(1, gotPolicyTypeList.size());
151 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
153 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
154 "onap.policies.optimization.AffinityPolicy", null);
155 assertEquals(1, gotPolicyTypeList.size());
156 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
158 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
159 assertEquals(2, gotPolicyTypeList.size());
160 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
162 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, "0.0.0");
163 assertEquals(2, gotPolicyTypeList.size());
164 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
169 public void testPolicyTypesGetFiltered() throws Exception {
170 assertThatThrownBy(() -> {
171 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
172 }).hasMessage("dao is marked @NonNull but is null");
174 assertThatThrownBy(() -> {
175 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
176 }).hasMessage("dao is marked @NonNull but is null");
178 assertThatThrownBy(() -> {
179 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
180 }).hasMessage("filter is marked @NonNull but is null");
182 assertThatThrownBy(() -> {
183 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
184 }).hasMessage("dao is marked @NonNull but is null");
186 assertThatThrownBy(() -> {
187 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
188 }).hasMessage("dao is marked @NonNull but is null");
190 assertThatThrownBy(() -> {
191 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
192 }).hasMessage("filter is marked @NonNull but is null");
194 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
196 assertNotNull(toscaServiceTemplate);
197 ToscaServiceTemplate createdServiceTemplate =
198 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
200 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
202 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
203 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
204 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
205 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
207 ToscaServiceTemplate gotServiceTemplate =
208 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
210 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
211 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
212 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
214 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
215 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
217 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
218 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
219 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
221 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
222 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build());
224 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
225 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
226 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
228 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
229 "onap.policies.optimization.AffinityPolicy", "0.0.0");
230 assertEquals(1, gotPolicyTypeList.size());
231 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
233 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
234 ToscaPolicyTypeFilter.builder().build());
235 assertEquals(2, gotPolicyTypeList.size());
236 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
238 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
239 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
240 assertEquals(1, gotPolicyTypeList.size());
241 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
243 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
244 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build());
245 assertEquals(1, gotPolicyTypeList.size());
246 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
248 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
249 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
250 assertEquals(1, gotPolicyTypeList.size());
251 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
255 public void testPolicyTypesCreate() throws Exception {
256 assertThatThrownBy(() -> {
257 new AuthorativeToscaProvider().createPolicyTypes(null, null);
258 }).hasMessage("dao is marked @NonNull but is null");
260 assertThatThrownBy(() -> {
261 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
262 }).hasMessage("dao is marked @NonNull but is null");
264 assertThatThrownBy(() -> {
265 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
266 }).hasMessage("serviceTemplate is marked @NonNull but is null");
268 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
269 assertThatThrownBy(() -> {
270 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
271 }).hasMessage("no policy types specified on service template");
273 testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
274 assertThatThrownBy(() -> {
275 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
276 }).hasMessage("An incoming list of concepts must have at least one entry");
278 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
280 assertNotNull(toscaServiceTemplate);
281 ToscaServiceTemplate createdServiceTemplate =
282 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
284 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
286 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
287 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
288 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
289 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
293 public void testPolicyTypesUpdate() throws Exception {
294 assertThatThrownBy(() -> {
295 new AuthorativeToscaProvider().createPolicyTypes(null, null);
296 }).hasMessage("dao is marked @NonNull but is null");
298 assertThatThrownBy(() -> {
299 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
300 }).hasMessage("dao is marked @NonNull but is null");
302 assertThatThrownBy(() -> {
303 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
304 }).hasMessage("dao is marked @NonNull but is null");
306 assertThatThrownBy(() -> {
307 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
308 }).hasMessage("serviceTemplate is marked @NonNull but is null");
310 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
312 assertNotNull(toscaServiceTemplate);
313 ToscaServiceTemplate createdServiceTemplate =
314 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
316 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
318 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
319 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
320 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
321 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
323 ToscaServiceTemplate updatedServiceTemplate =
324 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
326 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
327 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
328 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
332 public void testPolicyTypesDelete() throws Exception {
333 assertThatThrownBy(() -> {
334 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
335 }).hasMessage("dao is marked @NonNull but is null");
337 assertThatThrownBy(() -> {
338 new AuthorativeToscaProvider().deletePolicyType(null, null, "version");
339 }).hasMessage("dao is marked @NonNull but is null");
341 assertThatThrownBy(() -> {
342 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
343 }).hasMessage("dao is marked @NonNull but is null");
345 assertThatThrownBy(() -> {
346 new AuthorativeToscaProvider().deletePolicyType(null, "name", "version");
347 }).hasMessage("dao is marked @NonNull but is null");
349 assertThatThrownBy(() -> {
350 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
351 }).hasMessage("name is marked @NonNull but is null");
353 assertThatThrownBy(() -> {
354 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version");
355 }).hasMessage("name is marked @NonNull but is null");
357 assertThatThrownBy(() -> {
358 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
359 }).hasMessage("version is marked @NonNull but is null");
361 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
363 assertNotNull(toscaServiceTemplate);
364 ToscaServiceTemplate createdServiceTemplate =
365 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
367 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
369 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
370 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
371 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
372 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
374 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
375 policyTypeKey.getName(), policyTypeKey.getVersion());
377 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
378 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
379 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
381 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
382 policyTypeKey.getName(), policyTypeKey.getVersion());
384 assertTrue(gotServiceTemplate.getPolicyTypes().isEmpty());
388 public void testAssertPoliciesExist() throws PfModelException {
389 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
391 assertThatThrownBy(() -> {
392 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
393 }).hasMessage("version is marked @NonNull but is null");
395 assertThatThrownBy(() -> {
396 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
397 }).hasMessage("no policy types specified on service template");
399 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
400 assertThatThrownBy(() -> {
401 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
402 }).hasMessage("no policy types specified on service template");
404 testServiceTemplate.setPolicyTypes(new ArrayList<>());
405 assertThatThrownBy(() -> {
406 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
407 }).hasMessage("An incoming list of concepts must have at least one entry");