From 1e7e4a53684df04ba248c20d884ba907ca7c2870 Mon Sep 17 00:00:00 2001 From: Eliezio Oliveira Date: Wed, 31 Jul 2019 11:50:26 +0100 Subject: Add declarative acceptance tests First two UATs are for blueprints Echo and "PNF Configuration". The body of the ODL mount request was changed from XML to JSON, so it can be represented in a YAML file. Initial documentation about the UATs can be found at components/model-catalog/blueprint-model/uat-blueprints/README.md BluePrintArchiveUtils.recurseFiles() replaced by compressFolder() that uses native Java 7. Removed commons-compress as dependency since is no longer used. Change-Id: I96a584ae12ca009f90fe8fe9485eb57ce05e8add Issue-ID: CCSDK-1569 Signed-off-by: Eliezio Oliveira --- .../core/FileExtensionFunctions.kt | 2 +- .../core/utils/BluePrintArchiveUtils.kt | 109 +++++++++++++-------- .../db/resources/BlueprintCatalogServiceImpl.kt | 2 +- .../service/utils/BluePrintEnhancerUtils.kt | 2 +- .../enhancer/BluePrintEnhancerServiceImplTest.kt | 7 -- ms/controllerblueprints/parent/pom.xml | 9 -- 6 files changed, 73 insertions(+), 58 deletions(-) (limited to 'ms/controllerblueprints') diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/FileExtensionFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/FileExtensionFunctions.kt index 18091e630..518e9b236 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/FileExtensionFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/FileExtensionFunctions.kt @@ -52,7 +52,7 @@ fun File.compress(targetZipFileName: String): File { * Compress the current Dir to the target zip file and return the target zip file */ fun File.compress(targetZipFile: File): File { - BluePrintArchiveUtils.compress(this, targetZipFile, true) + BluePrintArchiveUtils.compress(this, targetZipFile) return targetZipFile } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintArchiveUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintArchiveUtils.kt index d3670419e..dcfa07feb 100755 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintArchiveUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/BluePrintArchiveUtils.kt @@ -1,6 +1,7 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. * Modifications Copyright © 2019 Bell Canada. + * Modifications Copyright © 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. @@ -17,17 +18,26 @@ package org.onap.ccsdk.cds.controllerblueprints.core.utils -import org.apache.commons.compress.archivers.zip.ZipArchiveEntry -import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream -import org.apache.commons.io.IOUtils +import com.google.common.base.Predicates import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.slf4j.LoggerFactory import java.io.BufferedInputStream +import java.io.ByteArrayOutputStream import java.io.File -import java.io.FileInputStream +import java.io.FileOutputStream import java.io.IOException +import java.io.OutputStream import java.nio.charset.Charset +import java.nio.file.FileVisitResult +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.SimpleFileVisitor +import java.nio.file.attribute.BasicFileAttributes +import java.util.function.Predicate +import java.util.zip.Deflater +import java.util.zip.ZipEntry import java.util.zip.ZipFile +import java.util.zip.ZipOutputStream class BluePrintArchiveUtils { @@ -39,14 +49,14 @@ class BluePrintArchiveUtils { * * @param source the base directory * @param destination the output filename - * @param absolute store absolute filepath (from directory) or only filename * @return True if OK */ - fun compress(source: File, destination: File, absolute: Boolean): Boolean { + fun compress(source: File, destination: File): Boolean { try { destination.createNewFile() - ZipArchiveOutputStream(destination).use { - recurseFiles(source, source, it, absolute) + val ignoreZipFiles = Predicate { path -> !path.endsWith(".zip") && !path.endsWith(".ZIP") } + FileOutputStream(destination).use { out -> + compressFolder(source.toPath(), out, pathFilter = ignoreZipFiles) } } catch (e: Exception) { log.error("Fail to compress folder($source) to path(${destination.path})", e) @@ -56,40 +66,61 @@ class BluePrintArchiveUtils { } /** - * Recursive traversal to add files - * - * @param root - * @param file - * @param zaos - * @param absolute - * @throws IOException + * In-memory compress an entire folder. */ - @Throws(IOException::class) - private fun recurseFiles(root: File, file: File, zaos: ZipArchiveOutputStream, - absolute: Boolean) { - if (file.isDirectory) { - // recursive call - val files = file.listFiles() - for (fileChild in files!!) { - recurseFiles(root, fileChild, zaos, absolute) - } - } else if (!file.name.endsWith(".zip") && !file.name.endsWith(".ZIP")) { - val filename = if (absolute) { - file.absolutePath.substring(root.absolutePath.length) - } else { - file.name - } - val zae = ZipArchiveEntry(filename) - zae.size = file.length() - zaos.putArchiveEntry(zae) - FileInputStream(file).use { - IOUtils.copy(it, zaos) - it.close() - } - zaos.closeArchiveEntry() - } + fun compressToBytes(baseDir: Path, compressionLevel: Int = Deflater.NO_COMPRESSION): ByteArray { + return compressFolder(baseDir, ByteArrayOutputStream(), compressionLevel = compressionLevel) + .toByteArray() } + /** + * Compress an entire folder. + * + * @param baseDir path of base folder to be packaged. + * @param output the output stream + * @param pathFilter filter to ignore files based on its path. + * @param compressionLevel the wanted compression level. + * @param fixedModificationTime to force every entry to have this modification time. + * Useful for reproducible operations, like tests, for example. + */ + private fun compressFolder(baseDir: Path, output: T, + pathFilter: Predicate = Predicates.alwaysTrue(), + compressionLevel: Int = Deflater.DEFAULT_COMPRESSION, + fixedModificationTime: Long? = null): T + where T : OutputStream { + ZipOutputStream(output) + .apply { setLevel(compressionLevel) } + .use { zos -> + Files.walkFileTree(baseDir, object : SimpleFileVisitor() { + @Throws(IOException::class) + override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { + if (pathFilter.test(file)) { + val zipEntry = ZipEntry(baseDir.relativize(file).toString()) + fixedModificationTime?.let { + zipEntry.time = it + } + zipEntry.time = 0; + zos.putNextEntry(zipEntry) + Files.copy(file, zos) + zos.closeEntry() + } + return FileVisitResult.CONTINUE + } + + @Throws(IOException::class) + override fun preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult { + val zipEntry = ZipEntry(baseDir.relativize(dir).toString() + "/") + fixedModificationTime?.let { + zipEntry.time = it + } + zos.putNextEntry(zipEntry) + zos.closeEntry() + return FileVisitResult.CONTINUE + } + }) + } + return output + } fun deCompress(zipFile: File, targetPath: String): File { val zip = ZipFile(zipFile, Charset.defaultCharset()) diff --git a/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/db/resources/BlueprintCatalogServiceImpl.kt b/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/db/resources/BlueprintCatalogServiceImpl.kt index 9780bbd31..b3436a991 100644 --- a/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/db/resources/BlueprintCatalogServiceImpl.kt +++ b/ms/controllerblueprints/modules/db-resources/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/db/resources/BlueprintCatalogServiceImpl.kt @@ -46,7 +46,7 @@ abstract class BlueprintCatalogServiceImpl( workingDir = blueprintFile.absolutePath archiveFile = normalizedFile(bluePrintPathConfiguration.blueprintArchivePath, processingId, "cba.zip") - if (!BluePrintArchiveUtils.compress(blueprintFile, archiveFile, true)) { + if (!BluePrintArchiveUtils.compress(blueprintFile, archiveFile)) { throw BluePrintException("Fail to compress blueprint") } } else { diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt index d4753e194..a0f8ca9c5 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/utils/BluePrintEnhancerUtils.kt @@ -109,7 +109,7 @@ class BluePrintEnhancerUtils { suspend fun compressToFilePart(enhanceDir: String, archiveDir: String): ResponseEntity { val compressedFile = normalizedFile(archiveDir, "enhanced-cba.zip") - BluePrintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile, true) + BluePrintArchiveUtils.compress(Paths.get(enhanceDir).toFile(), compressedFile) return prepareResourceEntity(compressedFile.name, compressedFile.readBytes()) } diff --git a/ms/controllerblueprints/modules/service/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.kt b/ms/controllerblueprints/modules/service/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.kt index 1f872c2da..d09479b6c 100644 --- a/ms/controllerblueprints/modules/service/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.kt +++ b/ms/controllerblueprints/modules/service/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.kt @@ -66,7 +66,6 @@ class BluePrintEnhancerServiceImplTest { testBaseConfigEnhancementAndValidation() testVFWEnhancementAndValidation() testGoldenEnhancementAndValidation() - testCapabilityRestconfEnhancementAndValidation() testRemoteScriptsEnhancementAndValidation() testCapabilityCliEnhancementAndValidation() } @@ -87,12 +86,6 @@ class BluePrintEnhancerServiceImplTest { testComponentInvokeEnhancementAndValidation(basePath, "golden-enhance") } - fun testCapabilityRestconfEnhancementAndValidation() { - val basePath = "./../../../../components/model-catalog/blueprint-model/test-blueprint/capability_restconf" - testComponentInvokeEnhancementAndValidation(basePath, "capability_restconf-enhance") - - } - fun testRemoteScriptsEnhancementAndValidation() { val basePath = "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts" testComponentInvokeEnhancementAndValidation(basePath, "remote_scripts-enhance") diff --git a/ms/controllerblueprints/parent/pom.xml b/ms/controllerblueprints/parent/pom.xml index f2f4ff12b..43208b914 100644 --- a/ms/controllerblueprints/parent/pom.xml +++ b/ms/controllerblueprints/parent/pom.xml @@ -97,11 +97,6 @@ commons-io 2.6 - - org.apache.commons - commons-compress - 1.15 - org.apache.velocity velocity @@ -297,10 +292,6 @@ commons-io commons-io - - org.apache.commons - commons-compress - com.jayway.jsonpath json-path -- cgit 1.2.3-korg