Skip to content

Commit

Permalink
Improve test mocks by moving MessageMock into a separate public class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Duda committed Oct 20, 2018
1 parent 1fb671a commit 12df806
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 98 deletions.
114 changes: 114 additions & 0 deletions core/main/java/com/codingchili/core/testing/MessageMock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.codingchili.core.testing;

import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonObject;

import com.codingchili.core.protocol.ResponseStatus;

import static com.codingchili.core.configuration.CoreStrings.*;
import static com.codingchili.core.protocol.ResponseStatus.ACCEPTED;

/**
* @author Robin Duda
* <p>
* Mock class for messages.
*/
public class MessageMock implements Message<Object> {
private JsonObject json;
private ResponseListener listener;

/**
* Creates a new message mock.
*
* @param route the route to which the message is destined.
* @param listener a listener that will be invoked when the message is written to.
* @param json the payload of the message.
*/
public MessageMock(String route, ResponseListener listener, JsonObject json) {
if (json == null) {
this.json = new JsonObject();
} else {
this.json = json;
}

this.json.put(PROTOCOL_ROUTE, route);
this.listener = listener;
}

@Override
public Object body() {
return json;
}

@Override
public void reply(Object message) {
JsonObject data = new JsonObject();

if (message instanceof JsonObject) {
data = (JsonObject) message;
} else if (message instanceof Buffer) {
// normally buffers passed directly, for testing purposes
// its wrapped in a json object.
try {
data = ((Buffer) message).toJsonObject();

if (!data.containsKey(PROTOCOL_STATUS)) {
data.put(PROTOCOL_STATUS, ACCEPTED);
}
} catch (DecodeException e) {
data.put(ID_BUFFER, message.toString());
data.put(PROTOCOL_STATUS, ACCEPTED);
}
}
listener.handle(data, responseStatusFromJson(data));
}

private ResponseStatus responseStatusFromJson(JsonObject json) {
return ResponseStatus.valueOf(json.getString(PROTOCOL_STATUS));
}

@Override
public String address() {
throw new UnsupportedOperationException();
}

@Override
public MultiMap headers() {
throw new UnsupportedOperationException();
}

@Override
public String replyAddress() {
throw new UnsupportedOperationException();
}

@Override
public boolean isSend() {
return false; // publish to all listeners for tests.
}

@Override
public void reply(Object message, DeliveryOptions options) {

}

@Override
public void fail(int failureCode, String message) {

}

@Override
public void reply(Object message, DeliveryOptions options, Handler replyHandler) {

}

@Override
public void reply(Object message, Handler replyHandler) {

}
}
99 changes: 1 addition & 98 deletions core/main/java/com/codingchili/core/testing/RequestMock.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package com.codingchili.core.testing;

import com.codingchili.core.listener.transport.ClusterRequest;
import com.codingchili.core.protocol.ResponseStatus;
import io.vertx.core.Handler;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonObject;

import static com.codingchili.core.configuration.CoreStrings.*;
import static com.codingchili.core.protocol.ResponseStatus.ACCEPTED;
import com.codingchili.core.listener.transport.ClusterRequest;

/**
* @author Robin Duda
Expand All @@ -37,92 +28,4 @@ private static class ClusterRequestMock extends ClusterRequest {
super(message);
}
}

private static class MessageMock implements Message {
private JsonObject json;
private ResponseListener listener;

MessageMock(String route, ResponseListener listener, JsonObject json) {
if (json == null) {
this.json = new JsonObject();
} else {
this.json = json;
}

this.json.put(PROTOCOL_ROUTE, route);
this.listener = listener;
}

@Override
public Object body() {
return json;
}

@Override
public void reply(Object message) {
JsonObject data = new JsonObject();

if (message instanceof JsonObject) {
data = (JsonObject) message;
} else if (message instanceof Buffer) {
// normally buffers passed directly, for testing purposes
// its wrapped in a json object.
try {
data = ((Buffer) message).toJsonObject();

if (!data.containsKey(PROTOCOL_STATUS)) {
data.put(PROTOCOL_STATUS, ACCEPTED);
}
} catch (DecodeException e) {
data.put(ID_BUFFER, message.toString());
data.put(PROTOCOL_STATUS, ACCEPTED);
}
}
listener.handle(data, responseStatusFromJson(data));
}

private ResponseStatus responseStatusFromJson(JsonObject json) {
return ResponseStatus.valueOf(json.getString(PROTOCOL_STATUS));
}

@Override
public String address() {
throw new UnsupportedOperationException();
}

@Override
public MultiMap headers() {
throw new UnsupportedOperationException();
}

@Override
public String replyAddress() {
throw new UnsupportedOperationException();
}

@Override
public boolean isSend() {
return false; // publish to all listeners for tests.
}

@Override
public void reply(Object message, DeliveryOptions options) {

}

@Override
public void fail(int failureCode, String message) {

}

@Override
public void reply(Object message, DeliveryOptions options, Handler replyHandler) {

}

@Override
public void reply(Object message, Handler replyHandler) {

}
}
}

0 comments on commit 12df806

Please sign in to comment.