Skip to content

Commit

Permalink
adds more assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored Nov 22, 2023
1 parent 3d85bf6 commit 68e408a
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 141 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/aws-lambda-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,3 @@ jobs:

- name: Build with Maven
run: ./mvnw clean verify

- if: ${{ github.ref == 'refs/heads/main' }}
name: Build and Publish Docker Image
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=${{ secrets.DOCKER_USERNAME }}/aws-lambda-project-${{ matrix.java }}
docker push ${{ secrets.DOCKER_USERNAME }}/aws-lambda-project-${{ matrix.java }}
2 changes: 2 additions & 0 deletions aws-lambda-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# aws-lambda

280 changes: 151 additions & 129 deletions aws-lambda-project/pom.xml
Original file line number Diff line number Diff line change
@@ -1,135 +1,157 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.awsspring</groupId>
<artifactId>aws-lambda-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aws-lambda-project-project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.4</spring-cloud.version>
<wrapper.version>1.0.29.RELEASE</wrapper.version>
<aws.java.sdk.version>2.21.24</aws.java.sdk.version>
<spring-cloud-function.version>4.0.4</spring-cloud-function.version>
<testcontainers.version>1.19.3</testcontainers.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0-RC2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.awsspring</groupId>
<artifactId>aws-lambda-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aws-lambda-project-project</name>
<description>Demo project for Spring Boot</description>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>lambda</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>${aws.java.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.0-RC1</spring-cloud.version>
<wrapper.version>1.0.31.RELEASE</wrapper.version>
<aws.java.sdk.version>2.21.24</aws.java.sdk.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.31.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>lambda</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-invoker</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>${aws.java.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>${wrapper.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.example.springcloudfunctionaws;

import io.restassured.RestAssured;
import io.restassured.response.ResponseBody;
import org.apache.commons.io.FileUtils;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
Expand All @@ -22,7 +24,16 @@
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.lambda.LambdaClient;
import software.amazon.awssdk.services.lambda.model.*;
import software.amazon.awssdk.services.lambda.model.CreateFunctionRequest;
import software.amazon.awssdk.services.lambda.model.CreateFunctionResponse;
import software.amazon.awssdk.services.lambda.model.CreateFunctionUrlConfigRequest;
import software.amazon.awssdk.services.lambda.model.CreateFunctionUrlConfigResponse;
import software.amazon.awssdk.services.lambda.model.Environment;
import software.amazon.awssdk.services.lambda.model.FunctionCode;
import software.amazon.awssdk.services.lambda.model.FunctionUrlAuthType;
import software.amazon.awssdk.services.lambda.model.GetFunctionConfigurationRequest;
import software.amazon.awssdk.services.lambda.model.GetFunctionConfigurationResponse;
import software.amazon.awssdk.services.lambda.model.PackageType;
import software.amazon.awssdk.services.lambda.model.Runtime;

import java.io.File;
Expand All @@ -40,6 +51,7 @@
@Testcontainers(parallel = true)
class SpringCloudFunctionAwsApplicationTests {

private static final Logger LOGGER = LoggerFactory.getLogger(SpringCloudFunctionAwsApplicationTests.class);
static String jar = buildJar();

static Network network = Network.newNetwork();
Expand All @@ -55,9 +67,9 @@ class SpringCloudFunctionAwsApplicationTests {
.withEnv("LOCALSTACK_HOST", "localhost.localstack.cloud")
.withEnv("LAMBDA_DOCKER_NETWORK", ((Network.NetworkImpl) network).getName())
.withNetworkAliases("localstack")
.withEnv("LAMBDA_DOCKER_FLAGS", testcontainersLabels());
.withEnv("LAMBDA_DOCKER_FLAGS", testContainersLabels());

static String testcontainersLabels() {
static String testContainersLabels() {
return Stream
.of(DockerClientFactory.DEFAULT_LABELS.entrySet().stream(),
ResourceReaper.instance().getLabels().entrySet().stream())
Expand Down Expand Up @@ -89,6 +101,9 @@ static String buildJar() {

@Test
void contextLoads() throws IOException {
Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER);
localstack.followOutput(logConsumer);

String fnName = "fetchByName-fn";
var envVars = Map.ofEntries(Map.entry("SPRING_DATASOURCE_URL", "jdbc:postgresql://postgres:5432/test"),
Map.entry("SPRING_DATASOURCE_USERNAME", postgres.getUsername()), Map.entry("SPRING_DATASOURCE_PASSWORD", postgres.getPassword()));
Expand All @@ -113,8 +128,10 @@ void contextLoads() throws IOException {
.build();

CreateFunctionResponse createFunctionResponse = lambdaClient.createFunction(createFunctionRequest);
LOGGER.info("createFunctionResponse :{}", createFunctionResponse);
WaiterResponse<GetFunctionConfigurationResponse> waiterResponse = lambdaClient.waiter()
.waitUntilFunctionActive(GetFunctionConfigurationRequest.builder().functionName(fnName).build());
LOGGER.info("waiterResponse :{}", waiterResponse);

CreateFunctionUrlConfigRequest createFunctionUrlConfigRequest = CreateFunctionUrlConfigRequest.builder()
.functionName(fnName)
Expand All @@ -124,10 +141,15 @@ void contextLoads() throws IOException {

String functionUrl = createFunctionUrlConfigResponse.functionUrl()
.replace("" + 4566, "" + localstack.getMappedPort(4566));
ResponseBody responseBody = RestAssured.given().body("""
LOGGER.info("functionURL :{}", functionUrl);
var responseBody = RestAssured.given().body("""
{"name": "profile"}
""").get(functionUrl).prettyPeek().andReturn().body();
assertThat(responseBody.asString()).isEqualTo("4");
responseBody = RestAssured.given().body("""
{"name": "junit"}
""").get(functionUrl).prettyPeek().andReturn().body();
assertThat(responseBody.asString()).isEqualTo("0");
}

}

0 comments on commit 68e408a

Please sign in to comment.