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.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
43
44
45 final class RegionMock {
46
47
48
49
50 private final transient String ahash;
51
52
53
54
55 private final transient String arange;
56
57
58
59
60 private final transient int prt;
61
62
63
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
77
78
79
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
89
90
91 public int port() {
92 return this.prt;
93 }
94
95
96
97
98
99 public String hash() {
100 return this.ahash;
101 }
102
103
104
105
106
107 public String range() {
108 return this.arange;
109 }
110
111
112
113
114
115
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 }