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.util.Date;
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 =
45 "src/test/resources/providers/TestClElementStatistics.json";
47 private static AtomicInteger dbNameCounter = new AtomicInteger();
49 private PolicyModelsProviderParameters parameters;
50 private ClElementStatisticsProvider clElementStatisticsProvider;
51 private ClElementStatisticsList inputClElementStats;
52 private String originalJson = ResourceUtils.getResourceAsString(CL_ELEMENT_STATS_JSON);
55 * Set up test ClElement statistics provider.
58 public void setupDao() throws Exception {
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");
69 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters);
70 inputClElementStats = CODER.decode(originalJson, ClElementStatisticsList.class);
74 public void teardown() {
75 clElementStatisticsProvider.close();
79 public void testClElementStatisticsCreate() throws Exception {
80 assertThatThrownBy(() -> {
81 clElementStatisticsProvider.createClElementStatistics(null);
82 }).hasMessageMatching(LIST_IS_NULL);
84 ClElementStatisticsList createdClElementStats = new ClElementStatisticsList();
85 createdClElementStats.setClElementStatistics(clElementStatisticsProvider
86 .createClElementStatistics(inputClElementStats.getClElementStatistics()));
88 assertEquals(inputClElementStats.toString().replaceAll("\\s+", ""),
89 createdClElementStats.toString().replaceAll("\\s+", ""));
93 public void testGetClElementStatistics() throws Exception {
95 List<ClElementStatistics> getResponse;
97 //Return empty list when no data present in db
98 getResponse = clElementStatisticsProvider.getClElementStatistics(null, null, null);
99 assertThat(getResponse).isEmpty();
101 clElementStatisticsProvider.createClElementStatistics(inputClElementStats
102 .getClElementStatistics());
103 ToscaConceptIdentifier identifier = inputClElementStats.getClElementStatistics().get(0)
104 .getControlLoopElementId();
105 Date date = inputClElementStats.getClElementStatistics().get(0).getTimeStamp();
106 assertEquals(1, clElementStatisticsProvider.getClElementStatistics(identifier.getName(),
107 identifier.getVersion(), date).size());
109 assertEquals(1, clElementStatisticsProvider.getFilteredClElementStatistics("name2",
110 "1.0.1", null, null, null,