From 24805416f0502a1e06bf57fd15c27d56150f3b30 Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Thu, 1 Aug 2019 16:19:31 +0000 Subject: Adding infrastructure for sdnc simulator - Changing package names as csit jenkins jobs are trigger based on component names defined in file path Change-Id: Ib8b75ce11185c37bed0d8f4e3918b6660cf24ec8 Issue-ID: SO-1950 Signed-off-by: waqas.ikram --- .../so/sdc/simulator/SdcSimulatorApplication.java | 36 ------- .../configration/WebSecurityConfigImpl.java | 71 ------------- .../simulator/controller/CatalogController.java | 64 ------------ .../controller/SdcSimulatorController.java | 48 --------- .../sdc/simulator/providers/ResourceProvider.java | 36 ------- .../simulator/providers/ResourceProviderImpl.java | 88 ---------------- .../org/onap/so/sdc/simulator/utils/Constants.java | 41 -------- .../so/sdcsimulator/SdcSimulatorApplication.java | 36 +++++++ .../configration/WebSecurityConfigImpl.java | 71 +++++++++++++ .../sdcsimulator/controller/CatalogController.java | 64 ++++++++++++ .../controller/SdcSimulatorController.java | 48 +++++++++ .../sdcsimulator/providers/ResourceProvider.java | 36 +++++++ .../providers/ResourceProviderImpl.java | 88 ++++++++++++++++ .../org/onap/so/sdcsimulator/utils/Constants.java | 45 +++++++++ .../controller/CatalogControllerTest.java | 110 -------------------- .../controller/SdcSimulatorControllerTest.java | 61 ----------- .../providers/ResourceProviderImplTest.java | 98 ------------------ .../controller/CatalogControllerTest.java | 111 +++++++++++++++++++++ .../controller/SdcSimulatorControllerTest.java | 61 +++++++++++ .../providers/ResourceProviderImplTest.java | 99 ++++++++++++++++++ 20 files changed, 659 insertions(+), 653 deletions(-) delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorApplication.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/configration/WebSecurityConfigImpl.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/CatalogController.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/SdcSimulatorController.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProvider.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/utils/Constants.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/SdcSimulatorApplication.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/configration/WebSecurityConfigImpl.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/controller/CatalogController.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/controller/SdcSimulatorController.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/providers/ResourceProvider.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/providers/ResourceProviderImpl.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/utils/Constants.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/CatalogControllerTest.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/SdcSimulatorControllerTest.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/controller/CatalogControllerTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/controller/SdcSimulatorControllerTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/providers/ResourceProviderImplTest.java (limited to 'plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src') diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorApplication.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorApplication.java deleted file mode 100644 index abb183b1..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorApplication.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; - -/** - * @author Waqas Ikram (waqas.ikram@est.tech) - */ -@SpringBootApplication(scanBasePackages = {"org.onap"}) -public class SdcSimulatorApplication extends SpringBootServletInitializer { - - public static void main(final String[] args) { - SpringApplication.run(SdcSimulatorApplication.class, args); - } -} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/configration/WebSecurityConfigImpl.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/configration/WebSecurityConfigImpl.java deleted file mode 100644 index bf971785..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/configration/WebSecurityConfigImpl.java +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator.configration; - -import org.onap.so.sdc.simulator.utils.Constants; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; - -/** - * @author waqas.ikram@ericsson.com - * - */ -@Configuration -@EnableWebSecurity -public class WebSecurityConfigImpl extends WebSecurityConfigurerAdapter { - - private final String username; - private final String password; - private final String role; - - public WebSecurityConfigImpl(@Value("${spring.security.username}") final String username, - @Value("${spring.security.password}") final String password, - @Value("${spring.security.role}") final String role) { - this.username = username; - this.password = password; - this.role = role; - } - - - @Override - protected void configure(final HttpSecurity http) throws Exception { - http.csrf().disable().authorizeRequests().antMatchers(Constants.CATALOG_URL + "/**/**").authenticated().and() - .httpBasic(); - } - - @Bean - public BCryptPasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } - - @Autowired - public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().passwordEncoder(passwordEncoder()).withUser(username).password(password) - .roles(role); - } - -} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/CatalogController.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/CatalogController.java deleted file mode 100644 index 01c0b39f..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/CatalogController.java +++ /dev/null @@ -1,64 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator.controller; - -import static org.onap.so.sdc.simulator.utils.Constants.CATALOG_URL; -import java.util.Optional; -import javax.ws.rs.core.MediaType; -import org.onap.so.sdc.simulator.providers.ResourceProvider; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * @author Waqas Ikram (waqas.ikram@est.tech) - * - */ -@RestController -@RequestMapping(path = CATALOG_URL) -public class CatalogController { - private static final Logger LOGGER = LoggerFactory.getLogger(CatalogController.class); - - private ResourceProvider resourceProvider; - - @Autowired - public CatalogController(final ResourceProvider resourceProvider) { - this.resourceProvider = resourceProvider; - } - - @GetMapping(value = "/resources/{csarId}/toscaModel", produces = MediaType.APPLICATION_OCTET_STREAM) - public ResponseEntity getCsar(@PathVariable("csarId") final String csarId) { - LOGGER.info("Running getCsar for {} ...", csarId); - final Optional resource = resourceProvider.getResource(csarId); - if (resource.isPresent()) { - return new ResponseEntity<>(resource.get(), HttpStatus.OK); - } - LOGGER.error("Unable to find csar: {}", csarId); - - return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); - } - -} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/SdcSimulatorController.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/SdcSimulatorController.java deleted file mode 100644 index 2baf2038..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/SdcSimulatorController.java +++ /dev/null @@ -1,48 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator.controller; - -import javax.ws.rs.core.MediaType; -import org.onap.so.sdc.simulator.utils.Constants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; - -/** - * @author Waqas Ikram (waqas.ikram@est.tech) - */ -@RestController -@RequestMapping(path = Constants.BASE_URL) -public class SdcSimulatorController { - private static final Logger LOGGER = LoggerFactory.getLogger(SdcSimulatorController.class); - - @GetMapping(value = "/healthcheck", produces = MediaType.APPLICATION_JSON) - @ResponseStatus(code = HttpStatus.OK) - public String healthCheck() { - LOGGER.info("Running health check ..."); - return Constants.HEALTHY; - } - -} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProvider.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProvider.java deleted file mode 100644 index 83994b4b..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProvider.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator.providers; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Optional; - -/** - * @author Eoin Hanan (eoin.hanan@est.tech) - */ -public interface ResourceProvider { - - Optional getResource(final String csarId); - - Optional getInputStream(final String csarId) throws IOException; - -} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java deleted file mode 100644 index efc98de7..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java +++ /dev/null @@ -1,88 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator.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 org.onap.so.sdc.simulator.utils.Constants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.ClassPathResource; -import org.springframework.stereotype.Service; -import org.springframework.util.StreamUtils; - -/** - * @author Eoin Hanan (eoin.hanan@est.tech) - */ -@Service -public class ResourceProviderImpl implements ResourceProvider { - - private static final Logger LOGGER = LoggerFactory.getLogger(ResourceProvider.class); - - private final String resourceLocation; - - public ResourceProviderImpl(@Value("${sdc.resource.location:/app/csars/}") final String resourceLocation) { - this.resourceLocation = resourceLocation; - } - - @Override - public Optional getResource(final String csarId) { - try { - final Optional optionalInputStream = getInputStream(csarId); - if (optionalInputStream.isPresent()) { - return Optional.of(StreamUtils.copyToByteArray(optionalInputStream.get())); - } - } catch (final IOException ioException) { - LOGGER.warn("Unable to create file stream ...", ioException); - } - - return Optional.empty(); - } - - @Override - public Optional getInputStream(final String csarId) throws IOException { - final Path filePath = Paths.get(resourceLocation, csarId + ".csar"); - if (Files.exists(filePath)) { - return Optional.of(Files.newInputStream(filePath)); - } - - LOGGER.info("Couldn't find file on file system '{}', will return default csar", filePath); - final ClassPathResource classPathResource = new ClassPathResource(getDefaultCsarPath(), this.getClass()); - if (classPathResource.exists()) { - return Optional.of(classPathResource.getInputStream()); - } - - LOGGER.error("Couldn't find default csar in classpath ...."); - return Optional.empty(); - } - - /* - * Used in test - */ - String getDefaultCsarPath() { - return Constants.DEFAULT_CSAR_PATH; - } -} \ No newline at end of file diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/utils/Constants.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/utils/Constants.java deleted file mode 100644 index 4fa06881..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/utils/Constants.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * ============LICENSE_START======================================================= Copyright (C) 2019 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.so.sdc.simulator.utils; - -/** - * @author Waqas Ikram (waqas.ikram@est.tech) - */ -public class Constants { - - public static final String BASE_URL = "/sdc/v1"; - - public static final String CATALOG_URL = BASE_URL + "/catalog"; - - public static final String HEALTHY = "healthy"; - - public static final String DEFAULT_CSAR_NAME = "default_csar_file"; - - public static final String DOT = "."; - - public static final String DOT_CSAR = DOT + "csar"; - - public static final String DEFAULT_CSAR_NAME_WITH_EXT = DEFAULT_CSAR_NAME + DOT_CSAR; - - public static final String DEFAULT_CSAR_PATH = "/csar/" + DEFAULT_CSAR_NAME_WITH_EXT; - - - private Constants() {} -} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/SdcSimulatorApplication.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/SdcSimulatorApplication.java new file mode 100644 index 00000000..25d21e53 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/SdcSimulatorApplication.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + */ +@SpringBootApplication(scanBasePackages = {"org.onap"}) +public class SdcSimulatorApplication extends SpringBootServletInitializer { + + public static void main(final String[] args) { + SpringApplication.run(SdcSimulatorApplication.class, args); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/configration/WebSecurityConfigImpl.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/configration/WebSecurityConfigImpl.java new file mode 100644 index 00000000..b2c51369 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/configration/WebSecurityConfigImpl.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.configration; + +import org.onap.so.sdcsimulator.utils.Constants; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; + +/** + * @author waqas.ikram@ericsson.com + * + */ +@Configuration +@EnableWebSecurity +public class WebSecurityConfigImpl extends WebSecurityConfigurerAdapter { + + private final String username; + private final String password; + private final String role; + + public WebSecurityConfigImpl(@Value("${spring.security.username}") final String username, + @Value("${spring.security.password}") final String password, + @Value("${spring.security.role}") final String role) { + this.username = username; + this.password = password; + this.role = role; + } + + + @Override + protected void configure(final HttpSecurity http) throws Exception { + http.csrf().disable().authorizeRequests().antMatchers(Constants.CATALOG_URL + "/**/**").authenticated().and() + .httpBasic(); + } + + @Bean + public BCryptPasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Autowired + public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().passwordEncoder(passwordEncoder()).withUser(username).password(password) + .roles(role); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/controller/CatalogController.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/controller/CatalogController.java new file mode 100644 index 00000000..e9f17762 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/controller/CatalogController.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.controller; + +import static org.onap.so.sdcsimulator.utils.Constants.CATALOG_URL; +import java.util.Optional; +import javax.ws.rs.core.MediaType; +import org.onap.so.sdcsimulator.providers.ResourceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@RestController +@RequestMapping(path = CATALOG_URL) +public class CatalogController { + private static final Logger LOGGER = LoggerFactory.getLogger(CatalogController.class); + + private ResourceProvider resourceProvider; + + @Autowired + public CatalogController(final ResourceProvider resourceProvider) { + this.resourceProvider = resourceProvider; + } + + @GetMapping(value = "/resources/{csarId}/toscaModel", produces = MediaType.APPLICATION_OCTET_STREAM) + public ResponseEntity getCsar(@PathVariable("csarId") final String csarId) { + LOGGER.info("Running getCsar for {} ...", csarId); + final Optional resource = resourceProvider.getResource(csarId); + if (resource.isPresent()) { + return new ResponseEntity<>(resource.get(), HttpStatus.OK); + } + LOGGER.error("Unable to find csar: {}", csarId); + + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/controller/SdcSimulatorController.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/controller/SdcSimulatorController.java new file mode 100644 index 00000000..4cd564d4 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/controller/SdcSimulatorController.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.controller; + +import javax.ws.rs.core.MediaType; +import org.onap.so.sdcsimulator.utils.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + */ +@RestController +@RequestMapping(path = Constants.BASE_URL) +public class SdcSimulatorController { + private static final Logger LOGGER = LoggerFactory.getLogger(SdcSimulatorController.class); + + @GetMapping(value = "/healthcheck", produces = MediaType.APPLICATION_JSON) + @ResponseStatus(code = HttpStatus.OK) + public String healthCheck() { + LOGGER.info("Running health check ..."); + return Constants.HEALTHY; + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/providers/ResourceProvider.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/providers/ResourceProvider.java new file mode 100644 index 00000000..4d5dcdd0 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/providers/ResourceProvider.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.providers; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; + +/** + * @author Eoin Hanan (eoin.hanan@est.tech) + */ +public interface ResourceProvider { + + Optional getResource(final String csarId); + + Optional getInputStream(final String csarId) throws IOException; + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/providers/ResourceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/providers/ResourceProviderImpl.java new file mode 100644 index 00000000..192ac896 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/providers/ResourceProviderImpl.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.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 org.onap.so.sdcsimulator.utils.Constants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Service; +import org.springframework.util.StreamUtils; + +/** + * @author Eoin Hanan (eoin.hanan@est.tech) + */ +@Service +public class ResourceProviderImpl implements ResourceProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(ResourceProvider.class); + + private final String resourceLocation; + + public ResourceProviderImpl(@Value("${sdc.resource.location:/app/csars/}") final String resourceLocation) { + this.resourceLocation = resourceLocation; + } + + @Override + public Optional getResource(final String csarId) { + try { + final Optional optionalInputStream = getInputStream(csarId); + if (optionalInputStream.isPresent()) { + return Optional.of(StreamUtils.copyToByteArray(optionalInputStream.get())); + } + } catch (final IOException ioException) { + LOGGER.warn("Unable to create file stream ...", ioException); + } + + return Optional.empty(); + } + + @Override + public Optional getInputStream(final String csarId) throws IOException { + final Path filePath = Paths.get(resourceLocation, csarId + ".csar"); + if (Files.exists(filePath)) { + return Optional.of(Files.newInputStream(filePath)); + } + + LOGGER.info("Couldn't find file on file system '{}', will return default csar", filePath); + final ClassPathResource classPathResource = new ClassPathResource(getDefaultCsarPath(), this.getClass()); + if (classPathResource.exists()) { + return Optional.of(classPathResource.getInputStream()); + } + + LOGGER.error("Couldn't find default csar in classpath ...."); + return Optional.empty(); + } + + /* + * Used in test + */ + String getDefaultCsarPath() { + return Constants.DEFAULT_CSAR_PATH; + } +} \ No newline at end of file diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/utils/Constants.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/utils/Constants.java new file mode 100644 index 00000000..e8412574 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/utils/Constants.java @@ -0,0 +1,45 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.utils; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + */ +public class Constants { + + public static final String BASE_URL = "/sdc/v1"; + + public static final String CATALOG_URL = BASE_URL + "/catalog"; + + public static final String HEALTHY = "healthy"; + + public static final String DEFAULT_CSAR_NAME = "default_csar_file"; + + public static final String DOT = "."; + + public static final String DOT_CSAR = DOT + "csar"; + + public static final String DEFAULT_CSAR_NAME_WITH_EXT = DEFAULT_CSAR_NAME + DOT_CSAR; + + public static final String DEFAULT_CSAR_PATH = "/csar/" + DEFAULT_CSAR_NAME_WITH_EXT; + + private Constants() {} +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/CatalogControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/CatalogControllerTest.java deleted file mode 100644 index 98ec448e..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/CatalogControllerTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator.controller; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import java.util.Base64; -import java.util.Optional; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.onap.so.sdc.simulator.providers.ResourceProvider; -import org.onap.so.sdc.simulator.utils.Constants; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.web.server.LocalServerPort; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -/** - * @author Waqas Ikram (waqas.ikram@est.tech) - * - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ActiveProfiles("test") -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@Configuration -public class CatalogControllerTest { - - private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U"; - - @LocalServerPort - private int port; - - @Autowired - private TestRestTemplate restTemplate; - - @Value("${spring.security.username}") - private String username; - - @Test - public void test_getCsar_validCsarId_matchContent() { - - final String url = getBaseUrl() + "/resources/" + Constants.DEFAULT_CSAR_NAME + "/toscaModel"; - - final ResponseEntity response = - restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class); - - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertTrue(response.hasBody()); - assertEquals(3982, response.getBody().length); - - } - - @Test - public void test_getCsar_invalidCsar_internalServerError() { - final ResourceProvider mockedResourceProvider = Mockito.mock(ResourceProvider.class); - Mockito.when(mockedResourceProvider.getResource(Mockito.anyString())).thenReturn(Optional.empty()); - final CatalogController objUnderTest = new CatalogController(mockedResourceProvider); - - final ResponseEntity response = objUnderTest.getCsar(Constants.DEFAULT_CSAR_NAME); - - assertFalse(response.hasBody()); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); - } - - private String getBaseUrl() { - return "http://localhost:" + port + Constants.CATALOG_URL; - } - - private HttpHeaders getHttpHeaders() { - final HttpHeaders requestHeaders = new HttpHeaders(); - requestHeaders.add("Authorization", getBasicAuth(username)); - requestHeaders.setContentType(MediaType.APPLICATION_JSON); - return requestHeaders; - } - - private String getBasicAuth(final String username) { - return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes())); - } - -} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/SdcSimulatorControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/SdcSimulatorControllerTest.java deleted file mode 100644 index 300b62a8..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/SdcSimulatorControllerTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator.controller; - -import static org.junit.Assert.assertEquals; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.so.sdc.simulator.utils.Constants; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.web.server.LocalServerPort; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.ResponseEntity; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -/** - * @author Waqas Ikram (waqas.ikram@est.tech) - */ -@RunWith(SpringJUnit4ClassRunner.class) -@ActiveProfiles("test") -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@Configuration -public class SdcSimulatorControllerTest { - - @LocalServerPort - private int port; - - @Autowired - private TestRestTemplate restTemplate; - - @Test - public void test_healthCheck_matchContent() { - final String url = "http://localhost:" + port + Constants.BASE_URL + "/healthcheck"; - final ResponseEntity object = restTemplate.getForEntity(url, String.class); - - assertEquals(Constants.HEALTHY, object.getBody()); - - } - -} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java deleted file mode 100644 index b112c403..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 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.so.sdc.simulator.providers; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertFalse; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.onap.so.sdc.simulator.utils.Constants; -import org.springframework.core.io.ClassPathResource; -import org.springframework.util.StreamUtils; - -/** - * @author Waqas Ikram (waqas.ikram@est.tech) - * @author Eoin Hanan (eoin.hanan@est.tech) - */ -public class ResourceProviderImplTest { - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - private static final String DUMMY_CONTENT = "Hell world"; - - @Test - public void test_getResource_withValidPath_matchContent() throws IOException { - final File folder = temporaryFolder.newFolder(); - final Path file = Files.createFile(folder.toPath().resolve("empty.csar")); - - Files.write(file, DUMMY_CONTENT.getBytes()); - - final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(folder.getPath()); - - assertArrayEquals(DUMMY_CONTENT.getBytes(), objUnderTest.getResource("empty").get()); - } - - @Test - public void test_getResource_withoutValidPath_matchContent() throws IOException { - final ClassPathResource classPathResource = new ClassPathResource(Constants.DEFAULT_CSAR_PATH, this.getClass()); - - final byte[] expectedResult = StreamUtils.copyToByteArray(classPathResource.getInputStream()); - - final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(""); - - assertArrayEquals(expectedResult, objUnderTest.getResource(Constants.DEFAULT_CSAR_NAME).get()); - } - - @Test - public void test_getResource_unbleToreadFileFromClasspath_emptyOptional() throws IOException { - - final ResourceProviderImpl objUnderTest = new ResourceProviderImpl("") { - @Override - String getDefaultCsarPath() { - return "/some/dummy/path"; - } - }; - assertFalse(objUnderTest.getResource(Constants.DEFAULT_CSAR_NAME).isPresent()); - - } - - @Test - public void test_getResource_withValidPathAndUnabletoRead_emptyOptional() throws IOException { - final File folder = temporaryFolder.newFolder(); - final Path file = Files.createFile(folder.toPath().resolve("empty.csar")); - - Files.write(file, DUMMY_CONTENT.getBytes()); - file.toFile().setReadable(false); - - final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(folder.getPath()); - - assertFalse(objUnderTest.getResource("empty").isPresent()); - - } - -} \ No newline at end of file diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/controller/CatalogControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/controller/CatalogControllerTest.java new file mode 100644 index 00000000..ca55f495 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/controller/CatalogControllerTest.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.Base64; +import java.util.Optional; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.so.sdcsimulator.controller.CatalogController; +import org.onap.so.sdcsimulator.providers.ResourceProvider; +import org.onap.so.sdcsimulator.utils.Constants; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles("test") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@Configuration +public class CatalogControllerTest { + + private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U"; + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Value("${spring.security.username}") + private String username; + + @Test + public void test_getCsar_validCsarId_matchContent() { + + final String url = getBaseUrl() + "/resources/" + Constants.DEFAULT_CSAR_NAME + "/toscaModel"; + + final ResponseEntity response = + restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response.hasBody()); + assertEquals(3982, response.getBody().length); + + } + + @Test + public void test_getCsar_invalidCsar_internalServerError() { + final ResourceProvider mockedResourceProvider = Mockito.mock(ResourceProvider.class); + Mockito.when(mockedResourceProvider.getResource(Mockito.anyString())).thenReturn(Optional.empty()); + final CatalogController objUnderTest = new CatalogController(mockedResourceProvider); + + final ResponseEntity response = objUnderTest.getCsar(Constants.DEFAULT_CSAR_NAME); + + assertFalse(response.hasBody()); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + + private String getBaseUrl() { + return "http://localhost:" + port + Constants.CATALOG_URL; + } + + private HttpHeaders getHttpHeaders() { + final HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.add("Authorization", getBasicAuth(username)); + requestHeaders.setContentType(MediaType.APPLICATION_JSON); + return requestHeaders; + } + + private String getBasicAuth(final String username) { + return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes())); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/controller/SdcSimulatorControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/controller/SdcSimulatorControllerTest.java new file mode 100644 index 00000000..9789f812 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/controller/SdcSimulatorControllerTest.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.controller; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.so.sdcsimulator.utils.Constants; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles("test") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@Configuration +public class SdcSimulatorControllerTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void test_healthCheck_matchContent() { + final String url = "http://localhost:" + port + Constants.BASE_URL + "/healthcheck"; + final ResponseEntity object = restTemplate.getForEntity(url, String.class); + + assertEquals(Constants.HEALTHY, object.getBody()); + + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/providers/ResourceProviderImplTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/providers/ResourceProviderImplTest.java new file mode 100644 index 00000000..e575f2a5 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdcsimulator/providers/ResourceProviderImplTest.java @@ -0,0 +1,99 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.so.sdcsimulator.providers; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.onap.so.sdcsimulator.providers.ResourceProviderImpl; +import org.onap.so.sdcsimulator.utils.Constants; +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.StreamUtils; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * @author Eoin Hanan (eoin.hanan@est.tech) + */ +public class ResourceProviderImplTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private static final String DUMMY_CONTENT = "Hell world"; + + @Test + public void test_getResource_withValidPath_matchContent() throws IOException { + final File folder = temporaryFolder.newFolder(); + final Path file = Files.createFile(folder.toPath().resolve("empty.csar")); + + Files.write(file, DUMMY_CONTENT.getBytes()); + + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(folder.getPath()); + + assertArrayEquals(DUMMY_CONTENT.getBytes(), objUnderTest.getResource("empty").get()); + } + + @Test + public void test_getResource_withoutValidPath_matchContent() throws IOException { + final ClassPathResource classPathResource = new ClassPathResource(Constants.DEFAULT_CSAR_PATH, this.getClass()); + + final byte[] expectedResult = StreamUtils.copyToByteArray(classPathResource.getInputStream()); + + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(""); + + assertArrayEquals(expectedResult, objUnderTest.getResource(Constants.DEFAULT_CSAR_NAME).get()); + } + + @Test + public void test_getResource_unbleToreadFileFromClasspath_emptyOptional() throws IOException { + + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl("") { + @Override + String getDefaultCsarPath() { + return "/some/dummy/path"; + } + }; + assertFalse(objUnderTest.getResource(Constants.DEFAULT_CSAR_NAME).isPresent()); + + } + + @Test + public void test_getResource_withValidPathAndUnabletoRead_emptyOptional() throws IOException { + final File folder = temporaryFolder.newFolder(); + final Path file = Files.createFile(folder.toPath().resolve("empty.csar")); + + Files.write(file, DUMMY_CONTENT.getBytes()); + file.toFile().setReadable(false); + + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(folder.getPath()); + + assertFalse(objUnderTest.getResource("empty").isPresent()); + + } + +} \ No newline at end of file -- cgit 1.2.3-korg