Java 11 migrate all remaining s (#1120)

* Moves saga to Java 11

* Moves semaphore to Java 11

* Moves servant to Java 11

* Moves serverless to Java 11

* Moves service-layer to Java 11

* Moves service-locator to Java 11

* Moves sharding to Java 11

* Moves singleton to Java 11

* Moves spatial-partition to Java 11

* Moves specification to Java 11

* Moves state to Java 11

* Moves step-builder to Java 11

* Moves strategy to Java 11

* Moves subclass-sandbox to Java 11

* Fixes checkstyle issues
This commit is contained in:
Anurag Agarwal
2020-01-04 22:06:08 +05:30
committed by Ilkka Seppälä
parent 310ae50248
commit cd2a2e7711
98 changed files with 718 additions and 855 deletions

View File

@ -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;
/**
@ -50,7 +48,7 @@ public abstract class AbstractDynamoDbHandler<T extends Serializable> {
}
private void initAmazonDynamoDb() {
AmazonDynamoDB amazonDynamoDb = AmazonDynamoDBClientBuilder
var amazonDynamoDb = AmazonDynamoDBClientBuilder
.standard()
.withRegion(Regions.US_EAST_1)
.build();
@ -71,10 +69,7 @@ public abstract class AbstractDynamoDbHandler<T extends Serializable> {
}
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");
}
/**
@ -85,14 +80,11 @@ public abstract class AbstractDynamoDbHandler<T extends Serializable> {
* @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);
}

View File

@ -28,7 +28,6 @@ import com.amazonaws.services.lambda.runtime.RequestHandler;
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;
@ -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);
}
}

View File

@ -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;
}
}

View File

@ -26,6 +26,7 @@ package com.iluwatar.serverless.baas.model;
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.
@ -96,30 +97,30 @@ public class Address implements Serializable {
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);

View File

@ -29,6 +29,7 @@ import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
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.
@ -92,15 +93,15 @@ public class Person implements Serializable {
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

View File

@ -30,10 +30,8 @@ import java.util.Map;
/**
* 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;
@ -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;

View File

@ -24,6 +24,7 @@
package com.iluwatar.serverless.faas;
import java.io.Serializable;
import java.util.Objects;
/**
* Lambda context information.
@ -110,28 +111,22 @@ public class LambdaInfo implements Serializable {
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

View File

@ -27,7 +27,6 @@ import com.amazonaws.services.lambda.runtime.Context;
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;
@ -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();
}
/**
@ -69,14 +66,10 @@ public class LambdaInfoApiHandler
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");
}
}

View File

@ -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 {
@ -66,8 +62,7 @@ public class FindPersonApiHandlerTest {
}
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"));
}
}

View File

@ -23,13 +23,15 @@
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.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iluwatar.serverless.baas.api.SavePersonApiHandler;
import com.iluwatar.serverless.baas.model.Address;
import com.iluwatar.serverless.baas.model.Person;
import org.junit.Assert;
@ -39,11 +41,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.*;
/**
* Unit tests for SavePersonApiHandler
* Created by dheeraj.mummar on 3/4/18.
* Unit tests for SavePersonApiHandler Created by dheeraj.mummar on 3/4/18.
*/
@RunWith(MockitoJUnitRunner.class)
public class SavePersonApiHandlerTest {
@ -63,40 +62,40 @@ public class SavePersonApiHandlerTest {
@Test
public void handleRequestSavePersonSuccessful() throws JsonProcessingException {
Person person = newPerson();
APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent =
this.savePersonApiHandler
.handleRequest(apiGatewayProxyRequestEvent(objectMapper.writeValueAsString(person)), mock(Context.class));
var person = newPerson();
var body = objectMapper.writeValueAsString(person);
var request = apiGatewayProxyRequestEvent(body);
var ctx = mock(Context.class);
var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx);
verify(dynamoDbMapper, times(1)).save(person);
Assert.assertNotNull(apiGatewayProxyResponseEvent);
Assert.assertEquals(new Integer(201), apiGatewayProxyResponseEvent.getStatusCode());
Assert.assertEquals(Integer.valueOf(201), apiGatewayProxyResponseEvent.getStatusCode());
}
@Test
public void handleRequestSavePersonException() {
APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent =
this.savePersonApiHandler
.handleRequest(apiGatewayProxyRequestEvent("invalid sample request"), mock(Context.class));
var request = apiGatewayProxyRequestEvent("invalid sample request");
var ctx = mock(Context.class);
var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx);
Assert.assertNotNull(apiGatewayProxyResponseEvent);
Assert.assertEquals(new Integer(400), apiGatewayProxyResponseEvent.getStatusCode());
Assert.assertEquals(Integer.valueOf(400), apiGatewayProxyResponseEvent.getStatusCode());
}
private APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent(String body) {
APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent = new APIGatewayProxyRequestEvent();
var apiGatewayProxyRequestEvent = new APIGatewayProxyRequestEvent();
return apiGatewayProxyRequestEvent.withBody(body);
}
private Person newPerson() {
Person person = new Person();
var person = new Person();
person.setFirstName("Thor");
person.setLastName("Odinson");
Address address = new Address();
var address = new Address();
address.setAddressLineOne("1 Odin ln");
address.setCity("Asgard");
address.setState("country of the Gods");
address.setZipCode("00001");
person.setAddress(address);
return person;
}
}

View File

@ -23,16 +23,16 @@
package com.iluwatar.serverless.faas.api;
import com.amazonaws.services.lambda.runtime.Context;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.amazonaws.services.lambda.runtime.Context;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
/**
* Unit tests for LambdaInfoApiHandler
*/
@ -41,8 +41,8 @@ public class LambdaInfoApiHandlerTest {
@Test
public void handleRequestWithMockContext() {
LambdaInfoApiHandler lambdaInfoApiHandler = new LambdaInfoApiHandler();
Context context = mock(Context.class);
var lambdaInfoApiHandler = new LambdaInfoApiHandler();
var context = mock(Context.class);
when(context.getAwsRequestId()).thenReturn("mock aws request id");
assertThat(lambdaInfoApiHandler.handleRequest(null, context), notNullValue());