From cb623959af429d7ac0999a113d3373cb8f950ee2 Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Wed, 21 Jun 2023 14:16:22 +0100 Subject: [PATCH] Adding logic to read response files from filesystem for client integration tests Issue-ID: CPS-1751 Change-Id: I62e24d09474184716c56149a06deae4500b7b286 Signed-off-by: waqas.ikram --- .../cps-ncmp-rest-stub-service/pom.xml | 6 ++ .../controller/NetworkCmProxyStubController.java | 62 +++++++++++------- .../ncmp/rest/stub/providers/ResourceProvider.java | 31 +++++++++ .../rest/stub/providers/ResourceProviderImpl.java | 67 +++++++++++++++++++ .../stub/providers/ResourceProviderSpec.groovy | 75 ++++++++++++++++++++++ cps-ncmp-rest-stub/pom.xml | 1 + 6 files changed, 220 insertions(+), 22 deletions(-) create mode 100644 cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProvider.java create mode 100644 cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/main/java/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderImpl.java create mode 100644 cps-ncmp-rest-stub/cps-ncmp-rest-stub-service/src/test/groovy/org/onap/cps/ncmp/rest/stub/providers/ResourceProviderSpec.groovy 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 fc33270ea..728facf49 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 @@ org.onap.cps cps-ncmp-rest + + + org.spockframework + spock-core + test + \ 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 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 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 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> searchCmHandles( final CmHandleQueryParameters cmHandleQueryParameters) { - List 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 optionalResponseObject = getResponseObject("cmHandlesSearch.json", + RestOutputCmHandle[].class); + if (optionalResponseObject.isPresent()) { + final List 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.emptyList()); } private ResponseEntity> populateAsyncResponse(final String topicParamInQuery) { @@ -122,6 +131,15 @@ public class NetworkCmProxyStubController implements NetworkCmProxyApi { return asyncResponseData; } + private Optional getResponseObject(final String filename, final Class type) throws IOException { + final Optional 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 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 000000000..9b15ab6b8 --- /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 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 000000000..c0779eb27 --- /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 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 000000000..7bfe5c322 --- /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 7fa44e613..3648d8eb9 100644 --- a/cps-ncmp-rest-stub/pom.xml +++ b/cps-ncmp-rest-stub/pom.xml @@ -32,6 +32,7 @@ ${project.parent.basedir}/.. true + true -- 2.16.6