-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c562da1
commit e9d0121
Showing
14 changed files
with
322 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 31 additions & 3 deletions
34
kstreamplify-core/src/test/java/com/michelin/kstreamplify/TopologyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,48 @@ | ||
package com.michelin.kstreamplify; | ||
|
||
import com.michelin.kstreamplify.context.KafkaStreamsExecutionContext; | ||
import com.michelin.kstreamplify.initializer.KafkaStreamsInitializer; | ||
import com.michelin.kstreamplify.initializer.KafkaStreamsStarterTest; | ||
import com.michelin.kstreamplify.initializer.KafkaStreamsStarterTopologyTest; | ||
import com.michelin.kstreamplify.model.TopologyExposeJsonModel; | ||
import com.michelin.kstreamplify.services.ConvertTopology; | ||
import org.apache.kafka.streams.StreamsBuilder; | ||
import org.apache.kafka.streams.TopologyTestDriver; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Collections; | ||
import java.util.Properties; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
public class TopologyTest { | ||
|
||
protected TopologyTestDriver testDriver; | ||
@Test | ||
public void convertTopologyForRestTest() { | ||
|
||
Properties properties = new Properties(); | ||
properties.setProperty("application.id", "test"); | ||
properties.setProperty("bootstrap.servers", "mock:1234"); | ||
properties.setProperty("state.dir", "/tmp/kafka-streams"); | ||
KafkaStreamsExecutionContext.registerProperties(properties); | ||
KafkaStreamsExecutionContext.setDlqTopicName("DLQ_TOPIC"); | ||
KafkaStreamsExecutionContext.setSerdesConfig(Collections.singletonMap("schema.registry.url", "mock://" + this.getClass().getName())); | ||
|
||
|
||
StreamsBuilder streamsBuilder = new StreamsBuilder(); | ||
KafkaStreamsStarterTopologyTest kafkaStreamsStarterTopologyTest = new KafkaStreamsStarterTopologyTest(); | ||
kafkaStreamsStarterTopologyTest.topology(streamsBuilder); | ||
KafkaStreamsInitializer kafkaStreamsInitializer = new KafkaStreamsInitializer(); | ||
kafkaStreamsInitializer.init(new KafkaStreamsStarterTest()); | ||
kafkaStreamsInitializer.init(kafkaStreamsStarterTopologyTest); | ||
TopologyExposeJsonModel topology = ConvertTopology.convertTopologyForRest("STREAM", kafkaStreamsInitializer.getTopology()); | ||
|
||
testDriver = new TopologyTestDriver(streamsBuilder.build(), properties, this.getInitialWallClockTime()); | ||
|
||
assertNotNull(topology); | ||
testDriver.advanceWallClockTime(Duration.ofDays(1)); | ||
} | ||
private Instant getInitialWallClockTime() { | ||
return Instant.ofEpochMilli(1577836800000L); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...ore/src/test/java/com/michelin/kstreamplify/deduplication/DedupKeyValueProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.michelin.kstreamplify.deduplication; | ||
|
||
import com.michelin.kstreamplify.avro.KafkaError; | ||
import com.michelin.kstreamplify.error.ProcessingResult; | ||
import org.apache.kafka.streams.processor.api.ProcessorContext; | ||
import org.apache.kafka.streams.processor.api.Record; | ||
import org.apache.kafka.streams.state.TimestampedKeyValueStore; | ||
import org.apache.kafka.streams.state.WindowStore; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
class DedupKeyValueProcessorTest { | ||
|
||
@Mock | ||
private ProcessorContext<String, ProcessingResult<KafkaError, KafkaError>> context; | ||
|
||
@Mock | ||
private WindowStore<String, String> windowStore; | ||
|
||
@InjectMocks | ||
private DedupKeyValueProcessor<KafkaError> dedupKeyValueProcessor; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
when(context.getStateStore("dedupStoreName")).thenReturn(windowStore); | ||
} | ||
|
||
@Test | ||
void testProcessNewRecord() { | ||
String key = "some-key"; | ||
KafkaError value = new KafkaError(); | ||
|
||
Record<String, KafkaError> record = new Record<>(key, value, 0); | ||
|
||
DedupKeyValueProcessor<KafkaError> dedupKeyValueProcessor = new DedupKeyValueProcessor<>("dedupStoreName",Duration.ZERO); | ||
dedupKeyValueProcessor.init(context); | ||
dedupKeyValueProcessor.process(record); | ||
|
||
// verify(windowStore).put(eq(key), any()); | ||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
...rc/test/java/com/michelin/kstreamplify/deduplication/DedupWithPredicateProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.michelin.kstreamplify.deduplication; | ||
|
||
import com.michelin.kstreamplify.avro.KafkaError; | ||
import com.michelin.kstreamplify.error.ProcessingResult; | ||
import org.apache.kafka.streams.KeyValue; | ||
import org.apache.kafka.streams.processor.api.MockProcessorContext; | ||
import org.apache.kafka.streams.processor.api.ProcessorContext; | ||
import org.apache.kafka.streams.processor.api.Record; | ||
import org.apache.kafka.streams.state.TimestampedKeyValueStore; | ||
import org.apache.kafka.streams.state.ValueAndTimestamp; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.Duration; | ||
import java.util.Iterator; | ||
|
||
import static com.google.common.base.Verify.verify; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class DedupWithPredicateProcessorTest { | ||
|
||
private DedupWithPredicateProcessor<String, KafkaError> processor; | ||
private ProcessorContext<String, ProcessingResult<KafkaError, KafkaError>> context; | ||
private TimestampedKeyValueStore<String, KafkaError> store; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
// Initialize mock objects and the processor | ||
context = mock(ProcessorContext.class); | ||
store = mock(TimestampedKeyValueStore.class); | ||
|
||
// Create an instance of DedupWithPredicateProcessor for testing | ||
processor = new DedupWithPredicateProcessor<>("testStore", Duration.ofHours(1), TestKeyExtractor::extract); | ||
|
||
// Stub the context.getStateStore method to return the mock store | ||
when(context.getStateStore("testStore")).thenReturn(store); | ||
|
||
processor.init(context); | ||
} | ||
|
||
@Test | ||
public void testProcessFirstTime() { | ||
// Create a test record | ||
Record<String, KafkaError> record = new Record<>("key", new KafkaError(), 0L); | ||
|
||
// Stub store.get to return null, indicating it's the first time | ||
when(store.get("key")).thenReturn(null); | ||
|
||
// Call the process method | ||
processor.process(record); | ||
|
||
// Verify that the record is stored in the store and forwarded | ||
store.put(eq("key"), any()); | ||
context.forward(any()); | ||
} | ||
|
||
@Test | ||
public void testProcessDuplicate() { | ||
// Create a test record | ||
Record<String, KafkaError> record = new Record<>("key", new KafkaError(), 0L); | ||
|
||
// Stub store.get to return a value, indicating a duplicate | ||
when(store.get("key")).thenReturn(ValueAndTimestamp.make(new KafkaError(), 0L)); | ||
|
||
// Call the process method | ||
processor.process(record); | ||
|
||
// Verify that the record is not stored again and not forwarded | ||
// verify(store, never()).put(any(), any()); | ||
// verify(context, never()).forward(any()); | ||
} | ||
|
||
// Add more test cases as needed | ||
|
||
// Example: Test error handling in process method | ||
@Test | ||
public void testProcessError() { | ||
// Create a test record that will trigger an exception | ||
Record<String, KafkaError> record = new Record<>("key", null, 0L); | ||
|
||
// Call the process method | ||
processor.process(record); | ||
|
||
// Verify that an error message is forwarded | ||
// verify(context).forward(argThat(result -> result.isFailure() && result.getErrorMessage().contains("Couldn't figure out"))); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...eamplify-core/src/test/java/com/michelin/kstreamplify/deduplication/TestKeyExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.michelin.kstreamplify.deduplication; | ||
|
||
import org.apache.avro.specific.SpecificRecord; | ||
|
||
public class TestKeyExtractor { | ||
public static <V extends SpecificRecord> String extract(V v) { | ||
return ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
kstreamplify-core/src/test/java/com/michelin/kstreamplify/model/DlqTopicTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.michelin.kstreamplify.model; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class DlqTopicTest { | ||
|
||
@Mock | ||
private DlqTopic dlqTopicMock; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
|
||
@Test | ||
public void testDlqTopicName() { | ||
DlqTopic dlqTopic = DlqTopic.builder() | ||
.name("TestTopic") | ||
.build(); | ||
|
||
when(dlqTopicMock.getName()).thenReturn("TestTopic"); | ||
|
||
assertEquals("TestTopic", dlqTopic.getName()); | ||
|
||
dlqTopic.builder().toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.