summaryrefslogtreecommitdiffstats
path: root/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org
diff options
context:
space:
mode:
authorwaqas.ikram <waqas.ikram@est.tech>2023-06-21 14:16:22 +0100
committerWaqas Ikram <waqas.ikram@est.tech>2023-06-26 09:21:46 +0000
commitcb623959af429d7ac0999a113d3373cb8f950ee2 (patch)
tree3064582cd2aa1da96accc29bfe874e242f1483b8 /cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org
parent4992e60c1fca462d199799852fc4c93609d49e5a (diff)
Adding logic to read response files from
filesystem for client integration tests Issue-ID: CPS-1751 Change-Id: I62e24d09474184716c56149a06deae4500b7b286 Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
Diffstat (limited to 'cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org')
-rw-r--r--cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/controller/NetworkCmProxyStubController.java62
-rw-r--r--cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProvider.java31
-rw-r--r--cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderImpl.java67
3 files changed, 138 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 ee035bf579..bf84b439f5 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,
diff --git a/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProvider.java b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProvider.java
new file mode 100644
index 0000000000..9b15ab6b87
--- /dev/null
+++ b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProvider.java
@@ -0,0 +1,31 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.rest.stub.providers;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Optional;
+
+public interface ResourceProvider {
+
+ Optional<InputStream> getResourceInputStream(final String filename) throws IOException;
+
+}
diff --git a/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderImpl.java b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderImpl.java
new file mode 100644
index 0000000000..c0779eb270
--- /dev/null
+++ b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderImpl.java
@@ -0,0 +1,67 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.ncmp.rest.stub.providers;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Optional;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.stereotype.Service;
+
+@Slf4j
+@Service
+public class ResourceProviderImpl implements ResourceProvider {
+
+ private String pathToResponseFiles;
+
+ @Autowired
+ public ResourceProviderImpl(@Value("${stub.path}") final String pathToResponseFiles) {
+ this.pathToResponseFiles = pathToResponseFiles;
+ }
+
+ @Override
+ public Optional<InputStream> getResourceInputStream(final String filename) throws IOException {
+ final Path path = Paths.get(pathToResponseFiles).resolve(filename);
+
+ if (Files.exists(path)) {
+ log.info("Found resource file on file system using path: {}", path);
+ return Optional.of(Files.newInputStream(path));
+ }
+
+ log.warn("Couldn't find file on file system '{}', will search it in classpath", path);
+
+ final ClassPathResource resource = new ClassPathResource(path.toString());
+ if (resource.exists()) {
+ log.info("Found resource in classpath using path: {}", path);
+ return Optional.of(resource.getInputStream());
+ }
+
+ log.error("{} file not found on classpath or on file system", path);
+ return Optional.empty();
+ }
+
+}