View Javadoc
1   /*
2    * Copyright (c) 2012-2022, 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.simpledb;
31  
32  import com.amazonaws.services.simpledb.model.SelectRequest;
33  import com.jcabi.aspects.Tv;
34  import org.apache.commons.lang3.RandomStringUtils;
35  import org.hamcrest.MatcherAssert;
36  import org.hamcrest.Matchers;
37  import org.junit.jupiter.api.Assumptions;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Integration case for {@link Region}.
42   *
43   * @since 0.1
44   */
45  final class RegionITCase {
46  
47      /**
48       * SimpleDB key.
49       */
50      private static final String KEY =
51          System.getProperty("failsafe.sdb.key");
52  
53      /**
54       * SimpleDB secret key.
55       */
56      private static final String SECRET =
57          System.getProperty("failsafe.sdb.secret");
58  
59      @Test
60      void putsAndRemovesIndividualItems() {
61          final Domain domain = this.domain();
62          try {
63              final String name = RandomStringUtils.randomAlphanumeric(Tv.TEN);
64              final String attr = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
65              final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
66              for (int idx = 0; idx < 2; ++idx) {
67                  domain.item(name).put(attr, value);
68                  MatcherAssert.assertThat(
69                      domain.item(name), Matchers.hasKey(attr)
70                  );
71                  domain.item(name).remove(attr);
72                  MatcherAssert.assertThat(
73                      domain.item(name), Matchers.not(Matchers.hasKey(attr))
74                  );
75              }
76          } finally {
77              domain.drop();
78          }
79      }
80  
81      @Test
82      void selectsMultipleItems() {
83          final Domain domain = this.domain();
84          try {
85              final String attr = "alpha";
86              domain.item("first").put(attr, "val-99");
87              domain.item("second").put("beta", "");
88              MatcherAssert.assertThat(
89                  domain.select(
90                      new SelectRequest().withSelectExpression(
91                          String.format(
92                              "SELECT * FROM `%s` WHERE `%s` = 'val-99'",
93                              domain.name(), attr
94                          )
95                      ).withConsistentRead(true)
96                  ),
97                  Matchers.hasItem(Matchers.hasKey(attr))
98              );
99          } finally {
100             domain.drop();
101         }
102     }
103 
104     /**
105      * Region.Simple can select many items.
106      */
107     @Test
108     void selectsManyItems() {
109         final Domain domain = this.domain();
110         try {
111             for (int idx = 0; idx < Tv.TEN; ++idx) {
112                 domain.item(String.format("i-%d", idx)).put("hey", "");
113             }
114             MatcherAssert.assertThat(
115                 domain.select(
116                     new SelectRequest().withSelectExpression(
117                         String.format("SELECT * FROM `%s`", domain.name())
118                     ).withConsistentRead(true)
119                 ),
120                 Matchers.iterableWithSize(Tv.TEN)
121             );
122         } finally {
123             domain.drop();
124         }
125     }
126 
127     /**
128      * Make domain.
129      * @return Domain
130      */
131     private Domain domain() {
132         Assumptions.assumeFalse(RegionITCase.KEY.isEmpty());
133         final Region region = new Region.Simple(
134             new Credentials.Simple(RegionITCase.KEY, RegionITCase.SECRET)
135         );
136         final Domain domain = region.domain(
137             String.format(
138                 "jcabi-test-%s",
139                 RandomStringUtils.randomAlphabetic(5)
140             )
141         );
142         domain.create();
143         return domain;
144     }
145 
146 }