Skip to content

Commit

Permalink
fix: supporting compound key in component client
Browse files Browse the repository at this point in the history
  • Loading branch information
aludwiko committed Nov 3, 2023
1 parent 81265fd commit 7f1e6b5
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.example.wiring.valueentities.customer.CustomerEntity;
import com.example.wiring.valueentities.headers.ForwardHeadersValueEntity;
import com.example.wiring.valueentities.user.AssignedCounterEntity;
import com.example.wiring.valueentities.user.CompoundIdCounterEntity;
import com.example.wiring.valueentities.user.User;
import com.example.wiring.valueentities.user.UserEntity;
import com.example.wiring.valueentities.user.UserSideEffect;
Expand Down Expand Up @@ -70,6 +71,7 @@

import static java.time.temporal.ChronoUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.awaitility.Awaitility.await;

@SpringBootTest(classes = Main.class)
Expand Down Expand Up @@ -503,6 +505,35 @@ public void verifyFindUsersByNameStreaming() {
new IsEqual(2));
}

@Test
public void shouldInvokeValueEntityWithCompoundKey() {
//given
execute(componentClient.forValueEntity("1", "2")
.call(CompoundIdCounterEntity::set).params(10));

//when
Integer result = execute(componentClient.forValueEntity("1", "2")
.call(CompoundIdCounterEntity::get));

//then
assertThat(result).isEqualTo(10);
}

@Test
public void shouldFailInvokeValueEntityWithWrongCompoundKey() {
assertThatThrownBy(() -> {
execute(componentClient.forValueEntity("1")
.call(CompoundIdCounterEntity::set).params(10));
}).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Expecting 2 instead of 1 when calling [set] method. Provide values for [id_part_1, id_part_2] ids.");

assertThatThrownBy(() -> {
execute(componentClient.forValueEntity("1", "1", "3")
.call(CompoundIdCounterEntity::set).params(10));
}).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Expecting 2 instead of 3 when calling [set] method. Provide values for [id_part_1, id_part_2] ids.");
}

@Test
public void verifyMultiTableViewForUserCounters() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2021 Lightbend Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.wiring.valueentities.user;

import kalix.javasdk.annotations.Id;
import kalix.javasdk.annotations.TypeId;
import kalix.javasdk.valueentity.ValueEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@TypeId("compound-id-counter")
@Id({"id_part_1", "id_part_2"})
@RequestMapping("/compound-id-counter")
public class CompoundIdCounterEntity extends ValueEntity<Integer> {


@Override
public Integer emptyState() {
return 0;
}

@PostMapping("/{id_part_1}/{id_part_2}/set/{value}")
public Effect<String> set(@PathVariable Integer value) {
System.out.println(commandContext().entityId());
return effects().updateState(value).thenReply("OK");
}

@GetMapping("/{id_part_1}/{id_part_2}")
public Effect<Integer> get() {
System.out.println(commandContext().entityId());
return effects().reply(currentState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import kalix.javasdk.action.Action;
import kalix.spring.KalixClient;

import java.util.Optional;
import java.util.List;

public class ActionCallBuilder {

Expand All @@ -57,153 +57,153 @@ public ActionCallBuilder(KalixClient kalixClient) {
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, R> DeferredCall<Any, R> call(Function<T, Action.Effect<R>> methodRef) {
return ComponentCall.noParams(kalixClient, methodRef, Optional.empty());
return ComponentCall.noParams(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, R> ComponentCall<A1, R> call(Function2<T, A1, Action.Effect<R>> methodRef) {
return new ComponentCall<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, R> ComponentCall2<A1, A2, R> call(Function3<T, A1, A2, Action.Effect<R>> methodRef) {
return new ComponentCall2<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall2<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, R> ComponentCall3<A1, A2, A3, R> call(Function4<T, A1, A2, A3, Action.Effect<R>> methodRef) {
return new ComponentCall3<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall3<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, R> ComponentCall4<A1, A2, A3, A4, R> call(Function5<T, A1, A2, A3, A4, Action.Effect<R>> methodRef) {
return new ComponentCall4<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall4<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, R> ComponentCall5<A1, A2, A3, A4, A5, R> call(Function6<T, A1, A2, A3, A4, A5, Action.Effect<R>> methodRef) {
return new ComponentCall5<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall5<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, R> ComponentCall6<A1, A2, A3, A4, A5, A6, R> call(Function7<T, A1, A2, A3, A4, A5, A6, Action.Effect<R>> methodRef) {
return new ComponentCall6<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall6<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, R> ComponentCall7<A1, A2, A3, A4, A5, A6, A7, R> call(Function8<T, A1, A2, A3, A4, A5, A6, A7, Action.Effect<R>> methodRef) {
return new ComponentCall7<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall7<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, R> ComponentCall8<A1, A2, A3, A4, A5, A6, A7, A8, R> call(Function9<T, A1, A2, A3, A4, A5, A6, A7, A8, Action.Effect<R>> methodRef) {
return new ComponentCall8<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall8<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, R> ComponentCall9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R> call(Function10<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, Action.Effect<R>> methodRef) {
return new ComponentCall9<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall9<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R> ComponentCall10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R> call(Function11<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, Action.Effect<R>> methodRef) {
return new ComponentCall10<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall10<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, R> ComponentCall11<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, R> call(Function12<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, Action.Effect<R>> methodRef) {
return new ComponentCall11<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall11<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, R> ComponentCall12<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, R> call(Function13<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, Action.Effect<R>> methodRef) {
return new ComponentCall12<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall12<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, R> ComponentCall13<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, R> call(Function14<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, Action.Effect<R>> methodRef) {
return new ComponentCall13<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall13<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, R> ComponentCall14<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, R> call(Function15<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, Action.Effect<R>> methodRef) {
return new ComponentCall14<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall14<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, R> ComponentCall15<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, R> call(Function16<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, Action.Effect<R>> methodRef) {
return new ComponentCall15<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall15<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, R> ComponentCall16<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, R> call(Function17<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, Action.Effect<R>> methodRef) {
return new ComponentCall16<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall16<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, R> ComponentCall17<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, R> call(Function18<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, Action.Effect<R>> methodRef) {
return new ComponentCall17<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall17<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, R> ComponentCall18<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, R> call(Function19<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, Action.Effect<R>> methodRef) {
return new ComponentCall18<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall18<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, R> ComponentCall19<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, R> call(Function20<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, Action.Effect<R>> methodRef) {
return new ComponentCall19<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall19<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, R> ComponentCall20<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, R> call(Function21<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, Action.Effect<R>> methodRef) {
return new ComponentCall20<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall20<>(kalixClient, methodRef, List.of());
}

/**
* Pass in an Action method reference annotated as a REST endpoint, e.g. <code>MyAction::create</code>
*/
public <T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, R> ComponentCall21<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, R> call(Function22<T, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, Action.Effect<R>> methodRef) {
return new ComponentCall21<>(kalixClient, methodRef, Optional.empty());
return new ComponentCall21<>(kalixClient, methodRef, List.of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import kalix.spring.KalixClient;

import java.util.List;

/**
* Utility to send requests to other Kalix components by composing a DeferredCall. To compose a call:
* 1. select component type (and pass id if necessary)
Expand Down Expand Up @@ -67,6 +69,15 @@ public ValueEntityCallBuilder forValueEntity(String valueEntityId) {
return new ValueEntityCallBuilder(kalixClient, valueEntityId);
}

/**
* Select ValueEntity as a call target component.
*
* @param valueEntityIds - compound entity ids used to create a call.
*/
public ValueEntityCallBuilder forValueEntity(String... valueEntityIds) {
return new ValueEntityCallBuilder(kalixClient, List.of(valueEntityIds));
}

/**
* Select EventSourcedEntity as a call target component.
* <p>
Expand All @@ -85,6 +96,15 @@ public EventSourcedEntityCallBuilder forEventSourcedEntity(String eventSourcedEn
return new EventSourcedEntityCallBuilder(kalixClient, eventSourcedEntityId);
}

/**
* Select EventSourcedEntity as a call target component.
*
* @param eventSourcedEntityIds - compound entity ids used to create a call.
*/
public EventSourcedEntityCallBuilder forEventSourcedEntity(String... eventSourcedEntityIds) {
return new EventSourcedEntityCallBuilder(kalixClient, List.of(eventSourcedEntityIds));
}

/**
* Select Workflow as a call target component.
* <p>
Expand All @@ -103,6 +123,15 @@ public WorkflowCallBuilder forWorkflow(String workflowId) {
return new WorkflowCallBuilder(kalixClient, workflowId);
}

/**
* Select Workflow as a call target component.
*
* @param workflowIds - compound workflow ids used to create a call.
*/
public WorkflowCallBuilder forWorkflow(String... workflowIds) {
return new WorkflowCallBuilder(kalixClient, List.of(workflowIds));
}

/**
* Select View as a call target component.
*/
Expand Down
Loading

0 comments on commit 7f1e6b5

Please sign in to comment.