Skip to content

Commit

Permalink
Added wiremock tests for network related tests.
Browse files Browse the repository at this point in the history
Not every configuration option could be tested because the message
dispatcher is initialized only once per JVM.
  • Loading branch information
bnouwt committed Dec 10, 2024
1 parent 2765de5 commit a4dba5e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 20 deletions.
8 changes: 7 additions & 1 deletion smart-connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<version>5.14.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.10.0</version>
<scope>test</scope>
</dependency>


<!-- jena -->
Expand Down Expand Up @@ -216,7 +222,7 @@

<properties>
<swagger-core-version>2.2.22</swagger-core-version>
<jetty-version>11.0.15</jetty-version>
<jetty-version>11.0.24</jetty-version>
<jersey3-version>3.1.9</jersey3-version>
<jackson-version>2.18.1</jackson-version>
<servlet-api-version>6.1.0</servlet-api-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void testConfigValidateTrue() {
}

LOG.info("Result: {}", askResult);

System.clearProperty(SmartConnectorConfig.CONF_KEY_VALIDATE_OUTGOING_BINDINGS_WRT_INCOMING_BINDINGS);
}

@Test
Expand Down Expand Up @@ -100,7 +102,7 @@ public void testConfigValidateFalse() {
}

LOG.info("Result: {}", askResult);
System.setProperty(SmartConnectorConfig.CONF_KEY_VALIDATE_OUTGOING_BINDINGS_WRT_INCOMING_BINDINGS, "true");
System.clearProperty(SmartConnectorConfig.CONF_KEY_VALIDATE_OUTGOING_BINDINGS_WRT_INCOMING_BINDINGS);
}

@Test
Expand Down Expand Up @@ -132,7 +134,7 @@ public void testConfigWaitForKnowledgeBaseNegative() {

LOG.info("Result: {}", askResult);
waitTimeout = 0;
System.setProperty(SmartConnectorConfig.CONF_KEY_KE_KB_WAIT_TIMEOUT, "10");
System.clearProperty(SmartConnectorConfig.CONF_KEY_KE_KB_WAIT_TIMEOUT);
}

@Test
Expand Down Expand Up @@ -161,26 +163,11 @@ public void testConfigWaitForKnowledgeBasePositive() {
}

LOG.info("Result: {}", askResult);
System.setProperty(SmartConnectorConfig.CONF_KEY_KE_KB_WAIT_TIMEOUT, "10");
}

public void testConfigHostname() {
System.setProperty(SmartConnectorConfig.CONF_KEY_KE_RUNTIME_HOSTNAME, "testerhost");

System.setProperty(SmartConnectorConfig.CONF_KEY_KE_RUNTIME_HOSTNAME, null);
}

public void testConfigKDUrl() {
System.setProperty(SmartConnectorConfig.CONF_KEY_KD_URL, "http://testerhost:1234");




System.setProperty(SmartConnectorConfig.CONF_KEY_KD_URL, null);
System.clearProperty(SmartConnectorConfig.CONF_KEY_KE_KB_WAIT_TIMEOUT);
}

@AfterEach
public void afterTTest() {
public void afterTest() {
try {
kn.stop().get();
} catch (InterruptedException | ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package eu.knowledge.engine.smartconnector.misc;

import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.status;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.mock;

import java.io.IOException;
import java.net.URI;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;

import eu.knowledge.engine.smartconnector.impl.SmartConnectorConfig;
import eu.knowledge.engine.smartconnector.runtime.KeRuntime;
import eu.knowledge.engine.smartconnector.runtime.messaging.MessageDispatcher;
import eu.knowledge.engine.smartconnector.runtime.messaging.RemoteKerConnection;
import eu.knowledge.engine.smartconnector.runtime.messaging.kd.model.KnowledgeEngineRuntimeConnectionDetails;

@WireMockTest
public class WireMockFirstConfigurationTest {

private static Logger LOG = LoggerFactory.getLogger(WireMockFirstConfigurationTest.class);

@BeforeAll
public static void before(WireMockRuntimeInfo wmRuntimeInfo) {

// set all system property for various tests below.
String host = wmRuntimeInfo.getHttpBaseUrl();
System.setProperty(SmartConnectorConfig.CONF_KEY_KD_URL, host);
System.setProperty(SmartConnectorConfig.CONF_KEY_KE_RUNTIME_PORT, "1234");
String value = "http://test${ke.runtime.hostname}:${ke.runtime.port}";
System.setProperty(SmartConnectorConfig.CONF_KEY_KE_RUNTIME_EXPOSED_URL, value);

LOG.info("Testing with exposed url: {}", value);
}

/*
* Because this unit test relies on the message dispatcher being instantiated
* during the @{code getMessageDispatcher()} method it does not work during
* Maven build, because the JVM (and so the MessageDispatcher) was already
* started before for other tests.
*
* For now, I do not see a way to tests these configuration properties without
* modifying the knowledge engine's KeRuntime characteristics.
*/
@Disabled
@Test
public void testConfigKDUrlAndHostnameAndPort() throws Exception {
stubFor(post("/ker/").willReturn(status(201).withBody("{}")));
stubFor(get("/ker/").willReturn(status(200).withBody("[]")));

// note that the message dispatcher is automatically started
MessageDispatcher md = KeRuntime.getMessageDispatcher();

Thread.sleep(5000);

verify(getRequestedFor(urlEqualTo("/ker/"))
.withRequestBody(matchingJsonPath("$.exposedUrl", matching("http://testlocalhost:1234"))));
}

/**
* Does a (very limited) test of the http timeout configuration option. We are
* setting it to 1 second and if this works, the test should succeed within 2
* seconds. If setting the configuration property fails, the test would take 5
* seconds (= default value for http timeout configuration property).
*
* @throws Exception
*
* @throws IOException
* @throws InterruptedException
*/
@Test
@Timeout(value = 3, unit = TimeUnit.SECONDS)
public void testConfigHttpConnectTimeout() throws Exception {
stubFor(post("/ker/").willReturn(status(201).withBody("{}")));
System.setProperty(SmartConnectorConfig.CONF_KEY_KE_HTTP_TIMEOUT, "1");

MessageDispatcher messageDispatcher = mock(MessageDispatcher.class);

var ker = new RemoteKerConnection(messageDispatcher,
new KnowledgeEngineRuntimeConnectionDetails().exposedUrl(URI.create("http://10.255.255.1/")));

ker.start();
assertFalse(ker.isAvailable());
System.clearProperty(SmartConnectorConfig.CONF_KEY_KE_HTTP_TIMEOUT);
}

@AfterAll
public static void after() {
System.clearProperty(SmartConnectorConfig.CONF_KEY_KD_URL);
System.clearProperty(SmartConnectorConfig.CONF_KEY_KE_RUNTIME_PORT);
System.clearProperty(SmartConnectorConfig.CONF_KEY_KE_RUNTIME_EXPOSED_URL);
}

}

0 comments on commit a4dba5e

Please sign in to comment.