1f4c21bbf9003f3219b44f3d5b59a48fb5d2a898
[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 =
50         "{\"supported_ei_types\":[{\"ei_type_identity\":\"eitype1\",\"ei_job_data_schema\":{\"title\":\"eijob1\"}}]}";
51     private static final String EI_PRODUCER_1_INFO_INVALID =
52         "{\"supported_ei_types\":[{\"ei_type_identity\":\"eitype1\",\"ei_job_data_schema\":\"title\":\"eijob1\"}}]}";
53     private static final String URL_STATUS = "/status";
54     private static final String EI_PRODUCER_1_STATUS_VALID = "{\"operational_state\":\"ENABLED\"}";
55     private static final String EI_PRODUCER_1_STATUS_INVALID = "\"operational_state\":\"ENABLED\"}";
56     private static final String URL_EI_JOBS = "/eijobs";
57     private static final String EI_JOB_1_INFO =
58         "{\"ei_job_identity\":\"eijob1\",\"ei_job_data\":{},\"ei_type_identity\":\"eitype1\"}";
59     private static final String EI_JOB_2_INFO =
60         "{\"ei_job_identity\":\"eijob2\",\"ei_job_data\":{},\"ei_type_identity\":\"eitype2\"}";
61
62     AsyncRestClient restClientMock = mock(AsyncRestClient.class);
63     EiProducerApiImpl apiUnderTest = new EiProducerApiImpl(restClientMock);
64     private static com.google.gson.Gson gson = new GsonBuilder().create();
65
66     private void whenGetReturnOK(String url, HttpStatus status, String body) {
67         ResponseEntity<String> ret = new ResponseEntity<>(body, status);
68         when(restClientMock.getForEntity(eq(url))).thenReturn(Mono.just(ret));
69     }
70
71     private void whenGetReturnFailure(String url, HttpStatus status, String body) {
72         HttpServerErrorException e = new HttpServerErrorException(status, body);
73         when(restClientMock.getForEntity(eq(url))).thenReturn(Mono.error(e));
74     }
75
76     @Test
77     void testGetAllEiProducerIdsFailure() {
78         whenGetReturnFailure(URL_EI_PRODUCERS, HttpStatus.NOT_FOUND, "");
79         ResponseEntity<String> returnedResp = apiUnderTest.getAllEiProducerIds();
80         assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
81     }
82
83     @Test
84     void testGetAllEiProducerIdsSuccess() {
85         String eiProducerIds = Arrays.asList(EI_PRODUCER_1, EI_PRODUCER_2).toString();
86
87         whenGetReturnOK(URL_EI_PRODUCERS, HttpStatus.OK, eiProducerIds);
88
89         ResponseEntity<String> returnedResp = apiUnderTest.getAllEiProducerIds();
90         assertEquals("[\"" + EI_PRODUCER_1 + "\",\"" + EI_PRODUCER_2 + "\"]", returnedResp.getBody());
91         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
92     }
93
94     @Test
95     void testGetEiProducerValidJson() {
96         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1, HttpStatus.OK, EI_PRODUCER_1_INFO_VALID);
97
98         ResponseEntity<ProducerRegistrationInfo> returnedResp = apiUnderTest.getEiProducer(EI_PRODUCER_1);
99
100         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
101         assertEquals(EI_PRODUCER_1_INFO_VALID, gson.toJson(returnedResp.getBody()));
102     }
103
104     @Test
105     public void whenGetEiProducerExceptionThrown_thenAssertionSucceeds() {
106         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1, HttpStatus.OK, EI_PRODUCER_1_INFO_INVALID);
107         Exception exception = assertThrows(JsonSyntaxException.class, () -> {
108             apiUnderTest.getEiProducer(EI_PRODUCER_1);
109         });
110
111         String expectedMessage = "Expected BEGIN_OBJECT but was STRING";
112         String actualMessage = exception.getMessage();
113         assertTrue(actualMessage.contains(expectedMessage));
114     }
115
116     @Test
117     void whenGetEiJobsForOneEiProducerExceptionThrown_thenAssertionSucceeds() {
118         whenGetReturnFailure(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_EI_JOBS, HttpStatus.NOT_FOUND, "");
119         Exception exception = assertThrows(IllegalStateException.class, () -> {
120             apiUnderTest.getEiJobsForOneEiProducer(EI_PRODUCER_1);
121         });
122
123         String expectedMessage = "Not a JSON Array: null";
124         String actualMessage = exception.getMessage();
125         assertTrue(actualMessage.contains(expectedMessage));
126     }
127
128     @Test
129     void testGetEiJobsForOneEiProducerSuccess() {
130         String eiJobs = Arrays.asList(EI_JOB_1_INFO, EI_JOB_2_INFO).toString();
131
132         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_EI_JOBS, HttpStatus.OK, eiJobs);
133
134         ResponseEntity<List<JobInfo>> returnedResp = apiUnderTest.getEiJobsForOneEiProducer(EI_PRODUCER_1);
135         assertTrue(gson.toJson(returnedResp.getBody()).contains("\"ei_job_identity\":\"eijob1\""));
136         assertTrue(gson.toJson(returnedResp.getBody()).contains("\"ei_job_identity\":\"eijob2\""));
137         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
138     }
139
140     @Test
141     void testGetEiProducerStatusValidJson() {
142         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_STATUS, HttpStatus.OK, EI_PRODUCER_1_STATUS_VALID);
143
144         ResponseEntity<ProducerStatusInfo> returnedResp = apiUnderTest.getEiProducerStatus(EI_PRODUCER_1);
145
146         assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
147         assertEquals(EI_PRODUCER_1_STATUS_VALID, gson.toJson(returnedResp.getBody()));
148     }
149
150     @Test
151     public void whenGetEiProducerStatusExceptionThrown_thenAssertionSucceeds() {
152         whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_STATUS, HttpStatus.OK,
153             EI_PRODUCER_1_STATUS_INVALID);
154         Exception exception = assertThrows(JsonSyntaxException.class, () -> {
155             apiUnderTest.getEiProducerStatus(EI_PRODUCER_1);
156         });
157
158         String expectedMessage = "Expected BEGIN_OBJECT but was STRING";
159         String actualMessage = exception.getMessage();
160         assertTrue(actualMessage.contains(expectedMessage));
161     }
162 }