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;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyList;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
32 import java.time.Instant;
33 import java.util.List;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics;
39 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ParticipantStatisticsRepository;
40 import org.onap.policy.common.utils.coder.Coder;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 class ParticipantStatisticsProviderTest {
47 private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
48 private static final Coder CODER = new StandardCoder();
49 private static final String PARTICIPANT_STATS_JSON = "src/test/resources/providers/TestParticipantStatistics.json";
51 private ParticipantStatisticsProvider participantStatisticsProvider;
52 private ParticipantStatisticsList inputParticipantStatistics;
53 private String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_STATS_JSON);
56 void beforeSetupDao() throws Exception {
57 var participantStatisticsRepository = mock(ParticipantStatisticsRepository.class);
58 participantStatisticsProvider = new ParticipantStatisticsProvider(participantStatisticsRepository);
59 inputParticipantStatistics = CODER.decode(originalJson, ParticipantStatisticsList.class);
61 var jpaParticipantStatisticsList =
62 ProviderUtils.getJpaAndValidateList(inputParticipantStatistics.getStatisticsList(),
63 JpaParticipantStatistics::new, "Participant Statistics");
65 for (var participantStat : jpaParticipantStatisticsList) {
66 when(participantStatisticsRepository.getById(eq(participantStat.getKey()))).thenReturn(participantStat);
67 when(participantStatisticsRepository.findAllById(eq(List.of(participantStat.getKey()))))
68 .thenReturn(List.of(participantStat));
71 when(participantStatisticsRepository.getFiltered(eq(JpaParticipantStatistics.class), any()))
72 .thenReturn(List.of(jpaParticipantStatisticsList.get(0)));
74 when(participantStatisticsRepository.saveAll(anyList())).thenReturn(jpaParticipantStatisticsList);
78 void testParticipantStatisticsCreate() throws Exception {
79 assertThatThrownBy(() -> {
80 participantStatisticsProvider.createParticipantStatistics(null);
81 }).hasMessageMatching(LIST_IS_NULL);
83 ParticipantStatisticsList createdStatsList = new ParticipantStatisticsList();
84 createdStatsList.setStatisticsList(participantStatisticsProvider
85 .createParticipantStatistics(inputParticipantStatistics.getStatisticsList()));
87 assertEquals(inputParticipantStatistics.toString().replaceAll("\\s+", ""),
88 createdStatsList.toString().replaceAll("\\s+", ""));
92 void testGetControlLoops() throws Exception {
93 // Return empty list when no data present in db
94 List<ParticipantStatistics> getResponse =
95 participantStatisticsProvider.getParticipantStatistics(null, null, null);
96 assertThat(getResponse).isEmpty();
98 participantStatisticsProvider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
99 ToscaConceptIdentifier identifier = inputParticipantStatistics.getStatisticsList().get(0).getParticipantId();
100 Instant instant = inputParticipantStatistics.getStatisticsList().get(0).getTimeStamp();
101 assertEquals(1, participantStatisticsProvider
102 .getParticipantStatistics(identifier.getName(), identifier.getVersion(), instant).size());
104 assertEquals(1, participantStatisticsProvider
105 .getFilteredParticipantStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size());