summaryrefslogtreecommitdiffstats
path: root/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java
diff options
context:
space:
mode:
Diffstat (limited to 'cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java')
-rw-r--r--cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java62
1 files changed, 40 insertions, 22 deletions
diff --git a/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java
index ee035bf57..bf84b439f 100644
--- a/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java
+++ b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java
@@ -26,9 +26,11 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.UUID;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@@ -42,21 +44,24 @@ import org.onap.cps.ncmp.rest.model.RestModuleReference;
import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.core.io.ClassPathResource;
+import org.onap.cps.ncmp.rest.stub.providers.ResourceProvider;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
-
@Slf4j
@RestController
@RequestMapping("${rest.api.ncmp-stub-base-path}")
public class NetworkCmProxyStubController implements NetworkCmProxyApi {
- @Value("${stub.path}")
- private String pathToResponseFiles;
+ @Autowired
+ private ResourceProvider resourceProvider;
+
+ @Autowired
+ private ObjectMapper objectMapper;
+
private static final String ASYNC_REQUEST_ID = "requestId";
@Override
@@ -70,16 +75,18 @@ public class NetworkCmProxyStubController implements NetworkCmProxyApi {
final Map<String, Object> asyncResponseData = asyncResponse.getBody();
Object responseObject = null;
// read JSON file and map/convert to java POJO
- final ClassPathResource resource = new ClassPathResource(
- pathToResponseFiles + "passthrough-operational-example.json");
- try (InputStream inputStream = resource.getInputStream()) {
- final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
- final ObjectMapper mapper = new ObjectMapper();
- responseObject = mapper.readValue(string, Object.class);
- } catch (final IOException exception) {
- log.error("Error reading the file.", exception);
+ try {
+ final Optional<Object> optionalResponseObject = getResponseObject(
+ "passthrough-operational-example.json", Object.class);
+ if (optionalResponseObject.isPresent()) {
+ responseObject = optionalResponseObject.get();
+ }
+
+ } catch (final IOException ioException) {
+ log.error("Error reading the file.", ioException);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
+
if (asyncResponseData == null) {
return ResponseEntity.ok(responseObject);
}
@@ -91,18 +98,20 @@ public class NetworkCmProxyStubController implements NetworkCmProxyApi {
@Override
public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
final CmHandleQueryParameters cmHandleQueryParameters) {
- List<RestOutputCmHandle> restOutputCmHandles = null;
// read JSON file and map/convert to java POJO
- final ClassPathResource resource = new ClassPathResource(pathToResponseFiles + "cmHandlesSearch.json");
- try (InputStream inputStream = resource.getInputStream()) {
- final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
- final ObjectMapper mapper = new ObjectMapper();
- restOutputCmHandles = Arrays.asList(mapper.readValue(string, RestOutputCmHandle[].class));
- } catch (final IOException exception) {
- log.error("Error reading the file.", exception);
+ try {
+ final Optional<RestOutputCmHandle[]> optionalResponseObject = getResponseObject("cmHandlesSearch.json",
+ RestOutputCmHandle[].class);
+ if (optionalResponseObject.isPresent()) {
+ final List<RestOutputCmHandle> restOutputCmHandles = Arrays.asList(optionalResponseObject.get());
+ return ResponseEntity.ok(restOutputCmHandles);
+ }
+ } catch (final IOException ioException) {
+ log.error("Error reading the file.", ioException);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
- return ResponseEntity.ok(restOutputCmHandles);
+
+ return ResponseEntity.ok(Collections.<RestOutputCmHandle>emptyList());
}
private ResponseEntity<Map<String, Object>> populateAsyncResponse(final String topicParamInQuery) {
@@ -122,6 +131,15 @@ public class NetworkCmProxyStubController implements NetworkCmProxyApi {
return asyncResponseData;
}
+ private <T> Optional<T> getResponseObject(final String filename, final Class<T> type) throws IOException {
+ final Optional<InputStream> optionalInputStream = resourceProvider.getResourceInputStream(filename);
+ if (optionalInputStream.isPresent()) {
+ final String content = new String(optionalInputStream.get().readAllBytes(), StandardCharsets.UTF_8);
+ return Optional.of(objectMapper.readValue(content, type));
+ }
+ return Optional.empty();
+ }
+
@Override
public ResponseEntity<Void> createResourceDataRunningForCmHandle(@NotNull @Valid final String resourceIdentifier,
final String datastoreName, final String cmHandle,