1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.dynamo;
31
32 import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
33 import com.amazonaws.services.dynamodbv2.model.AttributeValue;
34 import com.amazonaws.services.dynamodbv2.model.ConsumedCapacity;
35 import com.amazonaws.services.dynamodbv2.model.DeleteItemRequest;
36 import com.amazonaws.services.dynamodbv2.model.DeleteItemResult;
37 import com.amazonaws.services.dynamodbv2.model.DescribeTableRequest;
38 import com.amazonaws.services.dynamodbv2.model.DescribeTableResult;
39 import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
40 import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
41 import com.amazonaws.services.dynamodbv2.model.PutItemResult;
42 import com.amazonaws.services.dynamodbv2.model.TableDescription;
43 import org.hamcrest.Matchers;
44 import org.junit.jupiter.api.Test;
45 import org.mockito.Mockito;
46 import org.mockito.hamcrest.MockitoHamcrest;
47
48
49
50
51
52 final class AwsTableTest {
53
54
55
56
57 private static final String TABLE_NAME = "tableName";
58
59
60
61
62 private static final String KEY = "key";
63
64 @Test
65 void savesItemToDynamo() throws Exception {
66 final Credentials credentials = Mockito.mock(Credentials.class);
67 final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
68 Mockito.doReturn(aws).when(credentials).aws();
69 Mockito.doReturn(
70 new PutItemResult().withConsumedCapacity(
71 new ConsumedCapacity().withCapacityUnits(1.0d)
72 )
73 ).when(aws).putItem(Mockito.any(PutItemRequest.class));
74 Mockito.doReturn(
75 new DescribeTableResult().withTable(
76 new TableDescription().withKeySchema(
77 new KeySchemaElement().withAttributeName(AwsTableTest.KEY)
78 )
79 )
80 ).when(aws).describeTable(Mockito.any(DescribeTableRequest.class));
81 final String attr = "attribute-1";
82 final AttributeValue value = new AttributeValue("value-1");
83 final String name = "table-name";
84 final Table table = new AwsTable(
85 credentials, Mockito.mock(Region.class), name
86 );
87 table.put(new Attributes().with(attr, value));
88 Mockito.verify(aws).putItem(
89 (PutItemRequest) MockitoHamcrest.argThat(
90 Matchers.allOf(
91 Matchers.hasProperty(
92 AwsTableTest.TABLE_NAME,
93 Matchers.equalTo(name)
94 ),
95 Matchers.hasProperty(
96 "item",
97 Matchers.hasEntry(
98 Matchers.equalTo(attr),
99 Matchers.equalTo(value)
100 )
101 )
102 )
103 )
104 );
105 }
106
107 @Test
108 void deletesItemFromDynamo() throws Exception {
109 final Credentials credentials = Mockito.mock(Credentials.class);
110 final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
111 Mockito.doReturn(aws).when(credentials).aws();
112 Mockito.doReturn(
113 new DeleteItemResult().withConsumedCapacity(
114 new ConsumedCapacity().withCapacityUnits(1.0d)
115 )
116 ).when(aws).deleteItem(Mockito.any(DeleteItemRequest.class));
117 Mockito.doReturn(
118 new DescribeTableResult().withTable(
119 new TableDescription().withKeySchema(
120 new KeySchemaElement().withAttributeName(AwsTableTest.KEY)
121 )
122 )
123 ).when(aws).describeTable(Mockito.any(DescribeTableRequest.class));
124 final String attr = "attribute-2";
125 final AttributeValue value = new AttributeValue("value-2");
126 final String name = "table-name-2";
127 final Table table = new AwsTable(
128 credentials, Mockito.mock(Region.class), name
129 );
130 table.delete(new Attributes().with(attr, value));
131 Mockito.verify(aws).deleteItem(
132 (DeleteItemRequest) MockitoHamcrest.argThat(
133 Matchers.allOf(
134 Matchers.hasProperty(
135 AwsTableTest.TABLE_NAME,
136 Matchers.equalTo(name)
137 ),
138 Matchers.hasProperty(
139 AwsTableTest.KEY,
140 Matchers.hasEntry(
141 Matchers.equalTo(attr),
142 Matchers.equalTo(value)
143 )
144 )
145 )
146 )
147 );
148 }
149 }