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