1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.dynamo;
31
32 import com.amazonaws.services.dynamodbv2.model.AttributeAction;
33 import com.amazonaws.services.dynamodbv2.model.AttributeValue;
34 import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
35 import com.jcabi.aspects.Tv;
36 import java.util.Iterator;
37 import org.apache.commons.lang3.RandomStringUtils;
38 import org.hamcrest.MatcherAssert;
39 import org.hamcrest.Matchers;
40 import org.junit.jupiter.api.Assumptions;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Disabled;
43 import org.junit.jupiter.api.Test;
44
45
46
47
48
49 final class RegionITCase {
50
51 @BeforeEach
52 void itTestCheck() {
53 Assumptions.assumeFalse(System.getProperty("failsafe.port", "").isEmpty());
54 }
55
56 @Test
57 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
58 void worksWithAmazon() throws Exception {
59 final String name = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
60 final RegionMock mock = new RegionMock();
61 final Table tbl = mock.get(name).table(name);
62 final String attr = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
63 final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
64 final String hash = RandomStringUtils.randomAlphanumeric(Tv.TEN);
65 for (int idx = 0; idx < Tv.FIVE; ++idx) {
66 tbl.put(
67 new Attributes()
68 .with(mock.hash(), hash)
69 .with(mock.range(), idx)
70 .with(attr, value)
71 );
72 }
73 MatcherAssert.assertThat(
74 tbl.frame()
75 .where(mock.hash(), Conditions.equalTo(hash))
76 .through(new QueryValve().withLimit(1)),
77 Matchers.hasSize(Tv.FIVE)
78 );
79 final Frame frame = tbl.frame()
80 .where(attr, Conditions.equalTo(value))
81 .through(
82 new ScanValve()
83 .withLimit(Tv.TEN)
84 .withAttributeToGet(attr)
85 );
86 MatcherAssert.assertThat(frame, Matchers.hasSize(Tv.FIVE));
87 final Iterator<Item> items = frame.iterator();
88 final Item item = items.next();
89 final int range = Integer.parseInt(item.get(mock.range()).getN());
90 MatcherAssert.assertThat(
91 item.get(attr).getS(),
92 Matchers.equalTo(value)
93 );
94 item.put(
95 attr,
96 new AttributeValueUpdate(
97 new AttributeValue("empty"),
98 AttributeAction.PUT
99 )
100 );
101 MatcherAssert.assertThat(
102 tbl.frame()
103 .where(mock.hash(), hash)
104 .where(mock.range(), Conditions.equalTo(range))
105 .through(new ScanValve())
106 .iterator().next()
107 .get(attr).getS(),
108 Matchers.not(Matchers.equalTo(value))
109 );
110 items.remove();
111 }
112
113 @Test
114 @Disabled
115 void retrievesAttributesFromDynamo() throws Exception {
116 final String name = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
117 final RegionMock mock = new RegionMock();
118 final Table tbl = mock.get(name).table(name);
119 final int idx = Tv.TEN;
120 final String hash = "7afe5efa";
121 final String attr = "some-attribute";
122 tbl.put(
123 new Attributes()
124 .with(mock.hash(), hash)
125 .with(mock.range(), idx)
126 .with(attr, "test-value")
127 );
128 MatcherAssert.assertThat(
129 tbl.frame()
130 .where(mock.hash(), hash)
131 .where(mock.range(), Conditions.equalTo(idx))
132 .through(
133 new QueryValve()
134 .withAttributeToGet(attr)
135 .withConsistentRead(true)
136 .withLimit(Tv.FIFTY)
137 )
138 .iterator().next()
139 .has("something"),
140 Matchers.is(false)
141 );
142 }
143
144 }