14
14
15
15
package software .amazon .lambda .powertools .idempotency .persistence .dynamodb ;
16
16
17
- import java . io . IOException ;
18
- import java . net . ServerSocket ;
19
- import java . net . URI ;
17
+ import org . junit . jupiter . api . BeforeEach ;
18
+ import org . mockito . Mock ;
19
+ import org . mockito . MockitoAnnotations ;
20
20
21
- import org .junit .jupiter .api .AfterAll ;
22
- import org .junit .jupiter .api .BeforeAll ;
23
-
24
- import com .amazonaws .services .dynamodbv2 .local .main .ServerRunner ;
25
- import com .amazonaws .services .dynamodbv2 .local .server .DynamoDBProxyServer ;
26
-
27
- import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
28
- import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
29
- import software .amazon .awssdk .http .urlconnection .UrlConnectionHttpClient ;
30
- import software .amazon .awssdk .regions .Region ;
31
21
import software .amazon .awssdk .services .dynamodb .DynamoDbClient ;
32
- import software .amazon .awssdk .services .dynamodb .model .AttributeDefinition ;
33
- import software .amazon .awssdk .services .dynamodb .model .BillingMode ;
34
- import software .amazon .awssdk .services .dynamodb .model .CreateTableRequest ;
35
- import software .amazon .awssdk .services .dynamodb .model .DescribeTableRequest ;
36
- import software .amazon .awssdk .services .dynamodb .model .DescribeTableResponse ;
37
- import software .amazon .awssdk .services .dynamodb .model .KeySchemaElement ;
38
- import software .amazon .awssdk .services .dynamodb .model .KeyType ;
39
- import software .amazon .awssdk .services .dynamodb .model .ScalarAttributeType ;
40
22
41
23
public class DynamoDBConfig {
42
24
protected static final String TABLE_NAME = "idempotency_table" ;
43
- protected static DynamoDBProxyServer dynamoProxy ;
44
- protected static DynamoDbClient client ;
45
-
46
- @ BeforeAll
47
- public static void setupDynamo () {
48
- int port = getFreePort ();
49
- try {
50
- dynamoProxy = ServerRunner .createServerFromCommandLineArgs (new String [] {
51
- "-inMemory" ,
52
- "-port" ,
53
- Integer .toString (port )
54
- });
55
- dynamoProxy .start ();
56
- } catch (Exception e ) {
57
- throw new RuntimeException ();
58
- }
59
-
60
- client = DynamoDbClient .builder ()
61
- .httpClient (UrlConnectionHttpClient .builder ().build ())
62
- .region (Region .EU_WEST_1 )
63
- .endpointOverride (URI .create ("http://localhost:" + port ))
64
- .credentialsProvider (StaticCredentialsProvider .create (
65
- AwsBasicCredentials .create ("FAKE" , "FAKE" )))
66
- .build ();
67
-
68
- client .createTable (CreateTableRequest .builder ()
69
- .tableName (TABLE_NAME )
70
- .keySchema (KeySchemaElement .builder ().keyType (KeyType .HASH ).attributeName ("id" ).build ())
71
- .attributeDefinitions (
72
- AttributeDefinition .builder ().attributeName ("id" ).attributeType (ScalarAttributeType .S ).build ())
73
- .billingMode (BillingMode .PAY_PER_REQUEST )
74
- .build ());
75
-
76
- DescribeTableResponse response = client
77
- .describeTable (DescribeTableRequest .builder ().tableName (TABLE_NAME ).build ());
78
- if (response == null ) {
79
- throw new RuntimeException ("Table was not created within expected time" );
80
- }
81
- }
82
-
83
- @ AfterAll
84
- public static void teardownDynamo () {
85
- try {
86
- dynamoProxy .stop ();
87
- } catch (Exception e ) {
88
- throw new RuntimeException ();
89
- }
90
- }
91
-
92
- private static int getFreePort () {
93
- try {
94
- ServerSocket socket = new ServerSocket (0 );
95
- int port = socket .getLocalPort ();
96
- socket .close ();
97
- return port ;
98
- } catch (IOException ioe ) {
99
- throw new RuntimeException (ioe );
100
- }
25
+
26
+ @ Mock
27
+ protected DynamoDbClient client ;
28
+
29
+ @ BeforeEach
30
+ public void setupMocks () {
31
+ MockitoAnnotations .openMocks (this );
101
32
}
102
- }
33
+ }
0 commit comments