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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
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;
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;
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";
46 private static AtomicInteger dbNameCounter = new AtomicInteger();
48 private PolicyModelsProviderParameters parameters;
49 private ClElementStatisticsProvider clElementStatisticsProvider;
50 private ClElementStatisticsList inputClElementStats;
51 private String originalJson = ResourceUtils.getResourceAsString(CL_ELEMENT_STATS_JSON);
54 * Set up test ClElement statistics provider.
56 * @throws Exception on errors
59 public void beforeSetupDao() throws Exception {
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");
70 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters);
71 inputClElementStats = CODER.decode(originalJson, ClElementStatisticsList.class);
75 public void teardown() {
76 clElementStatisticsProvider.close();
80 public void testClElementStatisticsCreate() throws Exception {
81 assertThatThrownBy(() -> {
82 clElementStatisticsProvider.createClElementStatistics(null);
83 }).hasMessageMatching(LIST_IS_NULL);
85 ClElementStatisticsList createdClElementStats = new ClElementStatisticsList();
86 createdClElementStats.setClElementStatistics(
87 clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics()));
89 assertEquals(inputClElementStats.toString().replaceAll("\\s+", ""),
90 createdClElementStats.toString().replaceAll("\\s+", ""));
94 public void testGetClElementStatistics() throws Exception {
96 List<ClElementStatistics> getResponse;
98 // Return empty list when no data present in db
99 getResponse = clElementStatisticsProvider.getClElementStatistics(null, null, null, null);
100 assertThat(getResponse).isEmpty();
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());
109 assertEquals(1, clElementStatisticsProvider
110 .getFilteredClElementStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size());