Skip to content

Commit

Permalink
Merge pull request #30 from jupiter-tools/assert-silence-for-kafka
Browse files Browse the repository at this point in the history
assert silence for expected messages in Kafka
  • Loading branch information
antkorwin authored Jun 21, 2020
2 parents ab36cb0 + 9521dac commit 5dc6f82
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.3</version>
<version>0.4</version>
<packaging>pom</packaging>

<name>spring-boot-extensions-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-activemq/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.3</version>
<version>0.4</version>
</parent>

<artifactId>spring-test-activemq</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.3</version>
<version>0.4</version>
</parent>

<artifactId>spring-test-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ public class AssertReceivedMessages {

private final ExpectedMessagesOptions expectedMessagesOptions;
private final MessageBroker messageBroker;
private final ObjectMapper mapper;

public AssertReceivedMessages(ExpectedMessagesOptions expectedMessagesOptions,
MessageBroker messageBroker) {
this.expectedMessagesOptions = expectedMessagesOptions;
this.messageBroker = messageBroker;
this.mapper = new ObjectMapper();
}

/**
Expand All @@ -43,7 +41,10 @@ public void doAssert() {
return;
}

DataSet expectedDataSet = new JsonImport(new ImportFile(expectedMessagesOptions.getMessagesFile()));
DataSet expectedDataSet = new JsonImport(new ImportFile(expectedMessagesOptions.getMessagesFile()));
if (expectedMessagesOptions.getExpectedDataSetPreProcessor() != null) {
expectedDataSet = expectedMessagesOptions.getExpectedDataSetPreProcessor().run(expectedDataSet);
}

if (isEmptyDataSet(expectedDataSet)) {
processingEmptyDataSet(expectedMessagesOptions);
Expand All @@ -53,6 +54,21 @@ public void doAssert() {
processingDataSet(expectedMessagesOptions, expectedDataSet);
}

/**
* Waits for messages and throws an exception if receive something during the waiting timeout interval.
*/
public void doAssertSilence() {

for (String queue : expectedMessagesOptions.getAllQueues()) {
Object message = messageBroker.receive(queue, expectedMessagesOptions.getTimeout());

if (message != null) {
new Fail("not expected but found:").withObject(message).fire();
}
}
}


private boolean isEmptyDataSet(DataSet dataSet) {
Map<String, List<Map<String, Object>>> readDataSet = dataSet.read();
return readDataSet.isEmpty() || readDataSet.entrySet()
Expand Down Expand Up @@ -85,38 +101,22 @@ private void processingDataSet(ExpectedMessagesOptions expectedMessagesOptions,
}

receivedMessages.add(message);
DataSet actualDataSet = buildDataSetFromMessages(receivedMessages);
DataSet actualDataSet = new MessagesDataSet(receivedMessages);

if(isWaitingMoreMessages(startTime, actualDataSet, expectedDataSet)){
continue;
}

new MatchDataSets(actualDataSet, expectedDataSet).check();
return;
if (expectedMessagesOptions.getActualDataSetPreProcessor() != null) {
actualDataSet = expectedMessagesOptions.getActualDataSetPreProcessor()
.run(actualDataSet);
}

new MatchDataSets(actualDataSet, expectedDataSet).check();
return;
}
}

//TODO: replace this in a separate DataSet implementation
private DataSet buildDataSetFromMessages(List<Object> messages) {

Map<String, List<Map<String, Object>>> result = new HashMap<>();

for (Object message : messages) {

String className = message.getClass().getCanonicalName();
Map<String, Object> messageFields = mapper.convertValue(message, Map.class);

List<Map<String, Object>> entry = result.get(className);
if (entry == null) {
result.put(className, new ArrayList<>(Arrays.asList(messageFields)));
} else {
entry.add(messageFields);
}
}

return () -> result;
}

private boolean isWaitingMoreMessages(long startTime, DataSet actual, DataSet expected) {

if (timeLimit(startTime, expectedMessagesOptions.getTimeout())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jupiter.tools.spring.test.core.expected.list.messages;


import com.jupitertools.datasetroll.DataSet;

/**
* Pre-processing the DataSet from one state to another
*
* @author Korovin Anatoliy
*/
public interface DataSetPreProcessor {

/**
* transform the source dataset
*
* @param source input dataset
* @return transformed version of the dataset
*/
DataSet run(DataSet source);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class ExpectedMessagesOptions {

/**
* The name of a queue for messages
* The name of a queue(or topic) for messages
*/
private String queue;

Expand All @@ -28,4 +28,19 @@ public class ExpectedMessagesOptions {
* The path to the JSON file with expected messages
*/
private String messagesFile;

/**
* Applies to the expected data-set after retrieve from json-file
*/
private DataSetPreProcessor expectedDataSetPreProcessor;

/**
* Applies to the actual data-set after receiving messages from queue(topic)
*/
private DataSetPreProcessor actualDataSetPreProcessor;

/**
* The list of queues(or topics) to wait for messages when using {@link AssertReceivedMessages#doAssertSilence()}
*/
private String[] allQueues;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.jupiter.tools.spring.test.core.expected.list.messages;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jupitertools.datasetroll.DataSet;

/**
* DataSet to represent a list of messages.
* Matches canonical class name of a message object to the list of messages converted to Map.
*
* @author Korovin Anatoliy
*/
public class MessagesDataSet implements DataSet {

private final List<Object> messages;
private final ObjectMapper objectMapper;

/**
* Make the DataSet from a list of received messages.
*
* @param messages the list of messages objects
*/
public MessagesDataSet(List<Object> messages) {
this.messages = messages;
this.objectMapper = new ObjectMapper();
}

@Override
public Map<String, List<Map<String, Object>>> read() {

Map<String, List<Map<String, Object>>> result = new HashMap<>();

for (Object message : messages) {

String className = message.getClass().getCanonicalName();
Map<String, Object> messageFields = objectMapper.convertValue(message, Map.class);

List<Map<String, Object>> entry = result.get(className);
if (entry == null) {
result.put(className, new ArrayList<>(Arrays.asList(messageFields)));
} else {
entry.add(messageFields);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.jupiter.tools.spring.test.core.expected.list.messages;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class MessagesDataSetTest {

@Test
void singleTypeOfMessages() {
Foo foo1 = new Foo("111");
Foo foo2 = new Foo("222");
List<Object> messages = Arrays.asList(foo1, foo2);
MessagesDataSet dataSet = new MessagesDataSet(messages);
// Act
Map<String, List<Map<String, Object>>> map = dataSet.read();
// Assert
assertThat(map).containsKeys(Foo.class.getCanonicalName());
assertThat(map.get(Foo.class.getCanonicalName())).contains(ImmutableMap.of("name", "111"),
ImmutableMap.of("name", "222"));
}

@Test
void multipleTypes() {
Foo foo1 = new Foo("111");
Foo foo2 = new Foo("222");
Bar bar = new Bar("AAA");
List<Object> messages = Arrays.asList(foo1, foo2, bar);
MessagesDataSet dataSet = new MessagesDataSet(messages);
// Act
Map<String, List<Map<String, Object>>> map = dataSet.read();
// Assert
assertThat(map).containsKeys(Foo.class.getCanonicalName());
assertThat(map.get(Foo.class.getCanonicalName())).contains(ImmutableMap.of("name", "111"),
ImmutableMap.of("name", "222"));
assertThat(map).containsKeys(Bar.class.getCanonicalName());
assertThat(map.get(Bar.class.getCanonicalName())).contains(ImmutableMap.of("value", "AAA"));
}

@Data
@AllArgsConstructor
static class Foo {
private String name;
}

@Data
@AllArgsConstructor
static class Bar {
private String value;
}
}
2 changes: 1 addition & 1 deletion spring-test-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.3</version>
<version>0.4</version>
</parent>

<artifactId>spring-test-jpa</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-mysql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.3</version>
<version>0.4</version>
</parent>

<artifactId>spring-test-mysql</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-postgres/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.3</version>
<version>0.4</version>
</parent>

<artifactId>spring-test-postgres</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-rabbitmq/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.3</version>
<version>0.4</version>
</parent>

<artifactId>spring-test-rabbitmq</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-test-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.jupiter-tools</groupId>
<artifactId>spring-boot-extensions-parent</artifactId>
<version>0.3</version>
<version>0.4</version>
</parent>

<artifactId>spring-test-web</artifactId>
Expand Down

0 comments on commit 5dc6f82

Please sign in to comment.