Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<MojoExecutionException> errorRaiser = new ErrorRaiserMavenImpl(getLog());
URLCalculator<MojoExecutionException> urlCalculator = new URLCalculator<MojoExecutionException>(errorRaiser);
GalasaRestApiMetadata restApiMetadata = new GalasaRestApiMetadata();
BootstrapLoader<MojoExecutionException> bootstrapLoader = new BootstrapLoaderImpl<MojoExecutionException>(wrappedLog,errorRaiser);
TestCatalogArtifact<MojoExecutionException> wrappedTestCatalogArtifact = new TestCatalogArtifactMavenImpl(testCatalogArtifact, errorRaiser);
TestCatalogArtifactDeployer<MojoExecutionException> deployer = new TestCatalogArtifactDeployer<MojoExecutionException>(
wrappedLog, errorRaiser, bootstrapLoader, urlCalculator, restApiMetadata, authFactory);

// Deploy the test catalog to the Galasa server.
deployer.deployToServer(bootstrapUrl, testStream, galasaAccessToken, wrappedTestCatalogArtifact);
}

private Artifact getTestCatalogArtifact() {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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 <MojoExecutionException>{

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);
}
}
Original file line number Diff line number Diff line change
@@ -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<MojoExecutionException> {

private Artifact testCatalogArtifact;
private ErrorRaiser<MojoExecutionException> errorRaiser;

public TestCatalogArtifactMavenImpl(Artifact testCatalogArtifact, ErrorRaiser<MojoExecutionException> 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());
}
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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<Ex extends Exception> {
public Properties getBootstrapProperties(URL bootstrapUrl) throws Ex ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@
*
* 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<Ex extends Exception> implements BootstrapLoader<Ex> {
WrappedLog log ;
ErrorRaiser<Ex> errorRaiser ;

public BootstrapLoaderImpl( WrappedLog log , ErrorRaiser<Ex> 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();
String msg = MessageFormat.format("execute(): URLConnection: connected to:{0}",connection.getURL().toString());
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;
}
Expand Down
Loading