Skip to content

Commit

Permalink
Use mock server for selenium tests, fix #1116
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Nioche <[email protected]>
  • Loading branch information
jnioche committed Nov 8, 2023
1 parent ef31e50 commit 76379ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
16 changes: 15 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -252,6 +254,18 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mockserver</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>5.15.0</version>
</dependency>

</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
package com.digitalpebble.stormcrawler.protocol.selenium;

import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import com.digitalpebble.stormcrawler.Metadata;
import com.digitalpebble.stormcrawler.protocol.ProtocolResponse;
import java.time.Instant;
Expand All @@ -25,6 +28,7 @@
import org.apache.storm.Config;
import org.apache.storm.utils.MutableObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
Expand All @@ -33,6 +37,7 @@
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.BrowserWebDriverContainer.VncRecordingMode;
import org.testcontainers.containers.MockServerContainer;
import org.testcontainers.utility.DockerImageName;

/**
Expand All @@ -45,15 +50,28 @@ public class ProtocolTest {

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

private static final DockerImageName IMAGE =
DockerImageName.parse("selenium/standalone-chrome:118.0");
private static final DockerImageName SELENIUM_IMAGE =
DockerImageName.parse("selenium/standalone-chrome");

public static final DockerImageName MOCKSERVER_IMAGE =
DockerImageName.parse("mockserver/mockserver");

@Rule public MockServerContainer mockServer = new MockServerContainer(MOCKSERVER_IMAGE);

@Rule
public BrowserWebDriverContainer<?> chrome =
new BrowserWebDriverContainer<>(IMAGE)
new BrowserWebDriverContainer<>(SELENIUM_IMAGE)
.withCapabilities(new ChromeOptions())
.withRecordingMode(VncRecordingMode.SKIP, null);

@Before
public void init() {
org.mockserver.client.MockServerClient mockServerClient =
new org.mockserver.client.MockServerClient(
mockServer.getHost(), mockServer.getServerPort());
mockServerClient.when(request()).respond(response().withBody("Success!"));
}

public RemoteDriverProtocol getProtocol() {

LOG.info(
Expand Down Expand Up @@ -101,22 +119,25 @@ public RemoteDriverProtocol getProtocol() {
*
* @throws InterruptedException
*/

// TODO find a way of not hitting a real URL
public void testBlocking() throws InterruptedException {

RemoteDriverProtocol protocol = getProtocol();

MutableBoolean noException = new MutableBoolean(true);

MutableObject endTimeFirst = new MutableObject();
MutableObject startTimeSecond = new MutableObject();

String url = mockServer.getEndpoint();

// String url = "http://" + mockServer.getHost() + ":" +
// mockServer.getServerPort() + "/";

new Thread(
() -> {
try {
ProtocolResponse response =
protocol.getProtocolOutput(
"https://stormcrawler.net/", new Metadata());
protocol.getProtocolOutput(url, new Metadata());
endTimeFirst.setObject(
Instant.parse(
response.getMetadata()
Expand All @@ -132,8 +153,7 @@ public void testBlocking() throws InterruptedException {
() -> {
try {
ProtocolResponse response =
protocol.getProtocolOutput(
"https://stormcrawler.net/", new Metadata());
protocol.getProtocolOutput(url, new Metadata());
startTimeSecond.setObject(
Instant.parse(
response.getMetadata()
Expand Down

0 comments on commit 76379ef

Please sign in to comment.