Skip to content

Commit

Permalink
Add metadata unit test for Mock repo
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Sep 18, 2023
1 parent 3e9dab9 commit a89480d
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.codehaus.mojo.mrm.impl.maven;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.util.Optional;

abstract class AbstractTestSupport {

protected File getResourceAsFile(String path) throws FileNotFoundException {
try {
return new File(Optional.ofNullable(getClass().getResource(path))
.orElseThrow(() -> new FileNotFoundException(path))
.toURI());
} catch (URISyntaxException e) {
throw new FileNotFoundException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package org.codehaus.mojo.mrm.impl.maven;

import java.io.File;

import org.apache.maven.archetype.catalog.Archetype;
import org.apache.maven.archetype.catalog.ArchetypeCatalog;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class DiskArtifactStoreTest {
class DiskArtifactStoreTest extends AbstractTestSupport {

// MMOCKRM-10
@Test
void testArchetypeCatalog() throws Exception {
DiskArtifactStore artifactStore = new DiskArtifactStore(new File("src/test/resources/mmockrm-10"));
DiskArtifactStore artifactStore = new DiskArtifactStore(getResourceAsFile("/mmockrm-10"));
ArchetypeCatalog catalog = artifactStore.getArchetypeCatalog();
assertNotNull(catalog);
assertEquals(1, catalog.getArchetypes().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,35 @@
import org.apache.commons.io.IOUtils;
import org.apache.maven.archetype.catalog.Archetype;
import org.apache.maven.archetype.catalog.ArchetypeCatalog;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.codehaus.mojo.mrm.api.maven.Artifact;
import org.codehaus.mojo.mrm.api.maven.MetadataNotFoundException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MockArtifactStoreTest {
class MockArtifactStoreTest extends AbstractTestSupport {

@TempDir
Path temporaryFolder;

// MMOCKRM-3
@Test
void testInheritGavFromParent() {
void testInheritGavFromParent() throws Exception {
// don't fail
MockArtifactStore mockArtifactStore = new MockArtifactStore(new File("src/test/resources/mmockrm-3"));
MockArtifactStore mockArtifactStore = new MockArtifactStore(getResourceAsFile("/mmockrm-3"));
assertEquals(2, mockArtifactStore.getArtifactIds("localhost").size());
}

// MMOCKRM-6
@Test
void testClassifiers() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(new File("src/test/resources/mmockrm-7"));
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/mmockrm-7"));

Artifact pomArtifact = new Artifact("localhost", "mmockrm-7", "1.0", "pom");
assertNotNull(artifactStore.get(pomArtifact));
Expand All @@ -58,7 +62,7 @@ void testClassifiers() throws Exception {
// MMOCKRM-10
@Test
void testArchetypeCatalog() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(new File("src/test/resources/mmockrm-10"));
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/mmockrm-10"));
ArchetypeCatalog catalog = artifactStore.getArchetypeCatalog();
assertNotNull(catalog);
assertEquals(1, catalog.getArchetypes().size());
Expand All @@ -72,7 +76,7 @@ void testArchetypeCatalog() throws Exception {

@Test
void testDirectoryContent() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(new File("target/test-classes/mrm-15"));
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/mrm-15"));

Artifact pomArtifact = new Artifact("localhost", "mrm-15", "1.0", "pom");
assertNotNull(artifactStore.get(pomArtifact));
Expand Down Expand Up @@ -105,7 +109,7 @@ void testDirectoryContent() throws Exception {

@Test
void testEmptyJarContent() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(new File("target/test-classes/empty-jar"));
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/empty-jar"));

Artifact pomArtifact = new Artifact("localhost", "mrm-empty-jar", "1.0", "pom");
InputStream inputStreamPom = artifactStore.get(pomArtifact);
Expand Down Expand Up @@ -137,7 +141,7 @@ void testEmptyJarContent() throws Exception {

@Test
void testEmptyPluginJarContent() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(new File("target/test-classes/empty-plugin-jar"));
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/empty-plugin-jar"));

Artifact pomArtifact = new Artifact("localhost", "mrm-empty-plugin-jar", "1.0", "pom");
InputStream inputStreamPom = artifactStore.get(pomArtifact);
Expand Down Expand Up @@ -170,7 +174,7 @@ void testEmptyPluginJarContent() throws Exception {

@Test
void testDirectoryWithClassifierContent() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(new File("target/test-classes/mrm-xx"));
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/mrm-xx"));

Artifact pomArtifact = new Artifact("localhost", "mrm-xx", "1.0", "pom");
assertNotNull(artifactStore.get(pomArtifact));
Expand All @@ -181,4 +185,96 @@ void testDirectoryWithClassifierContent() throws Exception {
Artifact classifiedArtifact = new Artifact("localhost", "mrm-xx", "1.0", "javadoc-resources", "jar");
assertNotNull(artifactStore.get(classifiedArtifact));
}

@Test
void groupMetaDataShouldNotExistForNoPlugins() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/empty-jar"));

assertThrowsExactly(MetadataNotFoundException.class, () -> artifactStore.getMetadata("localhost"));
}

@Test
void groupMetaDataShouldExistPlugins() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/empty-plugin-jar"));

Metadata metadata = artifactStore.getMetadata("localhost");

assertNull(metadata.getArtifactId());
assertNull(metadata.getGroupId());
assertNull(metadata.getVersion());
assertNull(metadata.getVersioning());
assertEquals(2, metadata.getPlugins().size());

assertTrue(
metadata.getPlugins().stream()
.filter(p -> "mrm-empty-maven-plugin".equals(p.getArtifactId()))
.filter(p -> "mrm-empty".equals(p.getPrefix()))
.anyMatch(p -> "Test Plugin 1".equals(p.getName())),
"Plugin 1 not found in metadata");

assertTrue(
metadata.getPlugins().stream()
.filter(p -> "mrm-empty-plugin-jar".equals(p.getArtifactId()))
.filter(p -> "mrm-empty-plugin-jar".equals(p.getPrefix()))
.anyMatch(p -> "Test Plugin 2".equals(p.getName())),
"Plugin 2 not found in metadata");
}

@Test
void artifactMetaDataShouldExist() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/empty-jar"));

Metadata metadata = artifactStore.getMetadata("localhost/mrm-empty-jar");

assertEquals("localhost", metadata.getGroupId());
assertEquals("mrm-empty-jar", metadata.getArtifactId());
assertNull(metadata.getVersion());
assertTrue(metadata.getPlugins().isEmpty());
assertEquals("1.0", metadata.getVersioning().getLatest());
assertEquals("1.0", metadata.getVersioning().getRelease());
assertNull(metadata.getVersioning().getSnapshot());
assertNotNull(metadata.getVersioning().getLastUpdated());
assertTrue(metadata.getVersioning().getSnapshotVersions().isEmpty());
assertEquals(1, metadata.getVersioning().getVersions().size());
assertEquals("1.0", metadata.getVersioning().getVersions().get(0));
}

@Test
void artifactVersionMetaDataShouldNotExistForReleaseVersion() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/empty-jar"));

assertThrowsExactly(
MetadataNotFoundException.class, () -> artifactStore.getMetadata("localhost/mrm-empty-jar/1.0"));
}

@Test
void artifactVersionMetaDataShouldExist() throws Exception {
MockArtifactStore artifactStore = new MockArtifactStore(getResourceAsFile("/empty-jar-snapshot"));

Metadata metadata = artifactStore.getMetadata("localhost/mrm-empty-jar/1.0-SNAPSHOT");

assertEquals("localhost", metadata.getGroupId());
assertEquals("mrm-empty-jar", metadata.getArtifactId());
assertEquals("1.0-SNAPSHOT", metadata.getVersion());
assertTrue(metadata.getPlugins().isEmpty());
assertNull(metadata.getVersioning().getLatest());
assertNull(metadata.getVersioning().getRelease());
assertTrue(metadata.getVersioning().getVersions().isEmpty());
assertNotNull(metadata.getVersioning().getLastUpdated());
assertEquals(1, metadata.getVersioning().getSnapshot().getBuildNumber());
// assertNotNull(metadata.getVersioning().getSnapshot().getTimestamp()); - TODO check and fix
assertEquals(2, metadata.getVersioning().getSnapshotVersions().size());

assertTrue(metadata.getVersioning().getSnapshotVersions().stream()
.filter(v -> "".equals(v.getClassifier()))
.filter(v -> "pom".equals(v.getExtension()))
.filter(v -> !v.getUpdated().isEmpty())
.anyMatch(v -> "1.0-SNAPSHOT".equals(v.getVersion())));

assertTrue(metadata.getVersioning().getSnapshotVersions().stream()
.filter(v -> "".equals(v.getClassifier()))
.filter(v -> "jar".equals(v.getExtension()))
.filter(v -> !v.getUpdated().isEmpty())
.anyMatch(v -> "1.0-SNAPSHOT".equals(v.getVersion())));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>mrm-empty-jar</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>mrm-empty-maven-plugin</artifactId>
<version>1.0</version>
<packaging>maven-plugin</packaging>
<name>Test Plugin 1</name>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<artifactId>mrm-empty-plugin-jar</artifactId>
<version>1.0</version>
<packaging>maven-plugin</packaging>
<name>Test Plugin 2</name>
</project>

0 comments on commit a89480d

Please sign in to comment.