Skip to content

Commit

Permalink
added assert for test case without license in NpmArtifactGeneratorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
anki2189 committed Dec 8, 2020
1 parent 060e2cd commit 62a7f29
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
package org.carlspring.strongbox.artifact.generation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.carlspring.strongbox.util.MessageDigestUtils.calculateChecksum;
import static org.carlspring.strongbox.util.MessageDigestUtils.readChecksumFile;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;

import org.carlspring.strongbox.config.NpmLayoutProviderTestConfig;
import org.carlspring.strongbox.npm.metadata.License;
import org.carlspring.strongbox.npm.metadata.PackageVersion;
import org.carlspring.strongbox.storage.repository.Repository;
import org.carlspring.strongbox.testing.artifact.ArtifactManagementTestExecutionListener;
import org.carlspring.strongbox.testing.artifact.LicenseConfiguration;
import org.carlspring.strongbox.testing.artifact.LicenseType;
import org.carlspring.strongbox.testing.artifact.NpmTestArtifact;
import org.carlspring.strongbox.testing.repository.NpmRepository;
import org.carlspring.strongbox.testing.storage.repository.RepositoryManagementTestExecutionListener;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.codec.digest.MessageDigestAlgorithms;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.parallel.Execution;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.carlspring.strongbox.util.MessageDigestUtils.calculateChecksum;
import static org.carlspring.strongbox.util.MessageDigestUtils.readChecksumFile;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* @author Wojciech Pater
Expand All @@ -46,6 +63,130 @@ void testArtifactGeneration(@NpmRepository(repositoryId = REPOSITORY_RELEASES)
bytesSize = 2048)
Path path)
throws Exception
{
assertArtifactAndHash(path);

try (InputStream is = Files.newInputStream(path);
BufferedInputStream bis = new BufferedInputStream(is);
GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(bis);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzInputStream))
{
PackageVersion packageJson = null;
TarArchiveEntry entry = null;
TarArchiveEntry apacheLicenseEntry = null;
TarArchiveEntry licenseEntry = null;
TarArchiveEntry packageJsonEntry = null;
while ((entry = (TarArchiveEntry) tarArchiveInputStream.getNextEntry()) != null)
{
if (entry.getName().equals("LICENSE-Apache-2.0.md"))
{
apacheLicenseEntry = entry;
}
else if (entry.getName().equals("LICENSE"))
{
licenseEntry = entry;
}
else if (entry.getName().equals("package.json"))
{
packageJsonEntry = entry;
packageJson = new ObjectMapper().readValue(new String(IOUtils.toByteArray(tarArchiveInputStream)),
PackageVersion.class);
}
}

assertThat(packageJsonEntry)
.as("Did not find a license file package.json was expected at the default location in the TAR source package!")
.isNotNull();

assertThat(apacheLicenseEntry)
.as("Found license file LICENSE-Apache-2.0.md that was not expected at the default location in the TAR source package!")
.isNull();

assertThat(licenseEntry)
.as("Found license file LICENSE that was expected at the default location in the TAR source package!")
.isNull();

assertThat(packageJson.getLicenses().size())
.isEqualTo(0);
}
}


@ExtendWith({ RepositoryManagementTestExecutionListener.class,
ArtifactManagementTestExecutionListener.class })
@Test
void testArtifactGenerationWithLicense(@NpmRepository(repositoryId = REPOSITORY_RELEASES)
Repository repository,
@NpmTestArtifact(repositoryId = REPOSITORY_RELEASES,
id = "npm-test-view",
versions = "1.0.0",
scope = "@carlspring",
bytesSize = 2048,
licenses = { @LicenseConfiguration(license = LicenseType.APACHE_2_0,
destinationPath = "LICENSE-Apache-2.0.md"),
@LicenseConfiguration(license = LicenseType.MIT,
destinationPath = "LICENSE-MIT-License.md")})
Path path)
throws Exception
{
assertArtifactAndHash(path);

try (InputStream is = Files.newInputStream(path);
BufferedInputStream bis = new BufferedInputStream(is);
GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(bis);
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzInputStream))
{
PackageVersion packageJson = null;
TarArchiveEntry entry = null;
TarArchiveEntry apacheLicenseEntry = null;
TarArchiveEntry mitLicenseEntry = null;
TarArchiveEntry packageJsonEntry = null;
while ((entry = (TarArchiveEntry) tarArchiveInputStream.getNextEntry()) != null)
{
if (entry.getName().equals("LICENSE-Apache-2.0.md"))
{
apacheLicenseEntry = entry;
}
else if (entry.getName().equals("LICENSE-MIT-License.md"))
{
mitLicenseEntry = entry;
}
else if (entry.getName().equals("package.json"))
{
packageJsonEntry = entry;
packageJson = new ObjectMapper().readValue(new String(IOUtils.toByteArray(tarArchiveInputStream)),
PackageVersion.class);
}
}

assertThat(packageJsonEntry)
.as("Did not find a license file package.json was expected at the default location in the TAR source package!")
.isNotNull();

assertThat(apacheLicenseEntry)
.as("Did not find a license file LICENSE-Apache-2.0.md that was expected at the default location in the TAR source package!")
.isNotNull();

assertThat(mitLicenseEntry)
.as("Did not find a license file LICENSE-MIT-License.md that was expected at the default location in the TAR source package!")
.isNotNull();

assertThat(packageJson.getLicenses())
.isNotNull()
.as("licenses key value not present in package.json!");

List<String> licenses = packageJson.getLicenses()
.stream()
.map(License::getType)
.collect(Collectors.toList());

assertThat(licenses).contains("Apache 2.0", "MIT License");
}
}

private void assertArtifactAndHash(Path path)
throws IOException,
NoSuchAlgorithmException
{
assertThat(Files.exists(path)).as("Failed to generate NPM package.").isTrue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;

public class NpmArtifactGenerator
implements ArtifactGenerator
Expand Down Expand Up @@ -93,6 +93,12 @@ public void setPackagePath(Path packagePath)
this.packagePath = packagePath;
}

@Override
public void setLicenses(LicenseConfiguration[] licenses)
{
this.licenses = licenses;
}

public Path buildPackage(long bytesSize)
throws IOException
{
Expand Down Expand Up @@ -164,11 +170,8 @@ private void writePackageJson(TarArchiveOutputStream tarOut)
try (OutputStream outputStream = Files.newOutputStream(packageJsonPath, StandardOpenOption.CREATE);
BufferedOutputStream out = new BufferedOutputStream(outputStream))
{
List<License> licenses = getNpmLicenses();
if (!CollectionUtils.isEmpty(licenses))
{
packageJson.setLicenses(licenses);
}
populateNpmLicensesinPackageJson();

out.write(mapper.writeValueAsBytes(packageJson));
}

Expand All @@ -180,22 +183,23 @@ private void writePackageJson(TarArchiveOutputStream tarOut)
tarOut.closeArchiveEntry();
}

private List<License> getNpmLicenses()
private void populateNpmLicensesinPackageJson()
{
if (!ArrayUtils.isEmpty(licenses))
{
return Arrays.asList(licenses)
.stream()
.map(licenseConfig -> {

License npmLicense = new License();
npmLicense.setType(licenseConfig.license().getName());
npmLicense.setUrl(licenseConfig.license().getUrl());
return npmLicense;
})
.collect(Collectors.toList());
List<License> npmLicenses = Arrays.asList(licenses)
.stream()
.map(licenseConfig -> {

License npmLicense = new License();
npmLicense.setType(licenseConfig.license().getName());
npmLicense.setUrl(licenseConfig.license().getUrl());
return npmLicense;
})
.collect(Collectors.toList());

packageJson.setLicenses(npmLicenses);
}
return null;
}

private void writeContent(TarArchiveOutputStream tarOut,
Expand Down Expand Up @@ -251,8 +255,8 @@ public Path buildPublishJson(long bytesSize)
}

Path publishJsonPath = packagePath.resolveSibling("publish.json");
try (OutputStream out = new BufferedOutputStream(
Files.newOutputStream(publishJsonPath, StandardOpenOption.CREATE)))
try (OutputStream outputStream = Files.newOutputStream(publishJsonPath, StandardOpenOption.CREATE);
BufferedOutputStream out = new BufferedOutputStream(outputStream))
{
JsonFactory jFactory = new JsonFactory();
JsonGenerator jGenerator = jFactory.createGenerator(out, JsonEncoding.UTF8);
Expand Down Expand Up @@ -306,10 +310,4 @@ public Path buildPublishJson(long bytesSize)
return publishJsonPath;
}

@Override
public void setLicenses(LicenseConfiguration[] licenses)
{
this.licenses = licenses;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@
*/
String scope() default "";

/**
* License Configuration for test artifact
*/
LicenseConfiguration[] licenses() default {};

}

0 comments on commit 62a7f29

Please sign in to comment.