7daf3fbcf8616160939c6f427553994cd360d055
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 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.jupiter.api.Assertions.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.AcElementStatistics;
37 import org.onap.policy.clamp.models.acm.concepts.AcElementStatisticsList;
38 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAcElementStatistics;
39 import org.onap.policy.clamp.models.acm.persistence.repository.AcElementStatisticsRepository;
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 AcElementStatisticsProviderTest {
46     private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
47     private static final Coder CODER = new StandardCoder();
48     private static final String AC_ELEMENT_STATS_JSON = "src/test/resources/providers/TestAcElementStatistics.json";
49
50     private AcElementStatisticsProvider acElementStatisticsProvider;
51     private AcElementStatisticsList inputAcElementStats;
52     private final String originalJson = ResourceUtils.getResourceAsString(AC_ELEMENT_STATS_JSON);
53
54     /**
55      * Set up test AcElement statistics provider.
56      *
57      * @throws Exception on errors
58      */
59     @BeforeEach
60     void beforeSetupDao() throws Exception {
61
62         inputAcElementStats = CODER.decode(originalJson, AcElementStatisticsList.class);
63         var acElementStatisticsRepository = mock(AcElementStatisticsRepository.class);
64
65         var jpaAcElementStatisticsList =
66             ProviderUtils.getJpaAndValidateList(inputAcElementStats.getAcElementStatistics(),
67                 JpaAcElementStatistics::new, "automation composition element statistics");
68
69         for (var acElementStat : jpaAcElementStatisticsList) {
70             when(acElementStatisticsRepository.getById(eq(acElementStat.getKey()))).thenReturn(acElementStat);
71             when(acElementStatisticsRepository.findAllById(eq(List.of(acElementStat.getKey()))))
72                 .thenReturn(List.of(acElementStat));
73         }
74
75         when(acElementStatisticsRepository.saveAll(anyList())).thenReturn(jpaAcElementStatisticsList);
76
77         when(acElementStatisticsRepository.getFiltered(eq(JpaAcElementStatistics.class), any()))
78             .thenReturn(List.of(jpaAcElementStatisticsList.get(0)));
79
80         acElementStatisticsProvider = new AcElementStatisticsProvider(acElementStatisticsRepository);
81     }
82
83     @Test
84     void testAcElementStatisticsCreate() throws Exception {
85         assertThatThrownBy(() -> acElementStatisticsProvider.createAcElementStatistics(null))
86             .hasMessageMatching(LIST_IS_NULL);
87
88         AcElementStatisticsList createdAcElementStats = new AcElementStatisticsList();
89         createdAcElementStats.setAcElementStatistics(
90             acElementStatisticsProvider.createAcElementStatistics(inputAcElementStats.getAcElementStatistics()));
91
92         assertEquals(inputAcElementStats.toString().replaceAll("\\s+", ""),
93             createdAcElementStats.toString().replaceAll("\\s+", ""));
94     }
95
96     @Test
97     void testGetAcElementStatistics() throws Exception {
98
99         List<AcElementStatistics> getResponse;
100
101         // Return empty list when no data present in db
102         getResponse = acElementStatisticsProvider.getAcElementStatistics(null, null, null, null);
103         assertThat(getResponse).isEmpty();
104
105         acElementStatisticsProvider.createAcElementStatistics(inputAcElementStats.getAcElementStatistics());
106         ToscaConceptIdentifier identifier = inputAcElementStats.getAcElementStatistics().get(0).getParticipantId();
107         Instant instant = inputAcElementStats.getAcElementStatistics().get(0).getTimeStamp();
108         String id = inputAcElementStats.getAcElementStatistics().get(0).getId().toString();
109         assertEquals(1, acElementStatisticsProvider
110             .getAcElementStatistics(identifier.getName(), identifier.getVersion(), id, instant).size());
111
112         assertEquals(1, acElementStatisticsProvider
113             .getFilteredAcElementStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size());
114     }
115 }