Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package finos.traderx.messaging;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import java.util.Date;

/**
* Tests for the Envelope interface contract.
* We create a minimal implementation to verify:
* 1. Message metadata handling (type, topic, from, date)
* 2. Payload handling with generics
*/
class EnvelopeInterfaceTest {

static class TestEnvelope<T> implements Envelope<T> {
private final String type;
private final String topic;
private final T payload;
private final Date date;
private final String from;

TestEnvelope(String type, String topic, T payload, Date date, String from) {
this.type = type;
this.topic = topic;
this.payload = payload;
this.date = date;
this.from = from;
}

@Override
public String getType() {
return type;
}

@Override
public String getTopic() {
return topic;
}

@Override
public T getPayload() {
return payload;
}

@Override
public Date getDate() {
return date;
}

@Override
public String getFrom() {
return from;
}
}

@Test
void envelope_WithStringPayload_HandlesAllFields() {
// Arrange
String type = "test-type";
String topic = "/test/topic";
String payload = "test payload";
Date date = new Date();
String from = "test-sender";

// Act
TestEnvelope<String> envelope = new TestEnvelope<>(type, topic, payload, date, from);

// Assert
assertEquals(type, envelope.getType(), "Type should match");
assertEquals(topic, envelope.getTopic(), "Topic should match");
assertEquals(payload, envelope.getPayload(), "Payload should match");
assertEquals(date, envelope.getDate(), "Date should match");
assertEquals(from, envelope.getFrom(), "Sender should match");
}

@Test
void envelope_WithCustomPayload_HandlesGenericType() {
// Arrange
class CustomPayload {
String value;
CustomPayload(String value) { this.value = value; }
}

CustomPayload payload = new CustomPayload("test");
Date now = new Date();

// Act
TestEnvelope<CustomPayload> envelope = new TestEnvelope<>(
"custom", "/test", payload, now, "sender");

// Assert
assertSame(payload, envelope.getPayload(),
"Should handle custom payload type");
assertEquals("test", envelope.getPayload().value,
"Should preserve payload data");
}

@Test
void envelope_WithNullPayload_HandlesNull() {
// Arrange & Act
TestEnvelope<String> envelope = new TestEnvelope<>(
"null-test", "/test", null, new Date(), "sender");

// Assert
assertNull(envelope.getPayload(),
"Should handle null payload");
}

@Test
void envelope_DateField_ReturnsOriginalDate() {
// Arrange
Date date = new Date();
TestEnvelope<String> envelope = new TestEnvelope<>(
"test", "/test", "payload", date, "sender");

// Act
Date returnedDate = envelope.getDate();

// Assert
assertSame(date, returnedDate,
"Should return the exact Date instance provided");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package finos.traderx.messaging;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

/**
* Tests for the Publisher interface contract.
* We create a minimal implementation to verify:
* 1. Basic publishing functionality (with and without topic)
* 2. Connection management
* 3. Error handling with PubSubException
*/
class PublisherInterfaceTest {

static class TestPublisher implements Publisher<String> {
boolean connected = false;
String lastMessage = null;
String lastTopic = null;
boolean shouldThrowOnPublish = false;

@Override
public void publish(String message) throws PubSubException {
publish("/default", message);
}

@Override
public void publish(String topic, String message) throws PubSubException {
if (!isConnected()) {
throw new PubSubException("Not connected");
}
if (shouldThrowOnPublish) {
throw new PubSubException("Simulated publish error");
}
this.lastTopic = topic;
this.lastMessage = message;
}

@Override
public boolean isConnected() {
return connected;
}

@Override
public void connect() throws PubSubException {
if (connected) {
throw new PubSubException("Already connected");
}
connected = true;
}

@Override
public void disconnect() throws PubSubException {
if (!connected) {
throw new PubSubException("Already disconnected");
}
connected = false;
}
}

@Test
void publish_WhenConnected_PublishesMessage() throws PubSubException {
// Arrange
TestPublisher publisher = new TestPublisher();
publisher.connect();
String message = "test message";
String topic = "/test/topic";

// Act
publisher.publish(topic, message);

// Assert
assertEquals(message, publisher.lastMessage, "Message should be stored");
assertEquals(topic, publisher.lastTopic, "Topic should be stored");
}

@Test
void publish_WhenDisconnected_ThrowsPubSubException() {
// Arrange
TestPublisher publisher = new TestPublisher();
String message = "test message";

// Act & Assert
assertThrows(PubSubException.class, () -> publisher.publish(message),
"Publishing while disconnected should throw PubSubException");
}

@Test
void connect_WhenAlreadyConnected_ThrowsPubSubException() throws PubSubException {
// Arrange
TestPublisher publisher = new TestPublisher();
publisher.connect();

// Act & Assert
assertThrows(PubSubException.class, () -> publisher.connect(),
"Connecting when already connected should throw PubSubException");
}

@Test
void disconnect_WhenNotConnected_ThrowsPubSubException() {
// Arrange
TestPublisher publisher = new TestPublisher();

// Act & Assert
assertThrows(PubSubException.class, () -> publisher.disconnect(),
"Disconnecting when not connected should throw PubSubException");
}

@Test
void connectionLifecycle_WorksCorrectly() throws PubSubException {
// Arrange
TestPublisher publisher = new TestPublisher();

// Act & Assert - Connection cycle
assertFalse(publisher.isConnected(), "Should start disconnected");

publisher.connect();
assertTrue(publisher.isConnected(), "Should be connected after connect()");

publisher.disconnect();
assertFalse(publisher.isConnected(), "Should be disconnected after disconnect()");
}

@Test
void publish_WithError_ThrowsPubSubException() throws PubSubException {
// Arrange
TestPublisher publisher = new TestPublisher();
publisher.connect();
publisher.shouldThrowOnPublish = true;

// Act & Assert
assertThrows(PubSubException.class,
() -> publisher.publish("test message"),
"Should throw PubSubException when publish fails");
}
}
Loading