Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#246 Update deprecated java sample APIGatewayV2ProxyResponseEvent -> APIGatewayV2HTTPResponse #333

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions sample-apps/java-events/src/main/java/example/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyResponseEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Map;
import java.util.HashMap;

// Handler value: example.Handler
public class Handler implements RequestHandler<APIGatewayV2ProxyRequestEvent, APIGatewayV2ProxyResponseEvent>{
public class Handler implements RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse>{
Gson gson = new GsonBuilder().setPrettyPrinting().create();

@Override
public APIGatewayV2ProxyResponseEvent handleRequest(APIGatewayV2ProxyRequestEvent event, Context context)
{
public APIGatewayV2HTTPResponse handleRequest(APIGatewayV2HTTPEvent event, Context context) {
LambdaLogger logger = context.getLogger();
APIGatewayV2ProxyResponseEvent response = new APIGatewayV2ProxyResponseEvent();
response.setIsBase64Encoded(false);
response.setStatusCode(200);

HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "text/html");
response.setHeaders(headers);
response.setBody("<!DOCTYPE html><html><head><title>AWS Lambda sample</title></head><body>"+
"<h1>Welcome</h1><p>Page generated by a Lambda function.</p>" +
"</body></html>");

APIGatewayV2HTTPResponse response = APIGatewayV2HTTPResponse.builder()
.withIsBase64Encoded(false)
.withStatusCode(200)
.withHeaders(headers)
.withBody(
"<!DOCTYPE html><html><head><title>AWS Lambda sample</title></head><body>"+
"<h1>Welcome</h1><p>Page generated by a Lambda function.</p>" +
"</body></html>"
)
.build();

// log execution details
Util.logEnvironment(event, context, gson);
return response;
Expand Down
15 changes: 5 additions & 10 deletions sample-apps/java-events/src/test/java/example/InvokeTest.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
package example;

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

import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse;
import org.junit.jupiter.api.Test;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyResponseEvent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

class InvokeTest {
private static final Logger logger = LoggerFactory.getLogger(InvokeTest.class);

@Test
void invokeTest() {
logger.info("Invoke TEST");
APIGatewayV2ProxyRequestEvent event = new APIGatewayV2ProxyRequestEvent();
APIGatewayV2HTTPEvent event = new APIGatewayV2HTTPEvent();
Context context = new TestContext();
String requestId = context.getAwsRequestId();
Handler handler = new Handler();
APIGatewayV2ProxyResponseEvent result = handler.handleRequest(event, context);
APIGatewayV2HTTPResponse result = handler.handleRequest(event, context);
assertEquals(200, result.getStatusCode());
}

Expand Down