63ac44ae2297c1bb746159f8aca3b7d5757caef0
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / authorative / provider / AuthorativeToscaProviderGenericTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.authorative.provider;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import com.google.gson.GsonBuilder;
30 import java.util.List;
31 import java.util.Properties;
32 import org.apache.commons.lang3.ObjectUtils;
33 import org.eclipse.persistence.config.PersistenceUnitProperties;
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.dao.DaoParameters;
42 import org.onap.policy.models.dao.PfDao;
43 import org.onap.policy.models.dao.PfDaoFactory;
44 import org.onap.policy.models.dao.impl.DefaultPfDao;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.yaml.snakeyaml.Yaml;
48
49 /**
50  * Test of the {@link AuthorativeToscaProvider} class.
51  *
52  * @author Liam Fallon (liam.fallon@est.tech)
53  */
54 public class AuthorativeToscaProviderGenericTest {
55     private static final String POLICY_NO_VERSION_VERSION1 = "onap.policies.NoVersion:0.0.1";
56     private static final String POLICY_NO_VERSION = "onap.policies.NoVersion";
57     private static final String DAO_IS_NULL = "^dao is marked .*on.*ull but is null$";
58     private static final String VERSION_001 = "0.0.1";
59     private static String yamlAsJsonString;
60     private PfDao pfDao;
61     private StandardCoder standardCoder;
62
63     /**
64      * Read the policy type definition.
65      *
66      * @throws Exception on errors
67      */
68     @BeforeClass
69     public static void readPolicyDefinition() {
70         String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
71
72         Object yamlObject = new Yaml().load(yamlString);
73         yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
74     }
75
76     /**
77      * Set up the DAO towards the database.
78      *
79      * @throws Exception on database errors
80      */
81     @Before
82     public void setupDao() throws Exception {
83         final DaoParameters daoParameters = new DaoParameters();
84         daoParameters.setPluginClass(DefaultPfDao.class.getName());
85
86         daoParameters.setPersistenceUnit("ToscaConceptTest");
87
88         Properties jdbcProperties = new Properties();
89         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
90         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
91
92         if (System.getProperty("USE-MARIADB") != null) {
93             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
94             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
95         } else {
96             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
97             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL,
98                             "jdbc:h2:mem:AuthorativeToscaProviderGenericTest");
99         }
100
101         daoParameters.setJdbcProperties(jdbcProperties);
102
103         pfDao = new PfDaoFactory().createPfDao(daoParameters);
104         pfDao.init(daoParameters);
105     }
106
107     /**
108      * Set up GSON.
109      */
110     @Before
111     public void setupGson() {
112         standardCoder = new StandardCoder();
113     }
114
115     @After
116     public void teardown() {
117         pfDao.close();
118     }
119
120     @Test
121     public void testCreateGetDelete() throws Exception {
122         assertThatThrownBy(() -> {
123             new AuthorativeToscaProvider().getServiceTemplateList(null, null, null);
124         }).hasMessageMatching(DAO_IS_NULL);
125
126         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
127
128         assertNotNull(toscaServiceTemplate);
129         ToscaServiceTemplate createdServiceTemplate =
130                 new AuthorativeToscaProvider().createServiceTemplate(pfDao, toscaServiceTemplate);
131
132         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
133
134         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
135         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
136         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
137         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
138
139         List<ToscaServiceTemplate> gotServiceTemplateList =
140                 new AuthorativeToscaProvider().getServiceTemplateList(pfDao, null, null);
141
142         ToscaPolicyType gotPolicyType = gotServiceTemplateList.get(0).getPolicyTypes().get(policyTypeKey.getName());
143         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
144         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
145
146         List<ToscaPolicyType> gotPolicyTypeList =
147                 new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
148         assertEquals(2, gotPolicyTypeList.size());
149         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
150
151         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
152         assertEquals(2, gotPolicyTypeList.size());
153         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
154
155         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
156         assertEquals(2, gotPolicyTypeList.size());
157         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
158
159         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
160         assertEquals(2, gotPolicyTypeList.size());
161         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
162
163         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
164                 VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
165
166         assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
167
168         ToscaServiceTemplate deletedServiceTemplate =
169                 new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", "0.0.1");
170         assertEquals(2, deletedServiceTemplate.getPolicyTypes().size());
171     }
172
173     @Test
174     public void testNullParameters() throws Exception {
175         assertThatThrownBy(() -> new AuthorativeToscaProvider().getServiceTemplateList(null, null, null))
176                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
177
178         assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, null))
179                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
180
181         assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(pfDao, null))
182                 .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
183
184         assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, new ToscaServiceTemplate()))
185                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
186
187         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, null))
188                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
189
190         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, "0.0.1"))
191                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
192
193         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", null))
194                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
195
196         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", "0.0.1"))
197                 .hasMessageMatching("^dao is marked .*on.*ull but is null$");
198
199         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, null))
200                 .hasMessageMatching("^name is marked .*on.*ull but is null$");
201
202         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, "0.0.1"))
203                 .hasMessageMatching("^name is marked .*on.*ull but is null$");
204
205         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", null))
206                 .hasMessageMatching("^version is marked .*on.*ull but is null$");
207     }
208 }