2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.portal.nonrtric.controlpanel.eiproducerapi;
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;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.JsonSyntaxException;
32 import java.util.Arrays;
33 import java.util.List;
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;
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\"}";
62 AsyncRestClient restClientMock = mock(AsyncRestClient.class);
63 EiProducerApiImpl apiUnderTest = new EiProducerApiImpl(restClientMock);
64 private static com.google.gson.Gson gson = new GsonBuilder().create();
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));
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));
77 void testGetAllEiProducerIdsFailure() {
78 whenGetReturnFailure(URL_EI_PRODUCERS, HttpStatus.NOT_FOUND, "");
79 ResponseEntity<String> returnedResp = apiUnderTest.getAllEiProducerIds();
80 assertEquals(HttpStatus.NOT_FOUND, returnedResp.getStatusCode());
84 void testGetAllEiProducerIdsSuccess() {
85 String eiProducerIds = Arrays.asList(EI_PRODUCER_1, EI_PRODUCER_2).toString();
87 whenGetReturnOK(URL_EI_PRODUCERS, HttpStatus.OK, eiProducerIds);
89 ResponseEntity<String> returnedResp = apiUnderTest.getAllEiProducerIds();
90 assertEquals("[\"" + EI_PRODUCER_1 + "\",\"" + EI_PRODUCER_2 + "\"]", returnedResp.getBody());
91 assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
95 void testGetEiProducerValidJson() {
96 whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1, HttpStatus.OK, EI_PRODUCER_1_INFO_VALID);
98 ResponseEntity<ProducerRegistrationInfo> returnedResp = apiUnderTest.getEiProducer(EI_PRODUCER_1);
100 assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
101 assertEquals(EI_PRODUCER_1_INFO_VALID, gson.toJson(returnedResp.getBody()));
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);
111 String expectedMessage = "Expected BEGIN_OBJECT but was STRING";
112 String actualMessage = exception.getMessage();
113 assertTrue(actualMessage.contains(expectedMessage));
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);
123 String expectedMessage = "Not a JSON Array: null";
124 String actualMessage = exception.getMessage();
125 assertTrue(actualMessage.contains(expectedMessage));
129 void testGetEiJobsForOneEiProducerSuccess() {
130 String eiJobs = Arrays.asList(EI_JOB_1_INFO, EI_JOB_2_INFO).toString();
132 whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_EI_JOBS, HttpStatus.OK, eiJobs);
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());
141 void testGetEiProducerStatusValidJson() {
142 whenGetReturnOK(URL_EI_PRODUCERS + "/" + EI_PRODUCER_1 + URL_STATUS, HttpStatus.OK, EI_PRODUCER_1_STATUS_VALID);
144 ResponseEntity<ProducerStatusInfo> returnedResp = apiUnderTest.getEiProducerStatus(EI_PRODUCER_1);
146 assertEquals(HttpStatus.OK, returnedResp.getStatusCode());
147 assertEquals(EI_PRODUCER_1_STATUS_VALID, gson.toJson(returnedResp.getBody()));
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);
158 String expectedMessage = "Expected BEGIN_OBJECT but was STRING";
159 String actualMessage = exception.getMessage();
160 assertTrue(actualMessage.contains(expectedMessage));