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;
31  
32  import com.amazonaws.services.dynamodbv2.model.AttributeAction;
33  import com.amazonaws.services.dynamodbv2.model.AttributeValue;
34  import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
35  import org.apache.commons.lang3.StringUtils;
36  import org.hamcrest.MatcherAssert;
37  import org.hamcrest.Matchers;
38  import org.junit.jupiter.api.Assertions;
39  import org.junit.jupiter.api.Test;
40  import org.mockito.Mockito;
41  
42  /**
43   * Test case for {@link AttributesUpdates}.
44   * @since 0.22
45   */
46  @SuppressWarnings("PMD.TooManyMethods")
47  final class AttributeUpdatesTest {
48  
49      @Test
50      void tellsIfEmptyOrNot() {
51          final AttributeUpdates attr = new AttributeUpdates();
52          MatcherAssert.assertThat(attr.isEmpty(), Matchers.is(Boolean.TRUE));
53          MatcherAssert.assertThat(
54              attr.with("testkey", Mockito.mock(AttributeValueUpdate.class))
55                  .isEmpty(),
56              Matchers.is(Boolean.FALSE)
57          );
58      }
59  
60      @Test
61      void addsAttributeValueUpdate() {
62          MatcherAssert.assertThat(0, Matchers.is(0));
63          final AttributeUpdates attr = new AttributeUpdates();
64          MatcherAssert.assertThat(
65              attr.with("testkey1", Mockito.mock(AttributeValueUpdate.class))
66                  .size(),
67              Matchers.is(1)
68          );
69      }
70  
71      @Test
72      void addsAttributeValue() {
73          MatcherAssert.assertThat(0, Matchers.is(0));
74          final AttributeUpdates attr = new AttributeUpdates();
75          MatcherAssert.assertThat(
76              attr.with("testkey2", Mockito.mock(AttributeValue.class)).size(),
77              Matchers.is(1)
78          );
79      }
80  
81      @Test
82      void addsObject() {
83          MatcherAssert.assertThat(0, Matchers.is(0));
84          final AttributeUpdates attr = new AttributeUpdates();
85          MatcherAssert.assertThat(
86              attr.with("testkey3", "value here").size(),
87              Matchers.is(1)
88          );
89      }
90  
91      @Test
92      void returnsKeySet() {
93          final String firstkey = "key1";
94          final AttributeUpdates attr = new AttributeUpdates()
95              .with(firstkey, "valuediff").with("key2", "value2");
96          MatcherAssert.assertThat(
97              attr.keySet().size(),
98              Matchers.is(2)
99          );
100         MatcherAssert.assertThat(
101             attr.keySet().iterator().next(),
102             Matchers.equalTo(firstkey)
103         );
104     }
105 
106     @Test
107     void returnsValues() {
108         final String firstvalue = "value3";
109         final AttributeUpdates attr = new AttributeUpdates()
110             .with("key3", firstvalue).with("key4", "value4");
111         MatcherAssert.assertThat(
112             attr.values().size(),
113             Matchers.is(2)
114         );
115         MatcherAssert.assertThat(
116             attr.values().iterator().next().getValue().getS(),
117             Matchers.equalTo(firstvalue)
118         );
119     }
120 
121     @Test
122     void returnsEntries() {
123         final AttributeUpdates attr = new AttributeUpdates()
124             .with("key5", "value5").with("key6", "value7");
125         MatcherAssert.assertThat(
126             attr.entrySet().size(),
127             Matchers.is(2)
128         );
129     }
130 
131     @Test
132     void getsEntry() {
133         final String key = "key10";
134         final AttributeUpdates attr = new AttributeUpdates()
135             .with(key, "value10");
136         MatcherAssert.assertThat(
137             attr.get(key),
138             Matchers.notNullValue()
139         );
140     }
141 
142     @Test
143     void containsKey() {
144         final String key = "key11";
145         final AttributeUpdates attr = new AttributeUpdates()
146             .with(key, "value11").with("otherkey1", "othervalue1");
147         MatcherAssert.assertThat(
148             attr.containsKey(key),
149             Matchers.is(Boolean.TRUE)
150         );
151     }
152 
153     @Test
154     void containsValue() {
155         final String value = "attrv";
156         final AttributeUpdates attr = new AttributeUpdates()
157             .with("attrkey", value).with("otherkey", "othervalue");
158         MatcherAssert.assertThat(
159             attr.containsValue(
160                 new AttributeValueUpdate(
161                     new AttributeValue(value), AttributeAction.PUT
162                 )
163             ),
164             Matchers.is(Boolean.TRUE)
165         );
166     }
167 
168     @Test
169     void canTurnToString() {
170         final AttributeUpdates attr = new AttributeUpdates()
171             .with("onekey", "onevalue").with("secondkey", "secondvalue");
172         MatcherAssert.assertThat(
173             attr.toString(),
174             Matchers.equalTo(
175                 StringUtils.join(
176                     "onekey={Value: {S: onevalue,},Action: PUT}; ",
177                     "secondkey={Value: {S: secondvalue,},Action: PUT}"
178                 )
179             )
180         );
181     }
182 
183     @Test
184     void addsMap() {
185         final AttributeUpdates attr = new AttributeUpdates();
186         MatcherAssert.assertThat(attr.size(), Matchers.is(0));
187         MatcherAssert.assertThat(
188             attr.with("testkey8", new AttributeUpdates().with("key", "value"))
189                 .size(),
190             Matchers.is(1)
191         );
192     }
193 
194     @Test
195     void putThrowsException() {
196         boolean passed;
197         try {
198             new AttributeUpdates().put(
199                 "key9", Mockito.mock(AttributeValueUpdate.class)
200             );
201             passed = false;
202         } catch (final UnsupportedOperationException ex) {
203             passed = true;
204         }
205         if (!passed) {
206             Assertions.fail("#put should not be supported");
207         }
208     }
209 
210     @Test
211     void putAllThrowsException() {
212         boolean passed;
213         try {
214             new AttributeUpdates().putAll(new AttributeUpdates());
215             passed = false;
216         } catch (final UnsupportedOperationException ex) {
217             passed = true;
218         }
219         if (!passed) {
220             Assertions.fail("#putAll should not be supported.");
221         }
222     }
223 
224     @Test
225     void removeThrowsException() {
226         boolean passed;
227         try {
228             new AttributeUpdates().remove("object to remove");
229             passed = false;
230         } catch (final UnsupportedOperationException ex) {
231             passed = true;
232         }
233         if (!passed) {
234             Assertions.fail("#remove should not be supported.");
235         }
236     }
237 
238     @Test
239     void clearThrowsException() {
240         boolean passed;
241         try {
242             new AttributeUpdates().clear();
243             passed = false;
244         } catch (final UnsupportedOperationException ex) {
245             passed = true;
246         }
247         if (!passed) {
248             Assertions.fail("#clear should not be supported.");
249         }
250     }
251 }