Skip to content

Commit f5a8966

Browse files
committed
add tests
-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
1 parent 0738e25 commit f5a8966

File tree

11 files changed

+236
-4
lines changed

11 files changed

+236
-4
lines changed

aws-serverless-java-container-springboot3/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,46 @@
191191
<scope>test</scope>
192192
</dependency>
193193

194+
<dependency>
195+
<groupId>org.springframework.boot</groupId>
196+
<artifactId>spring-boot-starter-data-jpa</artifactId>
197+
<version>3.1.3</version>
198+
<scope>test</scope>
199+
<exclusions>
200+
<exclusion>
201+
<groupId>org.springframework.boot</groupId>
202+
<artifactId>spring-boot-starter-aop</artifactId>
203+
</exclusion>
204+
<exclusion>
205+
<groupId>org.springframework.boot</groupId>
206+
<artifactId>spring-boot-starter-web</artifactId>
207+
</exclusion>
208+
<exclusion>
209+
<groupId>org.springframework.boot</groupId>
210+
<artifactId>spring-boot-starter-logging</artifactId>
211+
</exclusion>
212+
<exclusion>
213+
<groupId>org.springframework.boot</groupId>
214+
<artifactId>spring-boot-starter-tomcat</artifactId>
215+
</exclusion>
216+
<exclusion>
217+
<groupId>org.apache.tomcat.embed</groupId>
218+
<artifactId>tomcat-embed-core</artifactId>
219+
</exclusion>
220+
<exclusion>
221+
<groupId>org.apache.tomcat.embed</groupId>
222+
<artifactId>tomcat-embed-websocket</artifactId>
223+
</exclusion>
224+
</exclusions>
225+
</dependency>
226+
<dependency>
227+
<groupId>com.h2database</groupId>
228+
<artifactId>h2</artifactId>
229+
<version>2.2.222</version>
230+
<scope>test</scope>
231+
</dependency>
232+
233+
194234
</dependencies>
195235

196236
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.amazonaws.serverless.proxy.spring;
2+
3+
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
4+
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
5+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
6+
import com.amazonaws.serverless.proxy.spring.jpaapp.LambdaHandler;
7+
import com.amazonaws.serverless.proxy.spring.jpaapp.MessageController;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
import java.util.Arrays;
12+
import java.util.Collection;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
16+
public class JpaAppTest {
17+
18+
LambdaHandler handler;
19+
MockLambdaContext lambdaContext = new MockLambdaContext();
20+
21+
private String type;
22+
23+
public static Collection<Object> data() {
24+
return Arrays.asList(new Object[]{"API_GW", "ALB", "HTTP_API"});
25+
}
26+
27+
public void initJpaAppTest(String reqType) {
28+
type = reqType;
29+
handler = new LambdaHandler(type);
30+
}
31+
32+
@MethodSource("data")
33+
@ParameterizedTest
34+
void asyncRequest(String reqType) {
35+
initJpaAppTest(reqType);
36+
AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/async", "POST")
37+
.json()
38+
.body("{\"name\":\"kong\"}");
39+
AwsProxyResponse resp = handler.handleRequest(req, lambdaContext);
40+
assertEquals("{\"name\":\"KONG\"}", resp.getBody());
41+
}
42+
43+
@MethodSource("data")
44+
@ParameterizedTest
45+
void helloRequest_respondsWithSingleMessage(String reqType) {
46+
initJpaAppTest(reqType);
47+
AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/hello", "GET");
48+
AwsProxyResponse resp = handler.handleRequest(req, lambdaContext);
49+
assertEquals(MessageController.HELLO_MESSAGE, resp.getBody());
50+
}
51+
52+
}

aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void initServletAppTest(String reqType) {
4141
@MethodSource("data")
4242
@ParameterizedTest
4343
void asyncRequest(String reqType) {
44+
System.out.println("dlrlqja");
4445
initServletAppTest(reqType);
4546
AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/async", "POST")
4647
.json()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.amazonaws.serverless.proxy.spring.jpaapp;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
6+
7+
import javax.sql.DataSource;
8+
9+
@Configuration
10+
public class DatabaseConfig {
11+
12+
@Bean
13+
public DataSource dataSource() {
14+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
15+
dataSource.setDriverClassName("org.h2.Driver");
16+
dataSource.setUrl("jdbc:h2:mem:testdb");
17+
dataSource.setUsername("sa");
18+
dataSource.setPassword("");
19+
20+
return dataSource;
21+
}
22+
}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.amazonaws.serverless.proxy.spring.jpaapp;
2+
3+
import org.springframework.beans.factory.InitializingBean;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.logging.LogLevel;
6+
import org.springframework.boot.logging.LoggingSystem;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Import;
9+
10+
@SpringBootApplication(exclude = {
11+
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration.class,
12+
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class,
13+
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.class,
14+
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
15+
})
16+
@Import(MessageController.class)
17+
public class JpaApplication {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.amazonaws.serverless.proxy.spring.jpaapp;
2+
3+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
4+
import com.amazonaws.serverless.proxy.InitializationWrapper;
5+
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
6+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
7+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
8+
import com.amazonaws.serverless.proxy.model.HttpApiV2ProxyRequest;
9+
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler;
10+
import com.amazonaws.serverless.proxy.spring.SpringBootProxyHandlerBuilder;
11+
import com.amazonaws.services.lambda.runtime.Context;
12+
import com.amazonaws.services.lambda.runtime.RequestHandler;
13+
14+
public class LambdaHandler implements RequestHandler<AwsProxyRequestBuilder, AwsProxyResponse> {
15+
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
16+
private static SpringBootLambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyResponse> httpApiHandler;
17+
private String type;
18+
19+
public LambdaHandler(String reqType) {
20+
type = reqType;
21+
try {
22+
switch (type) {
23+
case "API_GW":
24+
case "ALB":
25+
handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
26+
.defaultProxy()
27+
.initializationWrapper(new InitializationWrapper())
28+
.servletApplication()
29+
.springBootApplication(JpaApplication.class)
30+
.buildAndInitialize();
31+
break;
32+
case "HTTP_API":
33+
httpApiHandler = new SpringBootProxyHandlerBuilder<HttpApiV2ProxyRequest>()
34+
.defaultHttpApiV2Proxy()
35+
.initializationWrapper(new InitializationWrapper())
36+
.servletApplication()
37+
.springBootApplication(JpaApplication.class)
38+
.buildAndInitialize();
39+
break;
40+
}
41+
} catch (ContainerInitializationException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
@Override
47+
public AwsProxyResponse handleRequest(AwsProxyRequestBuilder awsProxyRequest, Context context) {
48+
switch (type) {
49+
case "API_GW":
50+
return handler.proxy(awsProxyRequest.build(), context);
51+
case "ALB":
52+
return handler.proxy(awsProxyRequest.alb().build(), context);
53+
case "HTTP_API":
54+
return httpApiHandler.proxy(awsProxyRequest.toHttpApiV2Request(), context);
55+
default:
56+
throw new RuntimeException("Unknown request type: " + type);
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.amazonaws.serverless.proxy.spring.jpaapp;
2+
3+
import org.springframework.web.bind.annotation.RequestBody;
4+
import org.springframework.web.bind.annotation.RequestMethod;
5+
import org.springframework.web.bind.annotation.ResponseBody;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.context.request.async.DeferredResult;
9+
import java.util.Collections;
10+
import java.util.Map;
11+
12+
@RestController
13+
public class MessageController {
14+
15+
public static final String HELLO_MESSAGE = "Hello";
16+
17+
@RequestMapping(path="/hello", method=RequestMethod.GET, produces = {"text/plain"})
18+
public String hello() {
19+
return HELLO_MESSAGE;
20+
}
21+
22+
@SuppressWarnings({ "unchecked", "rawtypes" })
23+
@RequestMapping(path = "/async", method = RequestMethod.POST)
24+
@ResponseBody
25+
public DeferredResult<Map<String, String>> asyncResult(@RequestBody Map<String, String> value) {
26+
DeferredResult result = new DeferredResult<>();
27+
result.setResult(Collections.singletonMap("name", value.get("name").toUpperCase()));
28+
return result;
29+
}
30+
31+
}

aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/securityapp/SecurityApplication.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
77
import org.springframework.web.reactive.config.EnableWebFlux;
88

9-
@SpringBootApplication
9+
@SpringBootApplication(exclude = {
10+
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
11+
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
12+
})
1013
@EnableWebFluxSecurity
1114
@EnableWebFlux
1215
@Import(SecurityConfig.class)

aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/servletapp/ServletApplication.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration.class,
1010
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class,
1111
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.class,
12-
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
12+
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class,
13+
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
14+
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
1315
})
1416
@Import(MessageController.class)
1517
public class ServletApplication {

aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/slowapp/SlowTestApplication.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
@SpringBootApplication(exclude = {
1010
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration.class,
11-
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class
11+
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class,
12+
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
13+
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
1214
})
1315
public class SlowTestApplication {
1416

aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/webfluxapp/WebFluxTestApplication.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration.class,
1414
org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration.class,
1515
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration.class,
16-
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
16+
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class,
17+
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
18+
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
1719
})
1820
public class WebFluxTestApplication {
1921

0 commit comments

Comments
 (0)