Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
refactor so test catalog is mostly done with no maven specifics.
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Cobbett <77053+techcobweb@users.noreply.github.com>
  • Loading branch information
techcobweb committed Mar 8, 2024
1 parent b9f8e62 commit e7b6b0f
Show file tree
Hide file tree
Showing 26 changed files with 309 additions and 177 deletions.

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,11 +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.error.ErrorRaiser;
import dev.galasa.maven.plugin.url.URLCalculator;

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 @@ -60,15 +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();

protected ErrorRaiser<MojoExecutionException> errorRaiser = new ErrorRaiserMavenImpl(getLog());

protected URLCalculator<MojoExecutionException> urlCalculator = new URLCalculator<MojoExecutionException>(errorRaiser);

public void execute() throws MojoExecutionException, MojoFailureException {

boolean skip = (skipBundleTestCatalog || skipBundleTestCatalogOldSpelling);
Expand Down Expand Up @@ -102,23 +91,19 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

Properties bootstrapProperties = bootstrapLoader.getBootstrapProperties(bootstrapUrl,getLog());



String apiServerUrl = urlCalculator.calculateApiServerUrl(bootstrapProperties, bootstrapUrl);

URL testcatalogUrl = urlCalculator.calculateTestCatalogUrl(apiServerUrl, this.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 ( (this.galasaAccessToken!=null) && (!this.galasaAccessToken.isEmpty()) ) {
jwt = getAuthenticatedJwt(this.authFactory, this.galasaAccessToken, apiServerUrl) ;
}

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 @@ -131,95 +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) {
errorRaiser.raiseError(ioEx,"Problem publishing the test catalog. Could not open URL connection to the Galasa server.");
}

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) {
errorRaiser.raiseError(ioEx, "Problem publishing the test catalog. Problem dealing with response from Galasa server.");
}

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);
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 MojoExecutionException {
String jwt = null ;
try {
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) {
errorRaiser.raiseError(ex,"Failure when exchanging the galasa access token with a JWT");
}
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.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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package dev.galasa.maven.plugin.error;

// Logs errors, then raises an exception.
public interface ErrorRaiser <T extends Exception> {
void raiseError(String template, Object... parameters) throws T ;
void raiseError(Throwable cause, String template, Object... parameters) throws T ;
public interface ErrorRaiser <Ex extends Exception> {
void raiseError(String template, Object... parameters) throws Ex ;
void raiseError( Throwable cause, String template, Object... parameters) throws Ex;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.galasa.maven.plugin.common;

public class GalasaRestApiMetadata {
public String getGalasaRestApiVersion() {
return "0.32.0";
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Ex extends Exception> {
void transferTo(OutputStream outputStream) throws Ex;
}
Loading

0 comments on commit e7b6b0f

Please sign in to comment.