Search in sources :

Example 1 with Region

use of com.jcabi.dynamo.Region in project wring by yegor256.

the class Dynamo method connect.

/**
 * Connect.
 * @return Region
 */
private static Region connect() {
    final String key = Manifests.read("Wring-DynamoKey");
    final Credentials creds = new Credentials.Simple(key, Manifests.read("Wring-DynamoSecret"));
    final Region region;
    if (key.startsWith("AAAAA")) {
        final String property = System.getProperty("dynamo.port");
        if (property == null) {
            throw new IllegalStateException("You're not supposed to run this test outside of Maven");
        }
        final int port = Integer.parseInt(property);
        region = new Region.Simple(new Credentials.Direct(creds, port));
        Logger.warn(Dynamo.class, "test DynamoDB at port #%d", port);
    } else {
        region = new Region.Prefixed(new ReRegion(new Region.Simple(creds)), "wring-");
    }
    Logger.info(Dynamo.class, "DynamoDB connected as %s", key);
    return region;
}
Also used : ReRegion(com.jcabi.dynamo.retry.ReRegion) ReRegion(com.jcabi.dynamo.retry.ReRegion) Region(com.jcabi.dynamo.Region) Credentials(com.jcabi.dynamo.Credentials)

Example 2 with Region

use of com.jcabi.dynamo.Region in project jcabi-dynamo by jcabi.

the class MkRegionTest method storesAndReadsAttributes.

/**
 * MkRegion can store and read items.
 * @throws Exception If some problem inside
 */
@Test
public void storesAndReadsAttributes() throws Exception {
    final String name = "users";
    final String key = "id";
    final String attr = "description";
    final String nattr = "thenumber";
    final Region region = new MkRegion(new H2Data().with(name, new String[] { key }, attr, nattr));
    final Table table = region.table(name);
    table.put(new Attributes().with(key, "32443").with(attr, "first value to \n\t€ save").with(nattr, "150"));
    final Item item = table.frame().iterator().next();
    MatcherAssert.assertThat(item.has(attr), Matchers.is(true));
    MatcherAssert.assertThat(item.get(attr).getS(), Matchers.containsString("\n\t\u20ac save"));
    item.put(attr, new AttributeValueUpdate().withValue(new AttributeValue("this is another value")));
    MatcherAssert.assertThat(item.get(attr).getS(), Matchers.containsString("another value"));
    MatcherAssert.assertThat(item.get(nattr).getN(), Matchers.endsWith("50"));
}
Also used : Item(com.jcabi.dynamo.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.jcabi.dynamo.Table) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Attributes(com.jcabi.dynamo.Attributes) Region(com.jcabi.dynamo.Region) Test(org.junit.Test)

Example 3 with Region

use of com.jcabi.dynamo.Region in project jcabi-dynamo by jcabi.

the class ReRegionTest method retriesAwsCalls.

/**
 * ReRegion can retry on some AWS calls.
 * @throws Exception If some problem inside
 */
@Test
public void retriesAwsCalls() throws Exception {
    final Table table = Mockito.mock(Table.class);
    final Attributes attrs = new Attributes();
    final String msg = "hey you";
    Mockito.doThrow(new AmazonClientException(msg)).when(table).put(attrs);
    final Region origin = Mockito.mock(Region.class);
    Mockito.doReturn(table).when(origin).table(Mockito.anyString());
    final Region region = new ReRegion(origin);
    try {
        region.table("test").put(attrs);
        Assert.fail("exception expected here");
    } catch (final AmazonClientException ex) {
        assert ex.getMessage().equals(msg);
    }
    Mockito.verify(table, Mockito.times(Tv.THREE)).put(attrs);
}
Also used : Table(com.jcabi.dynamo.Table) AmazonClientException(com.amazonaws.AmazonClientException) Attributes(com.jcabi.dynamo.Attributes) Region(com.jcabi.dynamo.Region) Test(org.junit.Test)

Example 4 with Region

use of com.jcabi.dynamo.Region in project jcabi-dynamo by jcabi.

the class MkRegionTest method storesAndReadsSingleAttribute.

/**
 * MkRegion can store and read items.
 * @throws Exception If some problem inside
 */
@Test
public void storesAndReadsSingleAttribute() throws Exception {
    final String table = "ideas";
    final String key = "number";
    final String attr = "total";
    final Region region = new MkRegion(new H2Data().with(table, new String[] { key }, attr));
    final Table tbl = region.table(table);
    tbl.put(new Attributes().with(key, "32443").with(attr, "0"));
    final Item item = tbl.frame().iterator().next();
    item.put(attr, new AttributeValueUpdate().withValue(new AttributeValue().withN("2")).withAction(AttributeAction.PUT));
    MatcherAssert.assertThat(item.get(attr).getN(), Matchers.equalTo("2"));
}
Also used : Item(com.jcabi.dynamo.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.jcabi.dynamo.Table) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Attributes(com.jcabi.dynamo.Attributes) Region(com.jcabi.dynamo.Region) Test(org.junit.Test)

Aggregations

Region (com.jcabi.dynamo.Region)4 Attributes (com.jcabi.dynamo.Attributes)3 Table (com.jcabi.dynamo.Table)3 Test (org.junit.Test)3 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)2 AttributeValueUpdate (com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate)2 Item (com.jcabi.dynamo.Item)2 AmazonClientException (com.amazonaws.AmazonClientException)1 Credentials (com.jcabi.dynamo.Credentials)1 ReRegion (com.jcabi.dynamo.retry.ReRegion)1