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.sql.Connection;
30 import java.sql.DriverManager;
31 import java.util.ArrayList;
32 import java.util.List;
34 import org.apache.commons.lang3.ObjectUtils;
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;
60 private Connection connection;
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 // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
87 // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
88 connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
90 final DaoParameters daoParameters = new DaoParameters();
91 daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
93 // Use the persistence unit ToscaConceptTest to test towards the h2 database
94 // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
95 daoParameters.setPersistenceUnit("ToscaConceptTest");
97 pfDao = new PfDaoFactory().createPfDao(daoParameters);
98 pfDao.init(daoParameters);
105 public void setupGson() {
106 standardCoder = new StandardCoder();
110 public void teardown() throws Exception {
116 public void testPolicyTypesGet() throws Exception {
117 assertThatThrownBy(() -> {
118 new AuthorativeToscaProvider().getPolicyTypes(null, null, null);
119 }).hasMessage("dao is marked @NonNull but is null");
121 assertThatThrownBy(() -> {
122 new AuthorativeToscaProvider().getPolicyList(null, null, null);
123 }).hasMessage("dao is marked @NonNull but is null");
125 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
127 assertNotNull(toscaServiceTemplate);
128 ToscaServiceTemplate createdServiceTemplate =
129 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
131 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
133 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
134 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
135 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
136 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
138 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
139 policyTypeKey.getName(), policyTypeKey.getVersion());
141 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
142 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
143 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
145 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
146 "onap.policies.optimization.AffinityPolicy", "0.0.0");
147 assertEquals(1, gotPolicyTypeList.size());
148 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
150 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
151 "onap.policies.optimization.AffinityPolicy", null);
152 assertEquals(1, gotPolicyTypeList.size());
153 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
155 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
156 assertEquals(2, gotPolicyTypeList.size());
157 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
159 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, "0.0.0");
160 assertEquals(2, gotPolicyTypeList.size());
161 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
166 public void testPolicyTypesGetFiltered() throws Exception {
167 assertThatThrownBy(() -> {
168 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null);
169 }).hasMessage("dao is marked @NonNull but is null");
171 assertThatThrownBy(() -> {
172 new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
173 }).hasMessage("dao is marked @NonNull but is null");
175 assertThatThrownBy(() -> {
176 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null);
177 }).hasMessage("filter is marked @NonNull but is null");
179 assertThatThrownBy(() -> {
180 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null);
181 }).hasMessage("dao is marked @NonNull but is null");
183 assertThatThrownBy(() -> {
184 new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
185 }).hasMessage("dao is marked @NonNull but is null");
187 assertThatThrownBy(() -> {
188 new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null);
189 }).hasMessage("filter is marked @NonNull but is null");
191 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
193 assertNotNull(toscaServiceTemplate);
194 ToscaServiceTemplate createdServiceTemplate =
195 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
197 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
199 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
200 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
201 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
202 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
204 ToscaServiceTemplate gotServiceTemplate =
205 new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
207 ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
208 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
209 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
211 gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
212 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
214 gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).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()).version("0.0.0").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 List<ToscaPolicyType> gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao,
226 "onap.policies.optimization.AffinityPolicy", "0.0.0");
227 assertEquals(1, gotPolicyTypeList.size());
228 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
230 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
231 ToscaPolicyTypeFilter.builder().build());
232 assertEquals(2, gotPolicyTypeList.size());
233 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
235 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
236 ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).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()).version("0.0.0").build());
242 assertEquals(1, gotPolicyTypeList.size());
243 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
245 gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
246 ToscaPolicyTypeFilter.builder().version("1.0.0").build());
247 assertEquals(1, gotPolicyTypeList.size());
248 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
252 public void testPolicyTypesCreate() throws Exception {
253 assertThatThrownBy(() -> {
254 new AuthorativeToscaProvider().createPolicyTypes(null, null);
255 }).hasMessage("dao is marked @NonNull but is null");
257 assertThatThrownBy(() -> {
258 new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate());
259 }).hasMessage("dao is marked @NonNull but is null");
261 assertThatThrownBy(() -> {
262 new AuthorativeToscaProvider().createPolicyTypes(pfDao, null);
263 }).hasMessage("serviceTemplate is marked @NonNull but is null");
265 ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate();
266 assertThatThrownBy(() -> {
267 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
268 }).hasMessage("no policy types specified on service template");
270 testToscaServiceTemplate.setPolicyTypes(new ArrayList<>());
271 assertThatThrownBy(() -> {
272 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate);
273 }).hasMessage("An incoming list of concepts must have at least one entry");
275 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
277 assertNotNull(toscaServiceTemplate);
278 ToscaServiceTemplate createdServiceTemplate =
279 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
281 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
283 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
284 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
285 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
286 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
290 public void testPolicyTypesUpdate() throws Exception {
291 assertThatThrownBy(() -> {
292 new AuthorativeToscaProvider().createPolicyTypes(null, null);
293 }).hasMessage("dao is marked @NonNull but is null");
295 assertThatThrownBy(() -> {
296 new AuthorativeToscaProvider().updatePolicyTypes(null, null);
297 }).hasMessage("dao is marked @NonNull but is null");
299 assertThatThrownBy(() -> {
300 new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate());
301 }).hasMessage("dao is marked @NonNull but is null");
303 assertThatThrownBy(() -> {
304 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null);
305 }).hasMessage("serviceTemplate is marked @NonNull but is null");
307 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
309 assertNotNull(toscaServiceTemplate);
310 ToscaServiceTemplate createdServiceTemplate =
311 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
313 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
315 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
316 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
317 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
318 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
320 ToscaServiceTemplate updatedServiceTemplate =
321 new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
323 ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
324 assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
325 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription()));
329 public void testPolicyTypesDelete() throws Exception {
330 assertThatThrownBy(() -> {
331 new AuthorativeToscaProvider().deletePolicyType(null, null, null);
332 }).hasMessage("dao is marked @NonNull but is null");
334 assertThatThrownBy(() -> {
335 new AuthorativeToscaProvider().deletePolicyType(null, null, "version");
336 }).hasMessage("dao is marked @NonNull but is null");
338 assertThatThrownBy(() -> {
339 new AuthorativeToscaProvider().deletePolicyType(null, "name", null);
340 }).hasMessage("dao is marked @NonNull but is null");
342 assertThatThrownBy(() -> {
343 new AuthorativeToscaProvider().deletePolicyType(null, "name", "version");
344 }).hasMessage("dao is marked @NonNull but is null");
346 assertThatThrownBy(() -> {
347 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null);
348 }).hasMessage("name is marked @NonNull but is null");
350 assertThatThrownBy(() -> {
351 new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version");
352 }).hasMessage("name is marked @NonNull but is null");
354 assertThatThrownBy(() -> {
355 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
356 }).hasMessage("version is marked @NonNull but is null");
358 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
360 assertNotNull(toscaServiceTemplate);
361 ToscaServiceTemplate createdServiceTemplate =
362 new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
364 PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0");
366 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName());
367 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
368 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
369 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
371 ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
372 policyTypeKey.getName(), policyTypeKey.getVersion());
374 ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName());
375 assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
376 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription()));
378 ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
379 policyTypeKey.getName(), policyTypeKey.getVersion());
381 assertEquals(0, gotServiceTemplate.getPolicyTypes().get(0).size());
385 public void testAssertPoliciesExist() throws PfModelException {
386 ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
388 assertThatThrownBy(() -> {
389 new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null);
390 }).hasMessage("version is marked @NonNull but is null");
392 assertThatThrownBy(() -> {
393 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
394 }).hasMessage("no policy types specified on service template");
396 testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate());
397 assertThatThrownBy(() -> {
398 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
399 }).hasMessage("no policy types specified on service template");
401 testServiceTemplate.setPolicyTypes(new ArrayList<>());
402 assertThatThrownBy(() -> {
403 new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
404 }).hasMessage("An incoming list of concepts must have at least one entry");