diff options
Diffstat (limited to 'vnfmarket-be/vnf-sdk-marketplace')
4 files changed, 139 insertions, 59 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/pom.xml b/vnfmarket-be/vnf-sdk-marketplace/pom.xml index 57ce010a..a9885976 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/pom.xml +++ b/vnfmarket-be/vnf-sdk-marketplace/pom.xml @@ -295,7 +295,7 @@ due to Security Issues:- CVE-2019-10241,CVE-2019-10247,CVE-2019-10246 <forkMode>always</forkMode> <argLine> -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar - <!--${surefireArgLine}--> + ${surefireArgLine} </argLine> <skip>${maven.test.skip}</skip> <testFailureIgnore>${maven.test.failure.ignore}</testFailureIgnore> diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java index 777080f1..4b008395 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java @@ -17,8 +17,11 @@ package org.onap.vnfsdk.marketplace.common; import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -30,8 +33,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; -import java.io.FileWriter; -import java.io.FileReader; + import java.nio.file.Files; import java.nio.file.Paths; @@ -73,12 +75,12 @@ public final class FileUtil { boolean isFileDeleted=false; try { if (file.exists()){ - Files.delete(Paths.get(file.getPath())); - fileAbsPath = file.getAbsolutePath(); - logger.info("delete {} {}" ,hintInfo, fileAbsPath); + Files.delete(Paths.get(file.getPath())); + fileAbsPath = file.getAbsolutePath(); + logger.info("delete {} {}" ,hintInfo, fileAbsPath); } else{ - logger.info("file not exist. no need delete {} {}" ,hintInfo , fileAbsPath); + logger.info("file not exist. no need delete {} {}" ,hintInfo , fileAbsPath); } isFileDeleted=true; @@ -118,7 +120,7 @@ public final class FileUtil { } try (InputStream input = zipFile.getInputStream(entry); - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));) { + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));) { int length = 0; while ((length = input.read(buffer)) != -1) { bos.write(buffer, 0, length); @@ -188,7 +190,7 @@ public final class FileUtil { public static boolean deleteDirectory(File file) { if (!file.exists()) { - return true; + return true; } if (file.isDirectory()) { for (File f : file.listFiles()) { @@ -246,4 +248,40 @@ public final class FileUtil { return true; } + + /** + * Search file in folder + * @param folder + * @param keyword + * @return + */ + public static List<File> searchFiles(File folder, String keyword) { + List<File> result = new ArrayList<File>(); + if (folder.isFile()) + result.add(folder); + + File[] subFolders = folder.listFiles(new FileFilter() { + @Override + public boolean accept(File file) { + if (file.isDirectory()) { + return true; + } + if (file.getName().toLowerCase().contains(keyword)) { + return true; + } + return false; + } + }); + + if (subFolders != null) { + for (File file : subFolders) { + if (file.isFile()) { + result.add(file); + } else { + result.addAll(searchFiles(file, keyword)); + } + } + } + return result; + } } diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java index 2eff61f8..274cacd9 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/scenario/VTPScenarioResource.java @@ -16,12 +16,26 @@ package org.onap.vtp.scenario; -import java.io.*; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.text.MessageFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.regex.Matcher; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -33,6 +47,7 @@ import org.glassfish.jersey.media.multipart.BodyPartEntity; import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.glassfish.jersey.media.multipart.FormDataParam; import org.onap.vnfsdk.marketplace.common.CommonConstant; +import org.onap.vnfsdk.marketplace.common.FileUtil; import org.onap.vnfsdk.marketplace.common.ToolUtil; import org.onap.vtp.VTPResource; import org.onap.vtp.error.VTPError; @@ -70,7 +85,7 @@ public class VTPScenarioResource extends VTPResource{ args.addAll(Arrays.asList( PRODUCT_ARG, OPEN_CLI, "product-list", FORMAT, "json" - )); + )); JsonElement results = null; @@ -84,18 +99,18 @@ public class VTPScenarioResource extends VTPResource{ if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { JsonArray resultsArray = results.getAsJsonArray(); - for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { - JsonElement jsonElement = it.next(); - JsonObject n = jsonElement.getAsJsonObject(); - if (n.entrySet().iterator().hasNext()) { - String name = n.get("product").getAsString(); + for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + if (n.entrySet().iterator().hasNext()) { + String name = n.get("product").getAsString(); - if (OPEN_CLI.equalsIgnoreCase(name)) - continue; + if (OPEN_CLI.equalsIgnoreCase(name)) + continue; - list.getScenarios().add(new VTPTestScenario().setName(name).setDescription( - n.get(DESCRIPTION).getAsString())); - } + list.getScenarios().add(new VTPTestScenario().setName(name).setDescription( + n.get(DESCRIPTION).getAsString())); + } } } @@ -119,7 +134,7 @@ public class VTPScenarioResource extends VTPResource{ args.addAll(Arrays.asList( PRODUCT_ARG, OPEN_CLI, "service-list", PRODUCT_ARG, scenario, FORMAT, "json" - )); + )); JsonElement results = null; try { @@ -132,14 +147,14 @@ public class VTPScenarioResource extends VTPResource{ if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { JsonArray resultsArray = results.getAsJsonArray(); - for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { - JsonElement jsonElement = it.next(); - JsonObject n = jsonElement.getAsJsonObject(); - if (n.entrySet().iterator().hasNext()) { - list.getSuites().add(new VTPTestSuite().setName(n.get(SERVICE).getAsString()).setDescription( - n.get(DESCRIPTION).getAsString())); - } + for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + if (n.entrySet().iterator().hasNext()) { + list.getSuites().add(new VTPTestSuite().setName(n.get(SERVICE).getAsString()).setDescription( + n.get(DESCRIPTION).getAsString())); } + } } return list; @@ -164,7 +179,7 @@ public class VTPScenarioResource extends VTPResource{ args.addAll(Arrays.asList( PRODUCT_ARG, OPEN_CLI, "schema-list", PRODUCT_ARG, scenario, FORMAT, "json" - )); + )); if (testSuiteName != null) { args.add("--service"); args.add(testSuiteName); @@ -181,15 +196,15 @@ public class VTPScenarioResource extends VTPResource{ if (results != null && results.isJsonArray() && results.getAsJsonArray().size()>0) { JsonArray resultsArray = results.getAsJsonArray(); - for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { - JsonElement jsonElement = it.next(); - JsonObject n = jsonElement.getAsJsonObject(); - if (n.entrySet().iterator().hasNext()) - list.getTestCases().add( - new VTPTestCase().setTestCaseName( - n.get("command").getAsString()).setTestSuiteName( - n.get(SERVICE).getAsString())); - } + for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) { + JsonElement jsonElement = it.next(); + JsonObject n = jsonElement.getAsJsonObject(); + if (n.entrySet().iterator().hasNext()) + list.getTestCases().add( + new VTPTestCase().setTestCaseName( + n.get("command").getAsString()).setTestSuiteName( + n.get(SERVICE).getAsString())); + } } return list; @@ -204,9 +219,9 @@ public class VTPScenarioResource extends VTPResource{ message = "Failed to perform the operation", response = VTPError.class) }) public Response listTestcases( - @ApiParam("Test scenario name") @PathParam("scenario") String scenario, - @ApiParam("Test suite name") @QueryParam("testSuiteName") String testSuiteName - ) throws VTPException { + @ApiParam("Test scenario name") @PathParam("scenario") String scenario, + @ApiParam("Test suite name") @QueryParam("testSuiteName") String testSuiteName + ) throws VTPException { return Response.ok(this.listTestcasesHandler(testSuiteName, scenario).getTestCases().toString(), MediaType.APPLICATION_JSON).build(); } @@ -215,7 +230,7 @@ public class VTPScenarioResource extends VTPResource{ List<String> args = new ArrayList<>(); args.addAll(Arrays.asList( PRODUCT_ARG, OPEN_CLI, "schema-show", PRODUCT_ARG, scenario, "--service", testSuiteName, "--command", testCaseName , FORMAT, "json" - )); + )); JsonElement results = null; try { results = this.makeRpcAndGetJson(args); @@ -282,7 +297,7 @@ public class VTPScenarioResource extends VTPResource{ @ApiParam("Test scenario name") @PathParam("scenario") String scenario, @ApiParam(value = "Test case name") @PathParam("testSuiteName") String testSuiteName, @ApiParam(value = "Test case name") @PathParam("testCaseName") String testCaseName) - throws VTPException { + throws VTPException { return Response.ok(this.getTestcaseHandler(scenario, testSuiteName, testCaseName).toString(), MediaType.APPLICATION_JSON).build(); } @@ -311,8 +326,8 @@ public class VTPScenarioResource extends VTPResource{ try { FileUtils.deleteQuietly(yamlFile); FileUtils.deleteDirectory(scenarioDir); - FileUtils.copyInputStreamToFile(entity.getInputStream(), yamlFile); FileUtils.forceMkdir(scenarioDir); + FileUtils.copyInputStreamToFile(entity.getInputStream(), yamlFile); } catch (IOException e) { LOG.error("Save yaml {} failed", fileName, e); } @@ -333,7 +348,7 @@ public class VTPScenarioResource extends VTPResource{ } for (Object cmd : (List) serviceMap.get("commands")) { File source = new File(VTP_YAML_STORE, cmd.toString().replaceAll("::", Matcher.quoteReplacement(File.separator))); - if (!cn.hutool.core.io.FileUtil.isFile(source)) { + if (!source.isFile()) { LOG.error("Source {} is not a yaml file !!!", source.getName()); continue; } @@ -370,16 +385,7 @@ public class VTPScenarioResource extends VTPResource{ public Response deleteScenario(@ApiParam("Test scenario yaml") @PathParam("scenarioName") String scenarioName) throws VTPException { String scenario = scenarioName.substring(0, scenarioName.indexOf("-registry")); File scenarioDir = new File(VTP_YAML_STORE, scenario); - List<File> yamls = cn.hutool.core.io.FileUtil.loopFiles(scenarioDir, new FileFilter() { - @Override - public boolean accept(File pathname) { - final String path = pathname.getPath(); - if (null != path && path.endsWith(CommonConstant.YAML_SUFFIX)) { - return true; - } - return false; - } - }); + List<File> yamls = FileUtil.searchFiles(scenarioDir, CommonConstant.YAML_SUFFIX); if (!CollectionUtils.isEmpty(yamls)) { LOG.error("The scenario yaml {} has sub testcase yamls, delete failed", scenarioName); throw new VTPException( @@ -435,4 +441,4 @@ public class VTPScenarioResource extends VTPResource{ }); return Response.ok("Save success", MediaType.APPLICATION_JSON).build(); } -} +}
\ No newline at end of file diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPScenarioResourceTest.java b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPScenarioResourceTest.java index f0f0cce8..55457795 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPScenarioResourceTest.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/test/java/org/onap/vtp/VTPScenarioResourceTest.java @@ -17,6 +17,8 @@ package org.onap.vtp; import com.google.gson.JsonElement; import com.google.gson.JsonParser; +import org.glassfish.jersey.media.multipart.ContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataBodyPart; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,6 +39,12 @@ public class VTPScenarioResourceTest { VTPScenarioResource vtpScenarioResource; + @Mock + FormDataBodyPart formDataBodyPart; + @Mock + ContentDisposition contentDisposition; + + @Before public void setUp() throws Exception { @@ -85,4 +93,32 @@ public class VTPScenarioResourceTest { { vtpScenarioResource.getTestcaseHandler("open-cli","testsuit","testcase"); } + + @Test(expected = NullPointerException.class) + public void testStorageScenarios() throws Exception + { + List<FormDataBodyPart> bodyParts = new ArrayList<>(); + formDataBodyPart.setName("abc"); + formDataBodyPart.setValue("123"); + formDataBodyPart.setContentDisposition(contentDisposition); + bodyParts.add(formDataBodyPart); + vtpScenarioResource.storageScenarios(bodyParts); + } + + @Test + public void testDeleteScenario() throws Exception + { + vtpScenarioResource.deleteScenario("demo-registry.yaml"); + } + + @Test(expected = NullPointerException.class) + public void testStorageTestcases() throws Exception + { + List<FormDataBodyPart> bodyParts = new ArrayList<>(); + formDataBodyPart.setName("abc"); + formDataBodyPart.setValue("123"); + formDataBodyPart.setContentDisposition(contentDisposition); + bodyParts.add(formDataBodyPart); + vtpScenarioResource.storageTestcases(bodyParts); + } }
\ No newline at end of file |