diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/BootstrapLoader.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/BootstrapLoader.java deleted file mode 100644 index 1e4ddb4..0000000 --- a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/BootstrapLoader.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright contributors to the Galasa project - * - * SPDX-License-Identifier: EPL-2.0 - */ -package dev.galasa.maven.plugin; - -import java.net.URL; -import java.util.Properties; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; - -public interface BootstrapLoader { - public Properties getBootstrapProperties(URL bootstrapUrl, Log log ) throws MojoExecutionException ; -} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/DeployTestCatalog.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/DeployTestCatalog.java index 0aaffa3..184737a 100644 --- a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/DeployTestCatalog.java +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/DeployTestCatalog.java @@ -5,15 +5,7 @@ */ package dev.galasa.maven.plugin; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; import java.net.URL; -import java.util.Properties; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.http.client.HttpClient; -import org.apache.http.impl.client.HttpClientBuilder; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -22,9 +14,17 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; -import dev.galasa.maven.plugin.auth.AuthenticationService; -import dev.galasa.maven.plugin.auth.AuthenticationServiceFactory; -import dev.galasa.maven.plugin.auth.AuthenticationServiceFactoryImpl; + +import dev.galasa.maven.plugin.common.BootstrapLoader; +import dev.galasa.maven.plugin.common.BootstrapLoaderImpl; +import dev.galasa.maven.plugin.common.ErrorRaiser; +import dev.galasa.maven.plugin.common.GalasaRestApiMetadata; +import dev.galasa.maven.plugin.common.TestCatalogArtifact; +import dev.galasa.maven.plugin.common.TestCatalogArtifactDeployer; +import dev.galasa.maven.plugin.common.URLCalculator; +import dev.galasa.maven.plugin.common.WrappedLog; +import dev.galasa.maven.plugin.common.auth.AuthenticationServiceFactory; +import dev.galasa.maven.plugin.common.auth.AuthenticationServiceFactoryImpl; /** * Merge all the test catalogs on the dependency list @@ -58,11 +58,6 @@ public class DeployTestCatalog extends AbstractMojo { @Parameter(defaultValue = "${galasa.skip.deploytestcatalog}", readonly = true, required = false) public boolean skipDeployTestCatalog; - // A protected variable so we can inject a mock factory if needed during unit testing. - protected AuthenticationServiceFactory authFactory = new AuthenticationServiceFactoryImpl(); - - protected BootstrapLoader bootstrapLoader = new BootstrapLoaderImpl(); - public void execute() throws MojoExecutionException, MojoFailureException { boolean skip = (skipBundleTestCatalog || skipBundleTestCatalogOldSpelling); @@ -96,18 +91,19 @@ public void execute() throws MojoExecutionException, MojoFailureException { return; } - Properties bootstrapProperties = bootstrapLoader.getBootstrapProperties(bootstrapUrl,getLog()); - - URL testcatalogUrl = calculateTestCatalogUrl(bootstrapProperties, this.testStream, this.bootstrapUrl); - - String jwt = null ; - // For now, if no galasa token is supplied, that's ok. It's optional. - // If no galasa access token supplied by the user, the jwt will stay as null. - if ( (this.galasaAccessToken!=null) && (!this.galasaAccessToken.isEmpty()) ) { - jwt = getAuthenticatedJwt(this.authFactory, this.galasaAccessToken, this.bootstrapUrl) ; - } - - publishTestCatalogToGalasaServer(testcatalogUrl,jwt, testCatalogArtifact); + // Instantiate maven-specific versions of the interfaces. + WrappedLog wrappedLog = new WrappedLogMaven(getLog()); + AuthenticationServiceFactory authFactory = new AuthenticationServiceFactoryImpl(); + ErrorRaiser errorRaiser = new ErrorRaiserMavenImpl(getLog()); + URLCalculator urlCalculator = new URLCalculator(errorRaiser); + GalasaRestApiMetadata restApiMetadata = new GalasaRestApiMetadata(); + BootstrapLoader bootstrapLoader = new BootstrapLoaderImpl(wrappedLog,errorRaiser); + TestCatalogArtifact wrappedTestCatalogArtifact = new TestCatalogArtifactMavenImpl(testCatalogArtifact, errorRaiser); + TestCatalogArtifactDeployer deployer = new TestCatalogArtifactDeployer( + wrappedLog, errorRaiser, bootstrapLoader, urlCalculator, restApiMetadata, authFactory); + + // Deploy the test catalog to the Galasa server. + deployer.deployToServer(bootstrapUrl, testStream, galasaAccessToken, wrappedTestCatalogArtifact); } private Artifact getTestCatalogArtifact() { @@ -120,129 +116,5 @@ private Artifact getTestCatalogArtifact() { } return artifact; } - - private void publishTestCatalogToGalasaServer(URL testCatalogUrl, String jwt, Artifact testCatalogArtifact) throws MojoExecutionException { - - HttpURLConnection conn = null ; - try { - conn = (HttpURLConnection) testCatalogUrl.openConnection(); - } catch (IOException ioEx) { - String msg = "Problem publishing the test catalog. Could not open URL connection to the Galasa server."; - getLog().error(msg,ioEx); - throw new MojoExecutionException(msg,ioEx); - } - - if (conn==null) { - throw new MojoExecutionException("Deploy to Test Catalog Store failed. Could not open a URL connection to the Galasa server."); - } else { - try { - postTestCatalogToGalasaServer(conn, testCatalogUrl, jwt, testCatalogArtifact); - } finally { - conn.disconnect(); - } - getLog().info("Test Catalog successfully deployed to " + testCatalogUrl.toString()); - } - } - - private void postTestCatalogToGalasaServer(HttpURLConnection conn, URL testCatalogUrl, String jwt, Artifact testCatalogArtifact) throws MojoExecutionException { - - int rc = 0 ; - String response = ""; - String message = ""; - - try { - conn.setDoOutput(true); - conn.setDoInput(true); - conn.setRequestMethod("PUT"); - conn.addRequestProperty("Content-Type", "application/json"); - conn.addRequestProperty("Accept", "application/json"); - - // Only add the jwt header if we have a jwt value. - if (jwt == null) { - getLog().info("Not sending a JWT bearer token to the server, as the galasa.token property was not supplied."); - } else { - conn.addRequestProperty("Authorization", "Bearer "+jwt); - } - - conn.addRequestProperty("ClientApiVersion","0.32.0"); // The version of the API we coded this against. - - FileUtils.copyFile(testCatalogArtifact.getFile(), conn.getOutputStream()); - rc = conn.getResponseCode(); - message = conn.getResponseMessage(); - - InputStream is = null; - if (rc != HttpURLConnection.HTTP_OK) { - is = conn.getInputStream(); - } else { - is = conn.getErrorStream(); - } - - if (is != null) { - response = IOUtils.toString(is, "utf-8"); - } - } catch(IOException ioEx) { - String msg = "Problem publishing the test catalog. Problem dealing with response from Galasa server."; - getLog().error(msg, ioEx); - throw new MojoExecutionException(msg,ioEx); - } - - if (rc != HttpURLConnection.HTTP_OK) { - getLog().error("Deploy to Test Catalog Store failed:-"); - getLog().error(Integer.toString(rc) + " - " + message); - if (!response.isEmpty()) { - getLog().error(response); - String msg = "Failed to deploy the test catalog. The server did not reply with OK (200)"; - getLog().error(msg); - throw new MojoExecutionException(msg); - } - } - } - - // Protected so we can easily unit test it without mocking framework. - protected URL calculateTestCatalogUrl(Properties bootstrapProperties, String testStream, URL bootstrapUrl) throws MojoExecutionException { - String sTestcatalogUrl = bootstrapProperties.getProperty("framework.testcatalog.url"); - if (sTestcatalogUrl == null || sTestcatalogUrl.trim().isEmpty()) { - String sBootstrapUrl = bootstrapUrl.toString(); - if (!sBootstrapUrl.endsWith("/bootstrap")) { - String msg = "Unable to calculate the test catalog url, the bootstrap url does not end with /bootstrap, need a framework.testcatalog.url property in the bootstrap"; - getLog().error(msg); - throw new MojoExecutionException(msg); - } - - sTestcatalogUrl = sBootstrapUrl.substring(0, sBootstrapUrl.length() - 10) + "/testcatalog"; - } - - // convert into a proper URL - URL testCatalogUrl ; - try { - testCatalogUrl = new URL(sTestcatalogUrl + "/" + testStream); - } catch(Exception ex) { - String msg = "Problem publishing the test catalog. Badly formed URL to the Galasa server."; - getLog().error(msg,ex); - throw new MojoExecutionException(msg,ex); - } - return testCatalogUrl; - } - - /** - * Swap the galasa token for a JWT which we can use to talk to the API server on the ecosystem. - * @return A JWT string (Java Web Token). - * @throws MojoExecutionException - */ - private String getAuthenticatedJwt(AuthenticationServiceFactory authFactory, String galasaAccessToken, URL bootstrapUrl) throws MojoExecutionException { - String jwt; - try { - String apiServerUrlString = bootstrapUrl.toString().replaceAll("/bootstrap",""); - HttpClient httpClient = HttpClientBuilder.create().build(); - URL apiServerUrl = new URL(apiServerUrlString); - AuthenticationService authTokenService = authFactory.newAuthenticationService(apiServerUrl,galasaAccessToken,httpClient); - getLog().info("Turning the galasa access token into a jwt"); - jwt = authTokenService.getJWT(); - getLog().info("Java Web Token (jwt) obtained from the galasa ecosystem OK."); - } catch( Exception ex) { - getLog().error(ex.getMessage()); - throw new MojoExecutionException(ex.getMessage(),ex); - } - return jwt; - } + } diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/ErrorRaiserMavenImpl.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/ErrorRaiserMavenImpl.java new file mode 100644 index 0000000..ebf7a02 --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/ErrorRaiserMavenImpl.java @@ -0,0 +1,37 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.maven.plugin; + +import java.text.MessageFormat; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; + +import dev.galasa.maven.plugin.error.ErrorRaiser; + +// Logs errors, then raises an exception. +public class ErrorRaiserMavenImpl implements ErrorRaiser { + + Log log ; + + public ErrorRaiserMavenImpl(Log log) { + this.log = log; + } + + @Override + public void raiseError(String template, Object... parameters) throws MojoExecutionException { + String msg = MessageFormat.format(template,parameters); + log.error(msg); + throw new MojoExecutionException(msg); + } + + @Override + public void raiseError( Throwable cause, String template, Object... parameters) throws MojoExecutionException { + String msg = MessageFormat.format(template,parameters)+" cause:"+cause.toString(); + log.error(msg); + throw new MojoExecutionException(msg,cause); + } +} \ No newline at end of file diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/TestCatalogArtifactMavenImpl.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/TestCatalogArtifactMavenImpl.java new file mode 100644 index 0000000..c50bee3 --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/TestCatalogArtifactMavenImpl.java @@ -0,0 +1,37 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.maven.plugin; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.commons.io.FileUtils; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.MojoExecutionException; + +import dev.galasa.maven.plugin.error.ErrorRaiser; +import dev.galasa.maven.plugin.file.TestCatalogArtifact; + +public class TestCatalogArtifactMavenImpl implements TestCatalogArtifact { + + private Artifact testCatalogArtifact; + private ErrorRaiser errorRaiser; + + public TestCatalogArtifactMavenImpl(Artifact testCatalogArtifact, ErrorRaiser errorRaiser ) { + this.errorRaiser = errorRaiser; + this.testCatalogArtifact = testCatalogArtifact ; + } + + @Override + public void transferTo(OutputStream outputStream) throws MojoExecutionException { + try { + FileUtils.copyFile(this.testCatalogArtifact.getFile(), outputStream); + } catch( IOException ex ) { + errorRaiser.raiseError(ex,ex.toString()); + } + } + +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/WrappedLogMaven.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/WrappedLogMaven.java new file mode 100644 index 0000000..223c8a3 --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/WrappedLogMaven.java @@ -0,0 +1,25 @@ +package dev.galasa.maven.plugin; + +import org.apache.maven.plugin.logging.Log; + +import dev.galasa.maven.plugin.log.WrappedLog; + +public class WrappedLogMaven implements WrappedLog { + + private Log log ; + + public WrappedLogMaven(Log log) { + this.log = log; + } + + @Override + public void info(String message) { + log.info(message); + } + + @Override + public void error(String message) { + log.error(message); + } + +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/BootstrapLoader.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/BootstrapLoader.java new file mode 100644 index 0000000..d089585 --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/BootstrapLoader.java @@ -0,0 +1,13 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.maven.plugin; + +import java.net.URL; +import java.util.Properties; + +public interface BootstrapLoader { + public Properties getBootstrapProperties(URL bootstrapUrl) throws Ex ; +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/BootstrapLoaderImpl.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/BootstrapLoaderImpl.java similarity index 56% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/BootstrapLoaderImpl.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/BootstrapLoaderImpl.java index 498a388..78b5d3c 100644 --- a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/BootstrapLoaderImpl.java +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/BootstrapLoaderImpl.java @@ -3,19 +3,24 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package dev.galasa.maven.plugin; +package dev.galasa.maven.plugin.common; import java.net.URL; import java.net.URLConnection; import java.text.MessageFormat; import java.util.Properties; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugin.MojoExecutionException; -public class BootstrapLoaderImpl implements BootstrapLoader { +public class BootstrapLoaderImpl implements BootstrapLoader { + WrappedLog log ; + ErrorRaiser errorRaiser ; + + public BootstrapLoaderImpl( WrappedLog log , ErrorRaiser errorRaiser ) { + this.log = log ; + this.errorRaiser = errorRaiser; + } @Override - public Properties getBootstrapProperties(URL bootstrapUrl, Log log ) throws MojoExecutionException { + public Properties getBootstrapProperties(URL bootstrapUrl) throws Ex { Properties bootstrapProperties = new Properties(); try { URLConnection connection = bootstrapUrl.openConnection(); @@ -23,10 +28,8 @@ public Properties getBootstrapProperties(URL bootstrapUrl, Log log ) throws Mojo log.info(msg); bootstrapProperties.load(connection.getInputStream()); log.info("execute(): bootstrapProperties loaded: " + bootstrapProperties); - } catch (Exception e) { - String errMsg = MessageFormat.format("execute() - Unable to load bootstrap properties, Reason: {0}", e); - log.error(errMsg); - throw new MojoExecutionException(errMsg, e); + } catch (Exception ex) { + errorRaiser.raiseError(ex,"execute() - Unable to load bootstrap properties, Reason: {0}",ex.getMessage()); } return bootstrapProperties; } diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/ErrorRaiser.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/ErrorRaiser.java new file mode 100644 index 0000000..f35fc0f --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/ErrorRaiser.java @@ -0,0 +1,12 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.maven.plugin.error; + +// Logs errors, then raises an exception. +public interface ErrorRaiser { + void raiseError(String template, Object... parameters) throws Ex ; + void raiseError( Throwable cause, String template, Object... parameters) throws Ex; +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/GalasaRestApiMetadata.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/GalasaRestApiMetadata.java new file mode 100644 index 0000000..1574ea3 --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/GalasaRestApiMetadata.java @@ -0,0 +1,7 @@ +package dev.galasa.maven.plugin.common; + +public class GalasaRestApiMetadata { + public String getGalasaRestApiVersion() { + return "0.32.0"; + } +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/GsonFactory.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/GsonFactory.java similarity index 84% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/GsonFactory.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/GsonFactory.java index f4c22f3..053fa52 100644 --- a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/GsonFactory.java +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/GsonFactory.java @@ -1,4 +1,4 @@ -package dev.galasa.maven.plugin.auth; +package dev.galasa.maven.plugin.common; import com.google.gson.Gson; import com.google.gson.GsonBuilder; diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/TestCatalogArtifact.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/TestCatalogArtifact.java new file mode 100644 index 0000000..7f94e94 --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/TestCatalogArtifact.java @@ -0,0 +1,12 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.maven.plugin.common; + +import java.io.OutputStream; + +public interface TestCatalogArtifact { + void transferTo(OutputStream outputStream) throws Ex; +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/TestCatalogArtifactDeployer.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/TestCatalogArtifactDeployer.java new file mode 100644 index 0000000..a1a579a --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/TestCatalogArtifactDeployer.java @@ -0,0 +1,162 @@ +package dev.galasa.maven.plugin.file; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.Properties; + +import org.apache.commons.io.IOUtils; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.maven.plugin.MojoExecutionException; + +import dev.galasa.maven.plugin.BootstrapLoader; +import dev.galasa.maven.plugin.GalasaRestApiMetadata; +import dev.galasa.maven.plugin.auth.AuthenticationService; +import dev.galasa.maven.plugin.auth.AuthenticationServiceFactory; +import dev.galasa.maven.plugin.error.ErrorRaiser; +import dev.galasa.maven.plugin.log.WrappedLog; +import dev.galasa.maven.plugin.url.URLCalculator; + +public class TestCatalogArtifactDeployer { + + private WrappedLog log ; + private ErrorRaiser errorRaiser ; + private BootstrapLoader bootstrapLoader ; + private URLCalculator urlCalculator ; + private GalasaRestApiMetadata restApiMetadata; + private AuthenticationServiceFactory authFactory; + + public TestCatalogArtifactDeployer( + WrappedLog log, + ErrorRaiser errorRaiser, + BootstrapLoader bootstrapLoader, + URLCalculator urlCalculator, + GalasaRestApiMetadata restApiMetadata, + AuthenticationServiceFactory authFactory + ) { + this.log = log ; + this.errorRaiser = errorRaiser ; + this.bootstrapLoader = bootstrapLoader; + this.urlCalculator = urlCalculator; + this.restApiMetadata = restApiMetadata; + this.authFactory = authFactory; + } + + public void deployToServer(URL bootstrapUrl, String testStream, String galasaAccessToken, TestCatalogArtifact testCatalogArtifact ) throws Ex { + + Properties bootstrapProperties = bootstrapLoader.getBootstrapProperties(bootstrapUrl); + + String apiServerUrl = urlCalculator.calculateApiServerUrl(bootstrapProperties, bootstrapUrl); + + URL testcatalogUrl = urlCalculator.calculateTestCatalogUrl(apiServerUrl, testStream); + + String jwt = null ; + // For now, if no galasa token is supplied, that's ok. It's optional. + // If no galasa access token supplied by the user, the jwt will stay as null. + if ( (galasaAccessToken!=null) && (!galasaAccessToken.isEmpty()) ) { + jwt = getAuthenticatedJwt(this.authFactory, galasaAccessToken, apiServerUrl) ; + } + + publishTestCatalogToGalasaServer(restApiMetadata,testcatalogUrl,jwt, testCatalogArtifact); + } + + private void publishTestCatalogToGalasaServer(GalasaRestApiMetadata restApiMetadata, URL testCatalogUrl, String jwt, TestCatalogArtifact testCatalogArtifact) throws Ex { + + HttpURLConnection conn = null ; + try { + conn = (HttpURLConnection) testCatalogUrl.openConnection(); + } catch (IOException ioEx) { + this.errorRaiser.raiseError(ioEx,"Problem publishing the test catalog. Could not open URL connection to the Galasa server."); + } + + if (conn==null) { + this.errorRaiser.raiseError("Deploy to Test Catalog Store failed. Could not open a URL connection to the Galasa server."); + } else { + try { + postTestCatalogToGalasaServer(restApiMetadata, conn, testCatalogUrl, jwt, testCatalogArtifact); + } finally { + conn.disconnect(); + } + this.log.info("Test Catalog successfully deployed to " + testCatalogUrl.toString()); + } + } + + private void postTestCatalogToGalasaServer( + GalasaRestApiMetadata restApiMetadata, + HttpURLConnection conn, + URL testCatalogUrl, + String jwt, + TestCatalogArtifact testCatalogArtifact + ) throws Ex { + + int rc = 0 ; + String response = ""; + String message = ""; + + try { + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setRequestMethod("PUT"); + conn.addRequestProperty("Content-Type", "application/json"); + conn.addRequestProperty("Accept", "application/json"); + + // Only add the jwt header if we have a jwt value. + if (jwt == null) { + this.log.info("Not sending a JWT bearer token to the server, as the galasa.token property was not supplied."); + } else { + conn.addRequestProperty("Authorization", "Bearer "+jwt); + } + + conn.addRequestProperty("ClientApiVersion",restApiMetadata.getGalasaRestApiVersion()); // The version of the API we coded this against. + + testCatalogArtifact.transferTo(conn.getOutputStream()); + + rc = conn.getResponseCode(); + message = conn.getResponseMessage(); + + InputStream is = null; + if (rc != HttpURLConnection.HTTP_OK) { + is = conn.getInputStream(); + } else { + is = conn.getErrorStream(); + } + + if (is != null) { + response = IOUtils.toString(is, "utf-8"); + } + } catch(IOException ioEx) { + this.errorRaiser.raiseError(ioEx, "Problem publishing the test catalog. Problem dealing with response from Galasa server."); + } + + if (rc != HttpURLConnection.HTTP_OK) { + this.log.error("Deploy to Test Catalog Store failed:-"); + this.log.error(Integer.toString(rc) + " - " + message); + if (!response.isEmpty()) { + this.log.error(response); + errorRaiser.raiseError("Failed to deploy the test catalog. The server did not reply with OK (200)"); + } + } + } + + /** + * Swap the galasa token for a JWT which we can use to talk to the API server on the ecosystem. + * @return A JWT string (Java Web Token). + * @throws MojoExecutionException + */ + private String getAuthenticatedJwt(AuthenticationServiceFactory authFactory, String galasaAccessToken, String apiServerUrlString) throws Ex { + String jwt = null ; + try { + HttpClient httpClient = HttpClientBuilder.create().build(); + URL apiServerUrl = new URL(apiServerUrlString); + AuthenticationService authTokenService = authFactory.newAuthenticationService(apiServerUrl,galasaAccessToken,httpClient); + this.log.info("Turning the galasa access token into a JWT"); + jwt = authTokenService.getJWT(); + this.log.info("Java Web Token (JWT) obtained from the galasa ecosystem OK."); + } catch( Exception ex) { + this.errorRaiser.raiseError(ex,"Failure when exchanging the galasa access token with a JWT"); + } + return jwt; + } +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/URLCalculator.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/URLCalculator.java new file mode 100644 index 0000000..60a8b7a --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/URLCalculator.java @@ -0,0 +1,76 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.maven.plugin.url; + +import java.net.URL; +import java.util.Properties; + +import dev.galasa.maven.plugin.error.ErrorRaiser; + +public class URLCalculator { + + private ErrorRaiser errorRaiser ; + + public URLCalculator(ErrorRaiser errorRaiser) { + this.errorRaiser = errorRaiser ; + } + + public URL calculateTestCatalogUrl(String apiServerUrl , String testStream) throws T { + + // convert into a proper URL + URL testCatalogUrl = null ; + try { + testCatalogUrl = new URL(apiServerUrl + "/testcatalog/" + testStream); + } catch(Exception ex) { + errorRaiser.raiseError(ex,"Problem publishing the test catalog. Badly formed URL to the Galasa server."); + } + return testCatalogUrl; + } + + public String calculateApiServerUrl(Properties bootstrapProperties, URL bootstrapUrl) throws T { + + String apiServerUrl = null ; + + String sTestcatalogUrl = bootstrapProperties.getProperty("framework.testcatalog.url"); + if (sTestcatalogUrl == null || sTestcatalogUrl.trim().isEmpty()) { + + // There is no testcatalog URL specifically. So derive the API server URL from the bootstrap URL itself. + String sBootstrapUrl = cleanUrlString(bootstrapUrl.toString()); + + if (!sBootstrapUrl.endsWith("/bootstrap")) { + errorRaiser.raiseError("Unable to calculate the url to the API server, the bootstrap url does not end with /bootstrap, need a framework.testcatalog.url property in the bootstrap properties."); + } + + // strip off the '/bootstrap' ending. + apiServerUrl = sBootstrapUrl.substring(0, sBootstrapUrl.length() - 10); + } else { + // Derive the API server URL from the test catalog URL. + sTestcatalogUrl = cleanUrlString(sTestcatalogUrl); + + if (!sTestcatalogUrl.endsWith("/testcatalog")) { + errorRaiser.raiseError("Unable to calculate the url to the API server, the framework.testcatalog.url does not end in /testcatalog"); + } + + // Strip off the /testcatalog part of the URL. + apiServerUrl = sTestcatalogUrl.replace("/testcatalog", ""); + } + return apiServerUrl; + } + + private String cleanUrlString(String uncleanUrl) { + String url = uncleanUrl ; + + // Strip off any trailing or leading spaces. + url = url.trim(); + + // Strip off the trailing slash if there is one. + if (url.endsWith("/")) { + url = url.substring(0, url.length()-1); + } + return url; + } + +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/WrappedLog.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/WrappedLog.java new file mode 100644 index 0000000..14013a5 --- /dev/null +++ b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/WrappedLog.java @@ -0,0 +1,6 @@ +package dev.galasa.maven.plugin.log; + +public interface WrappedLog { + void info(String message); + void error(String message); +} diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationException.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationException.java similarity index 100% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationException.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationException.java diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationService.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationService.java similarity index 100% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationService.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationService.java diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationServiceFactory.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceFactory.java similarity index 100% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationServiceFactory.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceFactory.java diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationServiceFactoryImpl.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceFactoryImpl.java similarity index 100% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationServiceFactoryImpl.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceFactoryImpl.java diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationServiceImpl.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceImpl.java similarity index 100% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/AuthenticationServiceImpl.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceImpl.java diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/beans/AuthError.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/beans/AuthError.java similarity index 100% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/beans/AuthError.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/beans/AuthError.java diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/beans/AuthRequestPayload.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/beans/AuthRequestPayload.java similarity index 100% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/beans/AuthRequestPayload.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/beans/AuthRequestPayload.java diff --git a/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/beans/AuthResponsePayload.java b/galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/beans/AuthResponsePayload.java similarity index 100% rename from galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/auth/beans/AuthResponsePayload.java rename to galasa-maven-plugin/src/main/java/dev/galasa/maven/plugin/common/auth/beans/AuthResponsePayload.java diff --git a/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/DeployTestCatalogTest.java b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/DeployTestCatalogTest.java index bd69c40..5316fef 100644 --- a/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/DeployTestCatalogTest.java +++ b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/DeployTestCatalogTest.java @@ -6,10 +6,7 @@ package dev.galasa.maven.plugin; import java.net.URL; -import java.util.Properties; -import static org.assertj.core.api.Assertions.*; import org.junit.Test; -import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; public class DeployTestCatalogTest { @@ -224,35 +221,8 @@ public void TestSkipsNonObrProjects() throws Exception { - @Test - public void TestCalculateTestCatalogUrlUsesBootstrapPropertyIfSet() throws Exception { - DeployTestCatalog command = new DeployTestCatalog(); - Properties props = new Properties(); - props.setProperty("framework.testcatalog.url", "https://my.explicitly.set.bootstrap.url"); - URL calculatedUrl = command.calculateTestCatalogUrl(props, "myTestStream", new URL("https://my.bootstrap.url")); - assertThat(calculatedUrl.toString()).isEqualTo("https://my.explicitly.set.bootstrap.url/myTestStream"); - } - - @Test - public void TestCalculateTestCatalogUrlUsesBootstrapUrlByDefault() throws Exception { - DeployTestCatalog command = new DeployTestCatalog(); - Properties props = new Properties(); - // props.setProperty("framework.testcatalog.url", "https://my.explicitly.set.bootstrap.url"); - URL calculatedUrl = command.calculateTestCatalogUrl(props, "myTestStream", new URL("https://my.bootstrap.url/bootstrap")); - assertThat(calculatedUrl.toString()).isEqualTo("https://my.bootstrap.url/testcatalog/myTestStream"); - } - - @Test - public void TestCalculateTestCatalogUrlSpotsBootstrapUrlWhichDoesntEndInBootstrap() throws Exception { - DeployTestCatalog command = new DeployTestCatalog(); - Properties props = new Properties(); - // props.setProperty("framework.testcatalog.url", "https://my.explicitly.set.bootstrap.url"); - Exception ex = catchException( ()-> command.calculateTestCatalogUrl(props, "myTestStream", new URL("https://my.bootstrap.url/bootstrap-not-at-end")) ); - assertThat(ex).isInstanceOf(MojoExecutionException.class); - - assertThat(ex).hasMessageContaining("Unable to calculate the test catalog url, the bootstrap url does not end with /bootstrap, need a framework.testcatalog.url property in the bootstrap"); - } + // This is my exploration unit test. // diff --git a/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/ErrorRaiserTest.java b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/ErrorRaiserTest.java new file mode 100644 index 0000000..040fc33 --- /dev/null +++ b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/ErrorRaiserTest.java @@ -0,0 +1,48 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.maven.plugin.common; + +import static org.assertj.core.api.Assertions.*; + +import org.apache.maven.plugin.MojoExecutionException; +import org.junit.Test; + +import dev.galasa.maven.plugin.ErrorRaiserMavenImpl; +import dev.galasa.maven.plugin.MockLog; + +public class ErrorRaiserTest { + @Test + public void TestCanLogErrorWithOneParameterOk() throws Exception { + + MockLog mockLog = new MockLog(); + ErrorRaiser raiser = new ErrorRaiserMavenImpl(mockLog); + Exception ex = catchException(()-> raiser.raiseError("simple template {0}","param1")); + + assertThat(ex).isInstanceOf(MojoExecutionException.class).hasMessageContaining("simple template param1"); + mockLog.assertContainsRecord("ERROR:simple template param1"); + } + + @Test + public void TestCanLogErrorWithTwoParametersOk() throws Exception { + MockLog mockLog = new MockLog(); + ErrorRaiser raiser = new ErrorRaiserMavenImpl(mockLog); + Exception ex = catchException(()-> raiser.raiseError("simple template {0} {1}","param1","param2")); + + assertThat(ex).isInstanceOf(MojoExecutionException.class).hasMessageContaining("simple template param1 param2"); + mockLog.assertContainsRecord("ERROR:simple template param1 param2"); + } + + @Test + public void TestCanLogErrorWithZeroParametersOk() throws Exception { + MockLog mockLog = new MockLog(); + ErrorRaiser raiser = new ErrorRaiserMavenImpl(mockLog); + Exception ex = catchException(()-> raiser.raiseError("simple template")); + + assertThat(ex).isInstanceOf(MojoExecutionException.class).hasMessageContaining("simple template"); + mockLog.assertContainsRecord("ERROR:simple template"); + } + +} diff --git a/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/URLCalculatorTest.java b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/URLCalculatorTest.java new file mode 100644 index 0000000..e9531dd --- /dev/null +++ b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/URLCalculatorTest.java @@ -0,0 +1,66 @@ +/* + * Copyright contributors to the Galasa project + * + * SPDX-License-Identifier: EPL-2.0 + */ +package dev.galasa.maven.plugin.common; + +import java.net.URL; +import java.util.Properties; +import static org.assertj.core.api.Assertions.*; +import org.apache.maven.plugin.MojoExecutionException; +import org.junit.Test; + +import dev.galasa.maven.plugin.ErrorRaiserMavenImpl; +import dev.galasa.maven.plugin.MockLog; + +public class URLCalculatorTest { + @Test + public void TestCalculateTestCatalogUrlAddsSuffixesOk() throws Exception { + String streamName = "mystream"; + String apiServerUrl = "https://myserver/api"; + MockLog mockLog = new MockLog(); + ErrorRaiser raiser = new ErrorRaiserMavenImpl(mockLog); + URLCalculator calculator = new URLCalculator (raiser); + URL calculatedUrl = calculator.calculateTestCatalogUrl(apiServerUrl, streamName); + assertThat(calculatedUrl.toString()).isEqualTo("https://myserver/api/testcatalog/mystream"); + } + + + @Test + public void TestCalculateApiServerUrlUsesBootstrapPropertyIfSet() throws Exception { + Properties props = new Properties(); + props.setProperty("framework.testcatalog.url", "https://my.explicitly.set/testcatalog"); + URL bootstrapUrl = new URL("https://my/bootstrap"); + MockLog mockLog = new MockLog(); + ErrorRaiser raiser = new ErrorRaiserMavenImpl(mockLog); + URLCalculator calculator = new URLCalculator (raiser); + String calculatedUrl = calculator.calculateApiServerUrl(props, bootstrapUrl); + assertThat(calculatedUrl).isEqualTo("https://my.explicitly.set"); + } + + @Test + public void TestCalculateTestCatalogUrlUsesBootstrapUrlByDefault() throws Exception { + Properties props = new Properties(); + MockLog mockLog = new MockLog(); + ErrorRaiser raiser = new ErrorRaiserMavenImpl(mockLog); + URLCalculator calculator = new URLCalculator (raiser); + // props.setProperty("framework.testcatalog.url", "https://my.explicitly.set.bootstrap.url"); + String calculatedUrl = calculator.calculateApiServerUrl(props, new URL("https://my.bootstrap.url/bootstrap")); + assertThat(calculatedUrl.toString()).isEqualTo("https://my.bootstrap.url"); + } + + @Test + public void TestCalculateTestCatalogUrlSpotsBootstrapUrlWhichDoesntEndInBootstrap() throws Exception { + Properties props = new Properties(); + MockLog mockLog = new MockLog(); + ErrorRaiser raiser = new ErrorRaiserMavenImpl(mockLog); + URLCalculator calculator = new URLCalculator (raiser); + // props.setProperty("framework.testcatalog.url", "https://my.explicitly.set/bootstrap-not-at-end"); + Exception ex = catchException( ()-> calculator.calculateApiServerUrl(props, new URL("https://my.bootstrap.url/bootstrap-not-at-end")) ); + assertThat(ex).isInstanceOf(MojoExecutionException.class); + + assertThat(ex).hasMessageContaining("Unable to calculate the url to the API server, the bootstrap url does not end with /bootstrap, need a framework.testcatalog.url property in the bootstrap"); + } + +} diff --git a/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/auth/AuthenticationServiceFactoryImplTest.java b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceFactoryImplTest.java similarity index 92% rename from galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/auth/AuthenticationServiceFactoryImplTest.java rename to galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceFactoryImplTest.java index 872906c..b23e2bc 100644 --- a/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/auth/AuthenticationServiceFactoryImplTest.java +++ b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceFactoryImplTest.java @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package dev.galasa.maven.plugin.auth; +package dev.galasa.maven.plugin.common.auth; import java.net.URL; diff --git a/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/auth/AuthenticationServiceImplTest.java b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceImplTest.java similarity index 98% rename from galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/auth/AuthenticationServiceImplTest.java rename to galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceImplTest.java index 9a1b6a7..2864302 100644 --- a/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/auth/AuthenticationServiceImplTest.java +++ b/galasa-maven-plugin/src/test/java/dev/galasa/maven/plugin/common/auth/AuthenticationServiceImplTest.java @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: EPL-2.0 */ -package dev.galasa.maven.plugin.auth; +package dev.galasa.maven.plugin.common.auth; import static org.assertj.core.api.Assertions.*; @@ -20,7 +20,8 @@ import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; import dev.galasa.maven.plugin.MockHttpClient; -import dev.galasa.maven.plugin.auth.beans.*; +import dev.galasa.maven.plugin.common.GsonFactory; +import dev.galasa.maven.plugin.common.auth.beans.*; public class AuthenticationServiceImplTest {