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.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   * Test case for {@link AwsTable}.
50   * @since 0.1
51   */
52  final class AwsTableTest {
53  
54      /**
55       * Constant for 'tableName' attribute.
56       */
57      private static final String TABLE_NAME = "tableName";
58  
59      /**
60       * Constant for 'key' attribute.
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 }