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 org.hamcrest.MatcherAssert;
8   import org.hamcrest.Matchers;
9   import org.junit.jupiter.api.Assertions;
10  import org.junit.jupiter.api.Test;
11  import software.amazon.awssdk.services.dynamodb.model.AttributeAction;
12  import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
13  import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate;
14  
15  /**
16   * Test case for {@link AttributesUpdates}.
17   * @since 0.22
18   */
19  @SuppressWarnings("PMD.TooManyMethods")
20  final class AttributeUpdatesTest {
21  
22      @Test
23      void tellsIfEmpty() {
24          MatcherAssert.assertThat(
25              "should be true",
26              new AttributeUpdates().isEmpty(),
27              Matchers.is(Boolean.TRUE)
28          );
29      }
30  
31      @Test
32      void tellsIfNotEmpty() {
33          MatcherAssert.assertThat(
34              "should be false",
35              new AttributeUpdates()
36                  .with("testkey", AttributeValueUpdate.builder().build())
37                  .isEmpty(),
38              Matchers.is(Boolean.FALSE)
39          );
40      }
41  
42      @Test
43      void addsAttributeValueUpdate() {
44          MatcherAssert.assertThat(
45              "should be 1",
46              new AttributeUpdates()
47                  .with("testkey1", AttributeValueUpdate.builder().build())
48                  .size(),
49              Matchers.is(1)
50          );
51      }
52  
53      @Test
54      void addsAttributeValue() {
55          MatcherAssert.assertThat(
56              "should be 1",
57              new AttributeUpdates()
58                  .with("testkey2", AttributeValue.builder().s("mock").build())
59                  .size(),
60              Matchers.is(1)
61          );
62      }
63  
64      @Test
65      void addsObject() {
66          MatcherAssert.assertThat(
67              "should be 1",
68              new AttributeUpdates()
69                  .with("testkey3", "value here")
70                  .size(),
71              Matchers.is(1)
72          );
73      }
74  
75      @Test
76      void returnsKeySetSize() {
77          MatcherAssert.assertThat(
78              "should be 2",
79              new AttributeUpdates()
80                  .with("key1", "valuediff")
81                  .with("key2", "value2")
82                  .keySet()
83                  .size(),
84              Matchers.is(2)
85          );
86      }
87  
88      @Test
89      void returnsKeySetContent() {
90          final String firstkey = "key1";
91          MatcherAssert.assertThat(
92              "should be equal to 'key1'",
93              new AttributeUpdates()
94                  .with(firstkey, "valuediff")
95                  .with("key2", "value2")
96                  .keySet()
97                  .iterator()
98                  .next(),
99              Matchers.equalTo(firstkey)
100         );
101     }
102 
103     @Test
104     void returnsValuesSize() {
105         MatcherAssert.assertThat(
106             "should be 2",
107             new AttributeUpdates()
108                 .with("key3", "value3")
109                 .with("key4", "value4")
110                 .values()
111                 .size(),
112             Matchers.is(2)
113         );
114     }
115 
116     @Test
117     void returnsValuesContent() {
118         final String firstvalue = "value3";
119         MatcherAssert.assertThat(
120             "should be equal to 'value3'",
121             new AttributeUpdates()
122                 .with("key3", firstvalue)
123                 .with("key4", "value4")
124                 .values()
125                 .iterator()
126                 .next()
127                 .value()
128                 .s(),
129             Matchers.equalTo(firstvalue)
130         );
131     }
132 
133     @Test
134     void returnsEntries() {
135         MatcherAssert.assertThat(
136             "should be 2",
137             new AttributeUpdates()
138                 .with("key5", "value5")
139                 .with("key6", "value7")
140                 .entrySet()
141                 .size(),
142             Matchers.is(2)
143         );
144     }
145 
146     @Test
147     void getsEntry() {
148         final String key = "key10";
149         MatcherAssert.assertThat(
150             "should be not null",
151             new AttributeUpdates()
152                 .with(key, "value10")
153                 .get(key),
154             Matchers.notNullValue()
155         );
156     }
157 
158     @Test
159     void containsKey() {
160         final String key = "key11";
161         MatcherAssert.assertThat(
162             "should contains key 'key11'",
163             new AttributeUpdates()
164                 .with(key, "value11")
165                 .with("otherkey1", "othervalue1")
166                 .containsKey(key),
167             Matchers.is(Boolean.TRUE)
168         );
169     }
170 
171     @Test
172     void containsValue() {
173         final String value = "attrv";
174         MatcherAssert.assertThat(
175             "should contains value 'attrv'",
176             new AttributeUpdates()
177                 .with("attrkey", value)
178                 .with("otherkey", "othervalue")
179                 .containsValue(
180                     AttributeValueUpdate.builder()
181                         .value(AttributeValue.builder().s(value).build())
182                         .action(AttributeAction.PUT)
183                         .build()
184                 ),
185             Matchers.is(Boolean.TRUE)
186         );
187     }
188 
189     @Test
190     void canTurnToString() {
191         MatcherAssert.assertThat(
192             "should contain key names",
193             new AttributeUpdates()
194                 .with("onekey", "onevalue")
195                 .with("secondkey", "secondvalue")
196                 .toString(),
197             Matchers.allOf(
198                 Matchers.containsString("onekey="),
199                 Matchers.containsString("secondkey=")
200             )
201         );
202     }
203 
204     @Test
205     void addsMapReportsEmptySize() {
206         MatcherAssert.assertThat(
207             "should be 0",
208             new AttributeUpdates().size(),
209             Matchers.is(0)
210         );
211     }
212 
213     @Test
214     void addsMap() {
215         MatcherAssert.assertThat(
216             "should be 1",
217             new AttributeUpdates()
218                 .with("testkey8", new AttributeUpdates().with("key", "value"))
219                 .size(),
220             Matchers.is(1)
221         );
222     }
223 
224     @Test
225     void putThrowsException() {
226         boolean passed;
227         try {
228             new AttributeUpdates().put(
229                 "key9", AttributeValueUpdate.builder().build()
230             );
231             passed = false;
232         } catch (final UnsupportedOperationException ex) {
233             passed = true;
234         }
235         if (!passed) {
236             Assertions.fail("#put should not be supported");
237         }
238     }
239 
240     @Test
241     void putAllThrowsException() {
242         boolean passed;
243         try {
244             new AttributeUpdates().putAll(new AttributeUpdates());
245             passed = false;
246         } catch (final UnsupportedOperationException ex) {
247             passed = true;
248         }
249         if (!passed) {
250             Assertions.fail("#putAll should not be supported.");
251         }
252     }
253 
254     @Test
255     void removeThrowsException() {
256         boolean passed;
257         try {
258             new AttributeUpdates().remove("object to remove");
259             passed = false;
260         } catch (final UnsupportedOperationException ex) {
261             passed = true;
262         }
263         if (!passed) {
264             Assertions.fail("#remove should not be supported.");
265         }
266     }
267 
268     @Test
269     void clearThrowsException() {
270         boolean passed;
271         try {
272             new AttributeUpdates().clear();
273             passed = false;
274         } catch (final UnsupportedOperationException ex) {
275             passed = true;
276         }
277         if (!passed) {
278             Assertions.fail("#clear should not be supported.");
279         }
280     }
281 }