6f6d4351559edad30178dea315804df4e00fba6d
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2020-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.pdp.persistence.provider;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27
28 import java.time.Instant;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32 import org.eclipse.persistence.config.PersistenceUnitProperties;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.models.base.Validated;
37 import org.onap.policy.models.dao.DaoParameters;
38 import org.onap.policy.models.dao.PfDao;
39 import org.onap.policy.models.dao.PfDaoFactory;
40 import org.onap.policy.models.dao.impl.DefaultPfDao;
41 import org.onap.policy.models.pdp.concepts.PdpEngineWorkerStatistics;
42 import org.onap.policy.models.pdp.concepts.PdpStatistics;
43
44 public class PdpStatisticsProviderTest {
45     private static final String DAO_IS_NULL = "dao is marked .*ull but is null";
46     private static final String LIST_IS_NULL = "pdpStatisticsList is marked .*ull but is null";
47     private static final String GROUP0 = "group0";
48     private static final String NAME = "name";
49     private static final String GROUP = "group";
50     private static final String SUBGROUP = "subgroup";
51     private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1078884319);
52     private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1078884350);
53     private static final Long GENERATEDID1 = 1L;
54     private static final Long GENERATEDID2 = 2L;
55
56     private PfDao pfDao;
57
58     private ArrayList<PdpStatistics> pdpStatisticsTestList = new ArrayList<>();
59     private List<PdpEngineWorkerStatistics> engineStats = new ArrayList<>();
60     private String testListStr;
61     private String name1ListStr;
62     private String createdListStr;
63
64     /**
65      * Set up test Dao.
66      */
67     @Before
68     public void setupDao() throws Exception {
69         final DaoParameters daoParameters = new DaoParameters();
70         daoParameters.setPluginClass(DefaultPfDao.class.getName());
71
72         daoParameters.setPersistenceUnit("ToscaConceptTest");
73
74         Properties jdbcProperties = new Properties();
75         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
76         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
77
78         if (System.getProperty("USE-MARIADB") != null) {
79             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
80             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
81         } else {
82             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
83             jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:PdpStatisticsProviderTest");
84         }
85
86         daoParameters.setJdbcProperties(jdbcProperties);
87
88         pfDao = new PfDaoFactory().createPfDao(daoParameters);
89         pfDao.init(daoParameters);
90
91         PdpStatistics pdpStatistics = new PdpStatistics();
92         pdpStatistics.setPdpInstanceId(NAME);
93         pdpStatistics.setTimeStamp(TIMESTAMP1);
94         pdpStatistics.setGeneratedId(GENERATEDID1);
95         pdpStatistics.setPdpGroupName(GROUP);
96         pdpStatistics.setPdpSubGroupName(SUBGROUP);
97         pdpStatistics.setPolicyDeployCount(2);
98         pdpStatistics.setPolicyDeployFailCount(1);
99         pdpStatistics.setPolicyDeploySuccessCount(1);
100         pdpStatistics.setPolicyExecutedCount(2);
101         pdpStatistics.setPolicyExecutedFailCount(1);
102         pdpStatistics.setPolicyExecutedSuccessCount(1);
103         pdpStatistics.setEngineStats(engineStats);
104         pdpStatisticsTestList.add(pdpStatistics);
105         name1ListStr = pdpStatisticsTestList.toString();
106
107         PdpStatistics pdpStatistics2 = new PdpStatistics();
108         pdpStatistics2.setPdpInstanceId("name2");
109         pdpStatistics2.setTimeStamp(TIMESTAMP2);
110         pdpStatistics2.setGeneratedId(GENERATEDID2);
111         pdpStatistics2.setPdpGroupName(GROUP);
112         pdpStatistics2.setPdpSubGroupName(SUBGROUP);
113         pdpStatistics2.setPolicyDeployCount(2);
114         pdpStatistics2.setPolicyDeployFailCount(1);
115         pdpStatistics2.setPolicyDeploySuccessCount(1);
116         pdpStatistics2.setPolicyExecutedCount(2);
117         pdpStatistics2.setPolicyExecutedFailCount(1);
118         pdpStatistics2.setPolicyExecutedSuccessCount(1);
119         pdpStatistics2.setEngineStats(engineStats);
120         pdpStatisticsTestList.add(pdpStatistics2);
121         testListStr = pdpStatisticsTestList.toString();
122
123         List<PdpStatistics> createdPdpStatisticsList;
124         createdPdpStatisticsList = new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsTestList);
125         createdListStr = createdPdpStatisticsList.toString();
126         assertEquals(createdListStr.replaceAll("\\s+", ""), testListStr.replaceAll("\\s+", ""));
127     }
128
129     @After
130     public void teardown() {
131         pfDao.close();
132     }
133
134     @Test
135     public void testNotOkPdpStatistics() {
136         PdpStatistics pdpStatisticsErr = new PdpStatistics();
137         pdpStatisticsErr.setPdpInstanceId("NULL");
138         pdpStatisticsErr.setPdpGroupName(GROUP);
139         ArrayList<PdpStatistics> pdpStatisticsNullList = new ArrayList<>();
140         pdpStatisticsNullList.add(pdpStatisticsErr);
141
142         assertThatThrownBy(() -> {
143             new PdpStatisticsProvider().createPdpStatistics(pfDao, null);
144         }).hasMessageMatching(LIST_IS_NULL);
145
146         assertThatThrownBy(() -> {
147             new PdpStatisticsProvider().updatePdpStatistics(pfDao, null);
148         }).hasMessageMatching(LIST_IS_NULL);
149
150         assertThatThrownBy(() -> {
151             new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsNullList);
152         }).hasMessageContaining("pdp statistics").hasMessageContaining("key")
153                         .hasMessageContaining(Validated.IS_A_NULL_KEY);
154
155         assertThatThrownBy(() -> {
156             new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsNullList);
157         }).hasMessageContaining("pdp statistics").hasMessageContaining("key")
158                         .hasMessageContaining(Validated.IS_A_NULL_KEY);
159     }
160
161     @Test
162     public void testGetPdpStatistics() throws Exception {
163         assertThatThrownBy(() -> {
164             new PdpStatisticsProvider().createPdpStatistics(null, null);
165         }).hasMessageMatching(DAO_IS_NULL);
166         assertThatThrownBy(() -> {
167             new PdpStatisticsProvider().getPdpStatistics(null, null, null);
168         }).hasMessageMatching(DAO_IS_NULL);
169
170         List<PdpStatistics> getPdpStatisticsList;
171         getPdpStatisticsList = new PdpStatisticsProvider().getPdpStatistics(pfDao, NAME, TIMESTAMP1);
172         assertThat(getPdpStatisticsList).hasSize(1);
173         String gotListStr = getPdpStatisticsList.toString();
174         assertEquals(name1ListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", ""));
175
176         // name is null
177         getPdpStatisticsList = new PdpStatisticsProvider().getPdpStatistics(pfDao, null, TIMESTAMP1);
178         gotListStr = getPdpStatisticsList.toString();
179         assertEquals(testListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", ""));
180     }
181
182     @Test
183     public void testGetFilteredPdpStatistics() throws Exception {
184
185         assertThatThrownBy(() -> {
186             new PdpStatisticsProvider().getFilteredPdpStatistics(null, PdpFilterParameters.builder().build());
187         }).hasMessageMatching(DAO_IS_NULL);
188
189
190         List<PdpStatistics> createdPdpStatisticsList;
191         createdPdpStatisticsList = new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsTestList);
192         createdListStr = createdPdpStatisticsList.toString();
193         assertEquals(createdListStr.replaceAll("\\s+", ""), testListStr.replaceAll("\\s+", ""));
194
195         List<PdpStatistics> getPdpStatisticsList;
196         getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, PdpFilterParameters
197                         .builder().name(NAME).group(GROUP).startTime(TIMESTAMP1).endTime(TIMESTAMP2).build());
198         assertThat(getPdpStatisticsList).hasSize(1);
199         getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, PdpFilterParameters
200                         .builder().name("name2").group(GROUP).startTime(TIMESTAMP1).endTime(TIMESTAMP2).build());
201         assertThat(getPdpStatisticsList).hasSize(1);
202         getPdpStatisticsList = new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao,
203                         PdpFilterParameters.builder().name("name2").group(GROUP).subGroup(SUBGROUP)
204                                         .startTime(TIMESTAMP1).endTime(TIMESTAMP2).build());
205         assertThat(getPdpStatisticsList).hasSize(1);
206     }
207
208     @Test
209     public void testUpdatePdpStatistics() throws Exception {
210         assertThatThrownBy(() -> {
211             new PdpStatisticsProvider().updatePdpStatistics(null, null);
212         }).hasMessageMatching(DAO_IS_NULL);
213
214         pdpStatisticsTestList.get(0).setPdpGroupName(GROUP0);
215         testListStr = pdpStatisticsTestList.toString();
216         List<PdpStatistics> updatePdpStatisticsList =
217                 new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsTestList);
218         String gotListStr = updatePdpStatisticsList.toString();
219         assertEquals(testListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", ""));
220     }
221
222     @Test
223     public void testDeletePdpStatistics() throws Exception {
224         assertThatThrownBy(() -> {
225             new PdpStatisticsProvider().deletePdpStatistics(null, null, null);
226         }).hasMessageMatching(DAO_IS_NULL);
227
228         assertThatThrownBy(() -> {
229             new PdpStatisticsProvider().deletePdpStatistics(pfDao, null, null);
230         }).hasMessageMatching("name is marked .*ull but is null");
231
232         List<PdpStatistics> deletedPdpStatisticsList =
233                 new PdpStatisticsProvider().deletePdpStatistics(pfDao, NAME, null);
234         String gotListStr = deletedPdpStatisticsList.toString();
235         assertEquals(name1ListStr.replaceAll("\\s+", ""), gotListStr.replaceAll("\\s+", ""));
236     }
237
238 }