Hi,
Thank you for making Wiremock. Sorry, but I am having issues using it with Testng inside a Docker container.
It could very well be that I am new to Java as well.
But here are my stack trace and source code:
Running my.adapter.HttpSinkTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@2db7a79b
start
setting up mock server...
log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
finished setting up mock server...
stubbing...
stubbed...
sending...
process
defaultValue
[Event headers = {}, body.length = 7 ]
Test 11
response code:500
sent...
stop
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 14.968 sec <<< FAILURE!
testHttpSink(my.adapter.HttpSinkTest) Time elapsed: 4.469 sec <<< FAILURE!
com.github.tomakehurst.wiremock.client.VerificationException: Expected status 200 for http://localhost:8080/__admin/near-misses/request-pattern but was 500
at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:269)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.postJsonAssertOkAndReturnBody(HttpAdminClient.java:249)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.findTopNearMissesFor(HttpAdminClient.java:217)
at com.github.tomakehurst.wiremock.client.WireMock.findAllNearMissesFor(WireMock.java:523)
at com.github.tomakehurst.wiremock.client.WireMock.findNearMissesFor(WireMock.java:519)
at com.github.tomakehurst.wiremock.client.WireMock.verificationExceptionForNearMisses(WireMock.java:387)
at com.github.tomakehurst.wiremock.client.WireMock.verifyThat(WireMock.java:367)
at com.github.tomakehurst.wiremock.WireMockServer.verify(WireMockServer.java:279)
at rsae.adapter.HttpSinkTest.testHttpSink(HttpSinkTest.java:82)
Results :
Failed tests: testHttpSink(my.adapter.HttpSinkTest): Expected status 200 for http://localhost:8080/__admin/near-misses/request-pattern but was 500
package my.adapter;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import org.testng.Assert;
import com.github.tomakehurst.wiremock.WireMockServer;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.apache.flume.Channel;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.channel.PseudoTxnMemoryChannel;
import org.apache.flume.conf.Configurables;
import org.apache.flume.event.EventBuilder;
import org.apache.flume.lifecycle.LifecycleException;
import com.github.tomakehurst.wiremock.verification.NearMiss;
import java.util.List;
import java.io.*;
import java.net.*;
import java.util.concurrent.TimeUnit;
public class HttpSinkTest {
private Channel channel;
private HttpSink sink;
private WireMockServer wireMockServer;
private WireMock wireMock;
@BeforeTest
public void setUp() {
Context context = new Context();
context.put("endpoint", "http://localhost:8080/endpoint");
sink = new HttpSink();
channel = new PseudoTxnMemoryChannel();
sink.setChannel(channel);
sink.start();
Configurables.configure(channel, context);
Configurables.configure(sink, context);
System.out.println("setting up mock server...");
//wireMockServer = new WireMockServer(wireMockConfig().port(8080));
wireMockServer = new WireMockServer();
wireMockServer.start();
System.out.println("finished setting up mock server...");
//WireMock.configureFor("localhost", 8081);
//wireMock = new WireMock("localhost", 8081);
}
@AfterTest
public void tearDown() {
sink.stop();
wireMockServer.stop();
}
@Test
public void testHttpSink() throws
InterruptedException,
LifecycleException,
EventDeliveryException {
System.out.println("stubbing...");
wireMockServer.stubFor(post(urlEqualTo("/endpoint"))
//.withRequestBody(equalToJson(event("SUCCESS")))
.willReturn(aResponse().withStatus(200)));
System.out.println("stubbed...");
System.out.println("sending...");
Event event = EventBuilder.withBody(("Test 11").getBytes());
channel.put(event);
sink.process();
System.out.println("sent...");
wireMockServer.verify(
1,
postRequestedFor(
urlEqualTo("/endpoint")
)
);
System.out.println("verified...");
}
}
package my.adapter;
import org.apache.flume.sink.AbstractSink;
import org.apache.flume.conf.Configurable;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.Context;
import org.apache.flume.Channel;
import org.apache.flume.Event;
import org.apache.flume.Transaction;
import java.net.HttpURLConnection;
import java.io.IOException;
import java.net.URL;
import java.io.OutputStream;
import java.net.MalformedURLException;
public class HttpSink extends AbstractSink implements Configurable {
private String myProp;
private HttpURLConnection httpClient;
private static final String DEFAULT_CONTENT_TYPE = "text/plain";
private static final String DEFAULT_ACCEPT_HEADER = "text/plain";
private static final int DEFAULT_CONNECT_TIMEOUT = 5000;
private static final int DEFAULT_REQUEST_TIMEOUT = 5000;
private URL endpointUrl;
private String contentTypeHeader = DEFAULT_CONTENT_TYPE;
private String acceptHeader = DEFAULT_ACCEPT_HEADER;
private int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
private int requestTimeout = DEFAULT_REQUEST_TIMEOUT;
@Override
public void configure(Context context) {
String myProp = context.getString("myProp", "defaultValue");
try {
endpointUrl = new URL(context.getString("endpoint", ""));
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Endpoint URL invalid", e);
}
// Process the myProp value (e.g. validation)
// Store myProp for later retrieval by process() method
this.myProp = myProp;
}
@Override
public void start() {
System.out.println("start");
// Initialize the connection to the external repository (e.g. HDFS) that
// this Sink will forward Events to ..
}
@Override
public void stop () {
System.out.println("stop");
// Disconnect from the external respository and do any
// additional cleanup (e.g. releasing resources or nulling-out
// field values) ..
}
@Override
public Status process() throws EventDeliveryException {
Status status = null;
OutputStream outputStream = null;
// Start transaction
Channel ch = getChannel();
Transaction txn = ch.getTransaction();
txn.begin();
System.out.println("process");
System.out.println(myProp);
try {
// This try clause includes whatever Channel operations you want to do
Event event = ch.take();
System.out.println(event);
System.out.println(new String(event.getBody()));
httpClient = getConnection();
outputStream = httpClient.getOutputStream();
outputStream.write(event.getBody());
outputStream.flush();
outputStream.close();
int httpStatusCode = httpClient.getResponseCode();
System.out.println("response code:" + httpStatusCode);
httpClient.getInputStream().close();
// Send the Event to the external repository.
// storeSomeData(e);
txn.commit();
status = Status.READY;
} catch (Throwable t) {
txn.rollback();
// Log exception, handle individual exceptions as needed
status = Status.BACKOFF;
// re-throw all Errors
if (t instanceof Error) {
throw (Error)t;
}
} finally {
txn.close();
}
return status;
}
HttpURLConnection getConnection() throws IOException {
HttpURLConnection connection = (HttpURLConnection) endpointUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", contentTypeHeader);
connection.setRequestProperty("Accept", acceptHeader);
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(requestTimeout);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.connect();
return connection;
}
}
If you can point me to anything to try, that would be great cause I'm just stuck and don't know what else to do.
Thank you for your time!
I've tried testing to see if the wireMockServer is up:
try {
TimeUnit.SECONDS.sleep(10);
System.out.println("###1");
StringBuilder result = new StringBuilder();
System.out.println("###2");
URL url = new URL("http://localhost:8080/__admin");
System.out.println("###3");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println("###4");
conn.setRequestMethod("GET");
System.out.println("###5");
System.out.println(conn.getResponseCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println("###6");
String line;
System.out.println("###7");
while ((line = rd.readLine()) != null) {
System.out.println("###8");
result.append(line);
}
System.out.println("###9");
rd.close();
System.out.println("###10");
System.out.println(result.toString());
} catch(MalformedURLException ex) {
} catch(IOException ex) {
}
But it seems like the server just never comes up. But I would assume that the request will time out instead of returning 500.
Sorry, not sure if that helps or not, but that's the only clue I've got.
WireMock wraps all logging in its own Notifier
interface. It ships with no-op, Slf4j and console (stdout) implementations.
// Provide an alternative notifier. The default logs to slf4j.
.notifier(new ConsoleNotifier(true))
<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>
<groupId>my.adapter</groupId>
<artifactId>my-adapter</artifactId>
<version>0.0.1</version>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>http://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>jcenter</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.flume</groupId>
<artifactId>flume-ng-core</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.flume</groupId>
<artifactId>flume-ng-embedded-agent</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
RequestHandlerClass from context returned com.github.tomakehurst.wiremock.http.StubRequestHandler. Normalized mapped under returned 'null'
java.lang.NoClassDefFoundError: com/google/common/base/MoreObjects
I'd definitely suggest using the standalone version if you have this kind of problem.