Skip to content

Commit

Permalink
Refactor tests so they can be reused for all transport protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
mmodzelewski committed Oct 1, 2023
1 parent d43878b commit ffc65ff
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 204 deletions.
30 changes: 30 additions & 0 deletions src/main/java/rs/iggy/IggyClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package rs.iggy;

import rs.iggy.consumergroup.ConsumerGroupsClient;
import rs.iggy.consumeroffset.ConsumerOffsetsClient;
import rs.iggy.message.MessagesClient;
import rs.iggy.partition.PartitionsClient;
import rs.iggy.stream.StreamsClient;
import rs.iggy.system.SystemClient;
import rs.iggy.topic.TopicsClient;
import rs.iggy.user.UsersClient;

public interface IggyClient {

SystemClient system();

StreamsClient streams();

UsersClient users();

TopicsClient topics();

PartitionsClient partitions();

ConsumerGroupsClient consumerGroups();

ConsumerOffsetsClient consumerOffsets();

MessagesClient messages();

}
11 changes: 10 additions & 1 deletion src/main/java/rs/iggy/http/IggyHttpClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package rs.iggy.http;

import rs.iggy.IggyClient;
import rs.iggy.consumergroup.ConsumerGroupsClient;
import rs.iggy.consumeroffset.ConsumerOffsetsClient;
import rs.iggy.message.MessagesClient;
Expand All @@ -9,7 +10,7 @@
import rs.iggy.topic.TopicsClient;
import rs.iggy.user.UsersClient;

public class IggyHttpClient {
public class IggyHttpClient implements IggyClient {

private final SystemHttpClient systemClient;
private final StreamsHttpClient streamsClient;
Expand All @@ -32,34 +33,42 @@ public IggyHttpClient(String url) {
messagesClient = new MessagesHttpClient(httpClient);
}

@Override
public SystemClient system() {
return systemClient;
}

@Override
public StreamsClient streams() {
return streamsClient;
}

@Override
public UsersClient users() {
return usersClient;
}

@Override
public TopicsClient topics() {
return topicsClient;
}

@Override
public PartitionsClient partitions() {
return partitionsClient;
}

@Override
public ConsumerGroupsClient consumerGroups() {
return consumerGroupsClient;
}

@Override
public ConsumerOffsetsClient consumerOffsets() {
return consumerOffsetsClient;
}

@Override
public MessagesClient messages() {
return messagesClient;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
package rs.iggy;

import org.junit.jupiter.api.BeforeEach;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import rs.iggy.http.IggyHttpClient;
import static java.util.Optional.empty;

@Testcontainers
public abstract class BaseIntegrationTest {
public abstract class IntegrationTest {

public static final int HTTP_PORT = 3000;

@Container
protected final GenericContainer<?> iggyServer = new GenericContainer(
DockerImageName.parse("iggyrs/iggy:latest")).withExposedPorts(3000);
DockerImageName.parse("iggyrs/iggy:latest")).withExposedPorts(HTTP_PORT);

protected IggyClient client;

@BeforeEach
void beforeEachIntegrationTest() {
client = getClient();
}

abstract protected IggyClient getClient();

protected void setUpStream(IggyHttpClient client) {
protected void setUpStream() {
client.streams().createStream(42L, "test-stream");
}

protected void setUpStreamAndTopic(IggyHttpClient client) {
setUpStream(client);
protected void setUpStreamAndTopic() {
setUpStream();
client.topics().createTopic(42L, 42L, 1L, empty(), "test-topic");
}

protected void login(IggyHttpClient client) {
protected void login() {
client.users().login("iggy", "iggy");
}

Expand Down
16 changes: 0 additions & 16 deletions src/test/java/rs/iggy/http/BaseHttpClientIntegrationTest.java

This file was deleted.

14 changes: 14 additions & 0 deletions src/test/java/rs/iggy/http/HttpClientFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package rs.iggy.http;

import org.testcontainers.containers.GenericContainer;
import static rs.iggy.IntegrationTest.HTTP_PORT;

class HttpClientFactory {

static IggyHttpClient create(GenericContainer iggyServer) {
String address = iggyServer.getHost();
Integer port = iggyServer.getMappedPort(HTTP_PORT);
return new IggyHttpClient("http://" + address + ":" + port);
}

}
85 changes: 6 additions & 79 deletions src/test/java/rs/iggy/http/MessagesHttpClientTest.java
Original file line number Diff line number Diff line change
@@ -1,86 +1,13 @@
package rs.iggy.http;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import rs.iggy.message.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.UUID;
import static java.util.Optional.empty;
import static org.assertj.core.api.Assertions.assertThat;
import rs.iggy.IggyClient;
import rs.iggy.message.MessagesClientBaseTest;

class MessagesHttpClientTest extends BaseHttpClientIntegrationTest {
class MessagesHttpClientTest extends MessagesClientBaseTest {

MessagesClient messagesClient;

@BeforeEach
void beforeEach() {
messagesClient = client.messages();

login(client);
}

@Test
void shouldSendAndGetMessages() {
// given
setUpStreamAndTopic(client);

// when
String text = "message from java sdk";
messagesClient.sendMessages(42L, 42L, Partitioning.partitionId(1L),
List.of(new MessageToSend(getRandomId(), text.getBytes(), empty())));

var polledMessages = messagesClient.pollMessages(42L, 42L, empty(), 0L,
new PollingStrategy(PollingKind.Last, BigInteger.TEN), 10L, false);

// then
assertThat(polledMessages.messages()).hasSize(1);
}

@Test
void shouldSendMessageWithBalancedPartitioning() {
// given
setUpStreamAndTopic(client);

// when
String text = "message from java sdk";
messagesClient.sendMessages(42L, 42L, Partitioning.balanced(),
List.of(new MessageToSend(getRandomId(), text.getBytes(), empty())));

var polledMessages = messagesClient.pollMessages(42L, 42L, empty(), 0L,
new PollingStrategy(PollingKind.Last, BigInteger.TEN), 10L, false);

// then
assertThat(polledMessages.messages()).hasSize(1);
}

@Test
void shouldSendMessageWithMessageKeyPartitioning() {
// given
setUpStreamAndTopic(client);

// when
String text = "message from java sdk";
messagesClient.sendMessages(42L, 42L, Partitioning.messagesKey("test-key"),
List.of(new MessageToSend(getRandomId(), text.getBytes(), empty())));

var polledMessages = messagesClient.pollMessages(42L, 42L, empty(), 0L,
new PollingStrategy(PollingKind.Last, BigInteger.TEN), 10L, false);

// then
assertThat(polledMessages.messages()).hasSize(1);
}

private static BigInteger getRandomId() {
return new BigInteger(1, uuidToBytes(UUID.randomUUID()));
}

private static byte[] uuidToBytes(UUID uuid) {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
return byteBuffer.array();
@Override
protected IggyClient getClient() {
return HttpClientFactory.create(iggyServer);
}

}
44 changes: 6 additions & 38 deletions src/test/java/rs/iggy/http/PartitionsHttpClientTest.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
package rs.iggy.http;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import rs.iggy.partition.PartitionsClient;
import rs.iggy.topic.TopicDetails;
import rs.iggy.topic.TopicsClient;
import static org.assertj.core.api.Assertions.assertThat;
import rs.iggy.IggyClient;
import rs.iggy.partition.PartitionsClientBaseTest;

class PartitionsHttpClientTest extends BaseHttpClientIntegrationTest {
class PartitionsHttpClientTest extends PartitionsClientBaseTest {

TopicsClient topicsClient;
PartitionsClient partitionsClient;

@BeforeEach
void beforeEach() {
topicsClient = client.topics();
partitionsClient = client.partitions();

login(client);
setUpStreamAndTopic(client);
}


@Test
void shouldCreateAndDeletePartitions() {
// given
assert topicsClient.getTopic(42L, 42L).partitionsCount() == 1L;

// when
partitionsClient.createPartitions(42L, 42L, 10L);

// then
TopicDetails topic = topicsClient.getTopic(42L, 42L);
assertThat(topic.partitionsCount()).isEqualTo(11L);

// when
partitionsClient.deletePartitions(42L, 42L, 10L);

// then
topic = topicsClient.getTopic(42L, 42L);
assertThat(topic.partitionsCount()).isEqualTo(1L);
@Override
protected IggyClient getClient() {
return HttpClientFactory.create(iggyServer);
}

}
37 changes: 6 additions & 31 deletions src/test/java/rs/iggy/http/StreamHttpClientTest.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
package rs.iggy.http;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import rs.iggy.stream.StreamsClient;
import static org.assertj.core.api.Assertions.assertThat;
import rs.iggy.IggyClient;
import rs.iggy.stream.StreamClientBaseTest;

class StreamHttpClientTest extends BaseHttpClientIntegrationTest {
class StreamHttpClientTest extends StreamClientBaseTest {

StreamsClient streamsClient;

@BeforeEach
void beforeEach() {
streamsClient = client.streams();

login(client);
}

@Test
void shouldCreateAndDeleteStream() {
// when
streamsClient.createStream(42L, "test-stream");
var stream = streamsClient.getStream(42L);

// then
assertThat(stream).isNotNull();
assertThat(stream.id()).isEqualTo(42L);
assertThat(stream.name()).isEqualTo("test-stream");

// when
streamsClient.deleteStream(42L);
var streams = streamsClient.getStreams();

// then
assertThat(streams).isEmpty();
@Override
protected IggyClient getClient() {
return HttpClientFactory.create(iggyServer);
}

}
Loading

0 comments on commit ffc65ff

Please sign in to comment.