eb6a02898e3314509f565c9decdb57c8ada4ce2b
[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 = "src/test/resources/providers/TestClElementStatistics.json";
45
46     private static AtomicInteger dbNameCounter = new AtomicInteger();
47
48     private PolicyModelsProviderParameters parameters;
49     private ClElementStatisticsProvider clElementStatisticsProvider;
50     private ClElementStatisticsList inputClElementStats;
51     private String originalJson = ResourceUtils.getResourceAsString(CL_ELEMENT_STATS_JSON);
52
53     /**
54      * Set up test ClElement statistics provider.
55      *
56      * @throws Exception on errors
57      */
58     @Before
59     public void beforeSetupDao() throws Exception {
60
61         parameters = new PolicyModelsProviderParameters();
62         parameters.setDatabaseDriver("org.h2.Driver");
63         parameters.setName("PolicyProviderParameterGroup");
64         parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl");
65         parameters.setDatabaseUrl("jdbc:h2:mem:clElementTestDb" + dbNameCounter.getAndIncrement());
66         parameters.setDatabaseUser("policy");
67         parameters.setDatabasePassword("P01icY");
68         parameters.setPersistenceUnit("ToscaConceptTest");
69
70         clElementStatisticsProvider = new ClElementStatisticsProvider(parameters);
71         inputClElementStats = CODER.decode(originalJson, ClElementStatisticsList.class);
72     }
73
74     @After
75     public void teardown() {
76         clElementStatisticsProvider.close();
77     }
78
79     @Test
80     public void testClElementStatisticsCreate() throws Exception {
81         assertThatThrownBy(() -> {
82             clElementStatisticsProvider.createClElementStatistics(null);
83         }).hasMessageMatching(LIST_IS_NULL);
84
85         ClElementStatisticsList createdClElementStats = new ClElementStatisticsList();
86         createdClElementStats.setClElementStatistics(
87             clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics()));
88
89         assertEquals(inputClElementStats.toString().replaceAll("\\s+", ""),
90             createdClElementStats.toString().replaceAll("\\s+", ""));
91     }
92
93     @Test
94     public void testGetClElementStatistics() throws Exception {
95
96         List<ClElementStatistics> getResponse;
97
98         // Return empty list when no data present in db
99         getResponse = clElementStatisticsProvider.getClElementStatistics(null, null, null, null);
100         assertThat(getResponse).isEmpty();
101
102         clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics());
103         ToscaConceptIdentifier identifier = inputClElementStats.getClElementStatistics().get(0).getParticipantId();
104         Instant instant = inputClElementStats.getClElementStatistics().get(0).getTimeStamp();
105         String id = inputClElementStats.getClElementStatistics().get(0).getId().toString();
106         assertEquals(1, clElementStatisticsProvider
107             .getClElementStatistics(identifier.getName(), identifier.getVersion(), id, instant).size());
108
109         assertEquals(1, clElementStatisticsProvider
110             .getFilteredClElementStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size());
111     }
112 }