handling empty search result in discover service
Change-Id: I50b4e4a90f3da2d70f85389c9ff50e10b83f372a
diff --git a/capifcorejava/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/DiscoverService.java b/capifcorejava/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/DiscoverService.java
index 0d5e98a..08e3fd8 100644
--- a/capifcorejava/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/DiscoverService.java
+++ b/capifcorejava/src/main/java/org/oransc/nonrtric/plt/capifcore/controller/DiscoverService.java
@@ -30,9 +30,11 @@
import org.oransc.capif.model.DiscoveredAPIs;
import org.oransc.capif.model.Protocol;
import org.oransc.capif.model.ServiceAPIDescription;
+import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.server.ResponseStatusException;
@RestController
@RequestMapping("/service-apis/v1")
@@ -51,9 +53,12 @@
CommunicationType commType, Protocol protocol, String aefId, DataFormat dataFormat, String apiCat,
String supportedFeatures, String apiSupportedFeatures) {
DiscoveredAPIs discoveredApis = new DiscoveredAPIs();
-
ServiceAPIDescription[] apiDescriptions = apiRegister.getAPIs();
discoveredApis.setServiceAPIDescriptions(new ArrayList<>(Arrays.asList(apiDescriptions)));
+ if (discoveredApis.getServiceAPIDescriptions() == null
+ || discoveredApis.getServiceAPIDescriptions().isEmpty()) {
+ throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No service matching the provided filter criteria");
+ }
return ResponseEntity.ok(discoveredApis);
}
diff --git a/capifcorejava/src/test/java/org/oransc/nonrtric/plt/capifcore/controller/DiscoverServiceTest.java b/capifcorejava/src/test/java/org/oransc/nonrtric/plt/capifcore/controller/DiscoverServiceTest.java
index 50acc9a..f696502 100644
--- a/capifcorejava/src/test/java/org/oransc/nonrtric/plt/capifcore/controller/DiscoverServiceTest.java
+++ b/capifcorejava/src/test/java/org/oransc/nonrtric/plt/capifcore/controller/DiscoverServiceTest.java
@@ -21,24 +21,27 @@
package org.oransc.nonrtric.plt.capifcore.controller;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import java.util.ArrayList;
-import java.util.List;
-
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.oransc.capif.model.DiscoveredAPIs;
import org.oransc.capif.model.ServiceAPIDescription;
+import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
+import org.springframework.web.server.ResponseStatusException;
@ExtendWith(SpringExtension.class)
class DiscoverServiceTest {
ApiRegister apiRegisterMock;
+ DiscoveredAPIs discoveredApisMock;
@BeforeEach
void initEach() {
@@ -60,5 +63,21 @@
}
+ @Test
+ @DisplayName("Service Api Description Not Maching Filter Criteria With Exception ")
+ void testServiceApiDescriptionNotMatchingFilterCriteriaShouldThrowException() throws ResponseStatusException {
+ DiscoverService testService = new DiscoverService(apiRegisterMock);
+ ServiceAPIDescription[] resultApis = {};
+ when(apiRegisterMock.getAPIs()).thenReturn(resultApis);
+ ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> {
+ testService.allServiceAPIsGet(" ", null, null, null, null, null, null, null, null, null);
+ });
+ String actualMessage = "No service matching the provided filter criteria";
+ assertTrue(exception.getMessage().contains(actualMessage));
+ assertThat(exception.getStatus()).isEqualTo(HttpStatus.NOT_FOUND);
+
+ }
+
+
}