Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loicgreffier committed Oct 6, 2024
1 parent 4a7b7b9 commit 7cc29a3
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.michelin.kstreamplify.integration.container.KafkaIntegrationTest;
import com.michelin.kstreamplify.serde.SerdesUtils;
import com.michelin.kstreamplify.service.interactivequeries.KeyValueStoreService;
import com.michelin.kstreamplify.service.interactivequeries.WindowStoreService;
import com.michelin.kstreamplify.store.StateStoreRecord;
import com.michelin.kstreamplify.store.StreamsMetadata;
import io.confluent.kafka.serializers.KafkaAvroSerializer;
Expand Down Expand Up @@ -63,6 +64,7 @@
@Testcontainers
class InteractiveQueriesIntegrationTest extends KafkaIntegrationTest {
private final KeyValueStoreService keyValueStoreService = new KeyValueStoreService(initializer);
private final WindowStoreService windowStoreService = new WindowStoreService(initializer);

@BeforeAll
static void globalSetUp() throws ExecutionException, InterruptedException {
Expand Down Expand Up @@ -280,6 +282,42 @@ void shouldGetByKeyInStringAvroTimestampedKeyValueStore() throws IOException, In
assertNotNull(body.getTimestamp());
}

@Test
void shouldGetByKeyInStringAvroWindowStore() throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8081/store/window/STRING_AVRO_WINDOW_STORE/person"))
.GET()
.build();

HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
List<StateStoreRecord> body = objectMapper.readValue(response.body(), new TypeReference<>() {});

assertEquals(200, response.statusCode());
assertEquals("person", body.get(0).getKey());
assertEquals(1, ((Map<?, ?>) body.get(0).getValue()).get("id"));
assertEquals("John", ((Map<?, ?>) body.get(0).getValue()).get("firstName"));
assertEquals("Doe", ((Map<?, ?>) body.get(0).getValue()).get("lastName"));
assertEquals("2000-01-01T01:00:00Z", ((Map<?, ?>) body.get(0).getValue()).get("birthDate"));
assertNull(body.get(0).getTimestamp());
}

@Test
void shouldGetByKeyInStringAvroWindowStoreFromService() {
List<StateStoreRecord> stateStoreRecord = windowStoreService.getByKey(
"STRING_AVRO_WINDOW_STORE",
"person",
Instant.EPOCH,
Instant.now()
);

assertEquals("person", stateStoreRecord.get(0).getKey());
assertEquals(1L, ((Map<?, ?>) stateStoreRecord.get(0).getValue()).get("id"));
assertEquals("John", ((Map<?, ?>) stateStoreRecord.get(0).getValue()).get("firstName"));
assertEquals("Doe", ((Map<?, ?>) stateStoreRecord.get(0).getValue()).get("lastName"));
assertEquals("2000-01-01T01:00:00Z", ((Map<?, ?>) stateStoreRecord.get(0).getValue()).get("birthDate"));
assertNull(stateStoreRecord.get(0).getTimestamp());
}

@Test
void shouldGetAllInStringStringKeyValueStore() throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
Expand All @@ -296,10 +334,16 @@ void shouldGetAllInStringStringKeyValueStore() throws IOException, InterruptedEx
assertNull(body.get(0).getTimestamp());
}

@Test
void shouldGetAllInStringAvroKeyValueStore() throws IOException, InterruptedException {
@ParameterizedTest
@CsvSource({
"http://localhost:8081/store/key-value/STRING_AVRO_STORE",
"http://localhost:8081/store/key-value/local/STRING_AVRO_STORE",
"http://localhost:8081/store/window/STRING_AVRO_WINDOW_STORE",
"http://localhost:8081/store/window/local/STRING_AVRO_WINDOW_STORE"
})
void shouldGetAllInStringAvroStores(String url) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8081/store/key-value/STRING_AVRO_STORE"))
.uri(URI.create(url))
.GET()
.build();

Expand All @@ -315,22 +359,17 @@ void shouldGetAllInStringAvroKeyValueStore() throws IOException, InterruptedExce
assertNull(body.get(0).getTimestamp());
}

@Test
void shouldGetAllInStringAvroKeyValueStoreFromService() {
List<StateStoreRecord> stateQueryData = keyValueStoreService.getAll("STRING_AVRO_STORE");

assertEquals("person", stateQueryData.get(0).getKey());
assertEquals(1L, ((Map<?, ?>) stateQueryData.get(0).getValue()).get("id"));
assertEquals("John", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("firstName"));
assertEquals("Doe", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("lastName"));
assertEquals("2000-01-01T01:00:00Z", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("birthDate"));
assertNull(stateQueryData.get(0).getTimestamp());
}

@Test
void shouldGetAllInStringAvroTimestampedKeyValueStore() throws IOException, InterruptedException {
@ParameterizedTest
@CsvSource({
"http://localhost:8081/store/key-value/STRING_AVRO_TIMESTAMPED_STORE",
"http://localhost:8081/store/key-value/local/STRING_AVRO_TIMESTAMPED_STORE",
"http://localhost:8081/store/window/STRING_AVRO_TIMESTAMPED_WINDOW_STORE",
"http://localhost:8081/store/window/local/STRING_AVRO_TIMESTAMPED_WINDOW_STORE",
"http://localhost:8081/store/window/STRING_AVRO_TIMESTAMPED_WINDOW_STORE/person"
})
void shouldGetWithTimestamp(String url) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8081/store/key-value/STRING_AVRO_TIMESTAMPED_STORE"))
.uri(URI.create(url))
.GET()
.build();

Expand All @@ -347,19 +386,28 @@ void shouldGetAllInStringAvroTimestampedKeyValueStore() throws IOException, Inte
}

@Test
void shouldGetAllOnLocalHostInStringStringKeyValueStore() throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8081/store/key-value/local/STRING_STRING_STORE"))
.GET()
.build();
void shouldGetAllInStringAvroKeyValueStoreFromService() {
List<StateStoreRecord> stateQueryData = keyValueStoreService.getAll("STRING_AVRO_STORE");

HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
List<StateStoreRecord> body = objectMapper.readValue(response.body(), new TypeReference<>() {});
assertEquals("person", stateQueryData.get(0).getKey());
assertEquals(1L, ((Map<?, ?>) stateQueryData.get(0).getValue()).get("id"));
assertEquals("John", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("firstName"));
assertEquals("Doe", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("lastName"));
assertEquals("2000-01-01T01:00:00Z", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("birthDate"));
assertNull(stateQueryData.get(0).getTimestamp());
}

assertEquals(200, response.statusCode());
assertEquals("person", body.get(0).getKey());
assertEquals("Doe", body.get(0).getValue());
assertNull(body.get(0).getTimestamp());
@Test
void shouldGetAllInStringAvroWindowStoreFromService() {
List<StateStoreRecord> stateQueryData = windowStoreService
.getAll("STRING_AVRO_WINDOW_STORE", Instant.EPOCH, Instant.now());

assertEquals("person", stateQueryData.get(0).getKey());
assertEquals(1L, ((Map<?, ?>) stateQueryData.get(0).getValue()).get("id"));
assertEquals("John", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("firstName"));
assertEquals("Doe", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("lastName"));
assertEquals("2000-01-01T01:00:00Z", ((Map<?, ?>) stateQueryData.get(0).getValue()).get("birthDate"));
assertNull(stateQueryData.get(0).getTimestamp());
}

/**
Expand Down
3 changes: 1 addition & 2 deletions kstreamplify-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<artifactId>kstreamplify-spring-boot</artifactId>

<properties>
<kstreamplify-core.version>1.0.3-SNAPSHOT</kstreamplify-core.version>
<springdoc.version>2.6.0</springdoc.version>
</properties>

Expand Down Expand Up @@ -52,7 +51,7 @@
<dependency>
<groupId>com.michelin</groupId>
<artifactId>kstreamplify-core</artifactId>
<version>${kstreamplify-core.version}</version>
<version>1.0.3-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.michelin.kstreamplify.service.interactivequeries.KeyValueStoreService;
import com.michelin.kstreamplify.service.interactivequeries.WindowStoreService;
import com.michelin.kstreamplify.store.StateStoreRecord;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down
Loading

0 comments on commit 7cc29a3

Please sign in to comment.