Skip to content

Commit 30bb65b

Browse files
committed
replace generateStubs with convert (#5)
1 parent ba4765f commit 30bb65b

File tree

10 files changed

+72
-76
lines changed

10 files changed

+72
-76
lines changed

src/main/groovy/io/codearte/accurest/maven/AccurestConverter.groovy

-17
This file was deleted.

src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy

+49-13
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,71 @@ package io.codearte.accurest.maven
22

33
import groovy.transform.CompileStatic
44
import io.codearte.accurest.config.AccurestConfigProperties
5+
import io.codearte.accurest.wiremock.DslToWireMockClientConverter
6+
import io.codearte.accurest.wiremock.RecursiveFilesConverter
7+
import org.apache.maven.execution.MavenSession
8+
import org.apache.maven.model.path.PathTranslator
59
import org.apache.maven.plugin.AbstractMojo
610
import org.apache.maven.plugin.MojoExecutionException
711
import org.apache.maven.plugin.MojoFailureException
12+
import org.apache.maven.plugins.annotations.Component
13+
import org.apache.maven.plugins.annotations.LifecyclePhase
814
import org.apache.maven.plugins.annotations.Mojo
915
import org.apache.maven.plugins.annotations.Parameter
1016

11-
import static io.codearte.accurest.maven.AccurestConverter.convertAccurestToStubs
17+
import javax.inject.Inject
1218

13-
@Mojo(name = 'convert', requiresProject = false)
19+
@Mojo(name = 'convert', requiresProject = false, defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
1420
@CompileStatic
15-
public class ConvertMojo extends AbstractMojo {
21+
class ConvertMojo extends AbstractMojo {
1622

17-
@Parameter(property = 'contractsDir', defaultValue = '${basedir}')
18-
private File contractsDir
23+
@Parameter(defaultValue = '${basedir}', readonly = true, required = true)
24+
private File baseDir
1925

20-
@Parameter(property = 'mappingsDir', defaultValue = '${basedir}')
21-
private File mappingsDir
26+
@Parameter(defaultValue = '${project.build.directory}', readonly = true, required = true)
27+
private File projectBuildDirectory
2228

23-
public void execute() throws MojoExecutionException, MojoFailureException {
29+
@Parameter(property = 'contractsDir')
30+
private String contractsDir
31+
32+
@Parameter(property = 'mappingsDir')
33+
private String mappingsDir
34+
35+
@Component
36+
private MavenSession mavenSession
37+
38+
private final PathTranslator translator
39+
40+
@Inject
41+
ConvertMojo(PathTranslator translator) {
42+
this.translator = translator
43+
}
44+
45+
void execute() throws MojoExecutionException, MojoFailureException {
2446

2547
AccurestConfigProperties config = new AccurestConfigProperties()
26-
config.contractsDslDir = contractsDir
27-
config.stubsOutputDir = mappingsDir
48+
49+
config.contractsDslDir = resolveFile(baseDir, contractsDir, isInsideProject() ? 'src/test/accurest' : '')
50+
config.stubsOutputDir = resolveFile(projectBuildDirectory, mappingsDir, isInsideProject() ? 'mappings' : '')
2851

2952
log.info('Converting from accurest contracts written in GroovyDSL to WireMock stubs mappings')
30-
log.info(" Accurest contracts directory: ${config.contractsDslDir.absolutePath}")
31-
log.info("WireMock stubs mappings directory: ${config.stubsOutputDir.absolutePath}")
53+
log.info(" Accurest contracts directory: ${config.contractsDslDir}")
54+
log.info("WireMock stubs mappings directory: ${config.stubsOutputDir}")
55+
56+
RecursiveFilesConverter converter = new RecursiveFilesConverter(new DslToWireMockClientConverter(), config)
57+
converter.processFiles()
58+
}
59+
60+
private boolean isInsideProject() {
61+
mavenSession.getRequest().isProjectPresent()
62+
}
63+
64+
private File resolveFile(File baseDir, String requestedPath, String defaultPath) {
65+
requestedPath ? alignToBaseDirectory(requestedPath, baseDir) : alignToBaseDirectory(defaultPath, baseDir)
66+
}
3267

33-
convertAccurestToStubs(config)
68+
private File alignToBaseDirectory(String dir1, File dir) {
69+
return new File(translator.alignToBaseDirectory(dir1, dir))
3470
}
3571

3672
}
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
11
package io.codearte.accurest.maven
22

33
import groovy.transform.CompileStatic
4-
import io.codearte.accurest.config.AccurestConfigProperties
5-
import io.codearte.accurest.wiremock.DslToWireMockClientConverter
6-
import io.codearte.accurest.wiremock.RecursiveFilesConverter
7-
import org.apache.maven.plugin.AbstractMojo
8-
import org.apache.maven.plugin.MojoExecutionException
9-
import org.apache.maven.plugin.MojoFailureException
4+
import org.apache.maven.model.path.PathTranslator
105
import org.apache.maven.plugins.annotations.LifecyclePhase
116
import org.apache.maven.plugins.annotations.Mojo
12-
import org.apache.maven.plugins.annotations.Parameter
7+
8+
import javax.inject.Inject
139

1410
@Mojo(name = 'generateStubs', defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
1511
@CompileStatic
16-
public class GenerateStubsMojo extends AbstractMojo {
17-
18-
@Parameter(defaultValue = '${basedir}', readonly = true, required = true)
19-
private File baseDir
20-
21-
@Parameter(defaultValue = '${project.build.directory}', readonly = true, required = true)
22-
private File projectBuildDirectory
23-
24-
@Parameter(defaultValue = '/src/test/accurest')
25-
private String contractsDir
12+
@Deprecated
13+
class GenerateStubsMojo extends ConvertMojo {
2614

27-
@Parameter(defaultValue = 'mappings')
28-
private String mappingsDir
29-
30-
public void execute() throws MojoExecutionException, MojoFailureException {
31-
AccurestConfigProperties config = new AccurestConfigProperties()
32-
config.contractsDslDir = new File(baseDir, contractsDir)
33-
config.stubsOutputDir = new File(projectBuildDirectory, mappingsDir)
34-
35-
log.info('Accurest Plugin: Invoking GroovyDSL to WireMock client stubs conversion')
36-
log.info("From '${config.contractsDslDir}' to '${config.stubsOutputDir}'")
37-
38-
RecursiveFilesConverter converter = new RecursiveFilesConverter(new DslToWireMockClientConverter(), config)
39-
converter.processFiles()
15+
@Inject
16+
GenerateStubsMojo(PathTranslator translator) {
17+
super(translator)
4018
}
41-
4219
}

src/main/groovy/io/codearte/accurest/maven/GenerateTestsMojo.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import static java.lang.String.format
1717

1818
@Mojo(name = 'generateTests', defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES)
1919
@CompileStatic
20-
public class GenerateTestsMojo extends AbstractMojo {
20+
class GenerateTestsMojo extends AbstractMojo {
2121

2222
@Parameter(defaultValue = '${basedir}', readonly = true, required = true)
2323
private File baseDir
@@ -49,7 +49,7 @@ public class GenerateTestsMojo extends AbstractMojo {
4949
@Parameter
5050
private String nameSuffixForTests
5151

52-
public void execute() throws MojoExecutionException, MojoFailureException {
52+
void execute() throws MojoExecutionException, MojoFailureException {
5353
log.info('Generating server tests source code for Accurest contract verification')
5454

5555
AccurestConfigProperties config = new AccurestConfigProperties()

src/main/groovy/io/codearte/accurest/maven/RunMojo.groovy

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import javax.inject.Inject
1515

1616
@Mojo(name = 'run', requiresProject = false)
1717
@CompileStatic
18-
public class RunMojo extends AbstractMojo {
18+
class RunMojo extends AbstractMojo {
1919

2020
@Parameter(defaultValue = '${repositorySystemSession}', readonly = true)
2121
private RepositorySystemSession repoSession
@@ -38,12 +38,12 @@ public class RunMojo extends AbstractMojo {
3838
private final RemoteStubRunner remoteStubRunner
3939

4040
@Inject
41-
public RunMojo(LocalStubRunner localStubRunner, RemoteStubRunner remoteStubRunner) {
41+
RunMojo(LocalStubRunner localStubRunner, RemoteStubRunner remoteStubRunner) {
4242
this.localStubRunner = localStubRunner
4343
this.remoteStubRunner = remoteStubRunner
4444
}
4545

46-
public void execute() throws MojoExecutionException, MojoFailureException {
46+
void execute() throws MojoExecutionException, MojoFailureException {
4747
StubRunnerOptions options = new StubRunnerOptions(minPort, maxPort, "", false, stubsClassifier)
4848
log.debug("Launching StubRunner with args: $options")
4949
if (!stubs) {

src/main/groovy/io/codearte/accurest/maven/stubrunner/AetherStubDownloaderFactory.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import javax.inject.Singleton
1414
@Singleton
1515
@CompileStatic
1616
@Slf4j
17-
public class AetherStubDownloaderFactory {
17+
class AetherStubDownloaderFactory {
1818

1919
private final MavenProject project
2020
private final RepositorySystem repoSystem
2121

2222
@Inject
23-
public AetherStubDownloaderFactory(RepositorySystem repoSystem, MavenProject project) {
23+
AetherStubDownloaderFactory(RepositorySystem repoSystem, MavenProject project) {
2424
this.repoSystem = repoSystem
2525
this.project = project
2626
}

src/test/java/io/codearte/accurest/maven/PluginIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void should_build_project_Spring_Boot_Groovy_with_Accurest() throws Excep
3535
.assertErrorFreeLog()
3636
.assertLogText("Generating server tests source code for Accurest contract verification")
3737
.assertLogText("Generated 1 test classes.")
38-
.assertLogText("Accurest Plugin: Invoking GroovyDSL to WireMock client stubs conversion")
38+
.assertLogText("Converting from accurest contracts written in GroovyDSL to WireMock stubs mappings")
3939
.assertLogText("Creating new json")
4040
.assertErrorFreeLog();
4141
}
@@ -48,7 +48,7 @@ public void should_build_project_Spring_Boot_Java_with_Accurest() throws Excepti
4848
.assertErrorFreeLog()
4949
.assertLogText("Generating server tests source code for Accurest contract verification")
5050
.assertLogText("Generated 1 test classes.")
51-
.assertLogText("Accurest Plugin: Invoking GroovyDSL to WireMock client stubs conversion")
51+
.assertLogText("Converting from accurest contracts written in GroovyDSL to WireMock stubs mappings")
5252
.assertLogText("Creating new json")
5353
.assertErrorFreeLog();
5454
}

src/test/java/io/codearte/accurest/maven/PluginUnitTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ public class PluginUnitTest {
2222
@Test
2323
public void shouldGenerateWireMockStubsInDefaultLocation() throws Exception {
2424
File basedir = resources.getBasedir("basic");
25-
maven.executeMojo(basedir, "generateStubs");
25+
maven.executeMojo(basedir, "convert");
2626
assertFilesPresent(basedir, "target/mappings/Sample.json");
2727
}
2828

2929
@Test
3030
public void shouldGenerateWireMockFromStubsDirectory() throws Exception {
3131
File basedir = resources.getBasedir("withStubs");
32-
maven.executeMojo(basedir, "generateStubs", newParameter("contractsDir", "/src/test/resources/stubs"));
32+
maven.executeMojo(basedir, "convert", newParameter("contractsDir", "src/test/resources/stubs"));
3333
assertFilesPresent(basedir, "target/mappings/Sample.json");
3434
}
3535

3636
@Test
3737
public void shouldGenerateWireMockStubsInSelectedLocation() throws Exception {
3838
File basedir = resources.getBasedir("basic");
39-
maven.executeMojo(basedir, "generateStubs", newParameter("mappingsDir", "foo"));
39+
maven.executeMojo(basedir, "convert", newParameter("mappingsDir", "foo"));
4040
assertFilesPresent(basedir, "target/foo/Sample.json");
4141
}
4242

src/test/projects/spring-boot-groovy/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@
8686
<executions>
8787
<execution>
8888
<goals>
89-
<goal>generateStubs</goal>
89+
<goal>convert</goal>
9090
<goal>generateTests</goal>
9191
</goals>
9292
</execution>
9393
</executions>
9494
<configuration>
9595
<baseClassForTests>com.ofg.twitter.place.BaseMockMvcTest</baseClassForTests>
96-
<contractsDir>/src/test/resources/stubs</contractsDir>
96+
<contractsDir>src/test/resources/stubs</contractsDir>
9797
<testFramework>SPOCK</testFramework>
9898
<nameSuffixForTests>Test</nameSuffixForTests>
9999
</configuration>

src/test/projects/spring-boot-java/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<executions>
7676
<execution>
7777
<goals>
78-
<goal>generateStubs</goal>
78+
<goal>convert</goal>
7979
<goal>generateTests</goal>
8080
</goals>
8181
</execution>

0 commit comments

Comments
 (0)