-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/gradle license report reader (#284)
Co-authored-by: ohecker <8004361+ohecker@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
357 additions
and
258 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
core/src/main/java/com/devonfw/tools/solicitor/reader/gradle/GradleLicenseReportReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.devonfw.tools.solicitor.reader.gradle; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.devonfw.tools.solicitor.common.PackageURLHelper; | ||
import com.devonfw.tools.solicitor.common.SolicitorRuntimeException; | ||
import com.devonfw.tools.solicitor.model.inventory.ApplicationComponent; | ||
import com.devonfw.tools.solicitor.model.masterdata.Application; | ||
import com.devonfw.tools.solicitor.model.masterdata.UsagePattern; | ||
import com.devonfw.tools.solicitor.reader.AbstractReader; | ||
import com.devonfw.tools.solicitor.reader.Reader; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
|
||
/** | ||
* A {@link Reader} which reads data generated by the <a href="https://github.com/jk1/Gradle-License-Report">Gradle | ||
* License Report Plugin</a>. | ||
*/ | ||
@Component | ||
public class GradleLicenseReportReader extends AbstractReader implements Reader { | ||
|
||
/** | ||
* The supported type of this {@link Reader}. | ||
*/ | ||
public static final String SUPPORTED_TYPE = "gradle-license-report-json"; | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public Set<String> getSupportedTypes() { | ||
|
||
return Collections.singleton(SUPPORTED_TYPE); | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public void readInventory(String type, String sourceUrl, Application application, UsagePattern usagePattern, | ||
String repoType, String packageType, Map<String, String> configuration) { | ||
|
||
int components = 0; | ||
int licenses = 0; | ||
|
||
// According to tutorial https://github.com/FasterXML/jackson-databind/ | ||
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); | ||
try { | ||
Map<String, List> report = mapper.readValue(this.inputStreamFactory.createInputStreamFor(sourceUrl), Map.class); | ||
List<Map<String, String>> dependencies = report.get("dependencies"); | ||
|
||
// Extract groupId and artifactId | ||
for (Map<String, String> dependency : dependencies) { | ||
String[] dependencyParts = dependency.get("moduleName").split(":"); | ||
if (dependencyParts.length != 2) { | ||
throw new SolicitorRuntimeException( | ||
"Could not extract groupId, artifactId from moduleName: '" + dependency.get("moduleName") + "'"); | ||
} | ||
|
||
ApplicationComponent appComponent = getModelFactory().newApplicationComponent(); | ||
appComponent.setApplication(application); | ||
appComponent.setGroupId(dependencyParts[0]); | ||
appComponent.setArtifactId(dependencyParts[1]); | ||
appComponent.setVersion(dependency.get("moduleVersion")); | ||
|
||
// Extract the first element from moduleUrls if available | ||
Object urlsObject = dependency.get("moduleUrls"); | ||
if (urlsObject instanceof List<?>) { | ||
List<?> urlsList = (List<?>) urlsObject; | ||
if (!urlsList.isEmpty() && urlsList.get(0) instanceof String) { | ||
appComponent.setOssHomepage((String) urlsList.get(0)); | ||
} | ||
} | ||
|
||
appComponent.setUsagePattern(usagePattern); | ||
appComponent.setRepoType(repoType); | ||
appComponent.setPackageUrl(PackageURLHelper | ||
.fromMavenCoordinates(dependencyParts[0], dependencyParts[1], dependency.get("moduleVersion")).toString()); | ||
|
||
// Extract and process moduleLicenses | ||
Object licensesObject = dependency.get("moduleLicenses"); | ||
if (licensesObject instanceof List<?>) { | ||
List<?> licensesList = (List<?>) licensesObject; | ||
for (Object licenseObject : licensesList) { | ||
if (licenseObject instanceof Map<?, ?>) { | ||
Map<?, ?> licenseMap = (Map<?, ?>) licenseObject; | ||
String licenseName = (String) licenseMap.get("moduleLicense"); | ||
String licenseUrl = (String) licenseMap.get("moduleLicenseUrl"); | ||
addRawLicense(appComponent, licenseName, licenseUrl, sourceUrl); | ||
} | ||
} | ||
} | ||
|
||
components++; | ||
licenses++; | ||
} | ||
|
||
doLogging(sourceUrl, application, components, licenses); | ||
|
||
} catch (IOException e) { | ||
throw new SolicitorRuntimeException("Could not read Gradle License Report inventory source '" + sourceUrl + "'", | ||
e); | ||
} | ||
} | ||
|
||
} |
131 changes: 0 additions & 131 deletions
131
core/src/main/java/com/devonfw/tools/solicitor/reader/gradle/GradleReader.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...rc/test/java/com/devonfw/tools/solicitor/reader/gradle/GradleLicenseReportReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.devonfw.tools.solicitor.reader.gradle; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.devonfw.tools.solicitor.common.FileInputStreamFactory; | ||
import com.devonfw.tools.solicitor.model.ModelFactory; | ||
import com.devonfw.tools.solicitor.model.impl.ModelFactoryImpl; | ||
import com.devonfw.tools.solicitor.model.inventory.ApplicationComponent; | ||
import com.devonfw.tools.solicitor.model.masterdata.Application; | ||
import com.devonfw.tools.solicitor.model.masterdata.UsagePattern; | ||
|
||
class GradleLicenseReportReaderTest { | ||
|
||
Application application; | ||
|
||
public GradleLicenseReportReaderTest() { | ||
|
||
ModelFactory modelFactory = new ModelFactoryImpl(); | ||
this.application = modelFactory.newApplication("testApp", "0.0.0.TEST", "1.1.2111", "http://bla.com", "Java8"); | ||
GradleLicenseReportReader gr = new GradleLicenseReportReader(); | ||
gr.setModelFactory(modelFactory); | ||
gr.setInputStreamFactory(new FileInputStreamFactory()); | ||
gr.readInventory("gradle-license-report-json", "src/test/resources/gradleLicenseReport.json", this.application, | ||
UsagePattern.STATIC_LINKING, null, null, null); | ||
} | ||
|
||
@Test | ||
void testGetSupportedTypes() { | ||
|
||
GradleLicenseReportReader cut = new GradleLicenseReportReader(); | ||
|
||
assertEquals(1, cut.getSupportedTypes().size()); | ||
assertTrue(cut.getSupportedTypes().contains("gradle-license-report-json")); | ||
} | ||
|
||
@Test | ||
void testFindArtifact() { | ||
|
||
List<ApplicationComponent> lapc = this.application.getApplicationComponents(); | ||
boolean found = false; | ||
for (ApplicationComponent ap : lapc) { | ||
if (ap.getGroupId().equals("androidx.activity") && // | ||
ap.getArtifactId().equals("activity") && // | ||
ap.getVersion().equals("1.2.4")) { | ||
found = true; | ||
assertEquals("pkg:maven/androidx.activity/activity@1.2.4", ap.getPackageUrl()); | ||
break; | ||
} | ||
} | ||
assertEquals(3, lapc.size()); | ||
assertTrue(found); | ||
} | ||
|
||
@Test | ||
void testFindLicense() { | ||
|
||
List<ApplicationComponent> lapc = this.application.getApplicationComponents(); | ||
boolean found = false; | ||
for (ApplicationComponent ap : lapc) { | ||
if (ap.getArtifactId().equals("istack-commons-runtime") | ||
&& ap.getRawLicenses().get(0).getDeclaredLicense().equals("GPL2 w/ CPE")) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
assertTrue(found); | ||
} | ||
|
||
@Test | ||
void testMinimalInformation() { | ||
|
||
List<ApplicationComponent> lapc = this.application.getApplicationComponents(); | ||
boolean found = false; | ||
for (ApplicationComponent ap : lapc) { | ||
if (ap.getArtifactId().equals("annotation") && ap.getOssHomepage() == null && ap.getRawLicenses().isEmpty()) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
assertTrue(found); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.