-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-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
Showing
10 changed files
with
235 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
52 changes: 52 additions & 0 deletions
52
...container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/JpaAppTest.java
This file contains 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,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()); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...pringboot3/src/test/java/com/amazonaws/serverless/proxy/spring/jpaapp/DatabaseConfig.java
This file contains 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,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; | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
...pringboot3/src/test/java/com/amazonaws/serverless/proxy/spring/jpaapp/JpaApplication.java
This file contains 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,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 {} |
59 changes: 59 additions & 0 deletions
59
...springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/jpaapp/LambdaHandler.java
This file contains 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,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); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ngboot3/src/test/java/com/amazonaws/serverless/proxy/spring/jpaapp/MessageController.java
This file contains 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,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; | ||
} | ||
|
||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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