diff options
author | waqas.ikram <waqas.ikram@est.tech> | 2019-08-01 11:51:24 +0000 |
---|---|---|
committer | Waqas Ikram <waqas.ikram@est.tech> | 2019-08-01 11:52:49 +0000 |
commit | a42ef3e8f52fb5e47ddaf72e7f86ba09b90c7b99 (patch) | |
tree | 93c7587d9e268c2acccd3664589e5b7b530a1d2d /plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test | |
parent | 2b358fda2c644673fb816b75a1fa9412754480b6 (diff) |
Adding basic auth support to sdc sim
Change-Id: Ifa01f084150ec0f67e30e18a4f74c35633a4c0a7
Issue-ID: SO-1949
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
Diffstat (limited to 'plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test')
-rw-r--r-- | plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/CatalogControllerTest.java (renamed from plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java) | 50 | ||||
-rw-r--r-- | plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/controller/SdcSimulatorControllerTest.java | 61 | ||||
-rw-r--r-- | plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java | 8 |
3 files changed, 97 insertions, 22 deletions
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/controller/CatalogControllerTest.java index 7834425d..98ec448e 100644 --- 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/controller/CatalogControllerTest.java @@ -17,37 +17,45 @@ * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ - -package org.onap.so.sdc.simulator; +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.Constant; +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 SdcSimulatorControllerTest { +public class CatalogControllerTest { + + private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U"; @LocalServerPort private int port; @@ -55,42 +63,48 @@ public class SdcSimulatorControllerTest { @Autowired private TestRestTemplate restTemplate; - @Test - public void test_healthCheck_matchContent() { - final String url = getBaseUrl() + "/healthcheck"; - final ResponseEntity<String> object = restTemplate.getForEntity(url, String.class); - - assertEquals(Constant.HEALTHY, object.getBody()); - - } + @Value("${spring.security.username}") + private String username; @Test public void test_getCsar_validCsarId_matchContent() { - final String url = getBaseUrl() + "/resources/" + Constant.DEFAULT_CSAR_NAME + "/toscaModel"; + final String url = getBaseUrl() + "/resources/" + Constants.DEFAULT_CSAR_NAME + "/toscaModel"; - final ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class); + final ResponseEntity<byte[]> response = + restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class); + assertEquals(HttpStatus.OK, response.getStatusCode()); 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 CatalogController objUnderTest = new CatalogController(mockedResourceProvider); - final ResponseEntity<byte[]> response = objUnderTest.getCsar(Constant.DEFAULT_CSAR_NAME); + final ResponseEntity<byte[]> response = objUnderTest.getCsar(Constants.DEFAULT_CSAR_NAME); assertFalse(response.hasBody()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); } private String getBaseUrl() { - return "http://localhost:" + port + Constant.BASE_URL; + 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<String> 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()); } |