1d2b9913afb191bd326305b369d3c05ecbcfd653
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26
27 import java.time.Instant;
28 import java.util.List;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40
41 public class ClElementStatisticsProviderTest {
42     private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
43     private static final Coder CODER = new StandardCoder();
44     private static final String CL_ELEMENT_STATS_JSON =
45         "src/test/resources/providers/TestClElementStatistics.json";
46
47     private static AtomicInteger dbNameCounter = new AtomicInteger();
48
49     private PolicyModelsProviderParameters parameters;
50     private ClElementStatisticsProvider clElementStatisticsProvider;
51     private ClElementStatisticsList inputClElementStats;
52     private String originalJson = ResourceUtils.getResourceAsString(CL_ELEMENT_STATS_JSON);
53
54     /**
55      * Set up test ClElement statistics provider.
56      */
57     @Before
58     public void setupDao() throws Exception {
59
60         parameters = new PolicyModelsProviderParameters();
61         parameters.setDatabaseDriver("org.h2.Driver");
62         parameters.setName("PolicyProviderParameterGroup");
63         parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
64         parameters.setDatabaseUrl("jdbc:h2:mem:clElementTestDb" + dbNameCounter.getAndIncrement());
65         parameters.setDatabaseUser("policy");
66         parameters.setDatabasePassword("P01icY");
67         parameters.setPersistenceUnit("ToscaConceptTest");
68
69         clElementStatisticsProvider = new ClElementStatisticsProvider(parameters);
70         inputClElementStats = CODER.decode(originalJson, ClElementStatisticsList.class);
71     }
72
73     @After
74     public void teardown() {
75         clElementStatisticsProvider.close();
76     }
77
78     @Test
79     public void testClElementStatisticsCreate() throws Exception {
80         assertThatThrownBy(() -> {
81             clElementStatisticsProvider.createClElementStatistics(null);
82         }).hasMessageMatching(LIST_IS_NULL);
83
84         ClElementStatisticsList createdClElementStats = new ClElementStatisticsList();
85         createdClElementStats.setClElementStatistics(clElementStatisticsProvider
86             .createClElementStatistics(inputClElementStats.getClElementStatistics()));
87
88         assertEquals(inputClElementStats.toString().replaceAll("\\s+", ""),
89             createdClElementStats.toString().replaceAll("\\s+", ""));
90     }
91
92     @Test
93     public void testGetClElementStatistics() throws Exception {
94
95         List<ClElementStatistics> getResponse;
96
97         //Return empty list when no data present in db
98         getResponse = clElementStatisticsProvider.getClElementStatistics(null, null, null);
99         assertThat(getResponse).isEmpty();
100
101         clElementStatisticsProvider.createClElementStatistics(inputClElementStats
102             .getClElementStatistics());
103         ToscaConceptIdentifier identifier = inputClElementStats.getClElementStatistics().get(0)
104             .getControlLoopElementId();
105         Instant instant = inputClElementStats.getClElementStatistics().get(0).getTimeStamp();
106         assertEquals(1, clElementStatisticsProvider.getClElementStatistics(identifier.getName(),
107             identifier.getVersion(), instant).size());
108
109         assertEquals(1, clElementStatisticsProvider.getFilteredClElementStatistics("name2",
110             "1.0.1", null, null, null,
111             "DESC", 1).size());
112     }
113 }