@Test public void putItemWithHashKeyAndRangeKeyOverwriteItem() { try { createTable( hashRangeTableName, createStringAttributeDefinition("hashCode"), createStringAttributeDefinition("rangeCode")); } catch (ResourceInUseException riue) { // The table is already created } TestClassWithHashRangeKey value2a = new TestClassWithHashRangeKey(); value2a.setHashCode("hash2"); value2a.setRangeCode("range2"); value2a.setStringData("string2a"); value2a.setIntData(21); value2a.setStringSetData(set("stringSetVal2a1", "stringSetVal2a2")); value2a.setNumberSetData(set(3, 4)); mapper.save(value2a); TestClassWithHashRangeKey value2b = new TestClassWithHashRangeKey(); value2b.setHashCode("hash2"); value2b.setRangeCode("range2"); value2b.setStringData("string2b"); value2b.setIntData(22); value2b.setStringSetData(set("stringSetVal2b1", "stringSetVal2b2")); value2b.setNumberSetData(set(5, 6)); mapper.save(value2b); }
private void createNodeHistoryItems( NodeHistory nHistoryDB, String nid, String time, String values) { nHistoryDB.setNodeID(nid); nHistoryDB.setTime(time); nHistoryDB.setSAValues(values); mapper.save(nHistoryDB); String hashKey = nid; String rangeKey = time; NodeHistory nhKey = new NodeHistory(); nhKey.setNodeID(hashKey); Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withS(rangeKey)); DynamoDBQueryExpression<NodeHistory> queryExpression = new DynamoDBQueryExpression<NodeHistory>() .withHashKeyValues(nhKey) .withRangeKeyCondition("Time", rangeKeyCondition); List<NodeHistory> latestNodeHistory = mapper.query(NodeHistory.class, queryExpression); System.out.println("Item created: "); for (NodeHistory nh : latestNodeHistory) { System.out.format( "SerialNumber=%s, Time=%s, SAValues=%s\n", nh.getNodeID(), nh.getTime(), nh.getSAValues()); } }
@Test public void getHashRangeItemTest() { putItemWithHashKeyAndRangeKey(); putItemWithHashKeyAndRangeKeyOverwriteItem(); TestClassWithHashRangeKey value2c = new TestClassWithHashRangeKey(); value2c.setHashCode("hash2"); value2c.setRangeCode("range2c"); value2c.setStringData("string2c"); value2c.setIntData(23); value2c.setStringSetData(set("stringSetVal2c1", "stringSetVal2c2")); value2c.setNumberSetData(set(7, 8)); mapper.save(value2c); String hashCode = "hash2"; String rangeCode = "range2"; TestClassWithHashRangeKey value = mapper.load(TestClassWithHashRangeKey.class, hashCode, rangeCode); Assert.assertNotNull("Value not found.", value); Assert.assertEquals("Wrong hashCode.", hashCode, value.getHashCode()); Assert.assertEquals("Wrong rangeCode.", rangeCode, value.getRangeCode()); Assert.assertEquals("Wrong stringData.", "string2b", value.getStringData()); Assert.assertEquals("Wrong intData.", 22, value.getIntData()); Assert.assertEquals( "Wrong stringSetData.", set("stringSetVal2b1", "stringSetVal2b2"), value.getStringSetData()); Assert.assertEquals("Wrong numberSetData.", set(5, 6), value.getNumberSetData()); }
@BeforeClass public static void beforeClass() { dynamoAccessor = new Accessor(); dynMapper = dynamoAccessor.getDynMapper(); account = new Account(username); S3Link s3UserLink = dynMapper.createS3Link(bucketName, username + FieldNames.S3BucketKeys.userDataFormat); S3Link s3ContactLink = dynMapper.createS3Link(bucketName, username + FieldNames.S3BucketKeys.contactDataFormat); S3Link s3AllergyLink = dynMapper.createS3Link(bucketName, username + FieldNames.S3BucketKeys.allergyDataFormat); S3Link s3MedicationLink = dynMapper.createS3Link(bucketName, username + FieldNames.S3BucketKeys.medicationDataFormat); S3Link s3HistoryLink = dynMapper.createS3Link(bucketName, username + FieldNames.S3BucketKeys.historyDataFormat); account.setUserData(s3UserLink); account.setAllergyData(s3AllergyLink); account.setContactData(s3ContactLink); account.setHistoryData(s3HistoryLink); account.setMedicationData(s3MedicationLink); dynMapper.save(account); }
public List<CartItem> placeOrder(int userId) { List<CartItem> cartItems = getCartItemsFromUserId(userId); DynamoDBMapper mapper = this.conn.getMapper(); for (CartItem cartItem : cartItems) { cartItem.setIsOrdered(1); mapper.save(cartItem); } return cartItems; }
@Override public void requirePasswordChange(AppUser user, boolean require) throws NoSuchUserException { user = db.load(AppUser.class, user.getEmail()); if (user != null) { user.setPasswordChangeRequired(require); db.save(user); } else { throw new NoSuchUserException(user.getEmail()); } }
@Override public void setLocked(AppUser user, boolean locked) throws NoSuchUserException { user = db.load(AppUser.class, user.getEmail()); if (user != null) { user.setLocked(locked); db.save(user); } else { throw new NoSuchUserException(user.getEmail()); } }
/** * Saves Item into the cart. - DynamoDB. * * @param form */ public void saveCartItem(AddToCartForm form) { DynamoDBMapper mapper = this.conn.getMapper(); CartItem item = new CartItem(); item.setCartId(UUID.randomUUID().getLeastSignificantBits()); item.setProductId(form.getProductId()); item.setQuantity(form.getQuantity()); item.setUserId(form.getUserId()); item.setIsOrdered(form.getIsOrdered()); mapper.save(item); }
@Override public void revokeRole(AppUser user, String role) throws NoSuchUserException { user = db.load(AppUser.class, user.getEmail()); if (user != null) { user.revokeRole(role); db.save(user); } else { throw new NoSuchUserException(user.getEmail()); } }
public SimDetails displayDetails() { sim.setUserName("Pratheeth"); sim.setCountry("US"); sim.setExpiryDate("01/05/2016"); sim.setSimType("Prepaid"); mapper.save(sim); getSimDetails = mapper.load(SimDetails.class, "US", "Prasoon"); return getSimDetails; }
// Test: put item with HashKey @Test public void putItemWithHashKey() { createTable(hashTableName, createStringAttributeDefinition("code")); TestClassWithHashKey value = new TestClassWithHashKey(); value.setCode("hash1"); value.setStringData("string1"); value.setIntData(1); value.setStringSetData(set("stringSetVal1", "stringSetVal2")); value.setNumberSetData(set(1, 2)); mapper.save(value); }
/* * Updates one attribute/value pair for the specified user. */ public static void updateUserPreference(UserPreference updateUserPreference) { AmazonDynamoDBClient ddb = LoginActivity.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { mapper.save(updateUserPreference); } catch (AmazonServiceException ex) { LoginActivity.clientManager.wipeCredentialsOnAuthError(ex); } }
@Test public void utf8Test() { createGenericTable(hashTableName, "code"); TestClassWithHashKey value = new TestClassWithHashKey(); value.setCode("éáűőúöüóí"); value.setStringData("űáéúőóüöí"); mapper.save(value); TestClassWithHashKey readValue = mapper.load(TestClassWithHashKey.class, "éáűőúöüóí"); Assert.assertEquals("éáűőúöüóí", readValue.getCode()); Assert.assertEquals("űáéúőóüöí", readValue.getStringData()); }
@Test public void scanIndexForwardFalseTest() { createTable( hashRangeTableName, createStringAttributeDefinition("hashCode"), createStringAttributeDefinition("rangeCode")); { TestClassWithHashRangeKey c = new TestClassWithHashRangeKey(); c.setHashCode("code"); c.setRangeCode("1"); c.setStringData("first"); mapper.save(c); } { TestClassWithHashRangeKey c = new TestClassWithHashRangeKey(); c.setHashCode("code"); c.setRangeCode("2"); c.setStringData("second"); mapper.save(c); } String code = "code"; TestClassWithHashRangeKey hashKeyTemplate = new TestClassWithHashRangeKey(); hashKeyTemplate.setHashCode(code); Map<String, Condition> emptyRangeConditions = new HashMap<String, Condition>(); DynamoDBQueryExpression<TestClassWithHashRangeKey> query = new DynamoDBQueryExpression<TestClassWithHashRangeKey>() .withHashKeyValues(hashKeyTemplate) .withRangeKeyConditions(emptyRangeConditions) .withScanIndexForward(false) .withLimit(1); TestClassWithHashRangeKey res = mapper.query(TestClassWithHashRangeKey.class, query).get(0); Assert.assertEquals("second", res.getStringData()); }
@Override public String requestPasswordReset(AppUser _user) throws NoSuchUserException { AppUser user = db.load(AppUser.class, _user.getEmail()); if (user == null) { throw new NoSuchUserException(_user.getEmail()); } String signedToken = SignedToken.sign( _user.getEmail(), "hello", // TODO: Use KmsDao PASS_RESET_VALIDITY_MILLIS); user.setPasswordResetToken(signedToken); db.save(user); return signedToken; }
/* * Inserts ten users with userNo from 1 to 10 and random names. */ public static void insertUsers() { AmazonDynamoDBClient ddb = LoginActivity.clientManager.ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { for (int i = 1; i <= 10; i++) { UserPreference userPreference = new UserPreference(); userPreference.setUserNo(i); userPreference.setFirstName(Constants.getRandomName()); userPreference.setLastName(Constants.getRandomName()); mapper.save(userPreference); } } catch (AmazonServiceException ex) { LoginActivity.clientManager.wipeCredentialsOnAuthError(ex); } }
// returns list of items ordered so far by the user public CartItem decreaseQuantity(Long cartId) { DynamoDBMapper mapper = this.conn.getMapper(); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); Map<String, Condition> scanFilter = new HashMap<String, Condition>(); Condition scanCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withN(cartId.toString())); scanFilter.put("CartId", scanCondition); scanExpression.setScanFilter(scanFilter); PaginatedScanList<CartItem> items = mapper.scan(CartItem.class, scanExpression); CartItem item = items.get(0); item.setQuantity(item.getQuantity() - 1); mapper.save(item); return item; }
@Override public boolean resetPassword(String signedToken, String newPassword) { // TODO: use KmsDao String email = SignedToken.validate(signedToken, "hello"); if (email != null) { // Invalid or expired token return false; } AppUser user = db.load(AppUser.class, email); if (ObjectUtils.notEqual(signedToken, user.getPasswordResetToken())) { // Token is used more than once return false; } user.setPasswordHash(PasswordUtil.hash(newPassword, email)); user.setPasswordResetToken(null); db.save(user); return true; }
@Test public void limitTest() { createGenericHashRangeTable(hashRangeTableName, "hashCode", "rangeCode"); for (int i = 0; i < 10; i++) { TestClassWithHashRangeKey c = new TestClassWithHashRangeKey(); c.setHashCode("code"); c.setRangeCode(i + ""); mapper.save(c); } String code = "code"; TestClassWithHashRangeKey hashKeyTemplate = new TestClassWithHashRangeKey(); hashKeyTemplate.setHashCode(code); Map<String, Condition> emptyRangeConditions = new HashMap<String, Condition>(); DynamoDBQueryExpression<TestClassWithHashRangeKey> query1 = new DynamoDBQueryExpression<TestClassWithHashRangeKey>() .withHashKeyValues(hashKeyTemplate) .withRangeKeyConditions(emptyRangeConditions) .withLimit(1); DynamoDBQueryExpression<TestClassWithHashRangeKey> query3 = new DynamoDBQueryExpression<TestClassWithHashRangeKey>() .withHashKeyValues(hashKeyTemplate) .withRangeKeyConditions(emptyRangeConditions) .withLimit(3); DynamoDBQueryExpression<TestClassWithHashRangeKey> query20 = new DynamoDBQueryExpression<TestClassWithHashRangeKey>() .withHashKeyValues(hashKeyTemplate) .withRangeKeyConditions(emptyRangeConditions) .withLimit(20); Assert.assertEquals(1, mapper.query(TestClassWithHashRangeKey.class, query1).size()); Assert.assertEquals(3, mapper.query(TestClassWithHashRangeKey.class, query3).size()); Assert.assertEquals(10, mapper.query(TestClassWithHashRangeKey.class, query20).size()); }
@Test public void queryWithHashRangeKey() { putItemWithHashKeyAndRangeKey(); TestClassWithHashRangeKey value2c = new TestClassWithHashRangeKey(); value2c.setHashCode("hash2"); value2c.setRangeCode("range2c"); value2c.setStringData("string2c"); value2c.setIntData(23); value2c.setStringSetData(set("stringSetVal2c1", "stringSetVal2c2")); value2c.setNumberSetData(set(7, 8)); mapper.save(value2c); TestClassWithHashRangeKey value2d = new TestClassWithHashRangeKey(); value2d.setHashCode("hash2"); value2d.setRangeCode("range2d"); value2d.setStringData("string2d"); value2d.setIntData(24); value2d.setStringSetData(set("stringSetVal2d1", "stringSetVal2d2")); value2d.setNumberSetData(set(9, 10)); mapper.save(value2d); TestClassWithHashRangeKey value2e = new TestClassWithHashRangeKey(); value2e.setHashCode("hash2"); value2e.setRangeCode("range2e"); value2e.setStringData("string2e"); value2e.setIntData(25); value2e.setStringSetData(set("stringSetVal2e1", "stringSetVal2e2")); value2e.setNumberSetData(set(11, 12)); mapper.save(value2e); String hashCode = "hash2"; TestClassWithHashRangeKey hashKeyTemplate = new TestClassWithHashRangeKey(); hashKeyTemplate.setHashCode(hashCode); Condition rangeKeyCondition = new Condition(); List<AttributeValue> rangeValues = new ArrayList<AttributeValue>(); rangeValues.add(new AttributeValue().withS("range2c")); rangeValues.add(new AttributeValue().withS("range2d")); rangeKeyCondition.setComparisonOperator(ComparisonOperator.BETWEEN); rangeKeyCondition.setAttributeValueList(rangeValues); DynamoDBQueryExpression<TestClassWithHashRangeKey> query = new DynamoDBQueryExpression<TestClassWithHashRangeKey>() .withHashKeyValues(hashKeyTemplate) .withRangeKeyCondition("rangeCode", rangeKeyCondition); List<TestClassWithHashRangeKey> valueList = mapper.query(TestClassWithHashRangeKey.class, query); Assert.assertNotNull("Value list is null.", valueList); Assert.assertNotSame("Value list is empty.", 0, valueList.size()); Assert.assertEquals("Value list should have 2 items.", 2, valueList.size()); TestClassWithHashRangeKey value = valueList.get(0); Assert.assertEquals("Wrong hashCode.", hashCode, value.getHashCode()); Assert.assertEquals("Wrong rangeCode.", "range2c", value.getRangeCode()); Assert.assertEquals("Wrong stringData.", "string2c", value.getStringData()); Assert.assertEquals("Wrong intData.", 23, value.getIntData()); Assert.assertEquals( "Wrong stringSetData.", set("stringSetVal2c1", "stringSetVal2c2"), value.getStringSetData()); Assert.assertEquals("Wrong numberSetData.", set(7, 8), value.getNumberSetData()); value = valueList.get(1); Assert.assertEquals("Wrong hashCode.", hashCode, value.getHashCode()); Assert.assertEquals("Wrong rangeCode.", "range2d", value.getRangeCode()); Assert.assertEquals("Wrong stringData.", "string2d", value.getStringData()); Assert.assertEquals("Wrong intData.", 24, value.getIntData()); Assert.assertEquals( "Wrong stringSetData.", set("stringSetVal2d1", "stringSetVal2d2"), value.getStringSetData()); Assert.assertEquals("Wrong numberSetData.", set(9, 10), value.getNumberSetData()); }
@Override public void register(AppUser newUser) { newUser.setRoles(new HashSet<>()); db.save(newUser); }