This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TCK model test for event-sourced entities
- Loading branch information
Showing
13 changed files
with
707 additions
and
36 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
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
44 changes: 44 additions & 0 deletions
44
java-support/tck/src/main/java/io/cloudstate/javasupport/tck/JavaSupportTck.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,44 @@ | ||
/* | ||
* Copyright 2019 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 io.cloudstate.javasupport.tck; | ||
|
||
import com.example.shoppingcart.Shoppingcart; | ||
import io.cloudstate.javasupport.CloudState; | ||
import io.cloudstate.javasupport.tck.model.eventsourced.EventSourcedTckModelEntity; | ||
import io.cloudstate.javasupport.tck.model.eventsourced.EventSourcedTwoEntity; | ||
import io.cloudstate.samples.shoppingcart.ShoppingCartEntity; | ||
import io.cloudstate.tck.model.Eventsourced; | ||
|
||
public final class JavaSupportTck { | ||
public static final void main(String[] args) throws Exception { | ||
new CloudState() | ||
.registerEventSourcedEntity( | ||
EventSourcedTckModelEntity.class, | ||
Eventsourced.getDescriptor().findServiceByName("EventSourcedTckModel"), | ||
Eventsourced.getDescriptor()) | ||
.registerEventSourcedEntity( | ||
EventSourcedTwoEntity.class, | ||
Eventsourced.getDescriptor().findServiceByName("EventSourcedTwo")) | ||
.registerEventSourcedEntity( | ||
ShoppingCartEntity.class, | ||
Shoppingcart.getDescriptor().findServiceByName("ShoppingCart"), | ||
com.example.shoppingcart.persistence.Domain.getDescriptor()) | ||
.start() | ||
.toCompletableFuture() | ||
.get(); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...ain/java/io/cloudstate/javasupport/tck/model/eventsourced/EventSourcedTckModelEntity.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,84 @@ | ||
/* | ||
* Copyright 2019 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 io.cloudstate.javasupport.tck.model.eventsourced; | ||
|
||
import io.cloudstate.javasupport.Context; | ||
import io.cloudstate.javasupport.ServiceCall; | ||
import io.cloudstate.javasupport.ServiceCallRef; | ||
import io.cloudstate.javasupport.eventsourced.*; | ||
import io.cloudstate.tck.model.Eventsourced.*; | ||
import java.util.Optional; | ||
|
||
@EventSourcedEntity(persistenceId = "event-sourced-tck-model", snapshotEvery = 5) | ||
public class EventSourcedTckModelEntity { | ||
|
||
private final ServiceCallRef<Request> serviceTwoCall; | ||
|
||
private String state = ""; | ||
|
||
public EventSourcedTckModelEntity(Context context) { | ||
serviceTwoCall = | ||
context | ||
.serviceCallFactory() | ||
.lookup("cloudstate.tck.model.EventSourcedTwo", "Call", Request.class); | ||
} | ||
|
||
@Snapshot | ||
public Persisted snapshot() { | ||
return Persisted.newBuilder().setValue(state).build(); | ||
} | ||
|
||
@SnapshotHandler | ||
public void handleSnapshot(Persisted snapshot) { | ||
state = snapshot.getValue(); | ||
} | ||
|
||
@EventHandler | ||
public void handleEvent(Persisted event) { | ||
state += event.getValue(); | ||
} | ||
|
||
@CommandHandler | ||
public Optional<Response> process(Request request, CommandContext context) { | ||
boolean forwarding = false; | ||
for (RequestAction action : request.getActionsList()) { | ||
switch (action.getActionCase()) { | ||
case EMIT: | ||
context.emit(Persisted.newBuilder().setValue(action.getEmit().getValue()).build()); | ||
break; | ||
case FORWARD: | ||
forwarding = true; | ||
context.forward(serviceTwoRequest(action.getForward().getId())); | ||
break; | ||
case EFFECT: | ||
Effect effect = action.getEffect(); | ||
context.effect(serviceTwoRequest(effect.getId()), effect.getSynchronous()); | ||
break; | ||
case FAIL: | ||
context.fail(action.getFail().getMessage()); | ||
break; | ||
} | ||
} | ||
return forwarding | ||
? Optional.empty() | ||
: Optional.of(Response.newBuilder().setMessage(state).build()); | ||
} | ||
|
||
private ServiceCall serviceTwoRequest(String id) { | ||
return serviceTwoCall.createCall(Request.newBuilder().setId(id).build()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...src/main/java/io/cloudstate/javasupport/tck/model/eventsourced/EventSourcedTwoEntity.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,30 @@ | ||
/* | ||
* Copyright 2019 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 io.cloudstate.javasupport.tck.model.eventsourced; | ||
|
||
import io.cloudstate.javasupport.eventsourced.*; | ||
import io.cloudstate.tck.model.Eventsourced.*; | ||
|
||
@EventSourcedEntity | ||
public class EventSourcedTwoEntity { | ||
public EventSourcedTwoEntity() {} | ||
|
||
@CommandHandler | ||
public Response call(Request request) { | ||
return Response.newBuilder().build(); | ||
} | ||
} |
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,123 @@ | ||
// Copyright 2019 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. | ||
|
||
// | ||
// == Cloudstate TCK model test for event-sourced entities == | ||
// | ||
|
||
syntax = "proto3"; | ||
|
||
package cloudstate.tck.model; | ||
|
||
import "cloudstate/entity_key.proto"; | ||
|
||
option java_package = "io.cloudstate.tck.model"; | ||
|
||
// | ||
// The `EventSourcedTckModel` service should be implemented in the following ways: | ||
// | ||
// - The entity persistence-id must be `event-sourced-tck-model`. | ||
// - Snapshots must be configured for every 5 events. | ||
// - The state of the entity is simply a string. | ||
// - Event and snapshot string values are wrapped in `Persisted` messages. | ||
// - The snapshot handler must set the state to the value of a `Persisted` message. | ||
// - The event handler must append the value of a `Persisted` message to the state string. | ||
// - The `Process` method receives a `Request` message with actions to take. | ||
// - Request actions must be processed in order, and can require emitting events, forwarding, side effects, or failing. | ||
// - The `Process` method must reply with the state in a `Response`, after taking actions, unless forwarding or failing. | ||
// - Forwarding and side effects must always be made to the second service `EventSourcedTwo`. | ||
// | ||
service EventSourcedTckModel { | ||
rpc Process(Request) returns (Response); | ||
} | ||
|
||
// | ||
// The `EventSourcedTwo` service is only for verifying forward actions and side effects. | ||
// The `Call` method is not required to do anything, and may simply return an empty `Response` message. | ||
// | ||
service EventSourcedTwo { | ||
rpc Call(Request) returns (Response); | ||
} | ||
|
||
// | ||
// A `Request` message contains any actions that the entity should process. | ||
// Actions must be processed in order. Any actions after a `Fail` may be ignored. | ||
// | ||
message Request { | ||
string id = 1 [(.cloudstate.entity_key) = true]; | ||
repeated RequestAction actions = 2; | ||
} | ||
|
||
// | ||
// Each `RequestAction` is one of: | ||
// | ||
// - Emit: emit an event, with a given value. | ||
// - Forward: forward to another service, in place of replying with a Response. | ||
// - Effect: add a side effect to another service to the reply. | ||
// - Fail: fail the current `Process` command. | ||
// | ||
message RequestAction { | ||
oneof action { | ||
Emit emit = 1; | ||
Forward forward = 2; | ||
Effect effect = 3; | ||
Fail fail = 4; | ||
} | ||
} | ||
|
||
// | ||
// Emit an event, with the event value in a `Persisted` message. | ||
// | ||
message Emit { | ||
string value = 1; | ||
} | ||
|
||
// | ||
// Replace the response with a forward to `cloudstate.tck.model.EventSourcedTwo/Call`. | ||
// The payload must be a `Request` message with the given `id`. | ||
// | ||
message Forward { | ||
string id = 1; | ||
} | ||
|
||
// | ||
// Add a side effect to the reply, to `cloudstate.tck.model.EventSourcedTwo/Call`. | ||
// The payload must be a `Request` message with the given `id`. | ||
// The side effect should be marked synchronous based on the given `synchronous` value. | ||
// | ||
message Effect { | ||
string id = 1; | ||
bool synchronous = 2; | ||
} | ||
|
||
// | ||
// Fail the current command with the given description `message`. | ||
// | ||
message Fail { | ||
string message = 1; | ||
} | ||
|
||
// | ||
// The `Response` message for the `Process` must contain the current state (after processing actions). | ||
// | ||
message Response { | ||
string message = 1; | ||
} | ||
|
||
// | ||
// The `Persisted` message wraps both snapshot and event values. | ||
// | ||
message Persisted { | ||
string value = 1; | ||
} |
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.