From 7c1a76876693ee1cbfab6c5cdecbfc7af831fc5c Mon Sep 17 00:00:00 2001 From: Ryan O'Meara Date: Wed, 13 Jan 2016 14:37:13 -0500 Subject: [PATCH] ISSUE #3 Changed string search row to include lines and comments --- .../SampleReportDataIdentifiedFiles.java | 33 +++++---- .../SampleReportDataIdentifiedFilesTest.java | 71 +++++++++++++++++++ 2 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 protex-sdk-examples/src/test/java/com/blackducksoftware/sdk/protex/client/examples/test/report/SampleReportDataIdentifiedFilesTest.java diff --git a/protex-sdk-examples/src/main/java/com/blackducksoftware/sdk/protex/client/examples/report/SampleReportDataIdentifiedFiles.java b/protex-sdk-examples/src/main/java/com/blackducksoftware/sdk/protex/client/examples/report/SampleReportDataIdentifiedFiles.java index 6258bf0..862a0ab 100644 --- a/protex-sdk-examples/src/main/java/com/blackducksoftware/sdk/protex/client/examples/report/SampleReportDataIdentifiedFiles.java +++ b/protex-sdk-examples/src/main/java/com/blackducksoftware/sdk/protex/client/examples/report/SampleReportDataIdentifiedFiles.java @@ -24,6 +24,7 @@ import com.blackducksoftware.sdk.protex.project.codetree.identification.Identification; import com.blackducksoftware.sdk.protex.project.codetree.identification.IdentificationApi; import com.blackducksoftware.sdk.protex.project.codetree.identification.IdentificationType; +import com.blackducksoftware.sdk.protex.project.codetree.identification.IdentifiedStringSearchMatchLocation; import com.blackducksoftware.sdk.protex.project.codetree.identification.StringSearchIdentification; import com.blackducksoftware.sdk.protex.util.CodeTreeUtilities; @@ -144,22 +145,26 @@ public void doWork(String projectId, String parentPath, List thisL "")); } else if (IdentificationType.STRING_SEARCH.equals(id.getType())) { StringSearchIdentification ssId = (StringSearchIdentification) id; + StringBuilder fileLines = new StringBuilder(); + StringBuilder comments = new StringBuilder(); + + for (IdentifiedStringSearchMatchLocation hit : ssId.getMatchLocations()) { + if (fileLines.length() > 0) { + fileLines.append(','); + } + if (comments.length() > 0) { + comments.append(','); + } + fileLines.append(hit.getFirstLine() != null ? hit.getFirstLine().toString() : ""); + comments.append(hit.getIdentificationComment() != null ? hit.getIdentificationComment() : ""); + } + System.out.println(String.format( rowFormat, - "", - ssId.getType(), - filePath, - thisNodeFileInfo.getLength(), - "", - "", - componentInfo.getComponentName(), - versionName, - id.getIdentifiedLicenseInfo() == null ? "" : id.getIdentifiedLicenseInfo() - .getName(), - id.getIdentifiedUsageLevel(), - "", "", - "Matched File Line", "Comment", - ssId.getStringSearchId())); + "", ssId.getType(), filePath, thisNodeFileInfo.getLength(), "", "", + componentInfo.getComponentName(), versionName, + id.getIdentifiedLicenseInfo() == null ? "" : id.getIdentifiedLicenseInfo().getName(), + id.getIdentifiedUsageLevel(), "", "", fileLines.toString(), comments.toString(), ssId.getStringSearchId())); } else if (IdentificationType.DEPENDENCY.equals(id.getType())) { DependencyIdentification dpId = (DependencyIdentification) id; System.out.println(String.format( diff --git a/protex-sdk-examples/src/test/java/com/blackducksoftware/sdk/protex/client/examples/test/report/SampleReportDataIdentifiedFilesTest.java b/protex-sdk-examples/src/test/java/com/blackducksoftware/sdk/protex/client/examples/test/report/SampleReportDataIdentifiedFilesTest.java new file mode 100644 index 0000000..0bac03e --- /dev/null +++ b/protex-sdk-examples/src/test/java/com/blackducksoftware/sdk/protex/client/examples/test/report/SampleReportDataIdentifiedFilesTest.java @@ -0,0 +1,71 @@ +package com.blackducksoftware.sdk.protex.client.examples.test.report; + +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import com.blackducksoftware.sdk.protex.client.examples.SampleGetFilesIdentifiedWithConflicts; +import com.blackducksoftware.sdk.protex.client.examples.test.type.AbstractSdkSampleTest; +import com.blackducksoftware.sdk.protex.client.examples.test.type.TestSources; +import com.blackducksoftware.sdk.protex.client.examples.test.type.Tests; +import com.blackducksoftware.sdk.protex.common.BomRefreshMode; +import com.blackducksoftware.sdk.protex.common.UsageLevel; +import com.blackducksoftware.sdk.protex.component.Component; +import com.blackducksoftware.sdk.protex.license.License; +import com.blackducksoftware.sdk.protex.license.LicenseCategory; +import com.blackducksoftware.sdk.protex.license.LicenseInfo; +import com.blackducksoftware.sdk.protex.project.AnalysisSourceLocation; +import com.blackducksoftware.sdk.protex.project.ProjectRequest; +import com.blackducksoftware.sdk.protex.project.codetree.identification.IdentificationRequest; + +public class SampleReportDataIdentifiedFilesTest extends AbstractSdkSampleTest { + + private String projectId; + + @BeforeClass + protected void createProject() throws Exception { + ProjectRequest projectRequest = new ProjectRequest(); + projectRequest.setName("SampleReportDataIdentifiedFilesTest Project"); + + AnalysisSourceLocation sourceLocation = TestSources.getAnalysisSourceLocation(getProxy()); + + projectRequest.setAnalysisSourceLocation(sourceLocation); + + projectId = getProxy().getProjectApi().createProject(projectRequest, LicenseCategory.PROPRIETARY); + + TestSources.synchronousSourceScan(getProxy(), projectId, 1000); + + Component component = getProxy().getComponentApi().getComponentsByName("Cyclos", "3.0.7").get(0); + License license = getProxy().getLicenseApi().getLicenseById("gpl20"); + LicenseInfo licenseInfo = new LicenseInfo(); + licenseInfo.setLicenseId(license.getLicenseId()); + licenseInfo.setName(license.getName()); + + // Identify stuff to GPL + IdentificationRequest toGpl = new IdentificationRequest(); + toGpl.setIdentifiedComponentKey(component.getComponentKey()); + toGpl.setIdentifiedLicenseInfo(licenseInfo); + toGpl.setIdentifiedUsageLevel(UsageLevel.COMPONENT); + + getProxy().getIdentificationApi().addDeclaredIdentification(projectId, "/", toGpl, BomRefreshMode.SYNCHRONOUS); + } + + @Test(groups = { Tests.SOURCE_DEPENDENT_TEST }) + public void runSample() throws Exception { + String[] args = new String[4]; + args[0] = Tests.getServerUrl(); + args[1] = Tests.getServerUsername(); + args[2] = Tests.getServerPassword(); + args[3] = projectId; + + SampleGetFilesIdentifiedWithConflicts.main(args); + } + + @AfterClass(alwaysRun = true) + protected void deleteProject() throws Exception { + if (projectId != null) { + getProxy().getProjectApi().deleteProject(projectId); + } + } + +}