2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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;
29 import java.util.List;
30 import java.util.Properties;
31 import org.apache.commons.lang3.ObjectUtils;
32 import org.eclipse.persistence.config.PersistenceUnitProperties;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.dao.DaoParameters;
41 import org.onap.policy.models.dao.PfDao;
42 import org.onap.policy.models.dao.PfDaoFactory;
43 import org.onap.policy.models.dao.impl.DefaultPfDao;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.yaml.snakeyaml.Yaml;
49 * Test of the {@link AuthorativeToscaProvider} class.
51 * @author Liam Fallon (liam.fallon@est.tech)
53 public class AuthorativeToscaProviderGenericTest {
54 private static final String POLICY_NO_VERSION_VERSION1 = "onap.policies.NoVersion:0.0.1";
55 private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
56 private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
57 private static final String VERSION_001 = "0.0.1";
58 private static String yamlAsJsonString;
60 private StandardCoder standardCoder;
63 * Read the policy type definition.
65 * @throws Exception on errors
68 public static void readPolicyDefinition() {
69 String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
71 Object yamlObject = new Yaml().load(yamlString);
72 yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
76 * Set up the DAO towards the database.
78 * @throws Exception on database errors
81 public void setupDao() throws Exception {
82 final DaoParameters daoParameters = new DaoParameters();
83 daoParameters.setPluginClass(DefaultPfDao.class.getName());
85 daoParameters.setPersistenceUnit("ToscaConceptTest");
87 Properties jdbcProperties = new Properties();
88 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
89 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
91 // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
92 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
93 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
95 daoParameters.setJdbcProperties(jdbcProperties);
97 pfDao = new PfDaoFactory().createPfDao(daoParameters);
98 pfDao.init(daoParameters);
105 public void setupGson() {
106 standardCoder = new StandardCoder();
110 public void teardown() {
115 public void testCreateGetDelete() throws Exception {
116 assertThatThrownBy(() -> {
117 new AuthorativeToscaProvider().getServiceTemplateList(null, null, null);
118 }).hasMessageMatching(DAO_IS_NULL);
120 ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
122 assertNotNull(toscaServiceTemplate);
123 ToscaServiceTemplate createdServiceTemplate =
124 new AuthorativeToscaProvider().createServiceTemplate(pfDao, toscaServiceTemplate);
126 PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
128 ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
129 ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
130 assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
131 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
133 List<ToscaServiceTemplate> gotServiceTemplateList =
134 new AuthorativeToscaProvider().getServiceTemplateList(pfDao, null, null);
136 ToscaPolicyType gotPolicyType = gotServiceTemplateList.get(0).getPolicyTypes().get(policyTypeKey.getName());
137 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
138 assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
140 List<ToscaPolicyType> gotPolicyTypeList =
141 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
142 assertEquals(2, gotPolicyTypeList.size());
143 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
145 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
146 assertEquals(2, gotPolicyTypeList.size());
147 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
149 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
150 assertEquals(2, gotPolicyTypeList.size());
151 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
153 gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
154 assertEquals(2, gotPolicyTypeList.size());
155 assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
157 assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
158 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
160 assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
162 ToscaServiceTemplate deletedServiceTemplate =
163 new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", "0.0.1");
164 assertEquals(2, deletedServiceTemplate.getPolicyTypes().size());
168 public void testNullParameters() throws Exception {
169 assertThatThrownBy(() -> new AuthorativeToscaProvider().getServiceTemplateList(null, null, null))
170 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
172 assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, null))
173 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
175 assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(pfDao, null))
176 .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
178 assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, new ToscaServiceTemplate()))
179 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
181 assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, null))
182 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
184 assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, "0.0.1"))
185 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
187 assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", null))
188 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
190 assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", "0.0.1"))
191 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
193 assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, null))
194 .hasMessageMatching("^name is marked .*on.*ull but is null$");
196 assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, "0.0.1"))
197 .hasMessageMatching("^name is marked .*on.*ull but is null$");
199 assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", null))
200 .hasMessageMatching("^version is marked .*on.*ull but is null$");