Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
-Added jpaapp.JpaApplication for H2 and Spring Data JPA testing.
-Excluded JPA auto-configuration from other test apps to prevent interference.
-Implemented one async and one sync test case in JpaAppTest
  • Loading branch information
leekib committed Sep 15, 2023
1 parent 0738e25 commit 096c481
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 4 deletions.
40 changes: 40 additions & 0 deletions aws-serverless-java-container-springboot3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,46 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.1.3</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.2.222</version>
<scope>test</scope>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.amazonaws.serverless.proxy.spring;

import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.spring.jpaapp.LambdaHandler;
import com.amazonaws.serverless.proxy.spring.jpaapp.MessageController;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JpaAppTest {

LambdaHandler handler;
MockLambdaContext lambdaContext = new MockLambdaContext();

private String type;

public static Collection<Object> data() {
return Arrays.asList(new Object[]{"API_GW", "ALB", "HTTP_API"});
}

public void initJpaAppTest(String reqType) {
type = reqType;
handler = new LambdaHandler(type);
}

@MethodSource("data")
@ParameterizedTest
void asyncRequest(String reqType) {
initJpaAppTest(reqType);
AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/async", "POST")
.json()
.body("{\"name\":\"kong\"}");
AwsProxyResponse resp = handler.handleRequest(req, lambdaContext);
assertEquals("{\"name\":\"KONG\"}", resp.getBody());
}

@MethodSource("data")
@ParameterizedTest
void helloRequest_respondsWithSingleMessage(String reqType) {
initJpaAppTest(reqType);
AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/hello", "GET");
AwsProxyResponse resp = handler.handleRequest(req, lambdaContext);
assertEquals(MessageController.HELLO_MESSAGE, resp.getBody());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.amazonaws.serverless.proxy.spring.jpaapp;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class DatabaseConfig {

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:testdb");
dataSource.setUsername("sa");
dataSource.setPassword("");

return dataSource;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.amazonaws.serverless.proxy.spring.jpaapp;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@SpringBootApplication(exclude = {
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
@Import(MessageController.class)
public class JpaApplication {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.amazonaws.serverless.proxy.spring.jpaapp;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.InitializationWrapper;
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.model.HttpApiV2ProxyRequest;
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler;
import com.amazonaws.serverless.proxy.spring.SpringBootProxyHandlerBuilder;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaHandler implements RequestHandler<AwsProxyRequestBuilder, AwsProxyResponse> {
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
private static SpringBootLambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyResponse> httpApiHandler;
private String type;

public LambdaHandler(String reqType) {
type = reqType;
try {
switch (type) {
case "API_GW":
case "ALB":
handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
.defaultProxy()
.initializationWrapper(new InitializationWrapper())
.servletApplication()
.springBootApplication(JpaApplication.class)
.buildAndInitialize();
break;
case "HTTP_API":
httpApiHandler = new SpringBootProxyHandlerBuilder<HttpApiV2ProxyRequest>()
.defaultHttpApiV2Proxy()
.initializationWrapper(new InitializationWrapper())
.servletApplication()
.springBootApplication(JpaApplication.class)
.buildAndInitialize();
break;
}
} catch (ContainerInitializationException e) {
e.printStackTrace();
}
}

@Override
public AwsProxyResponse handleRequest(AwsProxyRequestBuilder awsProxyRequest, Context context) {
switch (type) {
case "API_GW":
return handler.proxy(awsProxyRequest.build(), context);
case "ALB":
return handler.proxy(awsProxyRequest.alb().build(), context);
case "HTTP_API":
return httpApiHandler.proxy(awsProxyRequest.toHttpApiV2Request(), context);
default:
throw new RuntimeException("Unknown request type: " + type);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.amazonaws.serverless.proxy.spring.jpaapp;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.async.DeferredResult;
import java.util.Collections;
import java.util.Map;

@RestController
public class MessageController {

public static final String HELLO_MESSAGE = "Hello";

@RequestMapping(path="/hello", method=RequestMethod.GET, produces = {"text/plain"})
public String hello() {
return HELLO_MESSAGE;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@RequestMapping(path = "/async", method = RequestMethod.POST)
@ResponseBody
public DeferredResult<Map<String, String>> asyncResult(@RequestBody Map<String, String> value) {
DeferredResult result = new DeferredResult<>();
result.setResult(Collections.singletonMap("name", value.get("name").toUpperCase()));
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.web.reactive.config.EnableWebFlux;

@SpringBootApplication
@SpringBootApplication(exclude = {
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
})
@EnableWebFluxSecurity
@EnableWebFlux
@Import(SecurityConfig.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class,
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
})
@Import(MessageController.class)
public class ServletApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

@SpringBootApplication(exclude = {
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class,
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
})
public class SlowTestApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class,
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
})
public class WebFluxTestApplication {

Expand Down

0 comments on commit 096c481

Please sign in to comment.