-
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 13 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
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 |
|---|---|---|
|
|
@@ -505,5 +505,4 @@ object ModelBuilder { | |
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
199 changes: 199 additions & 0 deletions
199
...n/java-gen/src/main/scala/com/lightbend/akkasls/codegen/java/ActionTestKitGenerator.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,199 @@ | ||
| /* | ||
| * 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.lightbend.akkasls.codegen | ||
| package java | ||
|
|
||
| import com.google.common.base.Charsets | ||
| import _root_.java.nio.file.{ Files, Path } | ||
|
|
||
| object ActionTestKitGenerator { | ||
| import com.lightbend.akkasls.codegen.SourceGeneratorUtils._ | ||
|
|
||
| def generate( | ||
| service: ModelBuilder.ActionService, | ||
| testSourceDirectory: Path, | ||
| generatedTestSourceDirectory: Path): Iterable[Path] = { | ||
| var generatedFiles: Seq[Path] = Vector.empty | ||
| val packageName = service.fqn.parent.javaPackage | ||
| val className = service.className | ||
|
|
||
| val packagePath = packageAsPath(packageName) | ||
| val testKitPath = generatedTestSourceDirectory.resolve(packagePath.resolve(className + "TestKit.java")) | ||
| testKitPath.getParent.toFile.mkdirs() | ||
| val sourceCode = generateSourceCode(service) | ||
| Files.write(testKitPath, sourceCode.getBytes(Charsets.UTF_8)) | ||
| generatedFiles :+= testKitPath | ||
|
|
||
| val testFilePath = testSourceDirectory.resolve(packagePath.resolve(className + "Test.java")) | ||
| if (!testFilePath.toFile.exists()) { | ||
| testFilePath.getParent.toFile.mkdirs() | ||
| Files.write(testFilePath, generateTestSourceCode(service).getBytes(Charsets.UTF_8)) | ||
| generatedFiles :+= testFilePath | ||
| } | ||
|
|
||
| generatedFiles | ||
| } | ||
|
|
||
| private[codegen] def generateSourceCode(service: ModelBuilder.ActionService): String = { | ||
|
|
||
| val packageName = service.fqn.parent.javaPackage | ||
| val className = service.className | ||
|
|
||
| val imports = generateImports( | ||
| commandTypes(service.commands), | ||
| "", | ||
| otherImports = Seq( | ||
| "java.util.ArrayList", | ||
| "java.util.List", | ||
| "java.util.function.Function", | ||
| "java.util.Optional", | ||
| s"$packageName.$className", | ||
| "com.akkaserverless.javasdk.action.ActionCreationContext", | ||
| "com.akkaserverless.javasdk.testkit.ActionResult", | ||
| "com.akkaserverless.javasdk.testkit.impl.ActionResultImpl", | ||
| "com.akkaserverless.javasdk.impl.action.ActionEffectImpl", | ||
| "com.akkaserverless.javasdk.testkit.impl.StubActionCreationContext", | ||
| "com.akkaserverless.javasdk.testkit.impl.StubActionContext")) | ||
|
|
||
| val testKitClassName = s"${className}TestKit" | ||
|
|
||
| s"""$managedComment | ||
| |package ${service.fqn.parent.javaPackage}; | ||
| | | ||
| |$imports | ||
| | | ||
| |public final class $testKitClassName { | ||
| | | ||
| | private Function<ActionCreationContext, $className> actionFactory; | ||
| | | ||
| | private $className createAction() { | ||
| | $className action = actionFactory.apply(new StubActionCreationContext()); | ||
| | action._internalSetActionContext(Optional.of(new StubActionContext())); | ||
| | return action; | ||
| | }; | ||
| | | ||
| | public static $testKitClassName of(Function<ActionCreationContext, $className> actionFactory) { | ||
| | return new $testKitClassName(actionFactory); | ||
| | } | ||
| | | ||
| | private $testKitClassName(Function<ActionCreationContext, $className> actionFactory) { | ||
| | this.actionFactory = actionFactory; | ||
| | } | ||
| | | ||
| | private <E> ActionResult<E> interpretEffects(Action.Effect<E> effect) { | ||
| | return new ActionResultImpl(effect); | ||
| | } | ||
| | | ||
| | ${Format.indent(generateServices(service), 2)} | ||
| | | ||
| |} | ||
| |""".stripMargin | ||
| } | ||
|
|
||
| private[codegen] def generateTestSourceCode(service: ModelBuilder.ActionService): String = { | ||
| val className = service.className | ||
| val packageName = service.fqn.parent.javaPackage | ||
| val imports = generateImports( | ||
| commandTypes(service.commands), | ||
| "", | ||
| otherImports = Seq( | ||
| s"$packageName.$className", | ||
| s"${packageName}.${className}TestKit", | ||
| "com.akkaserverless.javasdk.testkit.ActionResult", | ||
| "org.junit.Test", | ||
| "static org.junit.Assert.*")) | ||
|
|
||
| val testClassName = s"${className}Test" | ||
|
|
||
| s"""$unmanagedComment | ||
| |package ${service.fqn.parent.javaPackage}; | ||
| | | ||
| |$imports | ||
| | | ||
| |public class $testClassName { | ||
| | | ||
| | @Test | ||
| | public void exampleTest() { | ||
| | CounterJournalToTopicActionTestKit testKit = CounterJournalToTopicActionTestKit.of(CounterJournalToTopicAction::new); | ||
| | // use the testkit to execute a command | ||
| | // ActionResult<SomeResponse> result = testKit.someOperation(SomeRequest); | ||
| | // verify the response | ||
| | // SomeResponse actualResponse = result.getReply(); | ||
| | // assertEquals(expectedResponse, actualResponse); | ||
| | } | ||
| | | ||
| | ${Format.indent(generateTestingServices(service), 2)} | ||
| | | ||
| |} | ||
| |""".stripMargin | ||
| } | ||
|
|
||
| def generateServices(service: ModelBuilder.ActionService): String = { | ||
| require(!service.commands.isEmpty, "empty `commands` not allowed") | ||
|
|
||
| service.commands | ||
| .map { command => | ||
| s"""|public ActionResult<${selectOutput(command)}> ${lowerFirst(command.name)}(${selectInput( | ||
| command)} ${lowerFirst(command.inputType.protoName)}) { | ||
| | Action.Effect<${selectOutput(command)}> effect = createAction().${lowerFirst(command.name)}(${lowerFirst( | ||
| command.inputType.protoName)}); | ||
| | return interpretEffects(effect); | ||
| |} | ||
| |""".stripMargin + "\n" | ||
| } | ||
| .mkString("") | ||
| } | ||
|
|
||
| def generateTestingServices(service: ModelBuilder.ActionService): String = { | ||
| service.commands | ||
| .map { command => | ||
| s"""|@Test | ||
| |public void ${lowerFirst(command.name)}Test() { | ||
| | ${service.className}TestKit testKit = ${service.className}TestKit.of(${service.className}::new); | ||
| | // ActionResult<${selectOutput(command)}> result = testKit.${lowerFirst(command.name)}(${selectInput( | ||
| command)}.newBuilder()...build()); | ||
| |} | ||
| |""".stripMargin + "\n" | ||
| } | ||
| .mkString("") | ||
| } | ||
|
|
||
| def selectOutput(command: ModelBuilder.Command): String = { | ||
| val parent = command.outputType.parent | ||
| s"${filterJavaOuterClassname(parent.javaOuterClassnameOption)}${command.outputType.name}" | ||
|
|
||
| } | ||
|
|
||
| def selectInput(command: ModelBuilder.Command): String = { | ||
| val parent = command.inputType.parent | ||
| s"${filterJavaOuterClassname(parent.javaOuterClassnameOption)}${command.inputType.name}" | ||
|
|
||
| } | ||
|
|
||
| def filterJavaOuterClassname(javaOuterClassnameOption: Option[String]): String = { | ||
| javaOuterClassnameOption match { | ||
| case None => "" | ||
| case Some(value) => | ||
| if (value == "EmptyProto" || value == "AnyProto") | ||
| "" //TODO review this. Might not be needed if proto are 'well' defined. | ||
| // original .proto file on test might be badly defined | ||
| // e.g. `rpc Ignore(google.protobuf.Any) returns (google.protobuf.Empty)` might have to be `rpc Ignore(com.google.protobuf.Any) returns (com.google.protobuf.Empty)` | ||
| else value + "." | ||
| } | ||
| } | ||
|
|
||
| } | ||
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
Binary file added
BIN
+5.87 KB
.../java-gen/src/test/resources/descriptor-sets/java-eventsourced-counter-user-function.desc
Binary file not shown.
Oops, something went wrong.
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.