-
Notifications
You must be signed in to change notification settings - Fork 39
Asserting different effects #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
franciscolopezsancho
merged 29 commits into
lightbend:main
from
franciscolopezsancho:asserting-different-effects
Oct 6, 2021
Merged
Changes from 3 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
4d1d018
created stub and test
franciscolopezsancho 99c870b
first test passing
franciscolopezsancho f5cd830
following suggestions
franciscolopezsancho ff352a7
following review suggestion
franciscolopezsancho 0010b1c
added ActionTestkitGenerator
franciscolopezsancho d0edaef
changing order expected,actualValue
franciscolopezsancho df0528a
Codegen already functional
franciscolopezsancho 28a9b8a
Added test example generation
franciscolopezsancho f56765a
Merge branch 'main' of github.com:franciscolopezsancho/akkaserverless…
franciscolopezsancho 91c01c0
adding docs
franciscolopezsancho 742af2c
adding FIXME
franciscolopezsancho f240b9d
formatting
franciscolopezsancho 1dd9b12
adding tag to docs
franciscolopezsancho b862130
Merge branch 'main' of github.com:franciscolopezsancho/akkaserverless…
franciscolopezsancho ee4f266
adding missing import
franciscolopezsancho e79b1c3
changing selectInput/Output from ModelBuilder.Commands
franciscolopezsancho 75deb3d
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho e417a83
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho b221a9e
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho 997678f
Update samples/java-eventsourced-counter/src/test/java/com/example/ac…
franciscolopezsancho 1fa4764
adding codegen for RPCs streaming, client and server
franciscolopezsancho fb1ee72
adding comment after imports
franciscolopezsancho f4bb328
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho c65d83e
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho c0b2e8a
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho 10a7894
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho eee315b
adding suggestions
franciscolopezsancho ba010e0
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho 34524db
Update docs/src/modules/java/pages/actions.adoc
franciscolopezsancho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...tsourced-counter/src/main/java/com/example/domain/CounterJournalToTopicActionTestKit.java
This file contains hidden or 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,61 @@ | ||
| package com.example.domain; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Function; | ||
|
|
||
| import com.akkaserverless.javasdk.action.Action; | ||
| import com.akkaserverless.javasdk.action.ActionCreationContext; | ||
| import com.akkaserverless.javasdk.testkit.EventSourcedResult; | ||
| import com.akkaserverless.javasdk.testkit.ActionResult; | ||
| import com.akkaserverless.javasdk.testkit.impl.ActionResultImpl; | ||
| import com.akkaserverless.javasdk.impl.action.ActionEffectImpl; | ||
| import com.example.actions.CounterJournalToTopicAction; | ||
| import com.akkaserverless.javasdk.testkit.impl.StubActionCreationContext; | ||
| import com.akkaserverless.javasdk.testkit.impl.StubActionContext; | ||
| import com.example.actions.CounterTopicApi; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * TestKit for unit testing Counter | ||
| */ | ||
| public final class CounterJournalToTopicActionTestKit { | ||
|
|
||
| private Function<ActionCreationContext, CounterJournalToTopicAction> actionFactory; | ||
|
|
||
| private CounterJournalToTopicAction createAction() { | ||
| CounterJournalToTopicAction action = actionFactory.apply(new StubActionCreationContext()); | ||
| action._internalSetActionContext(Optional.of(new StubActionContext())); | ||
| return action; | ||
| }; | ||
|
|
||
| /** | ||
| * Create a testkit instance of Counter with a specific action id. | ||
| */ | ||
| public static CounterJournalToTopicActionTestKit of(Function<ActionCreationContext, CounterJournalToTopicAction> actionFactory) { | ||
| return new CounterJournalToTopicActionTestKit(actionFactory); | ||
| } | ||
|
|
||
| /** Construction is done through the static CounterTestKit.of-methods */ | ||
| private CounterJournalToTopicActionTestKit(Function<ActionCreationContext, CounterJournalToTopicAction> actionFactory) { | ||
| this.actionFactory = actionFactory; | ||
| } | ||
|
|
||
| private <E> ActionResult<E> interpretEffects(Action.Effect<E> effect) { | ||
| return new ActionResultImpl(effect); | ||
| } | ||
|
|
||
| public ActionResult<CounterTopicApi.Increased> increase(CounterDomain.ValueIncreased event) { | ||
| Action.Effect<CounterTopicApi.Increased> effect = createAction().increase(event); | ||
| return interpretEffects(effect); | ||
| } | ||
|
|
||
| //look at ACtionEffectImpl | ||
|
|
||
| public Action.Effect<CounterTopicApi.Decreased> decrease(CounterDomain.ValueDecreased event) { | ||
| Action.Effect<CounterTopicApi.Decreased> effect = createAction().decrease(event); | ||
| return effect; | ||
| } | ||
|
|
||
|
|
||
| } |
25 changes: 25 additions & 0 deletions
25
...ventsourced-counter/src/test/java/com/example/domain/CounterJournalToTopicActionTest.java
This file contains hidden or 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,25 @@ | ||
| package com.example.domain; | ||
|
|
||
| import com.akkaserverless.javasdk.action.Action; | ||
| import com.akkaserverless.javasdk.impl.action.ActionEffectImpl; | ||
| import com.akkaserverless.javasdk.testkit.ActionResult; | ||
| import com.example.actions.CounterTopicApi; | ||
| import com.example.actions.CounterJournalToTopicAction; | ||
| import com.google.protobuf.Empty; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| public class CounterJournalToTopicActionTest { | ||
|
|
||
| @Test | ||
| public void increaseTest() { | ||
| CounterJournalToTopicActionTestKit testKit = CounterJournalToTopicActionTestKit.of(CounterJournalToTopicAction::new); | ||
| int valueToincrease = 1; | ||
| ActionResult<CounterTopicApi.Increased> result = testKit.increase(CounterDomain.ValueIncreased.newBuilder().setValue(valueToincrease).build()); | ||
| assertTrue(result.isReply()); | ||
| CounterTopicApi.Increased replyMessage = result.getReplyMessage(); | ||
| assertEquals(replyMessage.getValue(),valueToincrease); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
51 changes: 51 additions & 0 deletions
51
testkit-java/src/main/java/com/akkaserverless/javasdk/testkit/ActionResult.java
This file contains hidden or 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,51 @@ | ||
| /* | ||
| * 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.akkaserverless.javasdk.testkit; | ||
|
|
||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.NoSuchElementException; | ||
| import com.akkaserverless.javasdk.ServiceCall; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import com.akkaserverless.javasdk.action.Action; | ||
|
|
||
| public interface ActionResult<T> { | ||
|
|
||
| /** @return true if the call had an effect with a reply, false if not */ | ||
| boolean isReply(); | ||
|
|
||
| T getReplyMessage(); | ||
|
franciscolopezsancho marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** @return true if the call was forwarded, false if not */ | ||
| boolean isForward(); | ||
|
|
||
| ServiceCall getForwardServiceCall(); | ||
|
|
||
| // TODO rewrite name. Async doesn't seem very descriptive. Are the rest sync? | ||
| /** @return true if the call was async, false if not */ | ||
| boolean isAsync(); | ||
|
|
||
| CompletableFuture<Action.Effect<T>> getAsyncEffect(); | ||
|
franciscolopezsancho marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** @return true if the call was an error, false if not */ | ||
| boolean isError(); | ||
|
|
||
| String getErrorDescription(); | ||
|
|
||
| /** @return true if the call had a noReply effect, false if not */ | ||
| boolean isNoReply(); | ||
| } | ||
96 changes: 96 additions & 0 deletions
96
testkit-java/src/main/scala/com/akkaserverless/javasdk/testkit/impl/ActionResultImpl.scala
This file contains hidden or 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,96 @@ | ||
| /* | ||
| * 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.akkaserverless.javasdk.testkit.impl | ||
|
|
||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import com.akkaserverless.javasdk.testkit.ActionResult | ||
| import com.akkaserverless.javasdk.impl.action.ActionEffectImpl | ||
| import com.akkaserverless.javasdk.action.Action | ||
| import com.akkaserverless.javasdk.ServiceCall | ||
| import java.util.concurrent.CompletableFuture; | ||
| import scala.concurrent.Future; | ||
| import scala.compat.java8.FutureConverters._ | ||
|
|
||
| final class ActionResultImpl[T](effect: Action.Effect[T]) extends ActionResult[T] { | ||
|
|
||
| /** @return true if the call had an effect with a reply, false if not */ | ||
| def isReply(): Boolean = effect.isInstanceOf[ActionEffectImpl.ReplyEffect[T]] | ||
|
|
||
| def getReplyMessage(): T = { | ||
| val reply = getEffectOfType(classOf[ActionEffectImpl.ReplyEffect[T]]) | ||
| reply.msg | ||
| } | ||
|
|
||
| //TODO add metadata?? | ||
|
|
||
| /** @return true if the call was forwarded, false if not */ | ||
| def isForward(): Boolean = effect.isInstanceOf[ActionEffectImpl.ForwardEffect[T]] | ||
|
|
||
| def getForwardServiceCall(): ServiceCall = { | ||
| val forward = getEffectOfType(classOf[ActionEffectImpl.ForwardEffect[T]]) | ||
| forward.serviceCall | ||
| } | ||
|
|
||
| // TODO rewrite | ||
| /** @return true if the call was async, false if not */ | ||
| def isAsync(): Boolean = effect.isInstanceOf[ActionEffectImpl.AsyncEffect[T]] | ||
|
|
||
| def getAsyncEffect(): CompletableFuture[Action.Effect[T]] = { | ||
| val async = getEffectOfType(classOf[ActionEffectImpl.AsyncEffect[T]]) | ||
| async.effect.toJava.toCompletableFuture | ||
| } | ||
|
|
||
| /** @return true if the call was an error, false if not */ | ||
| def isError(): Boolean = effect.isInstanceOf[ActionEffectImpl.ErrorEffect[T]] | ||
|
|
||
| def getErrorDescription(): String = { | ||
| val error = getEffectOfType(classOf[ActionEffectImpl.ErrorEffect[T]]) | ||
| error.description | ||
| } | ||
|
|
||
| /** @return true if the call had a noReply effect, false if not */ | ||
| def isNoReply(): Boolean = effect.isInstanceOf[ActionEffectImpl.NoReply[T]] | ||
|
|
||
| /** | ||
| * Look at the next effect and verify that it is of type E or fail if not or if there is no next effect. If successful | ||
| * this consumes the effect, so that the next call to this method looks at the next effect from here. | ||
| * | ||
| * @return | ||
| * The next effect if it is of type E, for additional assertions. | ||
| */ | ||
| private def getEffectOfType[E](expectedClass: Class[E]): E = { | ||
| if (expectedClass.isInstance(effect)) effect.asInstanceOf[E] | ||
| else | ||
| throw new NoSuchElementException( | ||
| "expected effect type [" + expectedClass.getName + "] but found [" + effect.getClass.getName + "]") | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
testkit-java/src/main/scala/com/akkaserverless/javasdk/testkit/impl/StubActionContext.scala
This file contains hidden or 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,41 @@ | ||
| /* | ||
| * 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.akkaserverless.javasdk.testkit.impl; | ||
|
|
||
| import com.akkaserverless.javasdk.action.ActionContext; | ||
| import com.akkaserverless.javasdk.Metadata; | ||
| import java.util.Optional; | ||
| import com.akkaserverless.javasdk.ServiceCallFactory | ||
| import com.akkaserverless.javasdk.valueentity.ValueEntityContext | ||
| import com.akkaserverless.javasdk.action.ActionCreationContext | ||
| import akka.stream.Materializer | ||
|
|
||
| /** Context for action calls. */ | ||
| final class StubActionContext extends ActionContext { | ||
|
|
||
| override def metadata() = Metadata.EMPTY | ||
|
franciscolopezsancho marked this conversation as resolved.
Outdated
|
||
|
|
||
| override def eventSubject() = Optional.of("testkitkat") | ||
|
franciscolopezsancho marked this conversation as resolved.
Outdated
|
||
|
|
||
| override def serviceCallFactory: ServiceCallFactory = TestKitServiceCallFactory | ||
|
|
||
| override def getGrpcClient[T](clientClass: Class[T], service: String): T = | ||
| throw new UnsupportedOperationException("Testing logic using a gRPC client is not possible with the testkit") | ||
| override def materializer(): Materializer = throw new UnsupportedOperationException( | ||
| "Accessing the materializer from testkit not supported yet") | ||
|
|
||
| } | ||
33 changes: 33 additions & 0 deletions
33
...va/src/main/scala/com/akkaserverless/javasdk/testkit/impl/StubActionCreationContext.scala
This file contains hidden or 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 @@ | ||
| /* | ||
| * 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.akkaserverless.javasdk.testkit.impl | ||
|
|
||
| import com.akkaserverless.javasdk.ServiceCallFactory | ||
| import com.akkaserverless.javasdk.valueentity.ValueEntityContext | ||
| import com.akkaserverless.javasdk.action.ActionCreationContext | ||
| import akka.stream.Materializer | ||
|
|
||
| /** | ||
| * INTERNAL API Used by the generated testkit | ||
| */ | ||
| final class StubActionCreationContext extends ActionCreationContext { | ||
| override def serviceCallFactory: ServiceCallFactory = TestKitServiceCallFactory | ||
| override def getGrpcClient[T](clientClass: Class[T], service: String): T = | ||
| throw new UnsupportedOperationException("Testing logic using a gRPC client is not possible with the testkit") | ||
| override def materializer(): Materializer = throw new UnsupportedOperationException( | ||
| "Accessing the materializer from testkit not supported yet") | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.