View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.dynamo.mock;
6   
7   import com.google.common.base.Joiner;
8   import com.google.common.collect.Lists;
9   import com.jcabi.dynamo.AttributeUpdates;
10  import com.jcabi.dynamo.Attributes;
11  import com.jcabi.dynamo.Conditions;
12  import java.io.File;
13  import java.nio.file.Path;
14  import java.util.Collections;
15  import org.hamcrest.MatcherAssert;
16  import org.hamcrest.Matchers;
17  import org.junit.jupiter.api.Disabled;
18  import org.junit.jupiter.api.Test;
19  import org.junit.jupiter.api.io.TempDir;
20  import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
21  import software.amazon.awssdk.services.dynamodb.model.ComparisonOperator;
22  import software.amazon.awssdk.services.dynamodb.model.Condition;
23  
24  /**
25   * Test case for {@link H2Data}.
26   * @since 0.10
27   */
28  final class H2DataTest {
29  
30      @Test
31      void storesAndReadsAttributes() throws Exception {
32          final String table = "users";
33          final String key = "user";
34          final int number = 43;
35          final String attr = "user.name";
36          final String value = "some\n\t\u20ac text";
37          final MkData data = new H2Data().with(
38              table, new String[] {key},
39              attr
40          );
41          data.put(table, new Attributes().with(key, number).with(attr, value));
42          MatcherAssert.assertThat(
43              "should stores and reads attributes",
44              data.iterate(
45                  table, new Conditions().with(key, Conditions.equalTo(number))
46              ).iterator().next(),
47              Matchers.hasEntry(
48                  Matchers.equalTo(attr),
49                  Matchers.equalTo(AttributeValue.builder().s(value).build())
50              )
51          );
52      }
53  
54      @Test
55      @Disabled
56      void storesToFileAndExists(@TempDir final Path temp) throws Exception {
57          final File file = temp.resolve("foo.txt").toFile();
58          final String table = "tbl";
59          final String key = "user";
60          final MkData data = new H2Data(file).with(
61              table, new String[] {key}
62          );
63          data.put(table, new Attributes().with(key, "x2"));
64          MatcherAssert.assertThat(
65              "should exists the file",
66              file.exists(),
67              Matchers.is(true)
68          );
69      }
70  
71      @Test
72      void createsManyTables() throws Exception {
73          MatcherAssert.assertThat(
74              "should create two tables",
75              new H2Data()
76                  .with("firsttable", new String[] {"firstid"}, "test")
77                  .with("secondtable", new String[]{"secondid"}),
78              Matchers.notNullValue()
79          );
80      }
81  
82      @Test
83      void createsTablesWithLongNames() throws Exception {
84          MatcherAssert.assertThat(
85              "should create table with long name",
86              new H2Data().with(
87                  Joiner.on("").join(Collections.nCopies(40, "X")),
88                  new String[]{"k1"}
89              ),
90              Matchers.notNullValue()
91          );
92      }
93  
94      @Test
95      void supportsTableNamesWithIllegalCharacters() throws Exception {
96          MatcherAssert.assertThat(
97              "should support illegal characters in table name",
98              new H2Data().with(".-.", new String[]{"pk"}),
99              Matchers.notNullValue()
100         );
101     }
102 
103     @Test
104     @Disabled
105     void supportsColumnNamesWithIllegalCharacters() throws Exception {
106         MatcherAssert.assertThat(
107             "should support illegal characters in column name",
108             new H2Data().with(
109                 "test", new String[] {"0-.col.-0"}
110             ),
111             Matchers.notNullValue()
112         );
113     }
114 
115     @Test
116     void deletesRecords() throws Exception {
117         final String table = "customers";
118         final String field = "name";
119         final String man = "Kevin";
120         final String woman = "Helen";
121         final H2Data data = new H2Data()
122             .with(table, new String[]{field});
123         data.put(
124             table,
125             new Attributes().with(field, man)
126         );
127         data.put(
128             table,
129             new Attributes().with(field, woman)
130         );
131         data.delete(table, new Attributes().with(field, man));
132         MatcherAssert.assertThat(
133             "should delete Kevin and keep only Helen",
134             Lists.newArrayList(
135                 data.iterate(table, new Conditions())
136             ),
137             Matchers.contains(
138                 Matchers.hasEntry(
139                     Matchers.equalTo(field),
140                     Matchers.equalTo(
141                         AttributeValue.builder().s(woman).build()
142                     )
143                 )
144             )
145         );
146     }
147 
148     @Test
149     void updatesTableAttributes() throws Exception {
150         final String table = "tests";
151         final String key = "tid";
152         final int number = 43;
153         final String attr = "descr";
154         final String updated = "Updated";
155         final MkData data = new H2Data().with(
156             table, new String[] {key}, attr
157         );
158         data.put(
159             table,
160             new Attributes()
161                 .with(key, number)
162                 .with(attr, "Dummy\n\t\u20ac text")
163         );
164         data.update(
165             table,
166             new Attributes().with(key, number),
167             new AttributeUpdates().with(attr, "something else")
168         );
169         data.update(
170             table,
171             new Attributes().with(key, number),
172             new AttributeUpdates().with(attr, updated)
173         );
174         MatcherAssert.assertThat(
175             "should update to single correct entry",
176             Lists.newArrayList(
177                 data.iterate(
178                     table,
179                     new Conditions().with(key, Conditions.equalTo(number))
180                 )
181             ),
182             Matchers.contains(
183                 Matchers.hasEntry(
184                     Matchers.equalTo(attr),
185                     Matchers.equalTo(
186                         AttributeValue.builder().s(updated).build()
187                     )
188                 )
189             )
190         );
191     }
192 
193     @Test
194     void fetchesAllWithoutConditions() throws Exception {
195         final String table = "x12";
196         final String key = "foo1";
197         final String value = "bar2";
198         final MkData data = new H2Data().with(
199             table, new String[] {key}, value
200         );
201         data.put(table, new Attributes().with(key, "101").with(value, 0));
202         data.put(table, new Attributes().with(key, "102").with(value, 1));
203         MatcherAssert.assertThat(
204             "should iterable with size 2",
205             data.iterate(table, new Conditions()),
206             Matchers.iterableWithSize(2)
207         );
208     }
209 
210     @Test
211     void fetchesWithComparison() throws Exception {
212         final String table = "x12";
213         final String key = "foo1";
214         final String value = "bar2";
215         final MkData data = new H2Data().with(
216             table, new String[] {key}, value
217         );
218         data.put(table, new Attributes().with(key, "101").with(value, 0));
219         data.put(table, new Attributes().with(key, "102").with(value, 1));
220         MatcherAssert.assertThat(
221             "should equal to '1'",
222             data.iterate(
223                 table,
224                 new Conditions().with(
225                     value,
226                     Condition.builder()
227                         .attributeValueList(
228                             AttributeValue.builder().n("0").build()
229                         )
230                         .comparisonOperator(ComparisonOperator.GT)
231                         .build()
232                 )
233             ).iterator().next().get(value).n(),
234             Matchers.equalTo("1")
235         );
236     }
237 
238 }