diff options
Diffstat (limited to 'plans/so/integration-etsi-testing/so-simulators/sdc-simulator')
12 files changed, 405 insertions, 52 deletions
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 27e7c3ea..e694fef1 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 @@ -10,6 +10,11 @@ <name>${project.artifactId}</name> <dependencies> <dependency> + <groupId>${project.parent.groupId}</groupId> + <artifactId>common</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <exclusions> 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 index b2c51369..a8ede989 100644 --- 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 @@ -20,15 +20,12 @@ package org.onap.so.sdcsimulator.configration; import org.onap.so.sdcsimulator.utils.Constants; +import org.onap.so.simulator.configuration.SimulatorSecurityConfigurer; +import org.onap.so.simulator.model.UserCredentials; 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 @@ -36,18 +33,12 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; */ @Configuration @EnableWebSecurity -public class WebSecurityConfigImpl extends WebSecurityConfigurerAdapter { +public class WebSecurityConfigImpl extends SimulatorSecurityConfigurer { - 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; + @Autowired + public WebSecurityConfigImpl(final UserCredentials userCredentials) { + super(userCredentials.getUsers()); } @@ -57,15 +48,5 @@ public class WebSecurityConfigImpl extends WebSecurityConfigurerAdapter { .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 index 60c1865d..eff63b84 100644 --- 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 @@ -49,6 +49,12 @@ public class CatalogController { this.resourceProvider = resourceProvider; } + @GetMapping(value = "/resources", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity<?> getResources() { + LOGGER.info("Running getResources ..."); + return ResponseEntity.ok().body(resourceProvider.getResource()); + } + @GetMapping(value = "/resources/{csarId}/toscaModel", produces = MediaType.APPLICATION_OCTET_STREAM) public ResponseEntity<byte[]> getCsar(@PathVariable("csarId") final String csarId) { LOGGER.info("Running getCsar for {} ...", csarId); diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/models/ResourceArtifact.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/models/ResourceArtifact.java new file mode 100644 index 00000000..0b9e6cf1 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdcsimulator/models/ResourceArtifact.java @@ -0,0 +1,245 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.models; + +import java.io.Serializable; +import org.springframework.util.ObjectUtils; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public class ResourceArtifact implements Serializable { + + private static final long serialVersionUID = 3967660000071162759L; + + @JsonProperty("uuid") + private String uuid; + + @JsonProperty("invariantUUID") + private String invariantUuid; + + @JsonProperty("name") + private String name; + + @JsonProperty("version") + private String version; + + @JsonProperty("toscaModelURL") + private String toscaModelUrl; + + @JsonProperty("category") + private String category; + + @JsonProperty("subCategory") + private String subCategory; + + @JsonProperty("resourceType") + private String resourceType; + + @JsonProperty("lifecycleState") + private String lifecycleState; + + @JsonProperty("lastUpdaterUserId") + private String lastUpdaterUserId; + + public String getUuid() { + return uuid; + } + + public void setUuid(final String uuid) { + this.uuid = uuid; + } + + public ResourceArtifact uuid(final String uuid) { + this.uuid = uuid; + return this; + } + + public String getInvariantUuid() { + return invariantUuid; + } + + public void setInvariantUuid(final String invariantUuid) { + this.invariantUuid = invariantUuid; + } + + public ResourceArtifact invariantUuid(final String invariantUuid) { + this.invariantUuid = invariantUuid; + return this; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public ResourceArtifact name(final String name) { + this.name = name; + return this; + } + + public String getVersion() { + return version; + } + + public void setVersion(final String version) { + this.version = version; + } + + public ResourceArtifact version(final String version) { + this.version = version; + return this; + } + + public String getToscaModelUrl() { + return toscaModelUrl; + } + + public void setToscaModelUrl(final String toscaModelUrl) { + this.toscaModelUrl = toscaModelUrl; + } + + public ResourceArtifact toscaModelUrl(final String toscaModelUrl) { + this.toscaModelUrl = toscaModelUrl; + return this; + } + + public String getCategory() { + return category; + } + + public void setCategory(final String category) { + this.category = category; + } + + public ResourceArtifact category(final String category) { + this.category = category; + return this; + } + + public String getSubCategory() { + return subCategory; + } + + public void setSubCategory(final String subCategory) { + this.subCategory = subCategory; + } + + public ResourceArtifact subCategory(final String subCategory) { + this.subCategory = subCategory; + return this; + } + + public String getResourceType() { + return resourceType; + } + + public void setResourceType(final String resourceType) { + this.resourceType = resourceType; + } + + public ResourceArtifact resourceType(final String resourceType) { + this.resourceType = resourceType; + return this; + } + + public String getLifecycleState() { + return lifecycleState; + } + + public void setLifecycleState(final String lifecycleState) { + this.lifecycleState = lifecycleState; + } + + public ResourceArtifact lifecycleState(final String lifecycleState) { + this.lifecycleState = lifecycleState; + return this; + } + + public String getLastUpdaterUserId() { + return lastUpdaterUserId; + } + + public void setLastUpdaterUserId(final String lastUpdaterUserId) { + this.lastUpdaterUserId = lastUpdaterUserId; + } + + public ResourceArtifact lastUpdaterUserId(final String lastUpdaterUserId) { + this.lastUpdaterUserId = lastUpdaterUserId; + return this; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((category == null) ? 0 : category.hashCode()); + result = prime * result + ((invariantUuid == null) ? 0 : invariantUuid.hashCode()); + result = prime * result + ((lastUpdaterUserId == null) ? 0 : lastUpdaterUserId.hashCode()); + result = prime * result + ((lifecycleState == null) ? 0 : lifecycleState.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((resourceType == null) ? 0 : resourceType.hashCode()); + result = prime * result + ((subCategory == null) ? 0 : subCategory.hashCode()); + result = prime * result + ((toscaModelUrl == null) ? 0 : toscaModelUrl.hashCode()); + result = prime * result + ((uuid == null) ? 0 : uuid.hashCode()); + result = prime * result + ((version == null) ? 0 : version.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (obj instanceof ResourceArtifact) { + final ResourceArtifact other = (ResourceArtifact) obj; + return ObjectUtils.nullSafeEquals(category, other.category) + && ObjectUtils.nullSafeEquals(invariantUuid, other.invariantUuid) + && ObjectUtils.nullSafeEquals(lastUpdaterUserId, other.lastUpdaterUserId) + && ObjectUtils.nullSafeEquals(lifecycleState, other.lifecycleState) + && ObjectUtils.nullSafeEquals(name, other.name) + && ObjectUtils.nullSafeEquals(resourceType, other.resourceType) + && ObjectUtils.nullSafeEquals(subCategory, other.subCategory) + && ObjectUtils.nullSafeEquals(toscaModelUrl, other.toscaModelUrl) + && ObjectUtils.nullSafeEquals(uuid, other.uuid) + && ObjectUtils.nullSafeEquals(version, other.version); + } + return false; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder(); + sb.append("class ResourceArtifact {\n"); + sb.append(" uuid: ").append(uuid).append("\n"); + sb.append(" invariantUuid: ").append(invariantUuid).append("\n"); + sb.append(" name: ").append(name).append("\n"); + sb.append(" version: ").append(version).append("\n"); + sb.append(" toscaModelUrl: ").append(toscaModelUrl).append("\n"); + sb.append(" category: ").append(category).append("\n"); + sb.append(" subCategory: ").append(subCategory).append("\n"); + sb.append(" lastUpdaterUserId: ").append(lastUpdaterUserId).append("\n"); + sb.append("}"); + return sb.toString(); + } + +} 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 index 4d5dcdd0..48353a7c 100644 --- 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 @@ -20,9 +20,9 @@ package org.onap.so.sdcsimulator.providers; -import java.io.IOException; -import java.io.InputStream; import java.util.Optional; +import java.util.Set; +import org.onap.so.sdcsimulator.models.ResourceArtifact; /** * @author Eoin Hanan (eoin.hanan@est.tech) @@ -31,6 +31,6 @@ public interface ResourceProvider { Optional<byte[]> getResource(final String csarId); - Optional<InputStream> getInputStream(final String csarId) throws IOException; + Set<ResourceArtifact> getResource(); } 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 index 192ac896..eacc9d5c 100644 --- 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 @@ -20,17 +20,29 @@ package org.onap.so.sdcsimulator.providers; +import static org.onap.so.sdcsimulator.utils.Constants.CATALOG_URL; +import static org.onap.so.sdcsimulator.utils.Constants.DOT_CSAR; +import static org.onap.so.sdcsimulator.utils.Constants.MAIN_RESOURCE_FOLDER; +import static org.onap.so.sdcsimulator.utils.Constants.WILD_CARD_REGEX; +import static org.springframework.core.io.support.ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX; import java.io.IOException; import java.io.InputStream; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.HashSet; import java.util.Optional; +import java.util.Set; +import org.onap.so.sdcsimulator.models.ResourceArtifact; import org.onap.so.sdcsimulator.utils.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.stereotype.Service; import org.springframework.util.StreamUtils; @@ -44,8 +56,13 @@ public class ResourceProviderImpl implements ResourceProvider { private final String resourceLocation; - public ResourceProviderImpl(@Value("${sdc.resource.location:/app/csars/}") final String resourceLocation) { + private final ResourcePatternResolver resourcePatternResolver; + + @Autowired + public ResourceProviderImpl(@Value("${sdc.resource.location:/app/csars/}") final String resourceLocation, + final ResourcePatternResolver resourcePatternResolver) { this.resourceLocation = resourceLocation; + this.resourcePatternResolver = resourcePatternResolver; } @Override @@ -63,15 +80,80 @@ public class ResourceProviderImpl implements ResourceProvider { } @Override - public Optional<InputStream> getInputStream(final String csarId) throws IOException { - final Path filePath = Paths.get(resourceLocation, csarId + ".csar"); + public Set<ResourceArtifact> getResource() { + final Set<ResourceArtifact> result = new HashSet<>(); + + final Path dir = Paths.get(resourceLocation); + if (Files.exists(dir)) { + try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir, WILD_CARD_REGEX + DOT_CSAR)) { + for (final Path entry : stream) { + final String filename = getFilenameWithoutExtension(entry); + final ResourceArtifact artifact = getResourceArtifact(filename); + result.add(artifact); + LOGGER.info("Found resource on file system: {}", artifact); + + + } + } catch (final IOException ioException) { + LOGGER.error("Unable to find resources on filesystem", ioException); + } + } + + try { + final String csarFileLocationPattern = + CLASSPATH_ALL_URL_PREFIX + MAIN_RESOURCE_FOLDER + WILD_CARD_REGEX + DOT_CSAR; + final Resource[] resources = resourcePatternResolver.getResources(csarFileLocationPattern); + if (resources != null) { + + for (final Resource resource : resources) { + final ResourceArtifact artifact = + getResourceArtifact(getFilenameWithoutExtension(resource.getFilename())); + result.add(artifact); + LOGGER.info("Found resource in classpath: {}", artifact); + } + } + + } catch (final IOException ioException) { + LOGGER.error("Unable to find resources in classpath", ioException); + } + + return result; + } + + private ResourceArtifact getResourceArtifact(final String filename) { + return new ResourceArtifact().uuid(filename).invariantUuid(filename).name(filename).version("1.0") + .toscaModelUrl(CATALOG_URL + "/resources/" + filename + "/toscaModel").category("Generic") + .subCategory("Network Service").resourceType("VF").lifecycleState("CERTIFIED") + .lastUpdaterUserId("SDC_SIMULATOR"); + } + + private String getFilenameWithoutExtension(final String filename) { + return filename.substring(0, filename.lastIndexOf('.')); + } + + private String getFilenameWithoutExtension(final Path file) { + return getFilenameWithoutExtension(file.getFileName().toString()); + } + + private Optional<InputStream> getInputStream(final String csarId) throws IOException { + final Path filePath = Paths.get(resourceLocation, csarId + DOT_CSAR); if (Files.exists(filePath)) { + LOGGER.info("Found resource in on file system using path: {}", filePath); return Optional.of(Files.newInputStream(filePath)); } + LOGGER.warn("Couldn't find file on file system '{}', will search it in classpath", filePath); - LOGGER.info("Couldn't find file on file system '{}', will return default csar", filePath); - final ClassPathResource classPathResource = new ClassPathResource(getDefaultCsarPath(), this.getClass()); + final String path = MAIN_RESOURCE_FOLDER + csarId + DOT_CSAR; + ClassPathResource classPathResource = getClassPathResource(path); if (classPathResource.exists()) { + LOGGER.info("Found resource in classpath using path: {}", path); + return Optional.of(classPathResource.getInputStream()); + } + + LOGGER.warn("Couldn't find file on file system '{}', will return default csar", filePath); + classPathResource = getClassPathResource(getDefaultCsarPath()); + if (classPathResource.exists()) { + LOGGER.info("Found default csar in classpath"); return Optional.of(classPathResource.getInputStream()); } @@ -79,10 +161,14 @@ public class ResourceProviderImpl implements ResourceProvider { return Optional.empty(); } + private ClassPathResource getClassPathResource(final String path) { + return new ClassPathResource(path, this.getClass()); + } + /* * 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 index e8412574..3ddb5bee 100644 --- 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 @@ -25,6 +25,8 @@ package org.onap.so.sdcsimulator.utils; */ public class Constants { + public static final String MAIN_RESOURCE_FOLDER = "/csar/"; + public static final String BASE_URL = "/sdc/v1"; public static final String CATALOG_URL = BASE_URL + "/catalog"; @@ -35,11 +37,13 @@ public class Constants { public static final String DOT = "."; + public static final String WILD_CARD_REGEX = "*"; + 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; + public static final String DEFAULT_CSAR_PATH = MAIN_RESOURCE_FOLDER + 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 5bb7950a..8ae1e8ac 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 @@ -5,7 +5,12 @@ server: 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 + users: + - username: mso + #password: Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U + password: $2a$04$Lcu/DWdyXsl/a3A0iqHTfOX1.zHQ3DlQS/nOPfafT.9pWbeEqlF7W + role: mso + - username: modeling + #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/main/resources/csar/73522444-e8e9-49c1-be29-d355800aa349.csar b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/csar/73522444-e8e9-49c1-be29-d355800aa349.csar Binary files differnew file mode 100644 index 00000000..db8f12d8 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/csar/73522444-e8e9-49c1-be29-d355800aa349.csar diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/csar/ba1c0c0e-9fb8-4b83-97bb-5e9af1e73393.csar b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/csar/ba1c0c0e-9fb8-4b83-97bb-5e9af1e73393.csar Binary files differnew file mode 100644 index 00000000..4e0400fa --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/csar/ba1c0c0e-9fb8-4b83-97bb-5e9af1e73393.csar 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 index ca55f495..e63a7f90 100644 --- 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 @@ -24,19 +24,21 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Base64; import java.util.Optional; +import java.util.Set; 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.models.ResourceArtifact; import org.onap.so.sdcsimulator.providers.ResourceProvider; import org.onap.so.sdcsimulator.utils.Constants; +import org.onap.so.simulator.model.UserCredentials; 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.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -64,8 +66,8 @@ public class CatalogControllerTest { @Autowired private TestRestTemplate restTemplate; - @Value("${spring.security.username}") - private String username; + @Autowired + private UserCredentials userCredentials; @Test public void test_getCsar_validCsarId_matchContent() { @@ -82,6 +84,19 @@ public class CatalogControllerTest { } @Test + public void test_getResources_validResourcesFromClassPath() { + + final ResponseEntity<Set<ResourceArtifact>> response = + restTemplate.exchange(getBaseUrl() + "/resources", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), + new ParameterizedTypeReference<Set<ResourceArtifact>>() {}); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertTrue(response.hasBody()); + assertEquals(3, response.getBody().size()); + + } + + @Test public void test_getCsar_invalidCsar_internalServerError() { final ResourceProvider mockedResourceProvider = Mockito.mock(ResourceProvider.class); Mockito.when(mockedResourceProvider.getResource(Mockito.anyString())).thenReturn(Optional.empty()); @@ -99,7 +114,7 @@ public class CatalogControllerTest { private HttpHeaders getHttpHeaders() { final HttpHeaders requestHeaders = new HttpHeaders(); - requestHeaders.add("Authorization", getBasicAuth(username)); + requestHeaders.add("Authorization", getBasicAuth(userCredentials.getUsers().get(0).getUsername())); requestHeaders.setContentType(MediaType.APPLICATION_JSON); return requestHeaders; } 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 index a7cb5dd7..c132184c 100644 --- 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 @@ -26,11 +26,13 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.UUID; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.onap.so.sdcsimulator.utils.Constants; import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.util.StreamUtils; /** @@ -42,18 +44,22 @@ public class ResourceProviderImplTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); - private static final String DUMMY_CONTENT = "Hell world"; + private static final String DUMMY_CONTENT = "Hello world"; + + private final PathMatchingResourcePatternResolver resourcePatternResolver = + new PathMatchingResourcePatternResolver(); @Test public void test_getResource_withValidPath_matchContent() throws IOException { final File folder = temporaryFolder.newFolder(); - final Path file = Files.createFile(folder.toPath().resolve("empty.csar")); + final String uuid = UUID.randomUUID().toString(); + final Path file = Files.createFile(folder.toPath().resolve(uuid + Constants.DOT_CSAR)); Files.write(file, DUMMY_CONTENT.getBytes()); - final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(folder.getPath()); + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(folder.getPath(), resourcePatternResolver); - assertArrayEquals(DUMMY_CONTENT.getBytes(), objUnderTest.getResource("empty").get()); + assertArrayEquals(DUMMY_CONTENT.getBytes(), objUnderTest.getResource(uuid).get()); } @Test @@ -62,21 +68,21 @@ public class ResourceProviderImplTest { final byte[] expectedResult = StreamUtils.copyToByteArray(classPathResource.getInputStream()); - final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(""); + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl("", resourcePatternResolver); assertArrayEquals(expectedResult, objUnderTest.getResource(Constants.DEFAULT_CSAR_NAME).get()); } @Test - public void test_getResource_unbleToreadFileFromClasspath_emptyOptional() throws IOException { + public void test_getResource_unbleToReadFileFromClasspath_emptyOptional() throws IOException { - final ResourceProviderImpl objUnderTest = new ResourceProviderImpl("") { + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl("", resourcePatternResolver) { @Override String getDefaultCsarPath() { return "/some/dummy/path"; } }; - assertFalse(objUnderTest.getResource(Constants.DEFAULT_CSAR_NAME).isPresent()); + assertFalse(objUnderTest.getResource(UUID.randomUUID().toString()).isPresent()); } } |