diff --git a/app-maven-plugin/pom.xml b/app-maven-plugin/pom.xml index 9b950b2d3..9f9f627e8 100644 --- a/app-maven-plugin/pom.xml +++ b/app-maven-plugin/pom.xml @@ -128,6 +128,13 @@ test + + pl.pragmatists + JUnitParams + 1.0.6 + test + + org.apache.maven.shared maven-verifier diff --git a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/AppEngineFactory.java b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/AppEngineFactory.java index 81b167a4f..27091861c 100644 --- a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/AppEngineFactory.java +++ b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/AppEngineFactory.java @@ -27,17 +27,38 @@ */ public interface AppEngineFactory { + /** + * Supported dev app server versions. + */ + public enum SupportedDevServerVersion { + V1, V2ALPHA; + + /** + * Parses {@code versionString} into a {@link SupportedDevServerVersion}. The aim is to let the + * users use lowercase in version strings. + */ + public static SupportedDevServerVersion parse(String versionString) { + if ("1".equals(versionString)) { + return V1; + } else if ("2-alpha".equals(versionString)) { + return V2ALPHA; + } else { + throw new IllegalArgumentException("Unsupported version value: " + versionString); + } + } + } + AppEngineStandardStaging standardStaging(); AppEngineFlexibleStaging flexibleStaging(); AppEngineDeployment deployment(); - AppEngineDevServer devServerRunSync(); + AppEngineDevServer devServerRunSync(SupportedDevServerVersion version); - AppEngineDevServer devServerRunAsync(int startSuccessTimeout); + AppEngineDevServer devServerRunAsync(int startSuccessTimeout, SupportedDevServerVersion version); - AppEngineDevServer devServerStop(); + AppEngineDevServer devServerStop(SupportedDevServerVersion version); GenRepoInfoFile genRepoInfoFile(); } diff --git a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/CloudSdkAppEngineFactory.java b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/CloudSdkAppEngineFactory.java index c84c3c8d8..43aba1b52 100644 --- a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/CloudSdkAppEngineFactory.java +++ b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/CloudSdkAppEngineFactory.java @@ -24,6 +24,7 @@ import com.google.cloud.tools.appengine.cloudsdk.CloudSdk; import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineDeployment; import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineDevServer; +import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineDevServer1; import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineFlexibleStaging; import com.google.cloud.tools.appengine.cloudsdk.CloudSdkAppEngineStandardStaging; import com.google.cloud.tools.appengine.cloudsdk.CloudSdkGenRepoInfoFile; @@ -65,21 +66,38 @@ public AppEngineDeployment deployment() { } @Override - public AppEngineDevServer devServerRunSync() { - return cloudSdkFactory.devServer(defaultCloudSdkBuilder().build()); + public AppEngineDevServer devServerRunSync(SupportedDevServerVersion version) { + return createDevServerForVersion(version); + } + + private AppEngineDevServer createDevServerForVersion(SupportedDevServerVersion version) { + return createDevServerForVersion(version, defaultCloudSdkBuilder().build()); + } + + private AppEngineDevServer createDevServerForVersion(SupportedDevServerVersion version, + CloudSdk cloudSdk) { + switch (version) { + case V1: + return cloudSdkFactory.devServer1(cloudSdk); + case V2ALPHA: + return cloudSdkFactory.devServer(cloudSdk); + default: + throw new IllegalArgumentException("Unsupported dev server version: " + version); + } } @Override - public AppEngineDevServer devServerRunAsync(int startSuccessTimeout) { + public AppEngineDevServer devServerRunAsync(int startSuccessTimeout, + SupportedDevServerVersion version) { CloudSdk.Builder builder = defaultCloudSdkBuilder() .async(true) .runDevAppServerWait(startSuccessTimeout); - return cloudSdkFactory.devServer(builder.build()); + return createDevServerForVersion(version, builder.build()); } @Override - public AppEngineDevServer devServerStop() { - return cloudSdkFactory.devServer(defaultCloudSdkBuilder().build()); + public AppEngineDevServer devServerStop(SupportedDevServerVersion version) { + return createDevServerForVersion(version); } @Override @@ -139,6 +157,10 @@ public AppEngineDevServer devServer(CloudSdk cloudSdk) { return new CloudSdkAppEngineDevServer(cloudSdk); } + public AppEngineDevServer devServer1(CloudSdk cloudSdk) { + return new CloudSdkAppEngineDevServer1(cloudSdk); + } + public GenRepoInfoFile genRepoInfoFile(CloudSdk cloudSdk) { return new CloudSdkGenRepoInfoFile(cloudSdk); } diff --git a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/RunAsyncMojo.java b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/RunAsyncMojo.java index a2cee94ff..78e8fed88 100644 --- a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/RunAsyncMojo.java +++ b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/RunAsyncMojo.java @@ -16,6 +16,8 @@ package com.google.cloud.tools.maven; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; + import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Execute; @@ -41,11 +43,13 @@ public class RunAsyncMojo extends RunMojo { public void execute() throws MojoExecutionException, MojoFailureException { verifyAppEngineStandardApp(); - getLog().info("Waiting " + startSuccessTimeout + " seconds for the Dev App Server to start."); + SupportedDevServerVersion version = convertDevserverVersionString(); + getLog().info("Waiting " + startSuccessTimeout + " seconds for the Dev App Server " + + devserverVersion + " to start."); - getAppEngineFactory().devServerRunAsync(startSuccessTimeout).run(this); + getAppEngineFactory().devServerRunAsync(startSuccessTimeout, version).run(this); - getLog().info("Dev App Server started."); + getLog().info("Dev App Server " + devserverVersion + " started."); getLog().info("Use the 'mvn appengine:stop' command to stop the server."); } diff --git a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/RunMojo.java b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/RunMojo.java index 70993a08f..5f372946f 100644 --- a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/RunMojo.java +++ b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/RunMojo.java @@ -17,6 +17,7 @@ package com.google.cloud.tools.maven; import com.google.cloud.tools.appengine.api.devserver.RunConfiguration; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; import org.apache.maven.model.Plugin; import org.apache.maven.plugin.MojoExecutionException; @@ -52,6 +53,14 @@ public class RunMojo extends CloudSdkMojo implements RunConfiguration { required = true) protected List services; + /** + * Version of the dev app server to use to run the services. Supported values are "1" and + * "2-alpha". (default: "1") + */ + @Parameter(alias = "devserver.version", property = "app.devserver.version", required = true, + defaultValue = "1") + protected String devserverVersion; + /** * Host name to which application modules should bind. (default: localhost) */ @@ -83,11 +92,19 @@ public class RunMojo extends CloudSdkMojo implements RunConfiguration { protected String authDomain; /** - * Path to the data (datastore, blobstore, etc.) associated with the application. (default: None) + * Path to the data (datastore, blobstore, etc.) associated with the application. Supported only + * for devserver version 2-alpha. (default: None) */ @Parameter(alias = "devserver.storagePath", property = "app.devserver.storagePath") protected File storagePath; + /** + * Path to a file used to store datastore contents (defaults to a file in --storage_path if not + * set). Supported only for devserver version 2-alpha.(default: None) + */ + @Parameter(alias = "devserver.datastorePath", property = "app.devserver.datastorePath") + protected File datastorePath; + /** * The log level below which logging messages generated by application code will not be displayed * on the console. Options: debug, info, warning, critical, error. (default: info) @@ -207,9 +224,24 @@ public class RunMojo extends CloudSdkMojo implements RunConfiguration { @Override public void execute() throws MojoExecutionException, MojoFailureException { + SupportedDevServerVersion convertedVersion = convertDevserverVersionString(); handleAppYamlsDeprecation(); verifyAppEngineStandardApp(); - getAppEngineFactory().devServerRunSync().run(this); + getAppEngineFactory().devServerRunSync(convertedVersion).run(this); + } + + /** + * Verifies that {@code version} is of the supported values. + * @throws MojoExecutionException if {@code version} cannot be converted to + * {@link SupportedDevServerVersion} + */ + protected SupportedDevServerVersion convertDevserverVersionString() + throws MojoExecutionException { + try { + return SupportedDevServerVersion.parse(devserverVersion); + } catch (IllegalArgumentException ex) { + throw new MojoExecutionException("Invalid version", ex); + } } /** @@ -396,7 +428,6 @@ public List getServices() { @Override public File getDatastorePath() { - // TODO Auto-generated method stub - return null; + return datastorePath; } } diff --git a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/StopMojo.java b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/StopMojo.java index ae09bc89e..8bbf11bb7 100644 --- a/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/StopMojo.java +++ b/app-maven-plugin/src/main/java/com/google/cloud/tools/maven/StopMojo.java @@ -17,6 +17,7 @@ package com.google.cloud.tools.maven; import com.google.cloud.tools.appengine.api.devserver.StopConfiguration; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -29,6 +30,26 @@ @Mojo(name = "stop") public class StopMojo extends CloudSdkMojo implements StopConfiguration { + /** + * Version of the dev app server to use to run the services. Supported values are "1" and + * "2-alpha". (default: "1") + */ + @Parameter(alias = "devserver.version", property = "app.devserver.version", required = true, + defaultValue = "1") + protected String devserverVersion; + + /** + * Host name to which application modules should bind. (default: localhost) + */ + @Parameter(alias = "devserver.host", property = "app.devserver.host") + protected String host; + + /** + * Lowest port to which application modules should bind. (default: 8080) + */ + @Parameter(alias = "devserver.port", property = "app.devserver.port") + protected Integer port; + /** * Host name to which the admin server was bound. (default: localhost) */ @@ -41,17 +62,34 @@ public class StopMojo extends CloudSdkMojo implements StopConfiguration { @Parameter(alias = "devserver.adminPort", property = "app.devserver.adminPort") protected Integer adminPort; + @Override public void execute() throws MojoExecutionException, MojoFailureException { - getAppEngineFactory().devServerStop().stop(this); + SupportedDevServerVersion convertedVersion = null; + try { + convertedVersion = SupportedDevServerVersion.parse(devserverVersion); + } catch (IllegalArgumentException ex) { + throw new MojoExecutionException("Invalid version", ex); + } + getAppEngineFactory().devServerStop(convertedVersion).stop(this); } @Override public String getAdminHost() { - return adminHost; + // https://github.com/GoogleCloudPlatform/app-maven-plugin/issues/164 + if (SupportedDevServerVersion.parse(devserverVersion) == SupportedDevServerVersion.V2ALPHA) { + return adminHost; + } else { + return host; + } } @Override public Integer getAdminPort() { - return adminPort; + // https://github.com/GoogleCloudPlatform/app-maven-plugin/issues/164 + if (SupportedDevServerVersion.parse(devserverVersion) == SupportedDevServerVersion.V2ALPHA) { + return adminPort; + } else { + return port; + } } } diff --git a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/CloudSdkAppEngineFactoryTest.java b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/CloudSdkAppEngineFactoryTest.java index db8cf02a2..ed195b41e 100644 --- a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/CloudSdkAppEngineFactoryTest.java +++ b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/CloudSdkAppEngineFactoryTest.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.when; import com.google.cloud.tools.appengine.cloudsdk.CloudSdk; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; import com.google.cloud.tools.maven.CloudSdkAppEngineFactory.DefaultProcessOutputLineListener; import org.junit.Before; @@ -31,7 +32,6 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; @@ -99,9 +99,20 @@ public void testDeployment() { } @Test - public void testDevServerRunSync() { + public void testDevServer1RunSync() { // invoke - factory.devServerRunSync(); + factory.devServerRunSync(SupportedDevServerVersion.V1); + + // verify + verify(cloudSdkBuilderMock).build(); + verify(cloudSdkFactoryMock).devServer1(cloudSdkMock); + verifyDefaultCloudSdkBuilder(); + } + + @Test + public void testDevServer2RunSync() { + // invoke + factory.devServerRunSync(SupportedDevServerVersion.V2ALPHA); // verify verify(cloudSdkBuilderMock).build(); @@ -110,11 +121,26 @@ public void testDevServerRunSync() { } @Test - public void testDevServerRunAsync() { + public void testDevServer1RunAsync() { + final int START_SUCCESS_TIMEOUT = 25; + + // invoke + factory.devServerRunAsync(START_SUCCESS_TIMEOUT, SupportedDevServerVersion.V1); + + // verify + verify(cloudSdkBuilderMock).build(); + verify(cloudSdkFactoryMock).devServer1(cloudSdkMock); + verify(cloudSdkBuilderMock).async(true); + verify(cloudSdkBuilderMock).runDevAppServerWait(START_SUCCESS_TIMEOUT); + verifyDefaultCloudSdkBuilder(); + } + + @Test + public void testDevServer2RunAsync() { final int START_SUCCESS_TIMEOUT = 25; // invoke - factory.devServerRunAsync(START_SUCCESS_TIMEOUT); + factory.devServerRunAsync(START_SUCCESS_TIMEOUT, SupportedDevServerVersion.V2ALPHA); // verify verify(cloudSdkBuilderMock).build(); @@ -125,9 +151,18 @@ public void testDevServerRunAsync() { } @Test - public void testDevServerStop() { + public void testDevServer1Stop() { + // invoke + factory.devServerStop(SupportedDevServerVersion.V1); + + // verify + verify(cloudSdkFactoryMock).devServer1(cloudSdkMock); + } + + @Test + public void testDevServer2Stop() { // invoke - factory.devServerStop(); + factory.devServerStop(SupportedDevServerVersion.V2ALPHA); // verify verify(cloudSdkFactoryMock).devServer(cloudSdkMock); diff --git a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/RunAsyncMojoTest.java b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/RunAsyncMojoTest.java index f599c912c..01b2e86e8 100644 --- a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/RunAsyncMojoTest.java +++ b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/RunAsyncMojoTest.java @@ -20,18 +20,24 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; + import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.MockitoAnnotations; import java.io.IOException; -@RunWith(MockitoJUnitRunner.class) +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; + +@RunWith(JUnitParamsRunner.class) public class RunAsyncMojoTest extends AbstractDevServerTest { @Mock @@ -40,13 +46,22 @@ public class RunAsyncMojoTest extends AbstractDevServerTest { @InjectMocks private RunAsyncMojo runAsyncMojo; + @Before + public void setUp(){ + MockitoAnnotations.initMocks(this); + } + @Test - public void testRunAsync() throws MojoFailureException, MojoExecutionException, IOException { + @Parameters({"1,V1", "2-alpha,V2ALPHA"}) + public void testRunAsync(String version, SupportedDevServerVersion mockVersion) + throws MojoFailureException, MojoExecutionException, IOException { final int START_SUCCESS_TIMEOUT = 25; // wire up + runAsyncMojo.devserverVersion = version; setUpAppEngineWebXml(); - when(factoryMock.devServerRunAsync(START_SUCCESS_TIMEOUT)).thenReturn(devServerMock); + when(factoryMock.devServerRunAsync(START_SUCCESS_TIMEOUT, mockVersion)) + .thenReturn(devServerMock); runAsyncMojo.startSuccessTimeout = START_SUCCESS_TIMEOUT; // invoke diff --git a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/RunMojoTest.java b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/RunMojoTest.java index 0e6ea6b70..f2a35b27c 100644 --- a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/RunMojoTest.java +++ b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/RunMojoTest.java @@ -21,6 +21,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; + import org.apache.maven.model.Plugin; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -32,13 +34,16 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.MockitoAnnotations; import java.io.File; import java.io.IOException; import java.util.Collections; -@RunWith(MockitoJUnitRunner.class) +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; + +@RunWith(JUnitParamsRunner.class) public class RunMojoTest extends AbstractDevServerTest { @Mock @@ -53,16 +58,31 @@ public class RunMojoTest extends AbstractDevServerTest { @Before public void setUp() { + MockitoAnnotations.initMocks(this); when(mavenProjectMock.getPlugin("com.google.cloud.tools:appengine-maven-plugin")) .thenReturn(mockPlugin); when(mockPlugin.getConfiguration()).thenReturn(mockConfiguration); } @Test - public void testRun() throws MojoFailureException, MojoExecutionException, IOException { + public void testInvalidVersionString() + throws IOException, MojoExecutionException, MojoFailureException { + runMojo.devserverVersion = "bogus-version"; + setUpAppEngineWebXml(); + expectedException.expect(MojoExecutionException.class); + expectedException.expectMessage("Invalid version"); + + runMojo.execute(); + } + + @Test + @Parameters({"1,V1", "2-alpha,V2ALPHA" }) + public void testRun(String version, SupportedDevServerVersion mockVersion) + throws MojoFailureException, MojoExecutionException, IOException { // wire up + runMojo.devserverVersion = version; setUpAppEngineWebXml(); - when(factoryMock.devServerRunSync()).thenReturn(devServerMock); + when(factoryMock.devServerRunSync(mockVersion)).thenReturn(devServerMock); // invoke runMojo.execute(); @@ -72,11 +92,14 @@ public void testRun() throws MojoFailureException, MojoExecutionException, IOExc } @Test - public void testRun_appYamlsSetAndOverridesServices() + @Parameters({"1,V1", "2-alpha,V2ALPHA" }) + public void testRun_appYamlsSetAndOverridesServices(String version, + SupportedDevServerVersion mockVersion) throws MojoFailureException, MojoExecutionException, IOException { + runMojo.devserverVersion = version; setUpAppEngineWebXml(); runMojo.appYamls = Collections.singletonList(new File("src/main/appengine")); - when(factoryMock.devServerRunSync()).thenReturn(devServerMock); + when(factoryMock.devServerRunSync(mockVersion)).thenReturn(devServerMock); runMojo.execute(); @@ -85,12 +108,14 @@ public void testRun_appYamlsSetAndOverridesServices() } @Test - public void testRun_appYamlsNotSetServicesIsUsed() + @Parameters({"1,V1", "2-alpha,V2ALPHA" }) + public void testRun_appYamlsNotSetServicesIsUsed(String version, + SupportedDevServerVersion mockVersion) throws MojoFailureException, MojoExecutionException, IOException { + runMojo.devserverVersion = version; setUpAppEngineWebXml(); - runMojo.appYamls = null; runMojo.services = Collections.singletonList(new File("src/main/appengine")); - when(factoryMock.devServerRunSync()).thenReturn(devServerMock); + when(factoryMock.devServerRunSync(mockVersion)).thenReturn(devServerMock); runMojo.execute(); @@ -99,12 +124,15 @@ public void testRun_appYamlsNotSetServicesIsUsed() } @Test - public void testRun_appYamlsEmptyServicesIsUsed() + @Parameters({"1,V1", "2-alpha,V2ALPHA" }) + public void testRun_appYamlsEmptyServicesIsUsed(String version, + SupportedDevServerVersion mockVersion) throws MojoFailureException, MojoExecutionException, IOException { + runMojo.devserverVersion = version; setUpAppEngineWebXml(); runMojo.appYamls = Collections.emptyList(); runMojo.services = Collections.singletonList(new File("src/main/appengine")); - when(factoryMock.devServerRunSync()).thenReturn(devServerMock); + when(factoryMock.devServerRunSync(mockVersion)).thenReturn(devServerMock); runMojo.execute(); @@ -113,12 +141,15 @@ public void testRun_appYamlsEmptyServicesIsUsed() } @Test - public void testRun_appYamlAndServicesSetCausesError() + @Parameters({"1,V1", "2-alpha,V2ALPHA" }) + public void testRun_appYamlAndServicesSetCausesError(String version, + SupportedDevServerVersion mockVersion) throws MojoFailureException, MojoExecutionException, IOException { + runMojo.devserverVersion = version; when(mockConfiguration.getChild("services")).thenReturn(mock(Xpp3Dom.class)); runMojo.appYamls = Collections.singletonList(new File("src/main/appengine")); runMojo.services = Collections.singletonList(new File("src/main/appengine2")); - when(factoryMock.devServerRunSync()).thenReturn(devServerMock); + when(factoryMock.devServerRunSync(mockVersion)).thenReturn(devServerMock); expectedException.expect(MojoExecutionException.class); expectedException.expectMessage("Both and are defined." @@ -128,8 +159,10 @@ public void testRun_appYamlAndServicesSetCausesError() } @Test - public void testRun_unexpectedConfigurationClass() + @Parameters({"1", "2-alpha" }) + public void testRun_unexpectedConfigurationClass(String version) throws MojoFailureException, MojoExecutionException, IOException { + runMojo.devserverVersion = version; when(mockConfiguration.getChild("services")).thenReturn(mock(Xpp3Dom.class)); runMojo.appYamls = Collections.singletonList(new File("src/main/appengine")); when(mockPlugin.getConfiguration()).thenReturn(new Object()); @@ -142,9 +175,12 @@ public void testRun_unexpectedConfigurationClass() } @Test - public void testRunFlexible() throws MojoFailureException, MojoExecutionException, IOException { + @Parameters({"1,V1", "2-alpha,V2ALPHA" }) + public void testRunFlexible(String version, SupportedDevServerVersion mockVersion) + throws MojoFailureException, MojoExecutionException, IOException { // wire up - when(factoryMock.devServerRunSync()).thenReturn(devServerMock); + runMojo.devserverVersion = version; + when(factoryMock.devServerRunSync(mockVersion)).thenReturn(devServerMock); // invoke expectedException.expect(MojoExecutionException.class); diff --git a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/StopMojoTest.java b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/StopMojoTest.java index a3358fa93..9696634b9 100644 --- a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/StopMojoTest.java +++ b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/StopMojoTest.java @@ -16,22 +16,38 @@ package com.google.cloud.tools.maven; +import static org.junit.Assert.*; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.google.cloud.tools.appengine.api.devserver.AppEngineDevServer; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; +import com.google.common.base.Joiner; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.MockitoAnnotations; -@RunWith(MockitoJUnitRunner.class) +import java.io.IOException; + +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; + +@RunWith(JUnitParamsRunner.class) public class StopMojoTest { + private static final String ADMIN_PORT = "2"; + private static final String PORT = "1"; + private static final String V1_VERSION = "1"; + private static final Object V2_VERSION = "2-alpha"; + @Mock private CloudSdkAppEngineFactory factoryMock; @@ -41,11 +57,32 @@ public class StopMojoTest { @InjectMocks private StopMojo stopMojo; + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp(){ + MockitoAnnotations.initMocks(this); + } + + @Test + public void testInvalidVersionString() + throws IOException, MojoExecutionException, MojoFailureException { + stopMojo.devserverVersion = "bogus-version"; + expectedException.expect(MojoExecutionException.class); + expectedException.expectMessage("Invalid version"); + + stopMojo.execute(); + } + @Test - public void testStop() throws MojoFailureException, MojoExecutionException { + @Parameters({"1,V1", "2-alpha,V2ALPHA"}) + public void testStop(String version, SupportedDevServerVersion mockVersion) + throws MojoFailureException, MojoExecutionException { // wire up - when(factoryMock.devServerStop()).thenReturn(devServerMock); + stopMojo.devserverVersion = version; + when(factoryMock.devServerStop(mockVersion)).thenReturn(devServerMock); // invoke stopMojo.execute(); @@ -54,4 +91,33 @@ public void testStop() throws MojoFailureException, MojoExecutionException { verify(devServerMock).stop(stopMojo); } + @Test + @Parameters({"host,adminhost,1,host", "host,adminhost,2-alpha,adminhost"}) + public void testGetAdminHost(String host, String adminHost, String version, String expected) + throws Exception { + stopMojo.devserverVersion = version; + stopMojo.adminHost = adminHost; + stopMojo.host = host; + + assertEquals(expected, stopMojo.getAdminHost()); + } + + @Test + @Parameters + public void testGetAdminPort(Integer port, Integer adminPort, String version, Integer expected) + throws Exception { + stopMojo.devserverVersion = version; + stopMojo.adminPort = adminPort; + stopMojo.port = port; + + assertEquals(expected, stopMojo.getAdminPort()); + } + + @SuppressWarnings("unused") // used for testGetAdminPort() + private Object[] parametersForTestGetAdminPort() { + return new Object[] { + new Object[]{ PORT, ADMIN_PORT, V1_VERSION, PORT }, + new Object[]{ PORT, ADMIN_PORT, V2_VERSION, ADMIN_PORT} + }; + } } diff --git a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/SupportedDevServerVersionTest.java b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/SupportedDevServerVersionTest.java new file mode 100644 index 000000000..5aa3fe524 --- /dev/null +++ b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/SupportedDevServerVersionTest.java @@ -0,0 +1,33 @@ +package com.google.cloud.tools.maven; + +import static org.junit.Assert.assertEquals; + +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class SupportedDevServerVersionTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void testParseV1() { + assertEquals(SupportedDevServerVersion.V1, SupportedDevServerVersion.parse("1")); + } + + @Test + public void testParseV2Alpha() { + assertEquals(SupportedDevServerVersion.V2ALPHA, SupportedDevServerVersion.parse("2-alpha")); + } + + @Test + public void testParseInvalidVersion() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Unsupported version value: foo"); + + SupportedDevServerVersion.parse("foo"); + } +} diff --git a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/it/RunAsyncMojoIntegrationTest.java b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/it/RunAsyncMojoIntegrationTest.java index 9d15d7d42..ae37ce785 100644 --- a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/it/RunAsyncMojoIntegrationTest.java +++ b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/it/RunAsyncMojoIntegrationTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; import com.google.cloud.tools.maven.it.util.UrlUtils; import com.google.cloud.tools.maven.it.verifier.StandardVerifier; @@ -28,7 +29,6 @@ import java.io.IOException; - public class RunAsyncMojoIntegrationTest extends AbstractMojoIntegrationTest { private static final String ADMIN_PORT = "28082"; @@ -36,29 +36,43 @@ public class RunAsyncMojoIntegrationTest extends AbstractMojoIntegrationTest { private static final String SERVER_URL = "http://localhost:" + SERVER_PORT; @Test - public void testRunAsyncStandard() + public void testRunAsyncStandardV1() throws IOException, VerificationException, InterruptedException { + test("testRunAsyncV1", SupportedDevServerVersion.V1); + } - Verifier verifier = new StandardVerifier("testRunAsync"); + @Test + public void testRunAsyncStandardV2Alpha() + throws IOException, VerificationException, InterruptedException { + test("testRunAsyncV2Alpha", SupportedDevServerVersion.V2ALPHA); + } + private void test(String name, SupportedDevServerVersion version) + throws IOException, VerificationException, InterruptedException { try { - // execute - verifier.setSystemProperty("app.devserver.port", SERVER_PORT); - verifier.setSystemProperty("app.devserver.adminPort", ADMIN_PORT); - verifier.executeGoal("appengine:start"); + Verifier verifier = createVerifier(name, version); + verifier.executeGoal("appengine:start"); - // verify - assertEquals("Hello from the App Engine Standard project.", - UrlUtils.getUrlContentWithRetries(SERVER_URL, 5000, 1000)); - verifier.verifyErrorFreeLog(); - verifier.verifyTextInLog("Dev App Server is now running"); + assertEquals("Hello from the App Engine Standard project.", + UrlUtils.getUrlContentWithRetries(SERVER_URL, 50000, 1000)); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Dev App Server is now running"); } finally { - // stop server - Verifier stopVerifier = new StandardVerifier("testRunAsyncStandard_stop"); - stopVerifier.setSystemProperty("app.devserver.adminPort", ADMIN_PORT); + Verifier stopVerifier = createVerifier(name + "_stop", version); stopVerifier.executeGoal("appengine:stop"); // wait up to 5 seconds for the server to stop assertTrue(UrlUtils.isUrlDownWithRetries(SERVER_URL, 5000, 100)); } } + + private Verifier createVerifier(String name, SupportedDevServerVersion version) + throws IOException, VerificationException { + Verifier verifier = new StandardVerifier(name); + verifier.setSystemProperty("app.devserver.port", SERVER_PORT); + if (version == SupportedDevServerVersion.V2ALPHA) { + verifier.setSystemProperty("app.devserver.adminPort", ADMIN_PORT); + verifier.setSystemProperty("app.devserver.version", "2-alpha"); + } + return verifier; + } } diff --git a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/it/RunMojoIntegrationTest.java b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/it/RunMojoIntegrationTest.java index 374b702a2..117c524a6 100644 --- a/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/it/RunMojoIntegrationTest.java +++ b/app-maven-plugin/src/test/java/com/google/cloud/tools/maven/it/RunMojoIntegrationTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.google.cloud.tools.maven.AppEngineFactory.SupportedDevServerVersion; import com.google.cloud.tools.maven.it.util.UrlUtils; import com.google.cloud.tools.maven.it.verifier.StandardVerifier; @@ -28,7 +29,6 @@ import java.io.IOException; - public class RunMojoIntegrationTest extends AbstractMojoIntegrationTest { private static final String ADMIN_PORT = "28082"; @@ -36,9 +36,19 @@ public class RunMojoIntegrationTest extends AbstractMojoIntegrationTest { private static final String SERVER_URL = "http://localhost:" + SERVER_PORT; @Test - public void testRunStandard() throws IOException, VerificationException, InterruptedException { + public void testRunStandardV1() throws IOException, VerificationException, InterruptedException { + test("testRunV1", SupportedDevServerVersion.V1); + } - final Verifier verifier = new StandardVerifier("testRun"); + @Test + public void testRunStandardV2Alpha() + throws IOException, VerificationException, InterruptedException { + test("testRunV2Alpha", SupportedDevServerVersion.V2ALPHA); + } + + private void test(final String name, final SupportedDevServerVersion version) + throws IOException, VerificationException, InterruptedException { + final Verifier verifier = createVerifier(name, version); final StringBuilder urlContent = new StringBuilder(); Thread thread = new Thread() { @@ -52,8 +62,7 @@ public void run() { } finally { // stop server try { - Verifier stopVerifier = new StandardVerifier("testRun_stop"); - stopVerifier.setSystemProperty("app.devserver.adminPort", ADMIN_PORT); + Verifier stopVerifier = createVerifier(name + "_stop", version); stopVerifier.executeGoal("appengine:stop"); // wait up to 5 seconds for the server to stop assertTrue(UrlUtils.isUrlDownWithRetries(SERVER_URL, 5000, 100)); @@ -77,6 +86,16 @@ public void run() { assertEquals("Hello from the App Engine Standard project.", urlContent.toString()); verifier.verifyErrorFreeLog(); verifier.verifyTextInLog("Dev App Server is now running"); + } + private Verifier createVerifier(String name, SupportedDevServerVersion version) + throws IOException, VerificationException { + Verifier verifier = new StandardVerifier(name); + verifier.setSystemProperty("app.devserver.port", SERVER_PORT); + if (version == SupportedDevServerVersion.V2ALPHA) { + verifier.setSystemProperty("app.devserver.adminPort", ADMIN_PORT); + verifier.setSystemProperty("app.devserver.version", "2-alpha"); + } + return verifier; } }