Skip to content

Commit

Permalink
Use latest available Apache Struts2 GA version 6.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jogep committed Sep 16, 2022
1 parent b376c15 commit c4d34b7
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 55 deletions.
2 changes: 1 addition & 1 deletion aws-serverless-java-container-struts/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</parent>

<properties>
<struts.version>2.5.30</struts.version>
<struts.version>6.0.3</struts.version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected void handleRequest(HttpServletRequest httpServletRequest,

@Override
public void initialize() throws ContainerInitializationException {
log.info("Initialize Struts2 Lambda Application ...");
log.info("Initialize Struts Lambda Application ...");
Timer.start(TIMER_STRUTS_COLD_START_INIT);
try {
if (this.startupHandler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
import org.apache.struts2.StrutsJUnit4TestCase;
import org.apache.struts2.StrutsRestTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -49,24 +49,25 @@
* Unit test class for the Struts2 AWS_PROXY default implementation
*/
@RunWith(Parameterized.class)
public class StrutsAwsProxyTest extends StrutsJUnit4TestCase<EchoAction> {
public class StrutsAwsProxyTest extends StrutsRestTestCase<EchoAction> {
private static final String CUSTOM_HEADER_KEY = "x-custom-header";
private static final String CUSTOM_HEADER_VALUE = "my-custom-value";
private static final String AUTHORIZER_PRINCIPAL_ID = "test-principal-" + UUID.randomUUID().toString();
private static final String HTTP_METHOD_GET = "GET";
private static final String QUERY_STRING_MODE = "mode";
private static final String QUERY_STRING_KEY = "message";
private static final String QUERY_STRING_ENCODED_VALUE = "Hello Struts2";
private static final String USER_PRINCIPAL = "user1";
private static final String CONTENT_TYPE_APPLICATION_JSON = "application/json; charset=UTF-8";


private static ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final StrutsLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = StrutsLambdaContainerHandler
.getAwsProxyHandler();
private final StrutsLambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyResponse> httpApiHandler = StrutsLambdaContainerHandler
.getHttpApiV2ProxyHandler();
private static Context lambdaContext = new MockLambdaContext();

private String type;
private final Context lambdaContext = new MockLambdaContext();
private final String type;

public StrutsAwsProxyTest(String reqType) {
type = reqType;
Expand All @@ -92,8 +93,8 @@ private AwsProxyResponse executeRequest(AwsProxyRequestBuilder requestBuilder, C

@Test
public void headers_getHeaders_echo() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "GET")
.queryString("mode", "headers")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", HTTP_METHOD_GET)
.queryString(QUERY_STRING_MODE, "headers")
.header(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE)
.json();

Expand All @@ -106,7 +107,7 @@ public void headers_getHeaders_echo() {

@Test
public void context_servletResponse_setCustomHeader() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "GET")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", HTTP_METHOD_GET)
.queryString("customHeader", "true")
.json();

Expand All @@ -118,7 +119,7 @@ public void context_servletResponse_setCustomHeader() {
@Test
public void context_serverInfo_correctContext() {
assumeTrue("API_GW".equals(type));
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "GET")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", HTTP_METHOD_GET)
.queryString(QUERY_STRING_KEY, "Hello Struts2")
.header("Content-Type", "application/json")
.queryString("contentType", "true");
Expand All @@ -131,8 +132,8 @@ public void context_serverInfo_correctContext() {

@Test
public void queryString_uriInfo_echo() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "GET")
.queryString("mode", "query-string")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", HTTP_METHOD_GET)
.queryString(QUERY_STRING_MODE, "query-string")
.queryString(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE)
.json();

Expand All @@ -146,8 +147,8 @@ public void queryString_uriInfo_echo() {

@Test
public void requestScheme_valid_expectHttps() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "GET")
.queryString("mode", "scheme")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", HTTP_METHOD_GET)
.queryString(QUERY_STRING_MODE, "scheme")
.queryString(QUERY_STRING_KEY, QUERY_STRING_ENCODED_VALUE)
.json();

Expand All @@ -161,8 +162,8 @@ public void requestScheme_valid_expectHttps() {
@Test
public void authorizer_securityContext_customPrincipalSuccess() {
assumeTrue("API_GW".equals(type));
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "GET")
.queryString("mode", "principal")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", HTTP_METHOD_GET)
.queryString(QUERY_STRING_MODE, "principal")
.json()
.authorizerPrincipal(AUTHORIZER_PRINCIPAL_ID);

Expand All @@ -175,7 +176,7 @@ public void authorizer_securityContext_customPrincipalSuccess() {

@Test
public void errors_unknownRoute_expect404() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/unknown", "GET");
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/unknown", HTTP_METHOD_GET);

AwsProxyResponse output = executeRequest(request, lambdaContext);
assertEquals(404, output.getStatusCode());
Expand All @@ -184,7 +185,7 @@ public void errors_unknownRoute_expect404() {
@Test
public void error_contentType_invalidContentType() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "POST")
.queryString("mode", "content-type")
.queryString(QUERY_STRING_MODE, "content-type")
.header("Content-Type", "application/octet-stream")
.body("asdasdasd");

Expand All @@ -195,7 +196,7 @@ public void error_contentType_invalidContentType() {
@Test
public void error_statusCode_methodNotAllowed() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "POST")
.queryString("mode", "not-allowed")
.queryString(QUERY_STRING_MODE, "not-allowed")
.json();

AwsProxyResponse output = executeRequest(request, lambdaContext);
Expand All @@ -209,7 +210,7 @@ public void responseBody_responseWriter_validBody() throws JsonProcessingExcepti
value.put(QUERY_STRING_KEY, CUSTOM_HEADER_VALUE);
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "POST")
.json()
.body(objectMapper.writeValueAsString(value));
.body(OBJECT_MAPPER.writeValueAsString(value));

AwsProxyResponse output = executeRequest(request, lambdaContext);
assertEquals(200, output.getStatusCode());
Expand All @@ -220,8 +221,8 @@ public void responseBody_responseWriter_validBody() throws JsonProcessingExcepti

@Test
public void statusCode_responseStatusCode_customStatusCode() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "GET")
.queryString("mode", "custom-status-code")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", HTTP_METHOD_GET)
.queryString(QUERY_STRING_MODE, "custom-status-code")
.queryString("status", "201")
.json();

Expand All @@ -231,7 +232,7 @@ public void statusCode_responseStatusCode_customStatusCode() {

@Test
public void base64_binaryResponse_base64Encoding() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "GET");
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", HTTP_METHOD_GET);

AwsProxyResponse response = executeRequest(request, lambdaContext);
assertNotNull(response.getBody());
Expand All @@ -241,7 +242,7 @@ public void base64_binaryResponse_base64Encoding() {
@Test
public void exception_mapException_mapToNotImplemented() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "POST")
.queryString("mode", "not-implemented");
.queryString(QUERY_STRING_MODE, "not-implemented");

AwsProxyResponse response = executeRequest(request, lambdaContext);
assertNotNull(response.getBody());
Expand All @@ -251,7 +252,7 @@ public void exception_mapException_mapToNotImplemented() {

@Test
public void stripBasePath_route_shouldRouteCorrectly() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/custompath/echo", "GET")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/custompath/echo", HTTP_METHOD_GET)
.json()
.queryString(QUERY_STRING_KEY, "stripped");
handler.stripBasePath("/custompath");
Expand All @@ -263,7 +264,7 @@ public void stripBasePath_route_shouldRouteCorrectly() {

@Test
public void stripBasePath_route_shouldReturn404() {
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/custompath/echo/status-code", "GET")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/custompath/echo/status-code", HTTP_METHOD_GET)
.json()
.queryString("status", "201");
handler.stripBasePath("/custom");
Expand All @@ -275,8 +276,8 @@ public void stripBasePath_route_shouldReturn404() {
@Test
public void securityContext_injectPrincipal_expectPrincipalName() {
assumeTrue("API_GW".equals(type));
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", "GET")
.queryString("mode", "principal")
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo-request-info", HTTP_METHOD_GET)
.queryString(QUERY_STRING_MODE, "principal")
.authorizerPrincipal(USER_PRINCIPAL);

AwsProxyResponse resp = executeRequest(request, lambdaContext);
Expand All @@ -295,7 +296,7 @@ public void queryParam_encoding_expectUnencodedParam() {
e.printStackTrace();
fail("Could not decode parameter");
}
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "GET").queryString(QUERY_STRING_KEY, decodedParam);
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", HTTP_METHOD_GET).queryString(QUERY_STRING_KEY, decodedParam);

AwsProxyResponse resp = executeRequest(request, lambdaContext);
assertEquals(200, resp.getStatusCode());
Expand All @@ -306,7 +307,7 @@ public void queryParam_encoding_expectUnencodedParam() {
public void queryParam_encoding_expectEncodedParam() {
assumeTrue("API_GW".equals(type));
String paramValue = "p%2Fz%2B3";
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", "GET").queryString(QUERY_STRING_KEY, paramValue);
AwsProxyRequestBuilder request = new AwsProxyRequestBuilder("/echo", HTTP_METHOD_GET).queryString(QUERY_STRING_KEY, paramValue);

AwsProxyResponse resp = executeRequest(request, lambdaContext);
assertEquals(200, resp.getStatusCode());
Expand All @@ -323,7 +324,7 @@ private void validateMapResponseModel(AwsProxyResponse output, String key, Strin
TypeReference<HashMap<String, Object>> typeRef
= new TypeReference<HashMap<String, Object>>() {
};
HashMap<String, Object> response = objectMapper.readValue(output.getBody(), typeRef);
HashMap<String, Object> response = OBJECT_MAPPER.readValue(output.getBody(), typeRef);
assertNotNull(response.get(key));
assertEquals(value, response.get(key));
} catch (IOException e) {
Expand All @@ -335,7 +336,7 @@ private void validateMapResponseModel(AwsProxyResponse output, String key, Strin
private void validateSingleValueModel(AwsProxyResponse output, String value) {
try {
assertNotNull(output.getBody());
assertEquals(value, objectMapper.readerFor(String.class).readValue(output.getBody()));
assertEquals(value, OBJECT_MAPPER.readerFor(String.class).readValue(output.getBody()));
} catch (Exception e) {
e.printStackTrace();
fail("Exception while parsing response body: " + e.getMessage());
Expand Down
17 changes: 17 additions & 0 deletions aws-serverless-java-container-struts/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2.LambdaAppender">
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Logger name="com.opensymphony.xwork2" level="warn"/>
<Logger name="org.apache.struts2" level="warn"/>
<Root level="info">
<AppenderRef ref="Lambda"/>
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ dependencies {
exclude group: 'org.apache.logging.log4j', module: 'log4j-api'
exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
}
implementation ('org.apache.struts:struts2-convention-plugin:2.5.30') {
implementation ('org.apache.struts:struts2-convention-plugin:6.0.3') {
exclude group: 'org.apache.struts', module: 'struts2-core'
}
implementation ('org.apache.struts:struts2-rest-plugin:2.5.30') {
implementation ('org.apache.struts:struts2-rest-plugin:6.0.3') {
exclude group: 'org.apache.struts', module: 'struts2-core'
}
implementation ('org.apache.struts:struts2-bean-validation-plugin:2.5.30') {
implementation ('org.apache.struts:struts2-bean-validation-plugin:6.0.3') {
exclude group: 'org.apache.struts', module: 'struts2-core'
}
implementation ('com.jgeppert.struts2:struts2-aws-lambda-support-plugin:1.3.0') {
implementation ('com.jgeppert.struts2:struts2-aws-lambda-support-plugin:1.4.0') {
exclude group: 'org.apache.struts', module: 'struts2-core'
}
implementation ('org.apache.struts:struts2-core:2.5.30') {
implementation ('org.apache.struts:struts2-core:6.0.3') {
exclude group: 'org.apache.logging.log4j', module: 'log4j-api'
}
implementation ('org.hibernate:hibernate-validator:5.4.3.Final')
implementation ('org.hibernate:hibernate-validator:6.1.7.Final')
implementation ('com.fasterxml.jackson.core:jackson-databind:2.13.3')
implementation ('org.apache.logging.log4j:log4j-core:2.17.2')
implementation ('org.apache.logging.log4j:log4j-api:2.17.2')
implementation ('org.apache.logging.log4j:log4j-slf4j-impl:2.17.2')
implementation ('com.amazonaws:aws-lambda-java-log4j2:1.5.1')

testImplementation('junit:junit:4.13.2')
testImplementation('org.apache.struts:struts2-junit-plugin:2.5.30') {
testImplementation('org.apache.struts:struts2-junit-plugin:6.0.3') {
exclude group: 'org.apache.struts', module: 'struts2-core'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<struts.version>2.5.30</struts.version>
<struts.version>6.0.3</struts.version>
<jackson.version>2.13.3</jackson.version>
<junit.version>4.13.2</junit.version>
<log4j.version>2.17.2</log4j.version>
<log4j.version>2.18.0</log4j.version>
</properties>

<repositories>
<repository>
<id>struts-staging</id>
<url>https://repository.apache.org/content/repositories/staging/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
Expand Down Expand Up @@ -63,14 +70,14 @@
<dependency>
<groupId>com.jgeppert.struts2</groupId>
<artifactId>struts2-aws-lambda-support-plugin</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>
</dependency>

<!-- bean validation based on hibernate validators-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.3.Final</version>
<version>6.1.7.Final</version>
</dependency>

<dependency>
Expand All @@ -88,6 +95,11 @@
<artifactId>jackson-databind</artifactId>
<version>\${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>\${jackson.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<!-- Use Jackson lib as content handler for all JSON requests -->
<bean type="org.apache.struts2.rest.handler.ContentTypeHandler" name="jackson"
class="org.apache.struts2.rest.handler.JacksonLibHandler"/>
class="org.apache.struts2.rest.handler.JacksonJsonHandler"/>
<constant name="struts.rest.handlerOverride.json" value="jackson"/>

<!-- Set to false if the json content can be returned for any kind of http method -->
Expand Down
12 changes: 6 additions & 6 deletions samples/struts/pet-store/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ configurations {
dependencies {
implementation (
'com.amazonaws.serverless:aws-serverless-java-container-struts2:[1.0,)',
'org.apache.struts:struts2-convention-plugin:2.5.30',
'org.apache.struts:struts2-rest-plugin:2.5.30',
'org.apache.struts:struts2-bean-validation-plugin:2.5.30',
'org.apache.struts:struts2-junit-plugin:2.5.30',
'com.jgeppert.struts2:struts2-aws-lambda-support-plugin:1.3.0',
'org.hibernate:hibernate-validator:5.4.3.Final',
'org.apache.struts:struts2-convention-plugin:6.0.3',
'org.apache.struts:struts2-rest-plugin:6.0.3',
'org.apache.struts:struts2-bean-validation-plugin:6.0.3',
'org.apache.struts:struts2-junit-plugin:6.0.3',
'com.jgeppert.struts2:struts2-aws-lambda-support-plugin:1.4.0',
'org.hibernate:hibernate-validator:6.1.7.Final',
'com.fasterxml.jackson.core:jackson-databind:2.13.3',
'org.apache.logging.log4j:log4j-core:2.17.2',
'org.apache.logging.log4j:log4j-api:2.17.2',
Expand Down
Loading

0 comments on commit c4d34b7

Please sign in to comment.