1
2
3
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.AttributeAction;
15 import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
16 import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate;
17
18
19
20
21
22
23 final class MkRegionTest {
24
25 @Test
26 void checksAttributeExists() throws Exception {
27 final String name = "users";
28 final String key = "id";
29 final String attr = "description";
30 final String nattr = "thenumber";
31 final Region region = new MkRegion(
32 new H2Data().with(name, new String[] {key}, attr, nattr)
33 );
34 final Table table = region.table(name);
35 table.put(
36 new Attributes()
37 .with(key, "32443")
38 .with(attr, "first value to \n\t€ save")
39 .with(nattr, "150")
40 );
41 MatcherAssert.assertThat(
42 "should be true",
43 table.frame().iterator().next().has(attr),
44 Matchers.is(true)
45 );
46 }
47
48 @Test
49 void readsAttributeValue() throws Exception {
50 final String name = "users";
51 final String key = "id";
52 final String attr = "description";
53 final Region region = new MkRegion(
54 new H2Data().with(name, new String[] {key}, attr)
55 );
56 final Table table = region.table(name);
57 table.put(
58 new Attributes()
59 .with(key, "32443")
60 .with(attr, "first value to \n\t€ save")
61 );
62 MatcherAssert.assertThat(
63 "should contains '\n\t\u20ac save'",
64 table.frame().iterator().next().get(attr).s(),
65 Matchers.containsString("\n\t\u20ac save")
66 );
67 }
68
69 @Test
70 void updatesAttributeValue() throws Exception {
71 final String name = "users";
72 final String key = "id";
73 final String attr = "description";
74 final Region region = new MkRegion(
75 new H2Data().with(name, new String[] {key}, attr)
76 );
77 final Table table = region.table(name);
78 table.put(
79 new Attributes()
80 .with(key, "32443")
81 .with(attr, "first value to \n\t€ save")
82 );
83 final Item item = table.frame().iterator().next();
84 item.put(
85 attr,
86 AttributeValueUpdate.builder().value(
87 AttributeValue.builder().s("this is another value").build()
88 ).build()
89 );
90 MatcherAssert.assertThat(
91 "should contains 'another value'",
92 item.get(attr).s(),
93 Matchers.containsString("another value")
94 );
95 }
96
97 @Test
98 void readsNumericAttribute() throws Exception {
99 final String name = "users";
100 final String key = "id";
101 final String nattr = "thenumber";
102 final Region region = new MkRegion(
103 new H2Data().with(name, new String[] {key}, nattr)
104 );
105 final Table table = region.table(name);
106 table.put(
107 new Attributes()
108 .with(key, "32443")
109 .with(nattr, "150")
110 );
111 MatcherAssert.assertThat(
112 "should ends with '50'",
113 table.frame().iterator().next().get(nattr).n(),
114 Matchers.endsWith("50")
115 );
116 }
117
118 @Test
119 void storesAndReadsSingleAttribute() throws Exception {
120 final String table = "ideas";
121 final String key = "number";
122 final String attr = "total";
123 final Region region = new MkRegion(
124 new H2Data().with(table, new String[] {key}, attr)
125 );
126 final Table tbl = region.table(table);
127 tbl.put(
128 new Attributes()
129 .with(key, "324439")
130 .with(attr, "0")
131 );
132 final Item item = tbl.frame().iterator().next();
133 item.put(
134 attr,
135 AttributeValueUpdate.builder().value(
136 AttributeValue.builder().n("2").build()
137 ).action(AttributeAction.PUT).build()
138 );
139 MatcherAssert.assertThat("should equal 2", item.get(attr).n(), Matchers.equalTo("2"));
140 }
141
142 }