Skip to content

Commit

Permalink
Merge pull request #12 from project-husky/husky-service-module
Browse files Browse the repository at this point in the history
add HuskyService integration tests
  • Loading branch information
ralych committed Feb 8, 2024
2 parents 165d10f + 8a3681f commit 37d10cb
Show file tree
Hide file tree
Showing 23 changed files with 3,476 additions and 7 deletions.
29 changes: 24 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http:https://maven.apache.org/POM/4.0.0"
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.husky.test.integration</groupId>
<artifactId>husky-integration-tests</artifactId>
Expand All @@ -12,9 +14,11 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<husky.version>2.2.1-SNAPSHOT</husky.version>
<spring.boot.version>2.7.13</spring.boot.version>
<spring.version>5.3.31</spring.version>
<spring.boot.version>2.7.18</spring.boot.version>
<camel.version>3.21.0</camel.version>
<junit.version>5.10.1</junit.version>
<ipf.version>4.7.0</ipf.version>
<maven-surefire-plugin.version>3.2.2</maven-surefire-plugin.version>
</properties>
<repositories>
Expand All @@ -39,9 +43,12 @@

</repositories>
<scm>
<connection>scm:git:https://github.com/project-husky/husky-integration-tests.git</connection>
<developerConnection>scm:git:[email protected]:project-husky/husky-integration-tests.git</developerConnection>
<url>https://github.com/project-husky/husky-integration-tests/tree/master</url>
<connection>
scm:git:https://github.com/project-husky/husky-integration-tests.git</connection>
<developerConnection>
scm:git:[email protected]:project-husky/husky-integration-tests.git</developerConnection>
<url>
https://github.com/project-husky/husky-integration-tests/tree/master</url>
<tag>HEAD</tag>
</scm>
<!--
Expand Down Expand Up @@ -81,6 +88,13 @@
<artifactId>husky-xua-gen-impl</artifactId>
<version>${husky.version}</version>
</dependency>

<dependency>
<groupId>org.projecthusky.communication</groupId>
<artifactId>husky-service</artifactId>
<version>${husky.version}</version>
</dependency>

<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
Expand Down Expand Up @@ -179,6 +193,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projecthusky.communication</groupId>
<artifactId>husky-service</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down Expand Up @@ -211,6 +229,7 @@
<artifactId>camel-cxf</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/projecthusky/communication/TestApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.projecthusky.communication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationListener;

@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer {

public static void main(final String[] args) {
final SpringApplication application = new SpringApplication(TestApplication.class);
addApplicationStartupHook(application);
var context = application.run(args);
context.close();
}

/**
* Adds a hook to the Application Ready event to run some magic.
*
* @param application The IPF {@link SpringApplication} instance.
*/
public static void addApplicationStartupHook(final SpringApplication application) {
application.addListeners((ApplicationListener<ApplicationReadyEvent>) event -> {
// Do some configuration magic...

});
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.projecthusky.communication.config;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.projecthusky.xua.exceptions.SerializeException;
import org.projecthusky.xua.serialization.OpenSaml2Serializer;
import org.projecthusky.xua.serialization.impl.OpenSaml2SerializerImpl;
import org.junit.jupiter.api.BeforeAll;
import org.opensaml.core.config.InitializationService;
import org.opensaml.core.xml.XMLObject;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public abstract class InitializerTestHelper {
@BeforeAll
public static void setUpBeforeClass() throws Exception {
// Initialize the library
InitializationService.initialize();
}

public Element loadXmlDokument(String aPath)
throws SAXException, IOException, ParserConfigurationException {
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
final Document doc = dbf.newDocumentBuilder()
.parse(this.getClass().getResourceAsStream(aPath));
return doc.getDocumentElement();
}

public void logXmlObject(Element element) {
try {
final Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "no");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty("{http:https://xml.apache.org/xslt}indent-amount", String.valueOf(4));

final ByteArrayOutputStream bas = new ByteArrayOutputStream();
tr.transform(new DOMSource(element), new StreamResult(bas));
LoggerFactory.getLogger(getClass()).info("\n" + bas.toString());
} catch (final TransformerException e) {
LoggerFactory.getLogger(getClass()).error("Error", e);
}
}

public void logXmlObject(XMLObject aXmlObject) {
try {
final OpenSaml2Serializer openSamlSerializer = new OpenSaml2SerializerImpl();
final String xmlString = openSamlSerializer.serializeToString(aXmlObject);
LoggerFactory.getLogger(getClass()).info("\n" + xmlString);
} catch (final SerializeException e) {
LoggerFactory.getLogger(getClass()).error("Error", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.projecthusky.communication.config;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.config.SocketConfig;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.bootstrap.HttpServer;
import org.apache.http.impl.bootstrap.ServerBootstrap;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* <!-- @formatter:off -->
* <div class="en">Helping class for client testing with simulated server</div>
* <div class="de"></div>
* <div class="fr"></div>
* <div class="it"></div>
* <!-- @formatter:on -->
*/
public abstract class ServerTestHelper extends InitializerTestHelper {

private static Logger logger = LoggerFactory.getLogger(ServerTestHelper.class);

private static HttpServer server;

private static int httpPort;

private static ServerBootstrap bootstrap;

protected static int getHttpPort() {
return httpPort;
}

protected static HttpServer getServer() {
return server;
}

public static HttpRequestHandler registerHandler() {
return null;
}

@BeforeAll
public static void setUpBefore() throws IOException {
final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(15000)
.setTcpNoDelay(true).build();

bootstrap = ServerBootstrap.bootstrap();
server = bootstrap.setServerInfo("Test/1.1").setSocketConfig(socketConfig)
.registerHandler("*", new HttpRequestHandler() {

@Override
public void handle(HttpRequest request, HttpResponse response,
HttpContext context) throws HttpException, IOException {
logger.debug("The request %s", request.getRequestLine());
response.setStatusCode(500);
response.setEntity(new StringEntity("Hello this is a testserver"));
}

}).create();

server.start();
httpPort = server.getLocalPort();

}

@AfterAll
public static void tearDownAfter() {
server.stop();
}

}
Loading

0 comments on commit 37d10cb

Please sign in to comment.