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

Java 11 migrate all remaining s #1120

Merged
merged 16 commits into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Moves serverless to Java 11
  • Loading branch information
anuragagarwal561994 committed Dec 28, 2019
commit bf38d1fdbf491b72ed496c51d849827a9a07eca1
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
package com.iluwatar.serverless.baas.api;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -50,7 +48,7 @@ public AbstractDynamoDbHandler() {
}

private void initAmazonDynamoDb() {
AmazonDynamoDB amazonDynamoDb = AmazonDynamoDBClientBuilder
var amazonDynamoDb = AmazonDynamoDBClientBuilder
.standard()
.withRegion(Regions.US_EAST_1)
.build();
Expand All @@ -71,10 +69,7 @@ public void setDynamoDbMapper(DynamoDBMapper dynamoDbMapper) {
}

protected Map<String, String> headers() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");

return headers;
return Map.of("Content-Type", "application/json");
}

/**
Expand All @@ -85,14 +80,11 @@ protected Map<String, String> headers() {
* @return - api gateway proxy response
*/
protected APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent(Integer statusCode, T body) {
APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent =
new APIGatewayProxyResponseEvent().withHeaders(headers());
var apiGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent().withHeaders(headers());
try {
apiGatewayProxyResponseEvent
.withStatusCode(statusCode)
.withBody(getObjectMapper()
.writeValueAsString(body));

.withBody(getObjectMapper().writeValueAsString(body));
} catch (JsonProcessingException jsonProcessingException) {
throw new RuntimeException(jsonProcessingException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.iluwatar.serverless.baas.model.Person;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -42,15 +41,15 @@ public class FindPersonApiHandler extends AbstractDynamoDbHandler<Person>
private static final Integer SUCCESS_STATUS_CODE = 200;

@Override
public APIGatewayProxyResponseEvent handleRequest(
APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent, Context context) {
Map<String, String> pathParameters = apiGatewayProxyRequestEvent.getPathParameters();
pathParameters.keySet().stream().map(key -> key + "=" + pathParameters.get(key))
.forEach(LOG::info);

Person person = this.getDynamoDbMapper().load(Person.class, apiGatewayProxyRequestEvent
.getPathParameters().get("id"));

public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent req, Context ctx) {
req.getPathParameters().forEach(FindPersonApiHandler::logKeyValue);
var id = req.getPathParameters().get("id");
var person = this.getDynamoDbMapper().load(Person.class, id);
return apiGatewayProxyResponseEvent(SUCCESS_STATUS_CODE, person);
}

private static void logKeyValue(String key, String value) {
LOG.info(key + "=" + value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,15 @@ public class SavePersonApiHandler extends AbstractDynamoDbHandler<Person>
private static final Integer BAD_REQUEST_STATUS_CODE = 400;

@Override
public APIGatewayProxyResponseEvent handleRequest(
APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent, Context context) {
APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent;
Person person;
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent req, Context ctx) {
try {
person = getObjectMapper().readValue(apiGatewayProxyRequestEvent.getBody(), Person.class);
var objectMapper = getObjectMapper();
var person = objectMapper.readValue(req.getBody(), Person.class);
getDynamoDbMapper().save(person);
apiGatewayProxyResponseEvent = apiGatewayProxyResponseEvent(CREATED_STATUS_CODE, person);
return apiGatewayProxyResponseEvent(CREATED_STATUS_CODE, person);
} catch (IOException ioException) {
LOG.error("unable to parse body", ioException);
apiGatewayProxyResponseEvent = apiGatewayProxyResponseEvent(BAD_REQUEST_STATUS_CODE, null);
return apiGatewayProxyResponseEvent(BAD_REQUEST_STATUS_CODE, null);
}

return apiGatewayProxyResponseEvent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import java.io.Serializable;
import java.util.Objects;

/**
* Address class Created by dheeraj.mummarareddy on 3/4/18.
Expand Down Expand Up @@ -96,30 +97,30 @@ public boolean equals(Object o) {
return false;
}

Address address = (Address) o;
var address = (Address) o;

if (addressLineOne != null ? !addressLineOne.equals(address.addressLineOne) :
address.addressLineOne != null) {
if (!Objects.equals(addressLineOne, address.addressLineOne)) {
return false;
}

if (addressLineTwo != null ? !addressLineTwo.equals(address.addressLineTwo) :
address.addressLineTwo != null) {
if (!Objects.equals(addressLineTwo, address.addressLineTwo)) {
return false;
}

if (city != null ? !city.equals(address.city) : address.city != null) {
if (!Objects.equals(city, address.city)) {
return false;
}
if (state != null ? !state.equals(address.state) : address.state != null) {

if (!Objects.equals(state, address.state)) {
return false;
}
return zipCode != null ? zipCode.equals(address.zipCode) : address.zipCode == null;

return Objects.equals(zipCode, address.zipCode);
}

@Override
public int hashCode() {
int result = addressLineOne != null ? addressLineOne.hashCode() : 0;
var result = addressLineOne != null ? addressLineOne.hashCode() : 0;
result = 31 * result + (addressLineTwo != null ? addressLineTwo.hashCode() : 0);
result = 31 * result + (city != null ? city.hashCode() : 0);
result = 31 * result + (state != null ? state.hashCode() : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.util.Objects;

/**
* Person class Created by dheeraj.mummarareddy on 3/4/18.
Expand Down Expand Up @@ -92,15 +93,15 @@ public boolean equals(Object o) {

Person person = (Person) o;

if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) {
if (!Objects.equals(firstName, person.firstName)) {
return false;
}

if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) {
if (!Objects.equals(lastName, person.lastName)) {
return false;
}

return address != null ? address.equals(person.address) : person.address == null;
return Objects.equals(address, person.address);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@

/**
* Api gateway response.
*
* @param <T> serializable object
*/
public class ApiGatewayResponse<T extends Serializable> implements Serializable {
public class ApiGatewayResponse implements Serializable {

private static final long serialVersionUID = 1181159426782844892L;

Expand All @@ -50,8 +48,12 @@ public class ApiGatewayResponse<T extends Serializable> implements Serializable
* @param headers - response headers
* @param isBase64Encoded - base64Encoded flag
*/
public ApiGatewayResponse(Integer statusCode, String body, Map<String, String> headers,
Boolean isBase64Encoded) {
public ApiGatewayResponse(
Integer statusCode,
String body,
Map<String, String> headers,
Boolean isBase64Encoded
) {
this.statusCode = statusCode;
this.body = body;
this.headers = headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.iluwatar.serverless.faas;

import java.io.Serializable;
import java.util.Objects;

/**
* Lambda context information.
Expand Down Expand Up @@ -110,28 +111,22 @@ public boolean equals(Object o) {

LambdaInfo that = (LambdaInfo) o;

if (awsRequestId != null ? !awsRequestId
.equals(that.awsRequestId) : that.awsRequestId != null) {
if (!Objects.equals(awsRequestId, that.awsRequestId)) {
return false;
}
if (logGroupName != null ? !logGroupName
.equals(that.logGroupName) : that.logGroupName != null) {
if (!Objects.equals(logGroupName, that.logGroupName)) {
return false;
}
if (logStreamName != null ? !logStreamName
.equals(that.logStreamName) : that.logStreamName != null) {
if (!Objects.equals(logStreamName, that.logStreamName)) {
return false;
}
if (functionName != null ? !functionName
.equals(that.functionName) : that.functionName != null) {
if (!Objects.equals(functionName, that.functionName)) {
return false;
}
if (functionVersion != null ? !functionVersion
.equals(that.functionVersion) : that.functionVersion != null) {
if (!Objects.equals(functionVersion, that.functionVersion)) {
return false;
}
return memoryLimitInMb != null ? memoryLimitInMb
.equals(that.memoryLimitInMb) : that.memoryLimitInMb == null;
return Objects.equals(memoryLimitInMb, that.memoryLimitInMb);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.iluwatar.serverless.faas.ApiGatewayResponse;
import com.iluwatar.serverless.faas.LambdaInfo;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -46,13 +45,11 @@ public class LambdaInfoApiHandler
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
LOG.info("received: " + input);

return new ApiGatewayResponse
.ApiGatewayResponseBuilder<LambdaInfo>()
return new ApiGatewayResponse.ApiGatewayResponseBuilder<LambdaInfo>()
.headers(headers())
.statusCode(SUCCESS_STATUS_CODE)
.body(lambdaInfo(context))
.build();

}

/**
Expand All @@ -69,14 +66,10 @@ private LambdaInfo lambdaInfo(Context context) {
lambdaInfo.setLogGroupName(context.getLogGroupName());
lambdaInfo.setLogStreamName(context.getLogStreamName());
lambdaInfo.setMemoryLimitInMb(context.getMemoryLimitInMB());

return lambdaInfo;
}

private Map<String, String> headers() {
var headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");

return headers;
return Map.of("Content-Type", "application/json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,23 @@

package com.iluwatar.serverless.baas.api;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.iluwatar.serverless.baas.api.FindPersonApiHandler;
import com.iluwatar.serverless.baas.api.SavePersonApiHandler;
import com.iluwatar.serverless.baas.model.Person;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
* Unit tests for FindPersonApiHandler
* Created by dheeraj.mummar on 3/5/18.
* Unit tests for FindPersonApiHandler Created by dheeraj.mummar on 3/5/18.
*/
@RunWith(MockitoJUnitRunner.class)
public class FindPersonApiHandlerTest {
Expand All @@ -66,8 +62,7 @@ public void handleRequest() {
}

private APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent() {
return new APIGatewayProxyRequestEvent()
.withPathParamters(Collections
.singletonMap("id", "37e7a1fe-3544-473d-b764-18128f02d72d"));
var request = new APIGatewayProxyRequestEvent();
return request.withPathParamters(Map.of("id", "37e7a1fe-3544-473d-b764-18128f02d72d"));
}
}
Loading