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.Collections;
8   import org.hamcrest.MatcherAssert;
9   import org.hamcrest.Matchers;
10  import org.junit.jupiter.api.Assertions;
11  import org.junit.jupiter.api.Test;
12  import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
13  import software.amazon.awssdk.services.dynamodb.model.Condition;
14  
15  /**
16   * Test case for {@link Conditions}.
17   * @since 0.1
18   */
19  @SuppressWarnings("PMD.TooManyMethods")
20  final class ConditionsTest {
21  
22      @Test
23      void worksAsMapWithCorrectKeySetSize() {
24          MatcherAssert.assertThat(
25              "should has size 1",
26              new Conditions().with(
27                  "id", Condition.builder().build()
28              ).keySet(),
29              Matchers.hasSize(1)
30          );
31      }
32  
33      @Test
34      void worksAsMapWithCorrectEntry() {
35          final String name = "id";
36          final Condition condition = Condition.builder().build();
37          MatcherAssert.assertThat(
38              "should has entry",
39              new Conditions().with(name, condition),
40              Matchers.hasEntry(name, condition)
41          );
42      }
43  
44      @Test
45      void worksAsMapFromExistingMap() {
46          final String name = "id";
47          final Condition condition = Condition.builder().build();
48          MatcherAssert.assertThat(
49              "should has entry from existing map",
50              new Conditions(new Conditions().with(name, condition)),
51              Matchers.hasEntry(name, condition)
52          );
53      }
54  
55      @Test
56      void reportsEmptyForNewInstance() {
57          MatcherAssert.assertThat(
58              "should not report non-empty for new instance",
59              new Conditions().isEmpty(),
60              Matchers.is(true)
61          );
62      }
63  
64      @Test
65      void reportsNonEmptyAfterAdd() {
66          MatcherAssert.assertThat(
67              "should not report empty after adding condition",
68              new Conditions()
69                  .with("\u00e4ttr", Condition.builder().build())
70                  .isEmpty(),
71              Matchers.is(false)
72          );
73      }
74  
75      @Test
76      void containsAddedKey() {
77          MatcherAssert.assertThat(
78              "should not miss added key",
79              new Conditions()
80                  .with("k\u00e9y", Condition.builder().build())
81                  .containsKey("k\u00e9y"),
82              Matchers.is(true)
83          );
84      }
85  
86      @Test
87      void doesNotContainAbsentKey() {
88          MatcherAssert.assertThat(
89              "should not contain absent key",
90              new Conditions()
91                  .with("pr\u00e9s", Condition.builder().build())
92                  .containsKey("n\u00f6pe"),
93              Matchers.is(false)
94          );
95      }
96  
97      @Test
98      void containsAddedValue() {
99          final Condition cond = Condition.builder().build();
100         MatcherAssert.assertThat(
101             "should not miss added value",
102             new Conditions()
103                 .with("n\u00e4me", cond)
104                 .containsValue(cond),
105             Matchers.is(true)
106         );
107     }
108 
109     @Test
110     void retrievesConditionByKey() {
111         final Condition cond = Condition.builder().build();
112         MatcherAssert.assertThat(
113             "should not return wrong condition for key",
114             new Conditions()
115                 .with("r\u00e9tr", cond)
116                 .get("r\u00e9tr"),
117             Matchers.equalTo(cond)
118         );
119     }
120 
121     @Test
122     void returnsCorrectSize() {
123         MatcherAssert.assertThat(
124             "should not return wrong size",
125             new Conditions()
126                 .with("f\u00ecrst", Condition.builder().build())
127                 .with("s\u00e9cond", Condition.builder().build())
128                 .size(),
129             Matchers.equalTo(2)
130         );
131     }
132 
133     @Test
134     void throwsOnPut() {
135         Assertions.assertThrows(
136             UnsupportedOperationException.class,
137             () -> new Conditions()
138                 .put("p\u00fct", Condition.builder().build())
139         );
140     }
141 
142     @Test
143     void throwsOnRemove() {
144         Assertions.assertThrows(
145             UnsupportedOperationException.class,
146             () -> new Conditions().remove("r\u00e9m")
147         );
148     }
149 
150     @Test
151     void throwsOnPutAll() {
152         Assertions.assertThrows(
153             UnsupportedOperationException.class,
154             () -> new Conditions().putAll(
155                 Collections.singletonMap(
156                     "b\u00fclk", Condition.builder().build()
157                 )
158             )
159         );
160     }
161 
162     @Test
163     void throwsOnClear() {
164         Assertions.assertThrows(
165             UnsupportedOperationException.class,
166             () -> new Conditions().clear()
167         );
168     }
169 
170     @Test
171     void createsEqualConditionForLong() {
172         MatcherAssert.assertThat(
173             "should not miss numeric attribute for Long",
174             Conditions.equalTo(42L)
175                 .attributeValueList().get(0).n(),
176             Matchers.equalTo("42")
177         );
178     }
179 
180     @Test
181     void createsEqualConditionForInteger() {
182         MatcherAssert.assertThat(
183             "should not miss numeric attribute for Integer",
184             Conditions.equalTo(7)
185                 .attributeValueList().get(0).n(),
186             Matchers.equalTo("7")
187         );
188     }
189 
190     @Test
191     void createsEqualConditionForObject() {
192         MatcherAssert.assertThat(
193             "should not miss string attribute for Object",
194             Conditions.equalTo("v\u00e4lue")
195                 .attributeValueList().get(0).s(),
196             Matchers.equalTo("v\u00e4lue")
197         );
198     }
199 
200     @Test
201     void mergesWithAttributeMap() {
202         MatcherAssert.assertThat(
203             "should not fail merging attribute map",
204             new Conditions().withAttributes(
205                 Collections.singletonMap(
206                     "m\u00e9rge",
207                     AttributeValue.builder().s("v\u00e4l").build()
208                 )
209             ).keySet(),
210             Matchers.hasSize(1)
211         );
212     }
213 
214     @Test
215     void combinesConditionsViaMap() {
216         MatcherAssert.assertThat(
217             "should not fail combining conditions via map",
218             new Conditions().with(
219                 Collections.singletonMap(
220                     "c\u00f6mb", Condition.builder().build()
221                 )
222             ).keySet(),
223             Matchers.hasSize(1)
224         );
225     }
226 
227     @Test
228     void addsConditionViaObject() {
229         MatcherAssert.assertThat(
230             "should not fail adding condition via object",
231             new Conditions().with("n\u00e4me", (Object) "v\u00e4lue"),
232             Matchers.hasKey("n\u00e4me")
233         );
234     }
235 
236     @Test
237     void formatsConditionsToString() {
238         MatcherAssert.assertThat(
239             "should not produce string without attribute name",
240             new Conditions()
241                 .with("r\u00e4nge", Conditions.equalTo("v\u00e4l"))
242                 .toString(),
243             Matchers.containsString("r\u00e4nge")
244         );
245     }
246 
247 }