@Test public void testHandleRequest() { OrderHandler handler = new OrderHandler(mock(AmazonDynamoDB.class)); APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent(); input.setBody("invalid : json"); APIGatewayProxyResponseEvent output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(400)); AmazonDynamoDB dynamoDB = mock(AmazonDynamoDB.class); doThrow(new ConditionalCheckFailedException("")).when(dynamoDB).updateItem(ArgumentMatchers.any(UpdateItemRequest.class)); handler = new OrderHandler(dynamoDB); input = new APIGatewayProxyRequestEvent(); input.setBody("{\"name\" : \"salmon\"}"); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(400)); assertThat(output.getBody(), is("salmon is out of stock")); dynamoDB = mock(AmazonDynamoDB.class); doThrow(new AmazonDynamoDBException("")).when(dynamoDB).updateItem(ArgumentMatchers.any(UpdateItemRequest.class)); handler = new OrderHandler(dynamoDB); input = new APIGatewayProxyRequestEvent(); input.setBody("{\"name\" : \"salmon\"}"); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(500)); dynamoDB = mock(AmazonDynamoDB.class); doAnswer((i) -> { UpdateItemRequest request = i.getArgument(0); assertThat(request.getTableName(), is("item")); assertThat(request.getKey().get("name").getS(), is("salmon")); assertThat(request.getConditionExpression(), is("stock > :zero")); assertThat(request.getUpdateExpression(), is("SET #stock = #stock - :stock")); assertThat(request.getExpressionAttributeNames().get("#stock"), is("stock")); assertThat(request.getExpressionAttributeValues().get(":stock").getN(), is("1")); assertThat(request.getExpressionAttributeValues().get(":zero").getN(), is("0")); return null; }).when(dynamoDB).updateItem(ArgumentMatchers.any(UpdateItemRequest.class)); handler = new OrderHandler(dynamoDB); input = new APIGatewayProxyRequestEvent(); input.setBody("{\"name\" : \"salmon\"}"); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(200)); assertThat(output.getBody(), is("{\"salmon\" : 1}")); }
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> params; try { params = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); } catch (JsonParseException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(e.getMessage()); } catch (IOException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody(e.getMessage()); } String name = params.get("name"); Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(name)); String condition = "stock > :zero"; String update = "SET #stock = #stock - :stock"; Map<String, String> names = new HashMap<>(); names.put("#stock", "stock"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":stock", new AttributeValue().withN(String.valueOf(1))); values.put(":zero", new AttributeValue().withN(String.valueOf(0))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withConditionExpression(condition).withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); } catch (ConditionalCheckFailedException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(name + " is out of stock"); } catch (AmazonDynamoDBException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody("HTTP " + e.getStatusCode() + " " + e.getErrorCode() + " in " + e.getServiceName() + "; " + e.getMessage()); } return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody("{\"" + name + "\" : 1}"); }
OrderHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> params; try { params = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); } catch (JsonParseException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(e.getMessage()); } catch (IOException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody(e.getMessage()); } String name = params.get("name"); Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(name)); String condition = "stock > :zero"; String update = "SET #stock = #stock - :stock"; Map<String, String> names = new HashMap<>(); names.put("#stock", "stock"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":stock", new AttributeValue().withN(String.valueOf(1))); values.put(":zero", new AttributeValue().withN(String.valueOf(0))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withConditionExpression(condition).withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); } catch (ConditionalCheckFailedException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(name + " is out of stock"); } catch (AmazonDynamoDBException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody("HTTP " + e.getStatusCode() + " " + e.getErrorCode() + " in " + e.getServiceName() + "; " + e.getMessage()); } return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody("{\"" + name + "\" : 1}"); } }
OrderHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> params; try { params = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); } catch (JsonParseException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(e.getMessage()); } catch (IOException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody(e.getMessage()); } String name = params.get("name"); Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(name)); String condition = "stock > :zero"; String update = "SET #stock = #stock - :stock"; Map<String, String> names = new HashMap<>(); names.put("#stock", "stock"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":stock", new AttributeValue().withN(String.valueOf(1))); values.put(":zero", new AttributeValue().withN(String.valueOf(0))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withConditionExpression(condition).withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); } catch (ConditionalCheckFailedException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(name + " is out of stock"); } catch (AmazonDynamoDBException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody("HTTP " + e.getStatusCode() + " " + e.getErrorCode() + " in " + e.getServiceName() + "; " + e.getMessage()); } return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody("{\"" + name + "\" : 1}"); } OrderHandler();  OrderHandler(AmazonDynamoDB dynamoDB); }
OrderHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> params; try { params = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); } catch (JsonParseException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(e.getMessage()); } catch (IOException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody(e.getMessage()); } String name = params.get("name"); Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(name)); String condition = "stock > :zero"; String update = "SET #stock = #stock - :stock"; Map<String, String> names = new HashMap<>(); names.put("#stock", "stock"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":stock", new AttributeValue().withN(String.valueOf(1))); values.put(":zero", new AttributeValue().withN(String.valueOf(0))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withConditionExpression(condition).withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); } catch (ConditionalCheckFailedException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(name + " is out of stock"); } catch (AmazonDynamoDBException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody("HTTP " + e.getStatusCode() + " " + e.getErrorCode() + " in " + e.getServiceName() + "; " + e.getMessage()); } return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody("{\"" + name + "\" : 1}"); } OrderHandler();  OrderHandler(AmazonDynamoDB dynamoDB); APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context); }
OrderHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { ObjectMapper mapper = new ObjectMapper(); Map<String, String> params; try { params = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); } catch (JsonParseException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(e.getMessage()); } catch (IOException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody(e.getMessage()); } String name = params.get("name"); Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(name)); String condition = "stock > :zero"; String update = "SET #stock = #stock - :stock"; Map<String, String> names = new HashMap<>(); names.put("#stock", "stock"); Map<String, AttributeValue> values = new HashMap<>(); values.put(":stock", new AttributeValue().withN(String.valueOf(1))); values.put(":zero", new AttributeValue().withN(String.valueOf(0))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withConditionExpression(condition).withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); try { dynamoDB.updateItem(request); } catch (ConditionalCheckFailedException e) { return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody(name + " is out of stock"); } catch (AmazonDynamoDBException e) { return new APIGatewayProxyResponseEvent().withStatusCode(500).withBody("HTTP " + e.getStatusCode() + " " + e.getErrorCode() + " in " + e.getServiceName() + "; " + e.getMessage()); } return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody("{\"" + name + "\" : 1}"); } OrderHandler();  OrderHandler(AmazonDynamoDB dynamoDB); APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context);  }