3c6b1eb83b0919b4a425beba9d394930900b09a9
[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.models.acm.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 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;
31
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.models.acm.concepts.ParticipantStatistics;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantStatisticsList;
38 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipantStatistics;
39 import org.onap.policy.clamp.models.acm.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;
44
45 class ParticipantStatisticsProviderTest {
46
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";
50
51     private ParticipantStatisticsProvider participantStatisticsProvider;
52     private ParticipantStatisticsList inputParticipantStatistics;
53     private String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_STATS_JSON);
54
55     @BeforeEach
56     void beforeSetupDao() throws Exception {
57         var participantStatisticsRepository = mock(ParticipantStatisticsRepository.class);
58         participantStatisticsProvider = new ParticipantStatisticsProvider(participantStatisticsRepository);
59         inputParticipantStatistics = CODER.decode(originalJson, ParticipantStatisticsList.class);
60
61         var jpaParticipantStatisticsList =
62                 ProviderUtils.getJpaAndValidateList(inputParticipantStatistics.getStatisticsList(),
63                         JpaParticipantStatistics::new, "Participant Statistics");
64
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));
69         }
70
71         when(participantStatisticsRepository.getFiltered(eq(JpaParticipantStatistics.class), any()))
72                 .thenReturn(List.of(jpaParticipantStatisticsList.get(0)));
73
74         when(participantStatisticsRepository.saveAll(anyList())).thenReturn(jpaParticipantStatisticsList);
75     }
76
77     @Test
78     void testParticipantStatisticsCreate() throws Exception {
79         assertThatThrownBy(() -> {
80             participantStatisticsProvider.createParticipantStatistics(null);
81         }).hasMessageMatching(LIST_IS_NULL);
82
83         ParticipantStatisticsList createdStatsList = new ParticipantStatisticsList();
84         createdStatsList.setStatisticsList(participantStatisticsProvider
85                 .createParticipantStatistics(inputParticipantStatistics.getStatisticsList()));
86
87         assertEquals(inputParticipantStatistics.toString().replaceAll("\\s+", ""),
88                 createdStatsList.toString().replaceAll("\\s+", ""));
89     }
90
91     @Test
92     void testGetAutomationCompositions() 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();
97
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());
103
104         assertEquals(1, participantStatisticsProvider
105                 .getFilteredParticipantStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size());
106     }
107 }