View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.dynamo;
6   
7   import java.util.Iterator;
8   import org.apache.commons.lang3.RandomStringUtils;
9   import org.hamcrest.MatcherAssert;
10  import org.hamcrest.Matchers;
11  import org.junit.jupiter.api.Assumptions;
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Disabled;
14  import org.junit.jupiter.api.Test;
15  import software.amazon.awssdk.services.dynamodb.model.AttributeAction;
16  import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
17  import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate;
18  
19  /**
20   * Integration case for {@link Region}.
21   * @since 0.1
22   */
23  final class RegionITCase {
24  
25      @BeforeEach
26      void itTestCheck() {
27          Assumptions.assumeFalse(System.getProperty("failsafe.port", "").isEmpty());
28      }
29  
30      @Test
31      void queriesFrameSize() throws Exception {
32          final String name = RandomStringUtils.secure().nextAlphabetic(8);
33          final RegionMock mock = new RegionMock();
34          final Table tbl = mock.get(name).table(name);
35          final String hash = RandomStringUtils.secure().nextAlphanumeric(10);
36          for (int idx = 0; idx < 5; ++idx) {
37              tbl.put(
38                  new Attributes()
39                      .with(mock.hash(), hash)
40                      .with(mock.range(), idx)
41                      .with("some-attr", "val")
42              );
43          }
44          MatcherAssert.assertThat(
45              "should has size 5",
46              tbl.frame()
47                  .where(mock.hash(), Conditions.equalTo(hash))
48                  .through(new QueryValve().withLimit(1)),
49              Matchers.hasSize(5)
50          );
51      }
52  
53      @Test
54      void scansFrameSize() throws Exception {
55          final String name = RandomStringUtils.secure().nextAlphabetic(8);
56          final RegionMock mock = new RegionMock();
57          final Table tbl = mock.get(name).table(name);
58          final String attr = RandomStringUtils.secure().nextAlphabetic(8);
59          final String value = RandomStringUtils.secure().nextAlphanumeric(10);
60          final String hash = RandomStringUtils.secure().nextAlphanumeric(10);
61          for (int idx = 0; idx < 5; ++idx) {
62              tbl.put(
63                  new Attributes()
64                      .with(mock.hash(), hash)
65                      .with(mock.range(), idx)
66                      .with(attr, value)
67              );
68          }
69          MatcherAssert.assertThat(
70              "should has size 5",
71              tbl.frame()
72                  .where(attr, Conditions.equalTo(value))
73                  .through(
74                      new ScanValve()
75                          .withLimit(10)
76                          .withAttributeToGet(attr)
77                  ),
78              Matchers.hasSize(5)
79          );
80      }
81  
82      @Test
83      void readsAttributeFromItem() throws Exception {
84          final String name = RandomStringUtils.secure().nextAlphabetic(8);
85          final RegionMock mock = new RegionMock();
86          final Table tbl = mock.get(name).table(name);
87          final String attr = RandomStringUtils.secure().nextAlphabetic(8);
88          final String value = RandomStringUtils.secure().nextAlphanumeric(10);
89          tbl.put(
90              new Attributes()
91                  .with(mock.hash(), "somehash")
92                  .with(mock.range(), 0)
93                  .with(attr, value)
94          );
95          MatcherAssert.assertThat(
96              "should equal to attribute value",
97              tbl.frame()
98                  .where(attr, Conditions.equalTo(value))
99                  .through(
100                     new ScanValve()
101                         .withLimit(10)
102                         .withAttributeToGet(attr)
103                 )
104                 .iterator().next().get(attr).s(),
105             Matchers.equalTo(value)
106         );
107     }
108 
109     @Test
110     void updatesAndReadsModifiedAttribute() throws Exception {
111         final String name = RandomStringUtils.secure().nextAlphabetic(8);
112         final RegionMock mock = new RegionMock();
113         final Table tbl = mock.get(name).table(name);
114         final String attr = RandomStringUtils.secure().nextAlphabetic(8);
115         final String value = RandomStringUtils.secure().nextAlphanumeric(10);
116         final String hash = RandomStringUtils.secure().nextAlphanumeric(10);
117         tbl.put(
118             new Attributes()
119                 .with(mock.hash(), hash)
120                 .with(mock.range(), 0)
121                 .with(attr, value)
122         );
123         final Iterator<Item> items = tbl.frame()
124             .where(attr, Conditions.equalTo(value))
125             .through(
126                 new ScanValve()
127                     .withLimit(10)
128                     .withAttributeToGet(attr)
129             ).iterator();
130         final Item item = items.next();
131         item.put(
132             attr,
133             AttributeValueUpdate.builder()
134                 .value(AttributeValue.builder().s("empty").build())
135                 .action(AttributeAction.PUT)
136                 .build()
137         );
138         MatcherAssert.assertThat(
139             "should not equal to original value",
140             tbl.frame()
141                 .where(mock.hash(), hash)
142                 .where(mock.range(), Conditions.equalTo(0))
143                 .through(new ScanValve())
144                 .iterator().next()
145                 .get(attr).s(),
146             Matchers.not(Matchers.equalTo(value))
147         );
148     }
149 
150     @Test
151     @Disabled
152     void retrievesAttributesFromDynamo() throws Exception {
153         final String name = RandomStringUtils.secure().nextAlphabetic(8);
154         final RegionMock mock = new RegionMock();
155         final Table tbl = mock.get(name).table(name);
156         final int idx = 10;
157         final String hash = "7abc5cba";
158         final String attr = "some-attribute";
159         tbl.put(
160             new Attributes()
161                 .with(mock.hash(), hash)
162                 .with(mock.range(), idx)
163                 .with(attr, "test-value")
164         );
165         MatcherAssert.assertThat(
166             "should not retrieve something",
167             tbl.frame()
168                 .where(mock.hash(), hash)
169                 .where(mock.range(), Conditions.equalTo(idx))
170                 .through(
171                     new QueryValve()
172                         .withAttributeToGet(attr)
173                         .withConsistentRead(true)
174                         .withLimit(50)
175                 )
176                 .iterator().next()
177                 .has("something"),
178             Matchers.is(false)
179         );
180     }
181 
182 }