Skip to content

Commit

Permalink
end to end test for all the field types in row state
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren committed Dec 19, 2024
1 parent 904f8d3 commit 2524a3a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import akka.javasdk.client.NoEntryFoundException;
import akka.javasdk.testkit.TestKit;
import akka.javasdk.testkit.TestKitSupport;
import akka.stream.javadsl.Sink;
import akkajavasdk.components.eventsourcedentities.counter.Counter;
import akkajavasdk.components.eventsourcedentities.counter.CounterEntity;
import akkajavasdk.components.keyvalueentities.user.AssignedCounterEntity;
import akkajavasdk.components.keyvalueentities.user.User;
import akkajavasdk.components.keyvalueentities.user.UserEntity;
import akkajavasdk.components.views.AllTheTypesKvEntity;
import akkajavasdk.components.views.AllTheTypesView;
import akkajavasdk.components.views.UserCounter;
import akkajavasdk.components.views.UserCounters;
import akkajavasdk.components.views.UserCountersView;
Expand All @@ -31,30 +34,23 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static akkajavasdk.components.pubsub.PublishVEToTopic.CUSTOMERS_TOPIC;

import static java.time.temporal.ChronoUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(Junit5LogCapturing.class)
public class ViewIntegrationTest extends TestKitSupport {


@Override
protected TestKit.Settings testKitSettings() {
// here only to show how to set different `Settings` in a test.
return TestKit.Settings.DEFAULT
.withTopicOutgoingMessages(CUSTOMERS_TOPIC);
}

private String newId() {
return UUID.randomUUID().toString();
}


@Test
public void verifyTransformedUserViewWiring() {

Expand Down Expand Up @@ -166,6 +162,31 @@ public void verifyCounterViewMultipleSubscriptions() {
new IsEqual<>(2));
}

@Test
public void verifyAllTheFieldTypesView() throws Exception {
// see that we can persist and read a row with all fields, no indexed columns
var id = newId();
var row = new AllTheTypesKvEntity.AllTheTypes(1, 2L, 3F, 4D, true, "text", 5, 6L, 7F, 8D, false, Instant.EPOCH, Optional.of("optional"), List.of("text1", "text2"),
new AllTheTypesKvEntity.ByEmail("test@example.com"),
AllTheTypesKvEntity.AnEnum.THREE, new AllTheTypesKvEntity.Recursive(new AllTheTypesKvEntity.Recursive(null)));
await(componentClient.forKeyValueEntity(id).method(AllTheTypesKvEntity::store).invokeAsync(row));


Awaitility.await()
.ignoreExceptions()
.atMost(10, TimeUnit.SECONDS)
.untilAsserted(() -> {
var rows = await(componentClient.forView()
.stream(AllTheTypesView::allRows)
.source().runWith(Sink.seq(), testKit.getMaterializer()));

assertThat(rows).hasSize(1);
}
);


}

@Disabled // pending primitive query parameters working
@Test
public void shouldAcceptPrimitivesForViewQueries() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akkajavasdk.components.views;

import akka.javasdk.annotations.ComponentId;
import akka.javasdk.keyvalueentity.KeyValueEntity;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

@ComponentId("all-the-types-kve")
public class AllTheTypesKvEntity extends KeyValueEntity<AllTheTypesKvEntity.AllTheTypes> {

public enum AnEnum {
ONE, TWO, THREE
}

// common query parameter for views in this file
public record ByEmail(String email) {
}

public record Recursive(Recursive recurse) {}

public record AllTheTypes(
int intValue,
long longValue,
float floatValue,
double doubleValue,
boolean booleanValue,
String stringValue,
Integer wrappedInt,
Long wrappedLong,
Float wrappedFloat,
Double wrappedDouble,
Boolean wrappedBoolean,
Instant instant,
// FIXME bytes does not work yet in runtime Byte[] bytes,
Optional<String> optionalString,
List<String> repeatedString,
ByEmail nestedMessage,
AnEnum anEnum,
Recursive recursive
) {}



public Effect<String> store(AllTheTypes value) {
return effects().updateState(value).thenReply("OK");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akkajavasdk.components.views;

import akka.javasdk.annotations.ComponentId;
import akka.javasdk.annotations.Consume;
import akka.javasdk.annotations.Query;
import akka.javasdk.view.TableUpdater;
import akka.javasdk.view.View;

@ComponentId("all_the_field_types_view")
public class AllTheTypesView extends View {


@Consume.FromKeyValueEntity(AllTheTypesKvEntity.class)
public static class Events extends TableUpdater<AllTheTypesKvEntity.AllTheTypes> { }

@Query("SELECT * FROM events")
public QueryStreamEffect<AllTheTypesKvEntity.AllTheTypes> allRows() {
return queryStreamResult();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ akka.javasdk {
"akkajavasdk.components.keyvalueentities.user.UserEntity",
"akkajavasdk.components.workflowentities.WalletEntity",
"akkajavasdk.components.keyvalueentities.user.AssignedCounterEntity",
"akkajavasdk.components.keyvalueentities.hierarchy.TextKvEntity"
"akkajavasdk.components.keyvalueentities.hierarchy.TextKvEntity",
"akkajavasdk.components.views.AllTheTypesKvEntity"
]
view = [
"akkajavasdk.components.views.user.UsersByEmailAndName",
Expand All @@ -44,7 +45,8 @@ akka.javasdk {
"akkajavasdk.components.views.counter.CountersByValueSubscriptions",
"akkajavasdk.components.pubsub.ViewFromCounterEventsTopic",
"akkajavasdk.components.views.user.UsersByPrimitives",
"akkajavasdk.components.views.hierarchy.HierarchyCountersByValue"
"akkajavasdk.components.views.hierarchy.HierarchyCountersByValue",
"akkajavasdk.components.views.AllTheTypesView"
]
workflow = [
"akkajavasdk.components.workflowentities.TransferWorkflow",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ public record ById(String id) {}

@ComponentId("recursive_view")
public static class RecursiveViewStateView extends View {
@Consume.FromTopic(value = "source")
@Consume.FromTopic(value = "recursivetopic")
public static class Events extends TableUpdater<Recursive> { }

@Query("SELECT * FROM events WHERE id = :id")
Expand All @@ -744,11 +744,11 @@ public QueryEffect<Employee> getEmployeeByEmail(ById id) {

@ComponentId("all_the_field_types_view")
public static class AllTheFieldTypesView extends View {
@Consume.FromTopic(value = "source")
@Consume.FromTopic(value = "allthetypestopic")
public static class Events extends TableUpdater<EveryType> { }

@Query("SELECT * FROM rows")
public QueryStreamEffect<Employee> getEmployeeByEmail(ById id) {
public QueryStreamEffect<Employee> allRows() {
return queryStreamResult();
}
}
Expand Down

0 comments on commit 2524a3a

Please sign in to comment.