summaryrefslogtreecommitdiffstats
path: root/cps-ncmp-rest-stub
diff options
context:
space:
mode:
Diffstat (limited to 'cps-ncmp-rest-stub')
-rw-r--r--cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/pom.xml6
-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
-rw-r--r--cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/test/groovy/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderSpec.groovy75
-rw-r--r--cps-ncmp-rest-stub/pom.xml1
6 files changed, 220 insertions, 22 deletions
diff --git a/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/pom.xml b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/pom.xml
index fc33270ea5..728facf494 100644
--- a/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/pom.xml
+++ b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/pom.xml
@@ -46,5 +46,11 @@
<groupId>org.onap.cps</groupId>
<artifactId>cps-ncmp-rest</artifactId>
</dependency>
+ <!-- T E S T - D E P E N D E N C I E S -->
+ <dependency>
+ <groupId>org.spockframework</groupId>
+ <artifactId>spock-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project> \ No newline at end of file
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();
+ }
+
+}
diff --git a/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/test/groovy/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderSpec.groovy b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/test/groovy/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderSpec.groovy
new file mode 100644
index 0000000000..7bfe5c3220
--- /dev/null
+++ b/cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/test/groovy/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderSpec.groovy
@@ -0,0 +1,75 @@
+/*-
+ * ============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.nio.file.Files
+import java.nio.file.Path
+import org.springframework.util.FileSystemUtils
+import spock.lang.Shared
+import spock.lang.Specification
+import spock.lang.TempDir
+
+class ResourceProviderSpec extends Specification {
+
+ @TempDir
+ @Shared
+ def tempDirectory
+
+ def setupSpec() {
+ tempDirectory = Files.createTempDirectory('spock-test')
+ Files.write(tempDirectory.resolve('file.txt'), 'Dummy file content'.getBytes())
+ }
+
+ def cleanupSpec() {
+ if(Files.exists(tempDirectory)) {
+ FileSystemUtils.deleteRecursively(tempDirectory)
+ }
+ }
+
+ def 'Resource Provider with existing file on #scenario'() {
+
+ given: 'a resource provider with base stub folder defined on #scenario'
+ def resourceProvider = new ResourceProviderImpl(dir)
+ when: 'attempting to access that file #filename'
+ def optional= resourceProvider.getResourceInputStream(filename)
+ then: 'it is present'
+ assert optional.isPresent()
+ where:
+ scenario | dir | filename
+ 'classpath' | '/stubs/' | 'passthrough-operational-example.json'
+ 'file system' | tempDirectory.toString() | 'file.txt'
+ }
+
+ def 'Resource Provider without required resource file on #scenario'() {
+
+ given: 'a resource provider with base stub folder defined on #scenario'
+ def resourceProvider = new ResourceProviderImpl(dir)
+ when: 'attempting to access unknown-file.txt'
+ def optional= resourceProvider.getResourceInputStream('unknown-file.txt')
+ then: 'it is empty'
+ assert optional.isEmpty()
+ where:
+ scenario | dir
+ 'classpath' | '/stubs/'
+ 'file system' | tempDirectory.toString()
+ }
+
+}
diff --git a/cps-ncmp-rest-stub/pom.xml b/cps-ncmp-rest-stub/pom.xml
index 7fa44e6133..3648d8eb9e 100644
--- a/cps-ncmp-rest-stub/pom.xml
+++ b/cps-ncmp-rest-stub/pom.xml
@@ -32,6 +32,7 @@
<properties>
<parent.directory>${project.parent.basedir}/..</parent.directory>
<sonar.skip>true</sonar.skip>
+ <jacoco.skip>true</jacoco.skip>
</properties>
<modules>