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.jcabi.dynamo.Attributes;
8   import com.jcabi.dynamo.Item;
9   import com.jcabi.dynamo.Region;
10  import com.jcabi.dynamo.Table;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
15  import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate;
16  
17  /**
18   * Test case for {@link MkItem}.
19   * @since 0.10
20   */
21  final class MkItemTest {
22  
23      @Test
24      void returnsFrameReference() throws Exception {
25          final String name = "fr\u00e4me";
26          final String key = "k\u00e9y";
27          final String attr = "\u00e4ttr";
28          final Region region = new MkRegion(
29              new H2Data().with(name, new String[]{key}, attr)
30          );
31          final Table table = region.table(name);
32          table.put(
33              new Attributes()
34                  .with(key, "28173")
35                  .with(attr, "v\u00e4lue")
36          );
37          MatcherAssert.assertThat(
38              "does not return frame from item",
39              table.frame().iterator().next().frame(),
40              Matchers.notNullValue()
41          );
42      }
43  
44      @Test
45      void checksAbsentAttributeReturnsFalse() throws Exception {
46          final String name = "h\u00e4s";
47          final String key = "k\u00e9y";
48          final String attr = "\u00e4ttr";
49          final Region region = new MkRegion(
50              new H2Data().with(name, new String[]{key}, attr)
51          );
52          final Table table = region.table(name);
53          table.put(
54              new Attributes()
55                  .with(key, "91823")
56                  .with(attr, "v\u00e4lue")
57          );
58          MatcherAssert.assertThat(
59              "does not return false for absent attribute",
60              table.frame().iterator().next().has("nonexistent"),
61              Matchers.is(false)
62          );
63      }
64  
65      @Test
66      void putsMultipleAttributes() throws Exception {
67          final String name = "m\u00fclti";
68          final String key = "k\u00e9y";
69          final String first = "f\u00edrst";
70          final String second = "s\u00e9cond";
71          final Region region = new MkRegion(
72              new H2Data().with(
73                  name, new String[]{key}, first, second
74              )
75          );
76          final Table table = region.table(name);
77          table.put(
78              new Attributes()
79                  .with(key, "71234")
80                  .with(first, "old\u00f6ne")
81                  .with(second, "old\u00f6two")
82          );
83          final Item item = table.frame().iterator().next();
84          item.put(
85              first,
86              AttributeValueUpdate.builder().value(
87                  AttributeValue.builder().s("n\u00e9w").build()
88              ).build()
89          );
90          MatcherAssert.assertThat(
91              "does not update attribute correctly",
92              item.get(first).s(),
93              Matchers.equalTo("n\u00e9w")
94          );
95      }
96  }