View Javadoc
1   /*
2    * Copyright (c) 2012-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Integration case for {@link Region}.
47   * @since 0.1
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 }