-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added wiremock tests for network related tests.
Not every configuration option could be tested because the message dispatcher is initialized only once per JVM.
- Loading branch information
Showing
3 changed files
with
125 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...src/test/java/eu/knowledge/engine/smartconnector/misc/WireMockFirstConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |