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.AttributeDefinition;
33  import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
34  import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
35  import com.amazonaws.services.dynamodbv2.model.KeyType;
36  import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
37  import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
38  import com.jcabi.dynamo.mock.MadeTable;
39  import com.jcabi.dynamo.retry.ReRegion;
40  
41  /**
42   * Mock of a {@link Region}.
43   * @since 0.16.2
44   */
45  final class RegionMock {
46  
47      /**
48       * Dynamo table hash key.
49       */
50      private final transient String ahash;
51  
52      /**
53       * Dynamo table range key.
54       */
55      private final transient String arange;
56  
57      /**
58       * DynamoDB Local port.
59       */
60      private final transient int prt;
61  
62      /**
63       * Ctor.
64       */
65      RegionMock() {
66          this(
67              "hash-key",
68              "range-key",
69              Integer.parseInt(
70                  System.getProperty("failsafe.port")
71              )
72          );
73      }
74  
75      /**
76       * Ctor.
77       * @param hash Hash
78       * @param range Range
79       * @param port Port
80       */
81      RegionMock(final String hash, final String range, final int port) {
82          this.ahash = hash;
83          this.arange = range;
84          this.prt = port;
85      }
86  
87      /**
88       * Get DynamoDB server port.
89       * @return TCP port
90       */
91      public int port() {
92          return this.prt;
93      }
94  
95      /**
96       * Get hash of the table.
97       * @return Hash attribute name
98       */
99      public String hash() {
100         return this.ahash;
101     }
102 
103     /**
104      * Get range of the table.
105      * @return Hash attribute name
106      */
107     public String range() {
108         return this.arange;
109     }
110 
111     /**
112      * Get region with a table.
113      * @param table Table name
114      * @return Region
115      * @throws Exception If fails
116      */
117     public Region get(final String table) throws Exception {
118         final Region region = new Region.Simple(
119             new Credentials.Direct(Credentials.TEST, this.prt)
120         );
121         final MadeTable mocker = new MadeTable(
122             region,
123             new CreateTableRequest()
124                 .withTableName(table)
125                 .withProvisionedThroughput(
126                     new ProvisionedThroughput()
127                         .withReadCapacityUnits(1L)
128                         .withWriteCapacityUnits(1L)
129                 )
130                 .withAttributeDefinitions(
131                     new AttributeDefinition()
132                         .withAttributeName(this.ahash)
133                         .withAttributeType(ScalarAttributeType.S),
134                     new AttributeDefinition()
135                         .withAttributeName(this.arange)
136                         .withAttributeType(ScalarAttributeType.N)
137                 )
138                 .withKeySchema(
139                     new KeySchemaElement()
140                         .withAttributeName(this.ahash)
141                         .withKeyType(KeyType.HASH),
142                     new KeySchemaElement()
143                         .withAttributeName(this.arange)
144                         .withKeyType(KeyType.RANGE)
145                 )
146         );
147         mocker.create();
148         mocker.createIfAbsent();
149         return new ReRegion(region);
150     }
151 
152 }