Uplift from master
[portal/nonrtric-controlpanel.git] / webapp-backend / src / test / java / org / oransc / portal / nonrtric / controlpanel / eiproducerapi / EiProducerApiImplTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.portal.nonrtric.controlpanel.eiproducerapi;
21
22 import static org.junit.Assert.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonSyntaxException;
31
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.junit.jupiter.api.Test;
36 import org.oransc.portal.nonrtric.controlpanel.model.JobInfo;
37 import org.oransc.portal.nonrtric.controlpanel.model.ProducerRegistrationInfo;
38 import org.oransc.portal.nonrtric.controlpanel.model.ProducerStatusInfo;
39 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.client.HttpServerErrorException;
43 import reactor.core.publisher.Mono;
44
45 class EiProducerApiImplTest {
46     private static final String URL_EI_PRODUCERS = "/eiproducers";
47     private static final String EI_PRODUCER_1 = "eiprod1";
48     private static final String EI_PRODUCER_2 = "eiprod2";
49     private static final String EI_PRODUCER_1_INFO_VALID = "{\"supported_ei_types\":[]}";
50     private static final String URL_STATUS = "/status";
51     private static final String EI_PRODUCER_1_STATUS_VALID = "{\"operational_state\":\"ENABLED\"}";
52     private static final String EI_PRODUCER_1_STATUS_INVALID = "\"operational_state\":\"ENABLED\"}";
53     private static final String URL_EI_JOBS = "/eijobs";
54     private static final String EI_JOB_1_INFO =
55         "{\"ei_job_identity\":\"eijob1\",\"ei_job_data\":{},\"ei_type_identity\":\"eitype1\"}";
56     private static final String EI_JOB_2_INFO =
57         "{\"ei_job_identity\":\"eijob2\",\"ei_job_data\":{},\"ei_type_identity\":\"eitype2\"}";
58
59     AsyncRestClient restClientMock = mock(AsyncRestClient.class);
60     EiProducerApiImpl apiUnderTest = new EiProducerApiImpl(restClientMock);
61     private static com.google.gson.Gson gson = new GsonBuilder().create();
62
63     private void whenGetReturnOK(String url, HttpStatus status, String body) {
64         ResponseEntity<String> ret = new ResponseEntity<>(body, status);
65         when(restClientMock.getForEntity(eq(url))).thenReturn(Mono.just(ret));
66     }
67
68     private void whenGetReturnFailure(String url, HttpStatus status, String body) {
69         HttpServerErrorException e = new HttpServerErrorException(status, body);
70         when(restClientMock.getForEntity(eq(url))).thenReturn(Mono.error(e));
71     }
72
73     @Test
74     void testGetAllEiProducerIdsFailure() {
75         whenGetReturnFailure(URL_EI_PRODUCERS, HttpStatus.NOT_FOUND, "");
76         ResponseEntity<String> returnedResp = apiUnderTest.getAllEiProducerIds();
77         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
78     }
79
80     @Test
81     void testGetAllEiProducerIdsSuccess() {
82         String eiProducerIds = Arrays.asList(EI_PRODUCER_1, EI_PRODUCER_2).toString();
83
84         whenGetReturnOK(URL_EI_PRODUCERS, HttpStatus.OK, eiProducerIds);
85
86         ResponseEntity<String> returnedResp = apiUnderTest.getAllEiProducerIds();
87         assertEquals("[\"" + EI_PRODUCER_1 + "\",\"" + EI_PRODUCER_2 + "\"]", returnedResp.getBody());
88         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
89     }
90
91     @Test
92     void testGetEiProducerValidJson() {
93         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1, HttpStatus.OK, EI_PRODUCER_1_INFO_VALID);
94
95         ResponseEntity<ProducerRegistrationInfo> returnedResp = apiUnderTest.getEiProducer(EI_PRODUCER_1);
96
97         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
98         assertEquals(EI_PRODUCER_1_INFO_VALID, gson.toJson(returnedResp.getBody()));
99     }
100
101     @Test
102     void whenGetEiJobsForOneEiProducerExceptionThrown_thenAssertionSucceeds() {
103         whenGetReturnFailure(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_EI_JOBS, HttpStatus.NOT_FOUND, "");
104         Exception exception = assertThrows(IllegalStateException.class, () -> {
105             apiUnderTest.getEiJobsForOneEiProducer(EI_PRODUCER_1);
106         });
107
108         String expectedMessage = "Not a JSON Array: null";
109         String actualMessage = exception.getMessage();
110         assertTrue(actualMessage.contains(expectedMessage));
111     }
112
113     @Test
114     void testGetEiJobsForOneEiProducerSuccess() {
115         String eiJobs = Arrays.asList(EI_JOB_1_INFO, EI_JOB_2_INFO).toString();
116
117         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_EI_JOBS, HttpStatus.OK, eiJobs);
118
119         ResponseEntity<List<JobInfo>> returnedResp = apiUnderTest.getEiJobsForOneEiProducer(EI_PRODUCER_1);
120         assertTrue(gson.toJson(returnedResp.getBody()).contains("\"ei_job_identity\":\"eijob1\""));
121         assertTrue(gson.toJson(returnedResp.getBody()).contains("\"ei_job_identity\":\"eijob2\""));
122         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
123     }
124
125     @Test
126     void testGetEiProducerStatusValidJson() {
127         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_STATUS, HttpStatus.OK, EI_PRODUCER_1_STATUS_VALID);
128
129         ResponseEntity<ProducerStatusInfo> returnedResp = apiUnderTest.getEiProducerStatus(EI_PRODUCER_1);
130
131         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
132         assertEquals(EI_PRODUCER_1_STATUS_VALID, gson.toJson(returnedResp.getBody()));
133     }
134
135     @Test
136     public void whenGetEiProducerStatusExceptionThrown_thenAssertionSucceeds() {
137         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_STATUS, HttpStatus.OK,
138             EI_PRODUCER_1_STATUS_INVALID);
139         Exception exception = assertThrows(JsonSyntaxException.class, () -> {
140             apiUnderTest.getEiProducerStatus(EI_PRODUCER_1);
141         });
142
143         String expectedMessage = "Expected BEGIN_OBJECT but was STRING";
144         String actualMessage = exception.getMessage();
145         assertTrue(actualMessage.contains(expectedMessage));
146     }
147 }