cf4136d3ecc33318f7cde1b8efb274ac7dc4775e
[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.controlloop.models.controlloop.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.controlloop.models.controlloop.concepts.ClElementStatistics;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics;
39 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ClElementStatisticsRepository;
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 ClElementStatisticsProviderTest {
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 CL_ELEMENT_STATS_JSON = "src/test/resources/providers/TestClElementStatistics.json";
49
50     private ClElementStatisticsProvider clElementStatisticsProvider;
51     private ClElementStatisticsList inputClElementStats;
52     private String originalJson = ResourceUtils.getResourceAsString(CL_ELEMENT_STATS_JSON);
53
54     /**
55      * Set up test ClElement statistics provider.
56      *
57      * @throws Exception on errors
58      */
59     @BeforeEach
60     void beforeSetupDao() throws Exception {
61
62         inputClElementStats = CODER.decode(originalJson, ClElementStatisticsList.class);
63         var clElementStatisticsRepository = mock(ClElementStatisticsRepository.class);
64
65         var jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate(inputClElementStats.getClElementStatistics(),
66                 JpaClElementStatistics::new, "control loop element statistics");
67
68         for (var clElementStat : jpaClElementStatisticsList) {
69             when(clElementStatisticsRepository.findAllById(List.of(clElementStat.getKey())))
70                     .thenReturn(List.of(clElementStat));
71         }
72
73         when(clElementStatisticsRepository.saveAll(anyList())).thenReturn(jpaClElementStatisticsList);
74
75         when(clElementStatisticsRepository.getFiltered(eq(JpaClElementStatistics.class), any()))
76                 .thenReturn(List.of(jpaClElementStatisticsList.get(0)));
77
78         clElementStatisticsProvider = new ClElementStatisticsProvider(clElementStatisticsRepository);
79     }
80
81     @Test
82     void testClElementStatisticsCreate() throws Exception {
83         assertThatThrownBy(() -> {
84             clElementStatisticsProvider.createClElementStatistics(null);
85         }).hasMessageMatching(LIST_IS_NULL);
86
87         ClElementStatisticsList createdClElementStats = new ClElementStatisticsList();
88         createdClElementStats.setClElementStatistics(
89                 clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics()));
90
91         assertEquals(inputClElementStats.toString().replaceAll("\\s+", ""),
92                 createdClElementStats.toString().replaceAll("\\s+", ""));
93     }
94
95     @Test
96     void testGetClElementStatistics() throws Exception {
97
98         List<ClElementStatistics> getResponse;
99
100         // Return empty list when no data present in db
101         getResponse = clElementStatisticsProvider.getClElementStatistics(null, null, null, null);
102         assertThat(getResponse).isEmpty();
103
104         clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics());
105         ToscaConceptIdentifier identifier = inputClElementStats.getClElementStatistics().get(0).getParticipantId();
106         Instant instant = inputClElementStats.getClElementStatistics().get(0).getTimeStamp();
107         String id = inputClElementStats.getClElementStatistics().get(0).getId().toString();
108         assertEquals(1, clElementStatisticsProvider
109                 .getClElementStatistics(identifier.getName(), identifier.getVersion(), id, instant).size());
110
111         assertEquals(1, clElementStatisticsProvider
112                 .getFilteredClElementStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size());
113     }
114 }