@Test public void testHandleRequest() { AmazonDynamoDB dynamoDB = mock(AmazonDynamoDB.class); doAnswer((i) -> { UpdateItemRequest request = i.getArgument(0); assertThat(request.getTableName(), is("item")); assertThat(request.getUpdateExpression(), is("SET #stock = :stock")); assertThat(request.getExpressionAttributeNames().get("#stock"), is("stock")); int stock = Integer.valueOf(request.getExpressionAttributeValues().get(":stock").getN()); assertTrue(0 <= stock && stock < 100); return null; }).when(dynamoDB).updateItem(ArgumentMatchers.any(UpdateItemRequest.class)); RestockHandler handler = new RestockHandler(dynamoDB); APIGatewayProxyResponseEvent output = handler.handleRequest(null, null); verify(dynamoDB, times(Item.values().length)).updateItem(ArgumentMatchers.any(UpdateItemRequest.class)); assertThat(output.getStatusCode(), is(200)); }
@Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { Random random = new SecureRandom(); Stream.of(Item.values()).forEach(i -> { Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(i.getName())); String update = "SET #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(random.nextInt(100)))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); dynamoDB.updateItem(request); }); return new APIGatewayProxyResponseEvent().withStatusCode(200); }
RestockHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { Random random = new SecureRandom(); Stream.of(Item.values()).forEach(i -> { Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(i.getName())); String update = "SET #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(random.nextInt(100)))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); dynamoDB.updateItem(request); }); return new APIGatewayProxyResponseEvent().withStatusCode(200); } }
RestockHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { Random random = new SecureRandom(); Stream.of(Item.values()).forEach(i -> { Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(i.getName())); String update = "SET #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(random.nextInt(100)))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); dynamoDB.updateItem(request); }); return new APIGatewayProxyResponseEvent().withStatusCode(200); } RestockHandler();  RestockHandler(AmazonDynamoDB dynamoDB); }
RestockHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { Random random = new SecureRandom(); Stream.of(Item.values()).forEach(i -> { Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(i.getName())); String update = "SET #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(random.nextInt(100)))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); dynamoDB.updateItem(request); }); return new APIGatewayProxyResponseEvent().withStatusCode(200); } RestockHandler();  RestockHandler(AmazonDynamoDB dynamoDB); @Override APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context); }
RestockHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { Random random = new SecureRandom(); Stream.of(Item.values()).forEach(i -> { Map<String, AttributeValue> key = new HashMap<>(); key.put("name", new AttributeValue().withS(i.getName())); String update = "SET #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(random.nextInt(100)))); UpdateItemRequest request = new UpdateItemRequest().withTableName("item").withKey(key) .withUpdateExpression(update).withExpressionAttributeNames(names) .withExpressionAttributeValues(values); dynamoDB.updateItem(request); }); return new APIGatewayProxyResponseEvent().withStatusCode(200); } RestockHandler();  RestockHandler(AmazonDynamoDB dynamoDB); @Override APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context);  }