From 213a871d46e9f6d72eb10c4afb25e06317409abe Mon Sep 17 00:00:00 2001 From: Philip Yurchuk Date: Thu, 1 Sep 2022 18:44:57 -0700 Subject: [PATCH] samkgg-1 Add Spock Added dependencies in Gradle for Spock and Groovy, removed jUnit. Replaced jUnit test with identical Spock test. Updated README. --- .idea/checkstyle-idea.xml | 22 ++++++++-------- HelloWorldFunction/build.gradle | 7 +++-- .../src/test/groovy/helloworld/AppSpec.groovy | 26 +++++++++++++++++++ .../src/test/java/helloworld/AppTest.java | 23 ---------------- README.md | 18 +++++-------- 5 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 HelloWorldFunction/src/test/groovy/helloworld/AppSpec.groovy delete mode 100644 HelloWorldFunction/src/test/java/helloworld/AppTest.java diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml index f75a1f8..d6655cd 100644 --- a/.idea/checkstyle-idea.xml +++ b/.idea/checkstyle-idea.xml @@ -1,16 +1,16 @@ - - \ No newline at end of file diff --git a/HelloWorldFunction/build.gradle b/HelloWorldFunction/build.gradle index dc3800a..7e7f5b0 100644 --- a/HelloWorldFunction/build.gradle +++ b/HelloWorldFunction/build.gradle @@ -2,6 +2,7 @@ plugins { id 'application' id 'org.graalvm.buildtools.native' id 'org.jetbrains.kotlin.jvm' version '1.7.10' + id 'groovy' } repositories { @@ -21,9 +22,11 @@ dependencies { implementation 'com.amazonaws:aws-lambda-java-events:3.11.0' implementation 'com.amazonaws:aws-lambda-java-runtime-interface-client:2.1.1' - testImplementation(platform("org.junit:junit-bom:${junitVersion}")) - testImplementation('org.junit.jupiter:junit-jupiter') implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + + testImplementation platform('org.spockframework:spock-bom:2.2-groovy-4.0') + testImplementation 'org.spockframework:spock-core' + testImplementation 'org.apache.groovy:groovy-all:4.0.4' } graalvmNative { diff --git a/HelloWorldFunction/src/test/groovy/helloworld/AppSpec.groovy b/HelloWorldFunction/src/test/groovy/helloworld/AppSpec.groovy new file mode 100644 index 0000000..283a5bd --- /dev/null +++ b/HelloWorldFunction/src/test/groovy/helloworld/AppSpec.groovy @@ -0,0 +1,26 @@ +package helloworld + +import static org.junit.jupiter.api.Assertions.* +import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse +import spock.lang.Specification + +/** + * Created by Philip Yurchuk on 8/31/2022. + */ +class AppSpec extends Specification { + + def "Spock works"() { + given: + App app = new App() + when: + APIGatewayV2HTTPResponse result = app.handleRequest(null, null) + then: + assertEquals(result.getStatusCode(), 200) + assertEquals(result.headers['Content-Type'], 'application/json') + String content = result.getBody() + assertNotNull(content) + assertTrue(content.contains('"message"')) + assertTrue(content.contains('"hello world"')) + assertTrue(content.contains('"location"')) + } +} diff --git a/HelloWorldFunction/src/test/java/helloworld/AppTest.java b/HelloWorldFunction/src/test/java/helloworld/AppTest.java deleted file mode 100644 index bfe3703..0000000 --- a/HelloWorldFunction/src/test/java/helloworld/AppTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package helloworld; - -import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; -import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse; -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class AppTest { - @Test - public void successfulResponse() { - App app = new App(); - APIGatewayV2HTTPResponse result = app.handleRequest(null, null); - assertEquals(result.getStatusCode(), 200); - assertEquals(result.getHeaders().get("Content-Type"), "application/json"); - String content = result.getBody(); - assertNotNull(content); - assertTrue(content.contains("\"message\"")); - assertTrue(content.contains("\"hello world\"")); - assertTrue(content.contains("\"location\"")); - } -} diff --git a/README.md b/README.md index fa2a8db..bea0a24 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,11 @@ This project aims to demonstrate and document an AWS Lambda project using the above technologies as they are not yet common. It is a "learning in public" project. If you're interested in learning these technologies, follow along. If you're experienced and see anything that is not appropriate for a production SAM application, please point it out. No ego -here. +here. -The goal of this project is to document how to overcome the rough edges and steep learning curve when using Java and -GraalVM with Lambda. I'm also running Windows which is probably hard mode. +[How and why these technologies were selected.](https://philip.yurchuk.com/software/samkgg-aws-sam-kotlin-graalvm-gradle/) + +[Lessons Learned](https://github.com/madeupname/samkgg/wiki) ### Documentation @@ -18,6 +19,8 @@ I have all Java-related tooling installed via [SDKMAN](https://sdkman.io/). It e * [Gradle](https://gradle.org) version 7.3.3 * [GraalVM Gradle Plugin](https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html) * [Kotlin](https://kotlinlang.org) version 1.7.10 (on JDK 17) +* [Spock](https://docs.spockframework.org/) +* [Groovy](https://groovy-lang.org) * [Docker](https://docs.docker.com/) * [Windows Subsystem for Linux (WSL) 2](https://docs.microsoft.com/en-us/windows/wsl/setup/environment) for running Docker on Windows @@ -63,15 +66,6 @@ master branch. When a new CLI is released, it appears to pick up the latest temp * Converted App.java to Kotlin. Removed code to call external service since it adds unnecessary run time. * Set language levels to JVM 17 and Kotlin 1.7. -### FYI -* IntelliJ will grumble about not having Gradle files in the root, but it looks like there is no way to make this a traditional Gradle multi-project build. Each function needs its own Gradle build. - * https://github.com/aws/aws-sam-cli/issues/3227 -* In lieu of a multi-project build, you can add more build logic in via Makefiles. Yes, Makefiles: https://makefiletutorial.com -* Despite the GraalVM term "native image" the SAM package type is still zip. Hence, Lambda layers are allowed. -* Similarly, the use-container flag passed to "sam build" means it is going to build the functions inside a Docker container. Since it's building a binary for a specific platform, the build runs in that target platform with the necessary dependencies. -* The Kotlin stdlib JDK 8 dependency threw me, but that's the latest version. It still works with JVM 17 targets. - * Explained here: https://mbonnin.medium.com/the-different-kotlin-stdlibs-explained-83d7c6bf293 - ## Original README This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes support for [Graal VM](https://www.graalvm.org), and the following files and folders.