Skip to content

Commit

Permalink
Add documentation about overriding properties in unit tests (#251)
Browse files Browse the repository at this point in the history
* Add documentation about overriding properties in unit tests

* sonar
  • Loading branch information
loicgreffier authored Oct 6, 2024
1 parent d70b2f5 commit 62e58e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ need to do:
* [Open Telemetry](#open-telemetry)
* [Swagger](#swagger)
* [Testing](#testing)
* [Create your first test](#create-your-first-test)
* [Override Properties](#override-properties)
* [Motivation](#motivation)
* [Contribution](#contribution)

Expand Down Expand Up @@ -501,6 +503,8 @@ Both can be customized by using the [Springdoc properties](https://springdoc.org

Kstreamplify eases the use of the Topology Test Driver for testing Kafka Streams application.

### Create your first test

You can create a test class that extends `KafkaStreamsStarterTest`, override
the `KafkaStreamsStarterTest#getKafkaStreamsStarter()` to provide your `KafkaStreamsStarter` implementation,
and start writing your tests.
Expand Down Expand Up @@ -543,6 +547,22 @@ public class MyKafkaStreamsTest extends KafkaStreamsStarterTest {
}
```

### Override Properties

Kstreamplify runs the tests with default properties.
It is possible to provide additional properties or override the default ones:

```java
public class MyKafkaStreamsTest extends KafkaStreamsStarterTest {
@Override
protected Map<String, String> getSpecificProperties() {
return Map.of(
STATE_DIR_CONFIG, "/tmp/kafka-streams"
);
}
}
```

## Motivation

Developing applications with Kafka Streams can be challenging and often raises many questions for developers. It
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.nio.file.Paths;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.streams.StreamsBuilder;
Expand Down Expand Up @@ -82,7 +82,7 @@ private Properties getProperties() {
properties.setProperty(StreamsConfig.STATE_DIR_CONFIG, STATE_DIR + getClass().getSimpleName());

// Add specific properties or overwrite default properties
HashMap<String, String> propertiesMap = getSpecificProperties();
Map<String, String> propertiesMap = getSpecificProperties();
if (propertiesMap != null && !propertiesMap.isEmpty()) {
properties.putAll(propertiesMap);
}
Expand Down Expand Up @@ -111,8 +111,8 @@ protected Instant getInitialWallClockTime() {
*
* @return new/overwrite properties
*/
protected HashMap<String, String> getSpecificProperties() {
return new HashMap<>();
protected Map<String, String> getSpecificProperties() {
return Collections.emptyMap();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import com.michelin.kstreamplify.context.KafkaStreamsExecutionContext;
import com.michelin.kstreamplify.initializer.KafkaStreamsStarter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.kafka.streams.StreamsBuilder;
import org.junit.jupiter.api.Assertions;
Expand All @@ -25,21 +25,21 @@ public String dlqTopic() {

@Override
public void topology(StreamsBuilder streamsBuilder) {
// Do nothing
}
};
}

/**
* Overwrite the default storage path.
*
* @return the new/overwrite properties
* @return the new properties
*/
@Override
protected HashMap<String, String> getSpecificProperties() {
HashMap<String, String> propertiesMap = new HashMap<>();
propertiesMap.put(STATE_DIR_CONFIG, SPECIFIC_STORAGE_PATH);

return propertiesMap;
protected Map<String, String> getSpecificProperties() {
return Map.of(
STATE_DIR_CONFIG, SPECIFIC_STORAGE_PATH
);
}

/**
Expand All @@ -50,5 +50,4 @@ void shouldValidateStorageDirHasBeenOverride() {
Properties properties = KafkaStreamsExecutionContext.getProperties();
Assertions.assertEquals(SPECIFIC_STORAGE_PATH, properties.getProperty(STATE_DIR_CONFIG));
}

}

0 comments on commit 62e58e9

Please sign in to comment.