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.junit.After;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfModelException;
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 String yamlAsJsonString;
60 private StandardCoder standardCoder;
64 * Read the policy type definition.
66 * @throws Exception on errors
69 public static void readPolicyDefinition() {
71 ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.yaml");
73 Object yamlObject = new Yaml().load(yamlString);
74 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
78 * Set up the DAO towards the database.
80 * @throws Exception on database errors
83 public void setupDao() throws Exception {
84 final DaoParameters daoParameters = new DaoParameters();
85 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
87 daoParameters.setPersistenceUnit("ToscaConceptTest");
89 Properties jdbcProperties = new Properties();
90 jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
91 jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
94 jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
95 jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:h2:mem:testdb");
98 //jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
99 //jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
101 daoParameters.setJdbcProperties(jdbcProperties );
103 pfDao = new PfDaoFactory().createPfDao(daoParameters);
104 pfDao.init(daoParameters);
111 public void setupGson() {
112 standardCoder = new StandardCoder();
116 public void teardown() throws Exception {
121 public void testPolicyTypesGet() throws Exception {
122 assertThatThrownBy(() -> {
123 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
124 }).hasMessage("dao is marked @NonNull but is null");
126 assertThatThrownBy(() -> {
127 new AuthorativeToscaProvider().getPolicyList(null, null, null);
128 }).hasMessage("dao is marked @NonNull but is null");
130 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
132 assertNotNull(toscaServiceTemplate);
133 ToscaServiceTemplate createdServiceTemplate =
134 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
136 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
138 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
139 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
140 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
141 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
143 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
144 policyTypeKey.getName(), policyTypeKey.getVersion());
146 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
147 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
148 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
150 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
151 "onap.policies.optimization.AffinityPolicy", "0.0.0");
152 assertEquals(1, gotPolicyTypeList.size());
153 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
155 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
156 "onap.policies.optimization.AffinityPolicy", null);
157 assertEquals(1, gotPolicyTypeList.size());
158 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
160 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
161 assertEquals(2, gotPolicyTypeList.size());
162 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
164 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, "0.0.0");
165 assertEquals(2, gotPolicyTypeList.size());
166 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
171 public void testPolicyTypesGetFiltered() throws Exception {
172 assertThatThrownBy(() -> {
173 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
174 }).hasMessage("dao is marked @NonNull but is null");
176 assertThatThrownBy(() -> {
177 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
178 }).hasMessage("dao is marked @NonNull but is null");
180 assertThatThrownBy(() -> {
181 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
182 }).hasMessage("filter is marked @NonNull but is null");
184 assertThatThrownBy(() -> {
185 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
186 }).hasMessage("dao is marked @NonNull but is null");
188 assertThatThrownBy(() -> {
189 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
190 }).hasMessage("dao is marked @NonNull but is null");
192 assertThatThrownBy(() -> {
193 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
194 }).hasMessage("filter is marked @NonNull 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("onap.policies.optimization.AffinityPolicy:0.0.0");
204 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
205 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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(0).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(0).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("0.0.0").build());
226 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
227 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
228 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
230 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
231 "onap.policies.optimization.AffinityPolicy", "0.0.0");
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(2, 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("0.0.0").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(1, 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 }).hasMessage("dao is marked @NonNull but is null");
262 assertThatThrownBy(() -> {
263 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
264 }).hasMessage("dao is marked @NonNull but is null");
266 assertThatThrownBy(() -> {
267 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
268 }).hasMessage("serviceTemplate is marked @NonNull but is null");
270 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
271 assertThatThrownBy(() -> {
272 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
273 }).hasMessage("no policy types specified on service template");
275 testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
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("onap.policies.optimization.AffinityPolicy:0.0.0");
288 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
289 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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 }).hasMessage("dao is marked @NonNull but is null");
300 assertThatThrownBy(() -> {
301 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
302 }).hasMessage("dao is marked @NonNull but is null");
304 assertThatThrownBy(() -> {
305 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
306 }).hasMessage("dao is marked @NonNull but is null");
308 assertThatThrownBy(() -> {
309 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
310 }).hasMessage("serviceTemplate is marked @NonNull 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("onap.policies.optimization.AffinityPolicy:0.0.0");
320 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
321 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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(0).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 }).hasMessage("dao is marked @NonNull but is null");
339 assertThatThrownBy(() -> {
340 new AuthorativeToscaProvider().deletePolicyType(null, null, "version");
341 }).hasMessage("dao is marked @NonNull but is null");
343 assertThatThrownBy(() -> {
344 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
345 }).hasMessage("dao is marked @NonNull but is null");
347 assertThatThrownBy(() -> {
348 new AuthorativeToscaProvider().deletePolicyType(null, "name", "version");
349 }).hasMessage("dao is marked @NonNull but is null");
351 assertThatThrownBy(() -> {
352 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
353 }).hasMessage("name is marked @NonNull but is null");
355 assertThatThrownBy(() -> {
356 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version");
357 }).hasMessage("name is marked @NonNull but is null");
359 assertThatThrownBy(() -> {
360 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
361 }).hasMessage("version is marked @NonNull 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("onap.policies.optimization.AffinityPolicy:0.0.0");
371 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
372 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).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(0).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 assertEquals(0, gotServiceTemplate.getPolicyTypes().get(0).size());
390 public void testAssertPoliciesExist() throws PfModelException {
391 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
393 assertThatThrownBy(() -> {
394 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
395 }).hasMessage("version is marked @NonNull but is null");
397 assertThatThrownBy(() -> {
398 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
399 }).hasMessage("no policy types specified on service template");
401 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
402 assertThatThrownBy(() -> {
403 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
404 }).hasMessage("no policy types specified on service template");
406 testServiceTemplate.setPolicyTypes(new ArrayList<>());
407 assertThatThrownBy(() -> {
408 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
409 }).hasMessage("An incoming list of concepts must have at least one entry");