Add validator to check atleast one non null field check
[nonrtric/plt/rappmanager.git] / participants / participant-impl-dme / src / test / java / org / oransc / participant / dme / restclient / AcA1PmsClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.oransc.participant.dme.restclient;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
29
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.oransc.participant.dme.rest.DataConsumerApiClient;
32 import com.oransc.participant.dme.rest.DataProducerRegistrationApiClient;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.stream.Stream;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.junit.jupiter.params.ParameterizedTest;
40 import org.junit.jupiter.params.provider.Arguments;
41 import org.junit.jupiter.params.provider.MethodSource;
42 import org.oransc.participant.dme.exception.DmeException;
43 import org.springframework.boot.test.mock.mockito.MockBean;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.test.context.junit.jupiter.SpringExtension;
46
47 @ExtendWith(SpringExtension.class)
48 class AcA1PmsClientTest {
49
50     @MockBean
51     DataProducerRegistrationApiClient dataProducerRegistrationApiClient;
52
53     @MockBean
54     DataConsumerApiClient dataConsumerApiClient;
55
56     @MockBean
57     AcDmeClient acDmeClient;
58
59     ObjectMapper objectMapper = new ObjectMapper();
60
61     @BeforeEach
62     void initialize() {
63         acDmeClient = new AcDmeClient(dataProducerRegistrationApiClient, dataConsumerApiClient, objectMapper);
64     }
65
66     @Test
67     void testHealthyDme() {
68         when(dataProducerRegistrationApiClient.getInfoTypdentifiersWithHttpInfo()).thenReturn(
69                 ResponseEntity.ok().build());
70         assertTrue(acDmeClient.isDmeHealthy());
71     }
72
73     @Test
74     void testUnhealthyDme() {
75         when(dataProducerRegistrationApiClient.getInfoTypdentifiersWithHttpInfo()).thenReturn(
76                 ResponseEntity.internalServerError().build());
77         assertFalse(acDmeClient.isDmeHealthy());
78     }
79
80     @ParameterizedTest
81     @MethodSource("getInfoTypes")
82     void testcreateInfoTypeSuccess(Map<String, String> infoTypeMap) {
83         when(dataProducerRegistrationApiClient.putInfoTypeWithHttpInfo(any(), any())).thenReturn(
84                 ResponseEntity.ok().build());
85         assertDoesNotThrow(() -> acDmeClient.createInfoType(infoTypeMap));
86     }
87
88     @Test
89     void testcreateInfoTypeFailure() {
90         when(dataProducerRegistrationApiClient.putInfoTypeWithHttpInfo(any(), any())).thenReturn(
91                 ResponseEntity.internalServerError().build());
92         assertThrows(DmeException.class, () -> acDmeClient.createInfoType(Map.of("infotype1", "{}")));
93     }
94
95     @ParameterizedTest
96     @MethodSource("getDataProducers")
97     void testcreateDataProducerSuccess(Map<String, String> dataProducerMap) {
98         when(dataProducerRegistrationApiClient.putInfoProducerWithHttpInfo(any(), any())).thenReturn(
99                 ResponseEntity.ok().build());
100         assertDoesNotThrow(() -> acDmeClient.createDataProducer(dataProducerMap));
101     }
102
103
104     @Test
105     void testcreateDataProducerFailure() {
106         when(dataProducerRegistrationApiClient.putInfoProducerWithHttpInfo(any(), any())).thenReturn(
107                 ResponseEntity.internalServerError().build());
108         assertThrows(DmeException.class, () -> acDmeClient.createDataProducer(Map.of("producer1", "{}")));
109     }
110
111     @ParameterizedTest
112     @MethodSource("getDataConsumers")
113     void testcreateDataConsumerSuccess(Map<String, String> dataConsumerMap) {
114         when(dataConsumerApiClient.putIndividualInfoJobWithHttpInfo(any(), any())).thenReturn(
115                 ResponseEntity.ok().build());
116         assertDoesNotThrow(() -> acDmeClient.createDataConsumer(dataConsumerMap));
117     }
118
119     @Test
120     void testcreateDataConsumerFailure() {
121         when(dataConsumerApiClient.putIndividualInfoJobWithHttpInfo(any(), any())).thenReturn(
122                 ResponseEntity.internalServerError().build());
123         assertThrows(DmeException.class, () -> acDmeClient.createDataConsumer(Map.of("consumer1", "{}")));
124     }
125
126     @ParameterizedTest
127     @MethodSource("getDeleteDataProducers")
128     void testDeleteDataProducerSuccess() {
129         when(dataProducerRegistrationApiClient.deleteInfoProducerWithHttpInfo(any())).thenReturn(
130                 ResponseEntity.ok().build());
131         assertDoesNotThrow(() -> acDmeClient.deleteDataProducer(Set.of("producer1")));
132     }
133
134     @Test
135     void testDeleteDataProducerFailure() {
136         when(dataProducerRegistrationApiClient.deleteInfoProducerWithHttpInfo(any())).thenReturn(
137                 ResponseEntity.internalServerError().build());
138         assertThrows(DmeException.class, () -> acDmeClient.deleteDataProducer(Set.of("producer1")));
139     }
140
141     @ParameterizedTest
142     @MethodSource("getDeleteDataConsumers")
143     void testDeleteDataConsumerSuccess() {
144         when(dataConsumerApiClient.deleteIndividualInfoJobWithHttpInfo(any())).thenReturn(ResponseEntity.ok().build());
145         assertDoesNotThrow(() -> acDmeClient.deleteDataConsumer(Set.of("consumer1")));
146     }
147
148     @Test
149     void testDeleteDataConsumerFailure() {
150         when(dataConsumerApiClient.deleteIndividualInfoJobWithHttpInfo(any())).thenReturn(
151                 ResponseEntity.internalServerError().build());
152         assertThrows(DmeException.class, () -> acDmeClient.deleteDataConsumer(Set.of("consumer1")));
153     }
154
155     private static Stream<Arguments> getDeleteDataProducers() {
156         return Stream.of(Arguments.of(Set.of("producer1")), Arguments.of(Set.of()));
157     }
158
159     private static Stream<Arguments> getDeleteDataConsumers() {
160         return Stream.of(Arguments.of(Set.of("consumer1")), Arguments.of(Set.of()));
161     }
162     private static Stream<Arguments> getInfoTypes() {
163         return Stream.of(Arguments.of(Map.of("infotype1", "{}")), Arguments.of(Map.of()));
164     }
165
166     private static Stream<Arguments> getDataProducers() {
167         return Stream.of(Arguments.of(Map.of("dataproducers", "{}")), Arguments.of(Map.of()));
168     }
169
170     private static Stream<Arguments> getDataConsumers() {
171         return Stream.of(Arguments.of(Map.of("dataconsumer", "{}")), Arguments.of(Map.of()));
172     }
173 }