com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreate_withItem() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Item mockTableItem = mock(Item.class);
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));

    // When
    final StubItem returnedItem = dynamoDocumentStoreTemplate.create(stubItem);

    // Then
    final ArgumentCaptor<PutItemSpec> getItemRequestCaptor = ArgumentCaptor.forClass(PutItemSpec.class);
    verify(mockTable).putItem(getItemRequestCaptor.capture());

    final PutItemSpec spec = getItemRequestCaptor.getValue();
    assertEquals(itemId.value(), spec.getItem().get("id"));

    assertEquals(itemId.value(), returnedItem.getId());
    assertEquals(stubItem.getStringProperty(), returnedItem.getStringProperty());
    assertEquals(stubItem.getStringProperty2(), returnedItem.getStringProperty2());
    assertEquals(stubItem.getStringSetProperty(), returnedItem.getStringSetProperty());
}
 
Example #2
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreate_withItemWithNullProperty() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);
    stubItem.setStringProperty2(null);

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Item mockTableItem = mock(Item.class);
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));

    // When
    dynamoDocumentStoreTemplate.create(stubItem);

    // Then
    final ArgumentCaptor<PutItemSpec> getItemRequestCaptor = ArgumentCaptor.forClass(PutItemSpec.class);
    verify(mockTable).putItem(getItemRequestCaptor.capture());

    final PutItemSpec spec = getItemRequestCaptor.getValue();
    assertFalse(spec.getItem().hasAttribute("stringProperty2"));
}
 
Example #3
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotCreate_withItem() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);

    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);

    final Table mockTable = mock(Table.class);
    when(mockDynamoDBClient.getTable(any(String.class))).thenReturn(mockTable);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    final Item mockTableItem = mock(Item.class);
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));

    doThrow(RuntimeException.class).when(mockTable).putItem(any(PutItemSpec.class));
    RuntimeException thrownException = null;

    // When
    try {
        dynamoDocumentStoreTemplate.create(stubItem);
    } catch (final RuntimeException runtimeException) {
        thrownException = runtimeException;
    }

    // Then
    assertNotNull(thrownException);
}
 
Example #4
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUpdate_withItem() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);
    final StubItem previousStubItem = generateRandomStubItem(itemId);
    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    final Table mockTable = mock(Table.class);
    final Item mockTableItem = mock(Item.class);
    final PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.addComponent("id", itemId.value());
    final Item previousItem = mock(Item.class);

    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);
    when(mockDynamoDBClient.getTable(schemaName + "." + tableName)).thenReturn(mockTable);
    when(mockTable.getItem(any(PrimaryKey.class))).thenReturn(previousItem);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    when(previousItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(previousStubItem));
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));

    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    // When
    final StubItem returnedItem = dynamoDocumentStoreTemplate.update(stubItem);

    // Then
    final ArgumentCaptor<PutItemSpec> putItemRequestCaptor = ArgumentCaptor.forClass(PutItemSpec.class);
    verify(mockTable).putItem(putItemRequestCaptor.capture());
    final PutItemSpec spec = putItemRequestCaptor.getValue();
    assertEquals(itemId.value(), spec.getItem().get("id"));
    assertEquals(itemId.value(), returnedItem.getId());
    assertEquals(stubItem.getStringProperty(), returnedItem.getStringProperty());
    assertEquals(stubItem.getStringProperty2(), returnedItem.getStringProperty2());
    assertEquals(stubItem.getStringSetProperty(), returnedItem.getStringSetProperty());
}
 
Example #5
Source File: DynamoDocumentStoreTemplateTest.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotUpdate_withPutItemException() {
    // Given
    final ItemId itemId = new ItemId(randomId());
    final StubItem stubItem = generateRandomStubItem(itemId);
    final StubItem previousStubItem = generateRandomStubItem(itemId);
    final ItemConfiguration itemConfiguration = new ItemConfiguration(StubItem.class, tableName);
    final Collection<ItemConfiguration> itemConfigurations = Arrays.asList(itemConfiguration);
    final Table mockTable = mock(Table.class);
    final Item mockTableItem = mock(Item.class);
    final PrimaryKey primaryKey = new PrimaryKey();
    primaryKey.addComponent("id", itemId.value());
    final Item previousItem = mock(Item.class);

    when(mockDatabaseSchemaHolder.itemConfigurations()).thenReturn(itemConfigurations);
    when(mockDynamoDBClient.getTable(schemaName + "." + tableName)).thenReturn(mockTable);
    when(mockTable.getItem(any(PrimaryKey.class))).thenReturn(previousItem);

    final DynamoDocumentStoreTemplate dynamoDocumentStoreTemplate = new DynamoDocumentStoreTemplate(
            mockDatabaseSchemaHolder);
    when(previousItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(previousStubItem));
    when(mockTableItem.toJSON()).thenReturn(dynamoDocumentStoreTemplate.itemToString(stubItem));
    when(mockTable.putItem(any(PutItemSpec.class))).thenThrow(ConditionalCheckFailedException.class);

    dynamoDocumentStoreTemplate.initialize(mockAmazonDynamoDbClient);

    // When
    OptimisticLockException thrownException = null;
    try {
        dynamoDocumentStoreTemplate.update(stubItem);
    } catch (final OptimisticLockException optimisticLockException) {
        thrownException = optimisticLockException;
    }

    // Then
    assertNotNull(thrownException);
}
 
Example #6
Source File: APIDemoHandler.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    JSONObject responseJson = new JSONObject();

    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
    DynamoDB dynamoDb = new DynamoDB(client);

    try {
        JSONObject event = (JSONObject) parser.parse(reader);

        if (event.get("body") != null) {

            Person person = new Person((String) event.get("body"));

            dynamoDb.getTable(DYNAMODB_TABLE_NAME)
                .putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
                    .withString("name", person.getName())));
        }

        JSONObject responseBody = new JSONObject();
        responseBody.put("message", "New item created");

        JSONObject headerJson = new JSONObject();
        headerJson.put("x-custom-header", "my custom header value");

        responseJson.put("statusCode", 200);
        responseJson.put("headers", headerJson);
        responseJson.put("body", responseBody.toString());

    } catch (ParseException pex) {
        responseJson.put("statusCode", 400);
        responseJson.put("exception", pex);
    }

    OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
    writer.write(responseJson.toString());
    writer.close();
}
 
Example #7
Source File: SavePersonHandler.java    From tutorials with MIT License 5 votes vote down vote up
private PutItemOutcome persistData(PersonRequest personRequest) throws ConditionalCheckFailedException {
    return this.dynamoDb.getTable(DYNAMODB_TABLE_NAME)
      .putItem(
        new PutItemSpec().withItem(new Item()
          .withNumber("id", personRequest.getId())
          .withString("firstName", personRequest.getFirstName())
          .withString("lastName", personRequest.getLastName())
          .withNumber("age", personRequest.getAge())
          .withString("address", personRequest.getAddress())));
}
 
Example #8
Source File: MoviesItemOps02.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)  {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");

        int year = 2015;
        String title = "The Big New Movie";

        final Map<String, Object> infoMap = new HashMap<String, Object>();
        infoMap.put("plot",  "Nothing happens at all.");
        infoMap.put("rating",  0.0);
        Item item = new Item()
            .withPrimaryKey(new PrimaryKey("year", year, "title", title))
            .withMap("info", infoMap);
        
        // Attempt a conditional write.  We expect this to fail.

        PutItemSpec putItemSpec = new PutItemSpec()
            .withItem(item)
            .withConditionExpression("attribute_not_exists(#yr) and attribute_not_exists(title)")
            .withNameMap(new NameMap()
                .with("#yr", "year"));
        
        System.out.println("Attempting a conditional write...");
        try {
            table.putItem(putItemSpec);
            System.out.println("PutItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty());
        } catch (ConditionalCheckFailedException e) {
            e.printStackTrace(System.err);
            System.out.println("PutItem failed");
        }
    }