Skip to content

Commit

Permalink
Added test cases for openapi-generator-maven-plugin:generate input
Browse files Browse the repository at this point in the history
specifications:

* URLs of the form jar:jar-specific-uri!/spec.yaml

* Resources on the compilation classpath

in addition to the existing FILE test case.
  • Loading branch information
allen-ball authored and parenko committed May 6, 2024
1 parent 88239c6 commit 0d43b21
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 19 deletions.
6 changes: 6 additions & 0 deletions modules/openapi-generator-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-resolver-provider</artifactId>
<version>3.9.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-verifier</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,34 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.impl.DefaultServiceLocator;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.RepositorySystem;

public class CodeGenMojoTest extends BaseTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
}

public void testCommonConfigurationWithFileInputSpec() throws Exception {
testCommonConfiguration("file");
}

public void testCommonConfigurationWithResourceInputSpec() throws Exception {
testCommonConfiguration("resource");
}

public void testCommonConfigurationWithURLInputSpec() throws Exception {
testCommonConfiguration("url");
}

@SuppressWarnings("unchecked")
public void testCommonConfiguration() throws Exception {
private void testCommonConfiguration(String profile) throws Exception {
File folder = Files.createTempDirectory("test").toFile();
CodeGenMojo mojo = loadMojo(folder, "src/test/resources/default");
CodeGenMojo mojo = loadMojo(folder, "src/test/resources/default", profile);
mojo.execute();
assertEquals("java", getVariableValueFromObject(mojo, "generatorName"));
assertEquals("jersey2", getVariableValueFromObject(mojo, "library"));
Expand All @@ -65,7 +81,7 @@ public void testCommonConfiguration() throws Exception {
public void testHashGenerationFileContainsExecutionId() throws Exception {
// GIVEN
Path folder = Files.createTempDirectory("test");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "file", "executionId");

// WHEN
mojo.execute();
Expand All @@ -86,7 +102,7 @@ public void testSkipRegenerationForClasspathSpecFileNoChange() throws Exception
//GIVEN
/* Setup the mojo */
final Path folder = Files.createTempDirectory("test-classpath");
final CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/classpath", "executionId");
final CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/classpath", "resource", "executionId");

/* Perform an initial generation */
mojo.execute();
Expand Down Expand Up @@ -124,7 +140,7 @@ public void testSkipRegenerationForClasspathSpecFileWithChange() throws Exceptio
//GIVEN
/* Setup the mojo */
final Path folder = Files.createTempDirectory("test-classpath");
final CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/classpath", "executionId");
final CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/classpath", "resource", "executionId");

/* Perform an initial generation */
mojo.execute();
Expand Down Expand Up @@ -159,7 +175,7 @@ public void testSkipRegenerationForClasspathSpecFileWithChange() throws Exceptio
public void testCollapsedSpecProduced() throws Exception {
// GIVEN
Path folder = Files.createTempDirectory("test");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "file", "executionId");

// WHEN
mojo.execute();
Expand All @@ -172,7 +188,7 @@ public void testCollapsedSpecProduced() throws Exception {
public void testCollapsedSpecAddedToArtifacts() throws Exception {
// GIVEN
Path folder = Files.createTempDirectory("test");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "file", "executionId");

// WHEN
mojo.execute();
Expand All @@ -187,7 +203,7 @@ public void testCollapsedSpecAddedToArtifacts() throws Exception {
public void testAnyInputSpecMustBeProvided() throws Exception {
// GIVEN
Path folder = Files.createTempDirectory("test");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "file", "executionId");
mojo.inputSpec = null;
mojo.inputSpecRootDirectory = null;

Expand All @@ -201,7 +217,7 @@ public void testAnyInputSpecMustBeProvided() throws Exception {
public void testInputSpecRootDirectoryDoesNotRequireInputSpec() throws Exception {
// GIVEN
Path folder = Files.createTempDirectory("test");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "file", "executionId");
mojo.inputSpec = null;
mojo.inputSpecRootDirectory = "src/test/resources/default";

Expand All @@ -214,14 +230,14 @@ public void testInputSpecRootDirectoryDoesNotRequireInputSpec() throws Exception
assertTrue(hashFolder.resolve("_merged_spec.yaml-executionId.sha256").toFile().exists());
}

protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot) throws Exception {
return loadMojo(temporaryFolder, projectRoot, "default");
protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String profile) throws Exception {
return loadMojo(temporaryFolder, projectRoot, profile, "default");
}

protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String executionId) throws Exception {
protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String profile, String executionId) throws Exception {
File file = new File(projectRoot);
FileUtils.copyDirectory(file, temporaryFolder);
MavenProject project = readMavenProject(temporaryFolder);
MavenProject project = readMavenProject(temporaryFolder, profile);
MavenSession session = newMavenSession(project);
MojoExecution execution = newMojoExecution("generate");
MojoExecution executionWithId = copyWithExecutionId(executionId, execution);
Expand All @@ -234,15 +250,24 @@ private MojoExecution copyWithExecutionId(String executionId, MojoExecution exec
return executionWithId;
}

protected MavenProject readMavenProject(File basedir)
protected MavenProject readMavenProject(File basedir, String profile)
throws Exception {
File pom = new File(basedir, "pom.xml");
LocalRepository localRepo = new LocalRepository(new File(basedir, "local-repo"));
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
RepositorySystem system = locator.getService(RepositorySystem.class);
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setBaseDirectory(basedir);
if (profile != null) {
request.addActiveProfile(profile);
}
ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
configuration.setRepositorySession(new DefaultRepositorySystemSession());
configuration.setRepositorySession(session);
configuration.setResolveDependencies(true);
MavenProject project = lookup(ProjectBuilder.class).build(pom, configuration).getProject();
assertNotNull(project);
return project;
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<version>1</version>
<description>POM was created from install:install-file</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<versioning>
<release>1</release>
<versions>
<version>1</version>
</versions>
<lastUpdated>20210724220722</lastUpdated>
</versioning>
</metadata>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright 2020 OpenAPI-Generator Contributors (https://openapi-generator.tech)
~ Copyright 2020, 2021 OpenAPI-Generator Contributors (https://openapi-generator.tech)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand All @@ -22,14 +22,41 @@
<version>1.0.0-SNAPSHOT</version>
<name>OpenAPI Generator Configuration Test</name>
<url>https://openapi-generator.tech/</url>
<profiles>
<profile>
<id>file</id>
<properties>
<inputSpec>${basedir}/src/main/resources/petstore.yaml</inputSpec>
</properties>
</profile>
<profile>
<id>resource</id>
<properties>
<inputSpec>petstore.yaml</inputSpec>
</properties>
</profile>
<profile>
<id>url</id>
<properties>
<inputSpec>jar:file:${basedir}/local-repo/petstore/schema/1/schema-1.jar!/petstore.yaml</inputSpec>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>petstore</groupId>
<artifactId>schema</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<finalName>common-maven</finalName>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<configuration>
<inputSpec>${basedir}/petstore.yaml</inputSpec>
<inputSpec>${inputSpec}</inputSpec>
<generatorName>java</generatorName>
<configOptions>
<dateLibrary>joda</dateLibrary>
Expand All @@ -55,4 +82,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<settings>
<activeProfiles>
<activeProfile>default</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>default</id>
<localRepository>${basedir}/local-repo</localRepository>
</profile>
</profiles>
</settings>

0 comments on commit 0d43b21

Please sign in to comment.