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.mock;
31  
32  import com.amazonaws.services.dynamodbv2.model.AttributeValue;
33  import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
34  import com.amazonaws.services.dynamodbv2.model.Condition;
35  import com.google.common.base.Joiner;
36  import com.google.common.collect.Lists;
37  import com.jcabi.dynamo.AttributeUpdates;
38  import com.jcabi.dynamo.Attributes;
39  import com.jcabi.dynamo.Conditions;
40  import java.io.File;
41  import java.nio.file.Path;
42  import java.util.Collections;
43  import java.util.List;
44  import org.hamcrest.MatcherAssert;
45  import org.hamcrest.Matchers;
46  import org.junit.jupiter.api.Disabled;
47  import org.junit.jupiter.api.Test;
48  import org.junit.jupiter.api.io.TempDir;
49  
50  /**
51   * Test case for {@link H2Data}.
52   * @since 0.10
53   */
54  final class H2DataTest {
55  
56      @Test
57      void storesAndReadsAttributes() throws Exception {
58          final String table = "users";
59          final String key = "user";
60          final int number = 43;
61          final String attr = "user.name";
62          final String value = "some\n\t\u20ac text";
63          final MkData data = new H2Data().with(
64              table, new String[] {key},
65              attr
66          );
67          data.put(table, new Attributes().with(key, number).with(attr, value));
68          MatcherAssert.assertThat(
69              data.iterate(
70                  table, new Conditions().with(key, Conditions.equalTo(number))
71              ).iterator().next(),
72              Matchers.hasEntry(
73                  Matchers.equalTo(attr),
74                  Matchers.equalTo(new AttributeValue(value))
75              )
76          );
77      }
78  
79      @Test
80      @Disabled
81      void storesToFile(@TempDir final Path temp) throws Exception {
82          final File file = temp.resolve("foo.txt").toFile();
83          final String table = "tbl";
84          final String key = "user";
85          final MkData data = new H2Data(file).with(
86              table, new String[] {key}
87          );
88          data.put(table, new Attributes().with(key, "x2"));
89          MatcherAssert.assertThat(file.exists(), Matchers.is(true));
90          MatcherAssert.assertThat(file.length(), Matchers.greaterThan(0L));
91      }
92  
93      @Test
94      void createsManyTables() throws Exception {
95          new H2Data()
96              .with("firsttable", new String[] {"firstid"}, "test")
97              .with("secondtable", new String[]{"secondid"});
98      }
99  
100     @Test
101     void createsTablesWithLongNames() throws Exception {
102         new H2Data().with(
103             Joiner.on("").join(Collections.nCopies(40, "X")),
104             new String[]{"k1"}
105         );
106     }
107 
108     @Test
109     void supportsTableNamesWithIllegalCharacters() throws Exception {
110         new H2Data().with(".-.", new String[]{"pk"});
111     }
112 
113     @Test
114     @Disabled
115     void supportsColumnNamesWithIllegalCharacters() throws Exception {
116         final String key = "0-.col.-0";
117         final String table = "test";
118         new H2Data().with(
119             table, new String[] {key}
120         ).put(table, new Attributes().with(key, "value"));
121     }
122 
123     @Test
124     void deletesRecords() throws Exception {
125         final String table = "customers";
126         final String field = "name";
127         final String man = "Kevin";
128         final String woman = "Helen";
129         final H2Data data = new H2Data()
130             .with(table, new String[]{field});
131         data.put(
132             table,
133             new Attributes().with(field, man)
134         );
135         data.put(
136             table,
137             new Attributes().with(field, woman)
138         );
139         data.delete(table, new Attributes().with(field, man));
140         final List<Attributes> rest = Lists.newArrayList(
141             data.iterate(table, new Conditions())
142         );
143         MatcherAssert.assertThat(
144             rest.size(),
145             Matchers.equalTo(1)
146         );
147         MatcherAssert.assertThat(
148             rest.get(0).get(field).getS(),
149             Matchers.equalTo(woman)
150         );
151     }
152 
153     @Test
154     void updatesTableAttributes() throws Exception {
155         final String table = "tests";
156         final String key = "tid";
157         final int number = 43;
158         final String attr = "descr";
159         final String value = "Dummy\n\t\u20ac text";
160         final String updated = "Updated";
161         final MkData data = new H2Data().with(
162             table, new String[] {key}, attr
163         );
164         data.put(table, new Attributes().with(key, number).with(attr, value));
165         data.update(
166             table,
167             new Attributes().with(key, number),
168             new AttributeUpdates().with(attr, "something else")
169         );
170         data.update(
171             table,
172             new Attributes().with(key, number),
173             new AttributeUpdates().with(attr, updated)
174         );
175         final Iterable<Attributes> result = data.iterate(
176             table, new Conditions().with(key, Conditions.equalTo(number))
177         );
178         MatcherAssert.assertThat(
179             result.iterator().next(),
180             Matchers.hasEntry(
181                 Matchers.equalTo(attr),
182                 Matchers.equalTo(new AttributeValue(updated))
183             )
184         );
185         MatcherAssert.assertThat(
186             result,
187             Matchers.iterableWithSize(1)
188         );
189     }
190 
191     @Test
192     void fetchesWithComparison() throws Exception {
193         final String table = "x12";
194         final String key = "foo1";
195         final String value = "bar2";
196         final MkData data = new H2Data().with(table, new String[] {key}, value);
197         data.put(table, new Attributes().with(key, "101").with(value, 0));
198         data.put(table, new Attributes().with(key, "102").with(value, 1));
199         MatcherAssert.assertThat(
200             data.iterate(table, new Conditions()),
201             Matchers.iterableWithSize(2)
202         );
203         MatcherAssert.assertThat(
204             data.iterate(
205                 table,
206                 new Conditions().with(
207                     value,
208                     new Condition()
209                         .withAttributeValueList(
210                             new AttributeValue().withN("0")
211                         )
212                         .withComparisonOperator(ComparisonOperator.GT)
213                 )
214             ).iterator().next().get(value).getN(),
215             Matchers.equalTo("1")
216         );
217     }
218 
219 }