ASD package validation has been added for descriptor and invariant id.
This validates based on the rApp already created. If the application restarts the cache gets cleared.
Issue-ID: NONRTRIC-984
Change-Id: Icf76553dbe5dca710801fefa551ae9e81d986ca5
Signed-off-by: aravind.est <aravindhan.a@est.tech>
Rapp rapp = Rapp.builder().name(rappId).packageLocation(rappManagerConfiguration.getCsarLocation())
.packageName(csarFile.getName()).state(RappState.COMMISSIONED).build();
rapp.setRappResources(rappCsarConfigurationHandler.getRappResource(rapp));
+ rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
rappCacheService.putRapp(rapp);
return ResponseEntity.accepted().build();
} else {
import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
import com.oransc.rappmanager.models.exception.RappHandlerException;
import com.oransc.rappmanager.models.rapp.Rapp;
-import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
public boolean configureDeploymentArtifacts(Rapp rapp) {
- List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
- return deploymentItems.stream().filter(deploymentItem -> deploymentItem.getArtifactType()
- .equals(DeploymentItemArtifactType.HELMCHART))
+ return rapp.getAsdMetadata().getDeploymentItems().stream()
+ .filter(deploymentItem -> deploymentItem.getArtifactType()
+ .equals(DeploymentItemArtifactType.HELMCHART))
.allMatch(deploymentItem -> uploadHelmChart(rapp, deploymentItem));
}
/*
* ============LICENSE_START======================================================================
- * Copyright (C) 2023 OpenInfra Foundation Europe. All rights reserved.
+ * Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved.
* ===============================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import com.oransc.rappmanager.acm.service.AcmDeployer;
import com.oransc.rappmanager.dme.service.DmeDeployer;
import com.oransc.rappmanager.models.cache.RappCacheService;
+import com.oransc.rappmanager.models.csar.AsdMetadata;
import com.oransc.rappmanager.models.rapp.PrimeOrder;
import com.oransc.rappmanager.models.rapp.Rapp;
import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
import java.io.File;
import java.io.FileInputStream;
+import java.util.List;
import java.util.UUID;
import org.apache.http.entity.ContentType;
import org.junit.jupiter.api.Test;
UUID rappId = UUID.randomUUID();
Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+ AsdMetadata asdMetadata = new AsdMetadata();
+ asdMetadata.setDescriptorId(UUID.randomUUID().toString());
+ asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
+ asdMetadata.setDeploymentItems(List.of());
+ rapp.setAsdMetadata(asdMetadata);
rappCacheService.putRapp(rapp);
mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(1)));
UUID rappId = UUID.randomUUID();
Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+ AsdMetadata asdMetadata = new AsdMetadata();
+ asdMetadata.setDescriptorId(UUID.randomUUID().toString());
+ asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
+ asdMetadata.setDeploymentItems(List.of());
+ rapp.setAsdMetadata(asdMetadata);
rappCacheService.putRapp(rapp);
mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", rappId)).andExpect(status().isOk())
.andExpect(jsonPath("$.rappId").value(rappId.toString()))
UUID rappId = UUID.randomUUID();
Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+ AsdMetadata asdMetadata = new AsdMetadata();
+ asdMetadata.setDescriptorId(UUID.randomUUID().toString());
+ asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
+ asdMetadata.setDeploymentItems(List.of());
+ rapp.setAsdMetadata(asdMetadata);
rappCacheService.putRapp(rapp);
when(acmDeployer.primeRapp(any())).thenReturn(true);
when(dmeDeployer.primeRapp(any())).thenReturn(true);
UUID rappId = UUID.randomUUID();
Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
+ AsdMetadata asdMetadata = new AsdMetadata();
+ asdMetadata.setDescriptorId(UUID.randomUUID().toString());
+ asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
+ asdMetadata.setDeploymentItems(List.of());
+ rapp.setAsdMetadata(asdMetadata);
rappCacheService.putRapp(rapp);
when(acmDeployer.deprimeRapp(any())).thenReturn(true);
when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
UUID rappId = UUID.randomUUID();
Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+ AsdMetadata asdMetadata = new AsdMetadata();
+ asdMetadata.setDescriptorId(UUID.randomUUID().toString());
+ asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
+ asdMetadata.setDeploymentItems(List.of());
+ rapp.setAsdMetadata(asdMetadata);
rappCacheService.putRapp(rapp);
mockMvc.perform(MockMvcRequestBuilders.delete("/rapps/{rapp_id}", rappId)).andExpect(status().isOk());
}
@EnumSource(value = HttpStatus.class, names = {"CREATED", "CONFLICT"})
void testChartUpload(HttpStatus status) {
Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
- .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
- List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
+ .packageLocation(validCsarFileLocation)
+ .state(RappState.COMMISSIONED).build();
+ rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
+ List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
deploymentItems.forEach(deploymentItem -> mockServer.expect(ExpectedCount.once(),
requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
.andRespond(withStatus(status)));
String invalidRappFile = "invalid-rapp-package.csar";
Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(invalidRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+ rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
assertTrue(deploymentArtifactsService.configureDeploymentArtifacts(rapp));
}
void testChartUploadFailure() {
Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
- List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
+ rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
+ List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
.andRespond(withServerError()));
void testChartUploadFailureWithNotFound() {
Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
- List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
+ rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
+ List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST)).andRespond(
withStatus(HttpStatus.NOT_FOUND)));
void testChartUploadFailureWithException() {
Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
- List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
+ rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
+ List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST)).andRespond(
withException(new IOException())));
void testChartUploadFailureWithTooManyRequests() {
Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
.packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
- List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
+ rapp.setAsdMetadata(rappCsarConfigurationHandler.getAsdMetadata(rapp));
+ List<DeploymentItem> deploymentItems = rapp.getAsdMetadata().getDeploymentItems();
deploymentItems.stream().findFirst().ifPresent(deploymentItem -> mockServer.expect(ExpectedCount.once(),
requestTo(deploymentItem.getTargetServerUri())).andExpect(method(HttpMethod.POST))
.andRespond(withTooManyRequests()));
--- /dev/null
+/*
+ * ============LICENSE_START======================================================================
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * ===============================================================================================
+ * 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.
+ * ============LICENSE_END========================================================================
+ *
+ */
+package com.oransc.rappmanager.models.csar;
+
+import com.fasterxml.jackson.databind.PropertyNamingStrategies;
+import com.fasterxml.jackson.databind.annotation.JsonNaming;
+import java.util.List;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
+public class AsdMetadata {
+
+ String description;
+ String descriptorId;
+ String descriptorInvariantId;
+ String descriptorVersion;
+ String schemaVersion;
+ String functionDescription;
+ String provider;
+ String applicationName;
+ String applicationVersion;
+ List<DeploymentItem> deploymentItems;
+}
return asdLocation;
}
- public List<DeploymentItem> getDeploymentItems(Rapp rApp) {
- List<DeploymentItem> deploymentItems = new ArrayList<>();
- File csarFile = getCsarFile(rApp);
- String asdDefinitionLocation = getAsdDefinitionLocation(csarFile);
- if (asdDefinitionLocation != null && !asdDefinitionLocation.isEmpty()) {
- try {
- String asdContent = getFileFromCsar(csarFile, asdDefinitionLocation).toString();
- JsonNode jsonNode = getAsdContentNode(asdContent);
- JsonNode artifactsJsonNode = jsonNode.at(RappCsarPathProvider.ARTIFACTS_LOCATION_JSON_POINTER);
- artifactsJsonNode.forEach(artifactJsonNode -> {
- DeploymentItem deploymentItem =
- objectMapper.convertValue(artifactJsonNode.at("/properties"), DeploymentItem.class);
- deploymentItem.setFile(artifactJsonNode.at("/file").asText());
- deploymentItems.add(deploymentItem);
- });
- } catch (Exception e) {
- logger.warn("Unable to get the deployment items", e);
- }
- }
- return deploymentItems;
- }
+
public String getSmeProviderDomainPayload(Rapp rapp, RappSMEInstance rappSMEInstance) {
return rappResources;
}
+ public AsdMetadata getAsdMetadata(Rapp rApp) {
+ AsdMetadata asdMetadata = new AsdMetadata();
+ File csarFile = getCsarFile(rApp);
+ String asdDefinitionLocation = getAsdDefinitionLocation(csarFile);
+ if (asdDefinitionLocation != null && !asdDefinitionLocation.isEmpty()) {
+ try {
+ String asdContent = getFileFromCsar(csarFile, asdDefinitionLocation).toString();
+ if (asdContent != null && !asdContent.isEmpty()) {
+ JsonNode jsonNode = getAsdContentNode(asdContent);
+ JsonNode asdJsonNode = jsonNode.at(RappCsarPathProvider.ASD_LOCATION_JSON_POINTER);
+ asdMetadata = objectMapper.convertValue(asdJsonNode.at("/properties"), AsdMetadata.class);
+ asdMetadata.setDescription(asdJsonNode.at("/description").asText());
+
+ JsonNode artifactsJsonNode = jsonNode.at(RappCsarPathProvider.ASD_ARTIFACTS_LOCATION_JSON_POINTER);
+ final List<DeploymentItem> deploymentItems = new ArrayList<>();
+ artifactsJsonNode.forEach(artifactJsonNode -> {
+ DeploymentItem deploymentItem =
+ objectMapper.convertValue(artifactJsonNode.at("/properties"), DeploymentItem.class);
+ deploymentItem.setFile(artifactJsonNode.at("/file").asText());
+ deploymentItems.add(deploymentItem);
+ });
+ asdMetadata.setDeploymentItems(deploymentItems);
+ }
+ } catch (Exception e) {
+ logger.warn("Unable to get the asd metadata items", e);
+ }
+ }
+ return asdMetadata;
+ }
+
Set<String> getFileListFromCsar(File csarFile, String dirLocation) {
try (ZipFile zipFile = new ZipFile(csarFile)) {
return zipFile.stream().filter(Predicate.not(ZipEntry::isDirectory)).map(ZipEntry::getName)
public static final String DME_CONSUMER_INFO_TYPES_LOCATION = "Files/Dme/consumerinfotypes";
public static final String DME_INFO_PRODUCERS_LOCATION = "Files/Dme/infoproducers";
public static final String DME_INFO_CONSUMERS_LOCATION = "Files/Dme/infoconsumers";
- public static final String ARTIFACTS_LOCATION_JSON_POINTER =
- "/topology_template/node_templates/applicationServiceDescriptor/artifacts";
-}
\ No newline at end of file
+ public static final String ASD_LOCATION_JSON_POINTER =
+ "/topology_template/node_templates/applicationServiceDescriptor";
+ public static final String ASD_PROPERTIES_JSON_POINTER = ASD_LOCATION_JSON_POINTER + "/properties";
+ public static final String ASD_ARTIFACTS_LOCATION_JSON_POINTER = ASD_LOCATION_JSON_POINTER + "/artifacts";
+ public static final String ASD_DESCRIPTOR_JSON_POINTER = ASD_PROPERTIES_JSON_POINTER + "/descriptor_id";
+ public static final String ASD_DESCRIPTOR_VARIANT_LOCATION_JSON_POINTER =
+ ASD_PROPERTIES_JSON_POINTER + "/descriptor_invariant_id";
+}
private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
private final RappValidationUtils rappValidationUtils;
String invalidAsdErrorMsg = "ASD definition in rApp package is invalid.";
- private static final int VALIDATION_ORDER = 10;
+ private static final int VALIDATION_ORDER = 15;
@Override
public int getOrder() {
public void validate(Object target, Errors errors) {
MultipartFile multipartFile = (MultipartFile) target;
String asdLocation = rappValidationUtils.getAsdDefinitionLocation(multipartFile);
- if (asdLocation != null && !asdLocation.isEmpty() && rappValidationUtils.isFileExistsInCsar(multipartFile,
- asdLocation)) {
- try {
- String asdContent = rappValidationUtils.getFileFromCsar(multipartFile, asdLocation).toString();
- if(asdContent != null && !asdContent.isEmpty()) {
- JsonNode jsonNode = rappCsarConfigurationHandler.getAsdContentNode(asdContent);
- List<String> artifactFileList =
- jsonNode.at(RappCsarPathProvider.ARTIFACTS_LOCATION_JSON_POINTER).findValuesAsText("file");
- artifactFileList.forEach(
- artifactFile -> rappValidationUtils.isFileExistsInCsar(multipartFile, artifactFile));
- } else {
- throw new RappValidationException(invalidAsdErrorMsg);
- }
- } catch (RappValidationException e) {
- throw new RappValidationException(e.getMessage());
- } catch (Exception e) {
- throw new RappValidationException(invalidAsdErrorMsg);
- }
- } else {
+ try {
+ String asdContent = rappValidationUtils.getFileFromCsar(multipartFile, asdLocation).toString();
+ JsonNode jsonNode = rappCsarConfigurationHandler.getAsdContentNode(asdContent);
+ List<String> artifactFileList =
+ jsonNode.at(RappCsarPathProvider.ASD_ARTIFACTS_LOCATION_JSON_POINTER).findValuesAsText("file");
+ artifactFileList.forEach(
+ artifactFile -> rappValidationUtils.isFileExistsInCsar(multipartFile, artifactFile));
+ } catch (RappValidationException e) {
+ throw new RappValidationException(e.getMessage());
+ } catch (Exception e) {
throw new RappValidationException(invalidAsdErrorMsg);
}
}
--- /dev/null
+/*
+ * ============LICENSE_START======================================================================
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * ===============================================================================================
+ * 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.
+ * ============LICENSE_END========================================================================
+ *
+ */
+
+package com.oransc.rappmanager.models.csar.validator;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.oransc.rappmanager.models.cache.RappCacheService;
+import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
+import com.oransc.rappmanager.models.csar.RappCsarPathProvider;
+import com.oransc.rappmanager.models.exception.RappValidationException;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Component;
+import org.springframework.validation.Errors;
+import org.springframework.web.multipart.MultipartFile;
+
+@Component
+@RequiredArgsConstructor
+public class AsdDescriptorValidator implements RappValidator {
+
+ private final RappCacheService rappCacheService;
+ private final RappValidationUtils rappValidationUtils;
+ private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
+ String invalidAsdErrorMsg = "ASD definition in rApp package is invalid.";
+
+ private static final int VALIDATION_ORDER = 10;
+
+ @Override
+ public int getOrder() {
+ return VALIDATION_ORDER;
+ }
+
+ @Override
+ public void validate(Object target, Errors errors) {
+ MultipartFile multipartFile = (MultipartFile) target;
+ String asdLocation = rappValidationUtils.getAsdDefinitionLocation(multipartFile);
+ if (asdLocation != null && !asdLocation.isEmpty() && rappValidationUtils.isFileExistsInCsar(multipartFile,
+ asdLocation)) {
+ try {
+ String asdContent = rappValidationUtils.getFileFromCsar(multipartFile, asdLocation).toString();
+ if (asdContent != null && !asdContent.isEmpty()) {
+ JsonNode jsonNode = rappCsarConfigurationHandler.getAsdContentNode(asdContent);
+ checkAsdDescriptorExists(jsonNode.at(RappCsarPathProvider.ASD_DESCRIPTOR_JSON_POINTER).asText());
+ checkAsdDescriptorVariantExists(
+ jsonNode.at(RappCsarPathProvider.ASD_DESCRIPTOR_VARIANT_LOCATION_JSON_POINTER).asText());
+ } else {
+ throw new RappValidationException(invalidAsdErrorMsg);
+ }
+ } catch (RappValidationException e) {
+ throw new RappValidationException(e.getMessage());
+ } catch (Exception e) {
+ throw new RappValidationException(invalidAsdErrorMsg);
+ }
+ } else {
+ throw new RappValidationException(invalidAsdErrorMsg);
+ }
+ }
+
+ boolean checkAsdDescriptorExists(String descriptorId) {
+ if (rappCacheService.getAllRapp().stream()
+ .anyMatch(rapp -> rapp.getAsdMetadata().getDescriptorId().equals(descriptorId))) {
+ throw new RappValidationException("ASD descriptor already exists.");
+ }
+ return true;
+ }
+
+ boolean checkAsdDescriptorVariantExists(String descriptorVariantId) {
+ if (rappCacheService.getAllRapp().stream()
+ .anyMatch(rapp -> rapp.getAsdMetadata().getDescriptorInvariantId().equals(descriptorVariantId))) {
+ throw new RappValidationException("ASD descriptor invariant already exists.");
+ }
+ return true;
+ }
+}
package com.oransc.rappmanager.models.rapp;
+import com.oransc.rappmanager.models.csar.AsdMetadata;
import com.oransc.rappmanager.models.rappinstance.RappInstance;
import java.beans.Transient;
import java.util.HashMap;
String packageLocation;
String packageName;
RappResources rappResources;
+ AsdMetadata asdMetadata;
@Builder.Default
Map<UUID, RappInstance> rappInstances = new HashMap<>();
/*
* ============LICENSE_START======================================================================
- * Copyright (C) 2023 OpenInfra Foundation Europe. All rights reserved.
+ * Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved.
* ===============================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* ============LICENSE_END========================================================================
*/
-package com.oransc.rappmanager.models.cache;
+package com.oransc.rappmanager.models;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
@TestConfiguration
-class BeanTestConfiguration {
+public class BeanTestConfiguration {
@Bean
public CacheManager cacheManager() {
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import com.oransc.rappmanager.models.BeanTestConfiguration;
import com.oransc.rappmanager.models.rapp.Rapp;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oransc.rappmanager.models.rappinstance.RappSMEInstance;
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.util.List;
+import java.io.IOException;
import java.util.Set;
import java.util.UUID;
+import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
assertThat(fileByteArray.size()).isZero();
}
+ @Test
+ void testInvalidZipStreamGetFromCsar() throws IOException {
+ ZipArchiveInputStream zipArchiveInputStream = mock(ZipArchiveInputStream.class);
+ doThrow(new IOException()).when(zipArchiveInputStream).getNextEntry();
+ ByteArrayOutputStream fileByteArray = rappCsarConfigurationHandler.getFileFromCsar(zipArchiveInputStream, null);
+ assertThat(fileByteArray.size()).isZero();
+ }
+
@Test
void testListResources() {
UUID rappId = UUID.randomUUID();
}
@Test
- void testListDeploymentItems() {
+ void testGetAsdMetadata() {
Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
- List<DeploymentItem> deploymentItems = rappCsarConfigurationHandler.getDeploymentItems(rapp);
- assertEquals(2, deploymentItems.size());
+ AsdMetadata asdMetadata = rappCsarConfigurationHandler.getAsdMetadata(rapp);
+ assertEquals("123e4567-e89b-12d3-a456-426614174000", asdMetadata.getDescriptorId());
+ assertEquals("040eff2a-eb1a-4aff-bd46-37ce38092985", asdMetadata.getDescriptorInvariantId());
+ assertEquals(2, asdMetadata.getDeploymentItems().size());
}
@ParameterizedTest
@ValueSource(strings = {invalidRappNoAsdFile, invalidRappEmptyAsdFile})
- void testListDeploymentItemsNoAsd(String packageName) {
+ void testGetAsdMetadataNoAsd(String packageName) {
Rapp rapp = Rapp.builder().name("").packageName(packageName).packageLocation(validCsarFileLocation).build();
- assertThat(rappCsarConfigurationHandler.getDeploymentItems(rapp)).isEmpty();
+ assertThat(rappCsarConfigurationHandler.getAsdMetadata(rapp)).isNotNull();
}
@Test
- void testListDeploymentItemsWithException() throws JsonProcessingException {
+ void testGetAsdMetadataException() throws JsonProcessingException {
Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
doThrow(new JsonSyntaxException("")).when(rappCsarConfigurationHandler).getAsdContentNode(any());
- assertThat(rappCsarConfigurationHandler.getDeploymentItems(rapp)).isEmpty();
+ assertNull(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDescriptorId());
+ assertNull(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDescriptorInvariantId());
+ assertThat(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDeploymentItems()).isNull();
+ }
+
+ @Test
+ void testGetAsdMetadataNullAsdContent() throws JsonProcessingException {
+ Rapp rapp = Rapp.builder().name("").packageName(validRappFile).packageLocation(validCsarFileLocation).build();
+ doReturn(null).when(rappCsarConfigurationHandler).getAsdContentNode(any());
+ assertNull(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDescriptorId());
+ assertNull(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDescriptorInvariantId());
+ assertThat(rappCsarConfigurationHandler.getAsdMetadata(rapp).getDeploymentItems()).isNull();
}
@Test
assertTrue(exception.getMessage().startsWith("rApp package missing a file"));
}
- @Test
- void testCsarNoAsdFailure() {
- MultipartFile multipartFile = mock(MultipartFile.class);
- RappValidationException exception = assertThrows(RappValidationException.class,
- () -> artifactDefinitionValidator.validate(multipartFile, null));
- assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
- assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
- }
- @ParameterizedTest
- @NullSource
- @ValueSource(strings = {""})
- void testCsarAsdLocationNullFailure(String asdLocation) throws IOException {
- String rappCsarPath = validCsarFileLocation + File.separator + invalidRappAsdEmptyFile;
- MultipartFile multipartFile =
- new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
- new FileInputStream(rappCsarPath));
- doReturn(asdLocation).when(rappValidationUtils).getAsdDefinitionLocation(any());
- RappValidationException exception = assertThrows(RappValidationException.class,
- () -> artifactDefinitionValidator.validate(multipartFile, null));
- assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
- assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
- }
@ParameterizedTest
@NullSource
- @ValueSource(strings = {"", "{asasdasd"})
+ @ValueSource(strings = {"{asasdasd"})
void testCsarAsdContentInvalidFailure(String asdContent) throws IOException {
String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
MultipartFile multipartFile =
--- /dev/null
+/*
+ * ============LICENSE_START======================================================================
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * ===============================================================================================
+ * 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.
+ * ============LICENSE_END========================================================================
+ *
+ */
+
+package com.oransc.rappmanager.models.csar.validator;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.doReturn;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.oransc.rappmanager.models.BeanTestConfiguration;
+import com.oransc.rappmanager.models.cache.RappCacheService;
+import com.oransc.rappmanager.models.csar.AsdMetadata;
+import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
+import com.oransc.rappmanager.models.exception.RappValidationException;
+import com.oransc.rappmanager.models.rapp.Rapp;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.UUID;
+import org.apache.http.entity.ContentType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.SpyBean;
+import org.springframework.cache.CacheManager;
+import org.springframework.http.HttpStatus;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.web.multipart.MultipartFile;
+
+@SpringBootTest
+@ContextConfiguration(classes = {BeanTestConfiguration.class, AsdDescriptorValidator.class, RappValidationUtils.class,
+ ObjectMapper.class, RappCsarConfigurationHandler.class, RappCacheService.class, CacheManager.class})
+class AsdDescriptorValidatorTest {
+
+ @Autowired
+ AsdDescriptorValidator asdDescriptorValidator;
+ @SpyBean
+ RappValidationUtils rappValidationUtils;
+ @Autowired
+ RappCacheService rappCacheService;
+
+ String validCsarFileLocation = "src/test/resources/";
+ String validRappFile = "valid-rapp-package.csar";
+ String invalidRappAsdEmptyFile = "invalid-rapp-package-empty-asd-yaml.csar";
+
+ @Test
+ void testCsarContainsValidAsdFile() throws IOException {
+ String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
+ MultipartFile multipartFile =
+ new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
+ new FileInputStream(rappCsarPath));
+ assertDoesNotThrow(() -> asdDescriptorValidator.validate(multipartFile, null));
+ }
+
+ @Test
+ void testCsarContainsDuplicateDescriptorId() throws IOException {
+ String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
+ AsdMetadata asdMetadata = new AsdMetadata();
+ asdMetadata.setDescriptorId("123e4567-e89b-12d3-a456-426614174000");
+ asdMetadata.setDescriptorInvariantId(UUID.randomUUID().toString());
+ Rapp rapp = Rapp.builder().name("").asdMetadata(asdMetadata).build();
+ rappCacheService.putRapp(rapp);
+ MultipartFile multipartFile =
+ new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
+ new FileInputStream(rappCsarPath));
+ RappValidationException exception =
+ assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
+ assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
+ assertEquals("ASD descriptor already exists.", exception.getMessage());
+ rappCacheService.deleteRapp(rapp);
+ }
+
+ @Test
+ void testCsarContainsDuplicateDescriptorInvariantId() throws IOException {
+ String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
+ AsdMetadata asdMetadata = new AsdMetadata();
+ asdMetadata.setDescriptorId("");
+ asdMetadata.setDescriptorInvariantId("040eff2a-eb1a-4aff-bd46-37ce38092985");
+ Rapp rapp = Rapp.builder().name("").asdMetadata(asdMetadata).build();
+ rappCacheService.putRapp(rapp);
+ MultipartFile multipartFile =
+ new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
+ new FileInputStream(rappCsarPath));
+ RappValidationException exception =
+ assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
+ assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
+ assertEquals("ASD descriptor invariant already exists.", exception.getMessage());
+ rappCacheService.deleteRapp(rapp);
+ }
+
+ @Test
+ void testCsarContainsValidAsdFileFailure() throws IOException {
+ String rappCsarPath = validCsarFileLocation + File.separator + invalidRappAsdEmptyFile;
+ MultipartFile multipartFile =
+ new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
+ new FileInputStream(rappCsarPath));
+ RappValidationException exception =
+ assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
+ assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
+ assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
+ }
+
+ @ParameterizedTest
+ @NullSource
+ @ValueSource(strings = {""})
+ void testCsarAsdLocationNullFailure(String asdLocation) throws IOException {
+ String rappCsarPath = validCsarFileLocation + File.separator + invalidRappAsdEmptyFile;
+ MultipartFile multipartFile =
+ new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
+ new FileInputStream(rappCsarPath));
+ doReturn(asdLocation).when(rappValidationUtils).getAsdDefinitionLocation(any());
+ RappValidationException exception =
+ assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
+ assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
+ assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
+ }
+
+ @ParameterizedTest
+ @NullSource
+ @ValueSource(strings = {"", "{asasdasd"})
+ void testCsarAsdContentInvalidFailure(String asdContent) throws IOException {
+ String rappCsarPath = validCsarFileLocation + File.separator + validRappFile;
+ MultipartFile multipartFile =
+ new MockMultipartFile(rappCsarPath, rappCsarPath, ContentType.MULTIPART_FORM_DATA.getMimeType(),
+ new FileInputStream(rappCsarPath));
+ if (asdContent != null) {
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ byteArrayOutputStream.write(asdContent.getBytes(), 0, asdContent.getBytes().length);
+ doCallRealMethod().doReturn(byteArrayOutputStream).when(rappValidationUtils)
+ .getFileFromCsar(any(MultipartFile.class), any());
+ } else {
+ doCallRealMethod().doReturn(asdContent).when(rappValidationUtils)
+ .getFileFromCsar(any(MultipartFile.class), any());
+ }
+ RappValidationException exception =
+ assertThrows(RappValidationException.class, () -> asdDescriptorValidator.validate(multipartFile, null));
+ assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
+ assertEquals("ASD definition in rApp package is invalid.", exception.getMessage());
+ }
+}
import static org.mockito.Mockito.mock;
import com.fasterxml.jackson.databind.ObjectMapper;
+import com.oransc.rappmanager.models.BeanTestConfiguration;
+import com.oransc.rappmanager.models.cache.RappCacheService;
import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
import com.oransc.rappmanager.models.exception.RappValidationException;
import java.io.File;
import org.springframework.web.multipart.MultipartFile;
@SpringBootTest
-@ContextConfiguration(classes = {RappValidationHandler.class, NamingValidator.class, FileExistenceValidator.class,
- ArtifactDefinitionValidator.class, RappValidationUtils.class, RappCsarConfigurationHandler.class,
- ObjectMapper.class})
+@ContextConfiguration(classes = {BeanTestConfiguration.class, RappValidationHandler.class, NamingValidator.class,
+ FileExistenceValidator.class, ArtifactDefinitionValidator.class, AsdDescriptorValidator.class,
+ RappValidationUtils.class, RappCsarConfigurationHandler.class, ObjectMapper.class, RappCacheService.class})
class RappValidationHandlerTest {
@Autowired
RappValidationException exception = assertThrows(RappValidationException.class,
() -> rappValidationUtils.getFileFromCsar(multipartFile, null));
assertEquals(HttpStatus.BAD_REQUEST, exception.getStatusCode());
- assertEquals(String.format("Unable to get file %s from the multipart CSAR file", null), exception.getMessage());
+ assertEquals(String.format("Unable to get file %s from the multipart CSAR file", (Object) null), exception.getMessage());
}
@Test
description: "rapp-all"\r
properties:\r
descriptor_id: 123e4567-e89b-12d3-a456-426614174000\r
- descriptor_invariant_id: 123e4yyy-e89b-12d3-a456-426614174abc\r
+ descriptor_invariant_id: 040eff2a-eb1a-4aff-bd46-37ce38092985\r
descriptor_version: 1.0\r
schema_version: 2.0\r
function_description: rApp description\r