From a42ef3e8f52fb5e47ddaf72e7f86ba09b90c7b99 Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Thu, 1 Aug 2019 11:51:24 +0000 Subject: Adding basic auth support to sdc sim Change-Id: Ifa01f084150ec0f67e30e18a4f74c35633a4c0a7 Issue-ID: SO-1949 Signed-off-by: waqas.ikram --- .../so-simulators/sdc-simulator/pom.xml | 19 +++- .../so/sdc/simulator/SdcSimulatorController.java | 71 ------------- .../configration/WebSecurityConfigImpl.java | 71 +++++++++++++ .../simulator/controller/CatalogController.java | 64 ++++++++++++ .../controller/SdcSimulatorController.java | 48 +++++++++ .../simulator/providers/ResourceProviderImpl.java | 4 +- .../org/onap/so/sdc/simulator/utils/Constant.java | 40 -------- .../org/onap/so/sdc/simulator/utils/Constants.java | 41 ++++++++ .../src/main/resources/application.yaml | 12 ++- .../sdc/simulator/SdcSimulatorControllerTest.java | 96 ------------------ .../controller/CatalogControllerTest.java | 110 +++++++++++++++++++++ .../controller/SdcSimulatorControllerTest.java | 61 ++++++++++++ .../providers/ResourceProviderImplTest.java | 8 +- 13 files changed, 424 insertions(+), 221 deletions(-) delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorController.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/configration/WebSecurityConfigImpl.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/controller/CatalogController.java create 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/utils/Constant.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/utils/Constants.java delete mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/CatalogControllerTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/SdcSimulatorControllerTest.java (limited to 'plans/so/integration-etsi-testing/so-simulators/sdc-simulator') diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/pom.xml b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/pom.xml index 397d4a7c..e5cc3d32 100644 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/pom.xml +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/pom.xml @@ -1,15 +1,25 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> so-simulators org.onap.so.simulators 1.0-SNAPSHOT 4.0.0 - sdc-simulator ${project.artifactId} - + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-tomcat + + + + @@ -32,5 +42,4 @@ - - + \ 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/SdcSimulatorController.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorController.java deleted file mode 100644 index b844caa2..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorController.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; - -import java.util.Optional; -import javax.ws.rs.core.MediaType; -import org.onap.so.sdc.simulator.providers.ResourceProvider; -import org.onap.so.sdc.simulator.utils.Constant; -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.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; - -/** - * @author Waqas Ikram (waqas.ikram@est.tech) - */ -@RestController -@RequestMapping(path = Constant.BASE_URL) -public class SdcSimulatorController { - private static final Logger LOGGER = LoggerFactory.getLogger(SdcSimulatorController.class); - - private ResourceProvider resourceProvider; - - public SdcSimulatorController(@Autowired final ResourceProvider resourceProvider) { - this.resourceProvider = resourceProvider; - } - - @GetMapping(value = "/healthcheck", produces = MediaType.APPLICATION_JSON) - @ResponseStatus(code = HttpStatus.OK) - public String healthCheck() { - LOGGER.info("Running health check ..."); - return Constant.HEALTHY; - } - - @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/configration/WebSecurityConfigImpl.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/configration/WebSecurityConfigImpl.java new file mode 100644 index 00000000..bf971785 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/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.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 new file mode 100644 index 00000000..01c0b39f --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/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.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 new file mode 100644 index 00000000..2baf2038 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/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.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/ResourceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java index cdc96f61..efc98de7 100644 --- 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 @@ -26,7 +26,7 @@ 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.Constant; +import org.onap.so.sdc.simulator.utils.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -83,6 +83,6 @@ public class ResourceProviderImpl implements ResourceProvider { * Used in test */ String getDefaultCsarPath() { - return Constant.DEFAULT_CSAR_PATH; + 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/Constant.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/utils/Constant.java deleted file mode 100644 index d22de711..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/utils/Constant.java +++ /dev/null @@ -1,40 +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 Constant { - - public static final String BASE_URL = "/simulator/sdc/v1/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 Constant() { - } -} 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 new file mode 100644 index 00000000..4fa06881 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/utils/Constants.java @@ -0,0 +1,41 @@ +/* + * ============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/resources/application.yaml b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/application.yaml index 89a13aa5..5bb7950a 100644 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/application.yaml +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/application.yaml @@ -1,5 +1,11 @@ server: - port: 9991 - tomcat: - max-threads: 4 + port: 9991 + tomcat: + max-threads: 4 ssl-enable: false +spring: + security: + username: mso + #password: Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U + password: $2a$04$Lcu/DWdyXsl/a3A0iqHTfOX1.zHQ3DlQS/nOPfafT.9pWbeEqlF7W + role: mso \ No newline at end of file diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java deleted file mode 100644 index 7834425d..00000000 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java +++ /dev/null @@ -1,96 +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 static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -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.Constant; -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.HttpStatus; -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 = getBaseUrl() + "/healthcheck"; - final ResponseEntity object = restTemplate.getForEntity(url, String.class); - - assertEquals(Constant.HEALTHY, object.getBody()); - - } - - @Test - public void test_getCsar_validCsarId_matchContent() { - - final String url = getBaseUrl() + "/resources/" + Constant.DEFAULT_CSAR_NAME + "/toscaModel"; - - final ResponseEntity response = restTemplate.getForEntity(url, byte[].class); - - assertTrue(response.hasBody()); - assertEquals(3982, response.getBody().length); - - assertEquals(HttpStatus.OK, response.getStatusCode()); - } - - @Test - public void test_getCsar_invalidCsar_internalServerError() { - final ResourceProvider mockedResourceProvider = Mockito.mock(ResourceProvider.class); - Mockito.when(mockedResourceProvider.getResource(Mockito.anyString())).thenReturn(Optional.empty()); - final SdcSimulatorController objUnderTest = new SdcSimulatorController(mockedResourceProvider); - - final ResponseEntity response = objUnderTest.getCsar(Constant.DEFAULT_CSAR_NAME); - - assertFalse(response.hasBody()); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); - } - - private String getBaseUrl() { - return "http://localhost:" + port + Constant.BASE_URL; - } - -} 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 new file mode 100644 index 00000000..98ec448e --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/CatalogControllerTest.java @@ -0,0 +1,110 @@ +/*- + * ============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 new file mode 100644 index 00000000..300b62a8 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/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.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 index 4871d511..b112c403 100644 --- 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 @@ -30,7 +30,7 @@ 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.Constant; +import org.onap.so.sdc.simulator.utils.Constants; import org.springframework.core.io.ClassPathResource; import org.springframework.util.StreamUtils; @@ -59,13 +59,13 @@ public class ResourceProviderImplTest { @Test public void test_getResource_withoutValidPath_matchContent() throws IOException { - final ClassPathResource classPathResource = new ClassPathResource(Constant.DEFAULT_CSAR_PATH, this.getClass()); + 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(Constant.DEFAULT_CSAR_NAME).get()); + assertArrayEquals(expectedResult, objUnderTest.getResource(Constants.DEFAULT_CSAR_NAME).get()); } @Test @@ -77,7 +77,7 @@ public class ResourceProviderImplTest { return "/some/dummy/path"; } }; - assertFalse(objUnderTest.getResource(Constant.DEFAULT_CSAR_NAME).isPresent()); + assertFalse(objUnderTest.getResource(Constants.DEFAULT_CSAR_NAME).isPresent()); } -- cgit 1.2.3-korg