Replace Eclipselink with Hibernate
[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,2023 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.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;
47
48 /**
49  * Test of the {@link AuthorativeToscaProvider} class.
50  *
51  * @author Liam Fallon (liam.fallon@est.tech)
52  */
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;
59     private PfDao pfDao;
60     private StandardCoder standardCoder;
61
62     /**
63      * Read the policy type definition.
64      *
65      * @throws Exception on errors
66      */
67     @BeforeClass
68     public static void readPolicyDefinition() {
69         String yamlString = ResourceUtils.getResourceAsString("src/test/resources/onap.policies.NoVersion.yaml");
70
71         Object yamlObject = new Yaml().load(yamlString);
72         yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject);
73     }
74
75     /**
76      * Set up the DAO towards the database.
77      *
78      * @throws Exception on database errors
79      */
80     @Before
81     public void setupDao() throws Exception {
82         final DaoParameters daoParameters = new DaoParameters();
83         daoParameters.setPluginClass(DefaultPfDao.class.getName());
84
85         daoParameters.setPersistenceUnit("ToscaConceptTest");
86
87         Properties jdbcProperties = new Properties();
88         jdbcProperties.setProperty("javax.persistence.jdbc.user", "policy");
89         jdbcProperties.setProperty("javax.persistence.jdbc.password", "P01icY");
90         if (System.getProperty("USE-MARIADB") != null) {
91             jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.mariadb.jdbc.Driver");
92             jdbcProperties.setProperty("javax.persistence.jdbc.url", "jdbc:mariadb://localhost:3306/policy");
93         } else {
94             jdbcProperties.setProperty("javax.persistence.jdbc.driver", "org.h2.Driver");
95             jdbcProperties.setProperty("javax.persistence.jdbc.url",
96                 "jdbc:h2:mem:AuthorativeToscaProviderGenericTest");
97         }
98         daoParameters.setJdbcProperties(jdbcProperties);
99
100         pfDao = new PfDaoFactory().createPfDao(daoParameters);
101         pfDao.init(daoParameters);
102     }
103
104     /**
105      * Set up GSON.
106      */
107     @Before
108     public void setupGson() {
109         standardCoder = new StandardCoder();
110     }
111
112     @After
113     public void teardown() {
114         pfDao.close();
115     }
116
117     @Test
118     public void testCreateGetDelete() throws Exception {
119         assertThatThrownBy(() -> {
120             new AuthorativeToscaProvider().getServiceTemplateList(null, null, null);
121         }).hasMessageMatching(DAO_IS_NULL);
122
123         ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
124
125         assertNotNull(toscaServiceTemplate);
126         ToscaServiceTemplate createdServiceTemplate =
127             new AuthorativeToscaProvider().createServiceTemplate(pfDao, toscaServiceTemplate);
128
129         PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
130
131         ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
132         ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
133         assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
134         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
135
136         List<ToscaServiceTemplate> gotServiceTemplateList =
137             new AuthorativeToscaProvider().getServiceTemplateList(pfDao, null, null);
138
139         ToscaPolicyType gotPolicyType = gotServiceTemplateList.get(0).getPolicyTypes().get(policyTypeKey.getName());
140         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
141         assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
142
143         List<ToscaPolicyType> gotPolicyTypeList =
144             new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
145         assertEquals(2, gotPolicyTypeList.size());
146         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
147
148         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, null);
149         assertEquals(2, gotPolicyTypeList.size());
150         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
151
152         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null);
153         assertEquals(2, gotPolicyTypeList.size());
154         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
155
156         gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, VERSION_001);
157         assertEquals(2, gotPolicyTypeList.size());
158         assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
159
160         assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
161             VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
162
163         assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
164
165         ToscaServiceTemplate deletedServiceTemplate =
166             new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", "0.0.1");
167         assertEquals(2, deletedServiceTemplate.getPolicyTypes().size());
168     }
169
170     @Test
171     public void testNullParameters() throws Exception {
172         assertThatThrownBy(() -> new AuthorativeToscaProvider().getServiceTemplateList(null, null, null))
173             .hasMessageMatching("^dao is marked .*on.*ull but is null$");
174
175         assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, null))
176             .hasMessageMatching("^dao is marked .*on.*ull but is null$");
177
178         assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(pfDao, null))
179             .hasMessageMatching("^serviceTemplate is marked .*on.*ull but is null$");
180
181         assertThatThrownBy(() -> new AuthorativeToscaProvider().createServiceTemplate(null, new ToscaServiceTemplate()))
182             .hasMessageMatching("^dao is marked .*on.*ull but is null$");
183
184         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, null))
185             .hasMessageMatching("^dao is marked .*on.*ull but is null$");
186
187         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, null, "0.0.1"))
188             .hasMessageMatching("^dao is marked .*on.*ull but is null$");
189
190         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", null))
191             .hasMessageMatching("^dao is marked .*on.*ull but is null$");
192
193         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(null, "Dummy", "0.0.1"))
194             .hasMessageMatching("^dao is marked .*on.*ull but is null$");
195
196         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, null))
197             .hasMessageMatching("^name is marked .*on.*ull but is null$");
198
199         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, null, "0.0.1"))
200             .hasMessageMatching("^name is marked .*on.*ull but is null$");
201
202         assertThatThrownBy(() -> new AuthorativeToscaProvider().deleteServiceTemplate(pfDao, "Dummy", null))
203             .hasMessageMatching("^version is marked .*on.*ull but is null$");
204     }
205 }