@Override public AttributeValue marshall(Object obj) { String string = (String) obj; if (string.length() == 0) { // Sticking with the legacy behavior for now. return null; } return new AttributeValue().withS(string); } }
.withN(entry.getValue().getN()) .withNS(entry.getValue().getNS()) .withS(entry.getValue().getS()) .withSS(entry.getValue().getSS()) .withM(entry.getValue().getM())
@Override public AttributeValue marshall(Object obj) { return new AttributeValue().withS(obj.toString()); } }
private Map<String, AttributeValue> createPrimaryKey(String key) { Map<String, AttributeValue> k = new HashMap<>(); if (primaryKeyType == PrimaryKeyType.HASH) { k.put(primaryKeyName, new AttributeValue().withS(key)); } else if (primaryKeyType == PrimaryKeyType.HASH_AND_RANGE) { k.put(hashKeyName, new AttributeValue().withS(hashKeyValue)); k.put(primaryKeyName, new AttributeValue().withS(key)); } else { throw new RuntimeException("Assertion Error: impossible primary key type"); } return k; } }
@Override public AttributeValue marshall(Object obj) { return new AttributeValue().withS( DateUtils.formatISO8601Date(((Calendar) obj).getTime())); } }
@Override public AttributeValue marshall(Object obj) { return new AttributeValue().withS( DateUtils.formatISO8601Date((Date) obj)); } }
@Override public AttributeValue marshall(Object obj) { S3Link s3link = (S3Link) obj; if ( s3link.getBucketName() == null || s3link.getKey() == null ) { // insufficient S3 resource specification return null; } return new AttributeValue().withS(s3link.toJson()); } }
return result.withBOOL((Boolean)value); } else if (value instanceof String) { return result.withS((String) value); } else if (value instanceof BigDecimal) { BigDecimal bd = (BigDecimal) value;
@Override public List<Environment> listEnvironments( @NonNull final Cluster cluster, final String environmentNamePrefix) throws InternalServiceException { try { final DynamoDBQueryExpression<EnvironmentDDBRecord> queryExpression = new DynamoDBQueryExpression<EnvironmentDDBRecord>() .withHashKeyValues( EnvironmentDDBRecord.withHashKeys(cluster.generateAccountIdCluster())); if (environmentNamePrefix != null) { queryExpression.withRangeKeyCondition( ENVIRONMENT_NAME_RANGE_KEY, new Condition() .withComparisonOperator(ComparisonOperator.BEGINS_WITH) .withAttributeValueList(new AttributeValue().withS(environmentNamePrefix))); } return dynamoDBMapper .query(EnvironmentDDBRecord.class, queryExpression) .stream() .map(environmentMapper::toEnvironment) .collect(Collectors.toList()); } catch (final AmazonServiceException e) { throw new InternalServiceException( String.format("Could not query environments for cluster %s", cluster.toString()), e); } }
@Test public void testListEnvironmentsWithEnvironmentNamePrefix() throws Exception { final String environmentNamePrefix = "environmentNamePrefix"; when(environmentDDBRecords.stream()).thenReturn(Stream.of(environmentDDBRecord)); doReturn(environmentDDBRecords) .when(dynamoDBMapper) .query(eq(EnvironmentDDBRecord.class), any()); when(environmentMapper.toEnvironment(environmentDDBRecord)).thenReturn(environment); final List<Environment> result = environmentRepositoryDDB.listEnvironments(cluster, environmentNamePrefix); verify(dynamoDBMapper) .query(eq(EnvironmentDDBRecord.class), ddbQueryExpressionCaptor.capture()); verify(environmentMapper).toEnvironment(environmentDDBRecord); final EnvironmentDDBRecord queriedEnvironmentDDBRecord = ddbQueryExpressionCaptor.getValue().getHashKeyValues(); final Map<String, Condition> queryConditions = ddbQueryExpressionCaptor.getValue().getRangeKeyConditions(); assertThat(queriedEnvironmentDDBRecord.getAccountIdCluster()) .isEqualTo(cluster.generateAccountIdCluster()); assertThat(queryConditions).isNotEmpty().hasSize(1); assertThat(queryConditions).containsKey(ENVIRONMENT_NAME_RANGE_KEY); assertThat(queryConditions) .containsValue( new Condition() .withComparisonOperator(ComparisonOperator.BEGINS_WITH) .withAttributeValueList(new AttributeValue().withS(environmentNamePrefix))); assertThat(result.size()).isEqualTo(1); assertThat(result.get(0)).isEqualTo(environment); }
new Condition() .withComparisonOperator(ComparisonOperator.BEGINS_WITH.toString()) .withAttributeValueList(new AttributeValue().withS(clusterNamePrefix)));
@Override public AttributeValue marshall(Object obj) { final String string = (String) obj; if (string.length() == 0) { // Sticking with the legacy behavior for now. return null; } return new AttributeValue().withS(string); } }
@Override public AttributeValue marshall(Object obj) { return new AttributeValue().withS( DateUtils.formatISO8601Date((Date) obj)); } }
private Map<String, AttributeValue> getItem(String id, Map<String, Object> attributes) { Map<String, AttributeValue> item = new HashMap<>(); item.put(partitionKeyName, new AttributeValue().withS(id)); item.put(attributesKeyName, new AttributeValue().withM(ItemUtils.fromSimpleMap(attributes))); return item; }
@Override public Map<String, AttributeValue> transform(Parameters<?> parameters) { Map<String, AttributeValue> upperCased = new HashMap<String, AttributeValue>(); for (Map.Entry<String, AttributeValue> curr : parameters.getAttributeValues() .entrySet()) { upperCased.put(curr.getKey(), new AttributeValue().withS(StringUtils.upperCase(curr.getValue().getS()))); } return upperCased; }
@Test public void testUnannotatedObject() throws Exception { final Method getter = UnannotatedSubClass.class.getMethod("getChild"); final Method setter = UnannotatedSubClass.class .getMethod("setChild", UnannotatedSubClass.class); try { CONVERTER.unconvert(getter, setter, new AttributeValue().withS("")); Assert.fail("Expected DynamoDBMappingException"); } catch (final DynamoDBMappingException e) { } }
/** Test on IllegalArgumentException when providing duplicated keys. */ @Test(expected = IllegalArgumentException.class) public void testDuplicatedKeysException() { new PutItemRequest() .addItemEntry("hash-key", new AttributeValue().withS("1")) .addItemEntry("hash-key", new AttributeValue().withS("2")); }
@Test public void testMergeExpectedAttributeValueConditionsNoInternalAssertions() { Map<String, ExpectedAttributeValue> userProvidedConditions = new HashMap<String, ExpectedAttributeValue>(); ExpectedAttributeValue user = new ExpectedAttributeValue().withValue(new AttributeValue() .withS("user")); userProvidedConditions.put("user", user); Map<String, ExpectedAttributeValue> merged = DynamoDBMapper .mergeExpectedAttributeValueConditions( null, userProvidedConditions, "AND"); assertEquals(merged.get("user").getValue().getS(), "user"); assertEquals(merged.size(), 1); }
@Test public void testMergeExpectedAttributeValueConditionsNoUserProvidedConditions() { Map<String, ExpectedAttributeValue> internalAssertions = new HashMap<String, ExpectedAttributeValue>(); ExpectedAttributeValue internal = new ExpectedAttributeValue() .withValue(new AttributeValue().withS("internal")); internalAssertions.put("internal", internal); Map<String, ExpectedAttributeValue> merged = DynamoDBMapper .mergeExpectedAttributeValueConditions( internalAssertions, null, "AND"); assertEquals(merged.get("internal").getValue().getS(), "internal"); assertEquals(merged.size(), 1); }
/** Test on using predefined map entry setter to provide map parameter. */ @Test public void testPredefinedMapEntryMethod() { ScanRequest scanRequest = new ScanRequest() .addExclusiveStartKeyEntry("hash-key", new AttributeValue().withS("1")) .addExclusiveStartKeyEntry("range-key", new AttributeValue().withS("2")); Map<String, AttributeValue> item = scanRequest.getExclusiveStartKey(); assertEquals(2, item.size()); assertEquals("1", item.get("hash-key").getS()); assertEquals("2", item.get("range-key").getS()); }