]> git.basschouten.com Git - openhab-addons.git/blob
930211c4425d61cadd63f8d13c59af001a40ab73
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.dynamodb.internal;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.io.File;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import java.nio.file.Paths;
21 import java.nio.file.StandardOpenOption;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.io.TempDir;
29
30 import software.amazon.awssdk.core.retry.RetryMode;
31 import software.amazon.awssdk.regions.Region;
32
33 /**
34  *
35  * @author Sami Salonen - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class DynamoDBConfigTest {
40
41     private static Map<String, Object> mapFrom(String... args) {
42         assert args.length % 2 == 0;
43         Map<String, String> config = new HashMap<>();
44         for (int i = 1; i < args.length; i++) {
45             String key = args[i - 1];
46             String val = args[i];
47             config.put(key, val);
48         }
49         return Collections.unmodifiableMap(config);
50     }
51
52     public @TempDir @NonNullByDefault({}) File folder;
53
54     @Test
55     public void testEmpty() throws Exception {
56         assertNull(DynamoDBConfig.fromConfig(new HashMap<>()));
57     }
58
59     @Test
60     public void testInvalidRegion() throws Exception {
61         assertNull(DynamoDBConfig.fromConfig(Collections.singletonMap("region", "foobie")));
62     }
63
64     @Test
65     public void testRegionOnly() throws Exception {
66         assertNull(DynamoDBConfig.fromConfig(Collections.singletonMap("region", "eu-west-1")));
67     }
68
69     @Test
70     public void testRegionWithAccessKeys() throws Exception {
71         DynamoDBConfig fromConfig = DynamoDBConfig
72                 .fromConfig(mapFrom("region", "eu-west-1", "accessKey", "access1", "secretKey", "secret1"));
73         assert fromConfig != null;
74         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
75         assertEquals("access1", fromConfig.getCredentials().accessKeyId());
76         assertEquals("secret1", fromConfig.getCredentials().secretAccessKey());
77         assertEquals("openhab-", fromConfig.getTablePrefixLegacy());
78         assertEquals(1, fromConfig.getReadCapacityUnits());
79         assertEquals(1, fromConfig.getWriteCapacityUnits());
80         assertEquals(RetryMode.STANDARD, fromConfig.getRetryPolicy().retryMode());
81         assertEquals(ExpectedTableSchema.MAYBE_LEGACY, fromConfig.getTableRevision());
82     }
83
84     @SuppressWarnings("null")
85     @Test
86     public void testRegionWithProfilesConfigFile() throws Exception {
87         Path credsFile = Files.createFile(Paths.get(folder.getPath(), "creds"));
88         Files.write(
89                 credsFile, ("[fooprofile]\n" + "aws_access_key_id=testAccessKey\n"
90                         + "aws_secret_access_key=testSecretKey\n" + "aws_session_token=testSessionToken\n").getBytes(),
91                 StandardOpenOption.TRUNCATE_EXISTING);
92
93         DynamoDBConfig fromConfig = DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "profilesConfigFile",
94                 credsFile.toAbsolutePath().toString(), "profile", "fooprofile"));
95         assertNotNull(fromConfig);
96         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
97         assertEquals("openhab-", fromConfig.getTablePrefixLegacy());
98         assertEquals(1, fromConfig.getReadCapacityUnits());
99         assertEquals(1, fromConfig.getWriteCapacityUnits());
100         assertEquals(RetryMode.STANDARD, fromConfig.getRetryPolicy().retryMode());
101         assertEquals(ExpectedTableSchema.MAYBE_LEGACY, fromConfig.getTableRevision());
102     }
103
104     @SuppressWarnings("null")
105     @Test
106     public void testProfilesConfigFileRetryMode() throws Exception {
107         Path credsFile = Files.createFile(Paths.get(folder.getPath(), "creds"));
108         Files.write(credsFile,
109                 ("[fooprofile]\n" + "aws_access_key_id=testAccessKey\n" + "aws_secret_access_key=testSecretKey\n"
110                         + "aws_session_token=testSessionToken\n" + "retry_mode=legacy").getBytes(),
111                 StandardOpenOption.TRUNCATE_EXISTING);
112
113         DynamoDBConfig fromConfig = DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "profilesConfigFile",
114                 credsFile.toAbsolutePath().toString(), "profile", "fooprofile"));
115         assertNotNull(fromConfig);
116         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
117         assertEquals("openhab-", fromConfig.getTablePrefixLegacy());
118         assertEquals(1, fromConfig.getReadCapacityUnits());
119         assertEquals(1, fromConfig.getWriteCapacityUnits());
120         assertEquals(RetryMode.LEGACY, fromConfig.getRetryPolicy().retryMode());
121         assertEquals(ExpectedTableSchema.MAYBE_LEGACY, fromConfig.getTableRevision());
122     }
123
124     @Test
125     public void testEmptyConfiguration() throws Exception {
126         assertNull(DynamoDBConfig.fromConfig(mapFrom()));
127     }
128
129     @Test
130     public void testRegionWithInvalidProfilesConfigFile() throws Exception {
131         Path credsFile = Files.createFile(Paths.get(folder.getPath(), "creds"));
132         Files.write(credsFile,
133                 ("[fooprofile]\n" + "aws_access_key_idINVALIDKEY=testAccessKey\n"
134                         + "aws_secret_access_key=testSecretKey\n" + "aws_session_token=testSessionToken\n").getBytes(),
135                 StandardOpenOption.TRUNCATE_EXISTING);
136
137         assertNull(DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "profilesConfigFile",
138                 credsFile.toFile().getAbsolutePath(), "profile", "fooprofile")));
139     }
140
141     @Test
142     public void testRegionWithProfilesConfigFileMissingProfile() throws Exception {
143         Path credsFile = Files.createFile(Paths.get(folder.getPath(), "creds"));
144         Files.write(
145                 credsFile, ("[fooprofile]\n" + "aws_access_key_id=testAccessKey\n"
146                         + "aws_secret_access_key=testSecretKey\n" + "aws_session_token=testSessionToken\n").getBytes(),
147                 StandardOpenOption.TRUNCATE_EXISTING);
148
149         assertNull(DynamoDBConfig.fromConfig(
150                 mapFrom("region", "eu-west-1", "profilesConfigFile", credsFile.toAbsolutePath().toString())));
151     }
152
153     @SuppressWarnings("null")
154     @Test
155     public void testRegionWithAccessKeysWithLegacyPrefix() throws Exception {
156         DynamoDBConfig fromConfig = DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "accessKey", "access1",
157                 "secretKey", "secret1", "tablePrefix", "foobie-", "expireDays", "105"));
158         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
159         assertEquals("access1", fromConfig.getCredentials().accessKeyId());
160         assertEquals("secret1", fromConfig.getCredentials().secretAccessKey());
161         assertEquals("foobie-", fromConfig.getTablePrefixLegacy());
162         assertEquals(1, fromConfig.getReadCapacityUnits());
163         assertEquals(1, fromConfig.getWriteCapacityUnits());
164         assertEquals(RetryMode.STANDARD, fromConfig.getRetryPolicy().retryMode());
165         assertEquals(ExpectedTableSchema.LEGACY, fromConfig.getTableRevision());
166         assertNull(fromConfig.getExpireDays()); // not supported with legacy
167     }
168
169     @SuppressWarnings("null")
170     @Test
171     public void testRegionWithAccessKeysWithTable() throws Exception {
172         DynamoDBConfig fromConfig = DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "accessKey", "access1",
173                 "secretKey", "secret1", "table", "mytable", "expireDays", "105"));
174         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
175         assertEquals("access1", fromConfig.getCredentials().accessKeyId());
176         assertEquals("secret1", fromConfig.getCredentials().secretAccessKey());
177         assertEquals("mytable", fromConfig.getTable());
178         assertEquals(1, fromConfig.getReadCapacityUnits());
179         assertEquals(1, fromConfig.getWriteCapacityUnits());
180         assertEquals(RetryMode.STANDARD, fromConfig.getRetryPolicy().retryMode());
181         assertEquals(ExpectedTableSchema.NEW, fromConfig.getTableRevision());
182         assertEquals(105, fromConfig.getExpireDays());
183     }
184
185     @SuppressWarnings("null")
186     @Test
187     public void testRegionWithAccessKeysWithoutPrefixWithReadCapacityUnits() throws Exception {
188         DynamoDBConfig fromConfig = DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "accessKey", "access1",
189                 "secretKey", "secret1", "readCapacityUnits", "5", "expireDays", "105"));
190         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
191         assertEquals("access1", fromConfig.getCredentials().accessKeyId());
192         assertEquals("secret1", fromConfig.getCredentials().secretAccessKey());
193         assertEquals("openhab-", fromConfig.getTablePrefixLegacy());
194         assertEquals(5, fromConfig.getReadCapacityUnits());
195         assertEquals(1, fromConfig.getWriteCapacityUnits());
196         assertEquals(RetryMode.STANDARD, fromConfig.getRetryPolicy().retryMode());
197         assertEquals(ExpectedTableSchema.MAYBE_LEGACY, fromConfig.getTableRevision());
198         assertEquals(105, fromConfig.getExpireDays());
199     }
200
201     @SuppressWarnings("null")
202     @Test
203     public void testRegionWithAccessKeysWithoutPrefixWithWriteCapacityUnits() throws Exception {
204         DynamoDBConfig fromConfig = DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "accessKey", "access1",
205                 "secretKey", "secret1", "writeCapacityUnits", "5"));
206         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
207         assertEquals("access1", fromConfig.getCredentials().accessKeyId());
208         assertEquals("secret1", fromConfig.getCredentials().secretAccessKey());
209         assertEquals("openhab-", fromConfig.getTablePrefixLegacy());
210         assertEquals(1, fromConfig.getReadCapacityUnits());
211         assertEquals(5, fromConfig.getWriteCapacityUnits());
212         assertEquals(RetryMode.STANDARD, fromConfig.getRetryPolicy().retryMode());
213         assertEquals(ExpectedTableSchema.MAYBE_LEGACY, fromConfig.getTableRevision());
214         assertNull(fromConfig.getExpireDays()); // default is null
215     }
216
217     @SuppressWarnings("null")
218     @Test
219     public void testRegionWithAccessKeysWithoutPrefixWithReadWriteCapacityUnits() throws Exception {
220         DynamoDBConfig fromConfig = DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "accessKey", "access1",
221                 "secretKey", "secret1", "readCapacityUnits", "3", "writeCapacityUnits", "5", "expireDays", "105"));
222         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
223         assertEquals("access1", fromConfig.getCredentials().accessKeyId());
224         assertEquals("secret1", fromConfig.getCredentials().secretAccessKey());
225         assertEquals("openhab-", fromConfig.getTablePrefixLegacy());
226         assertEquals(3, fromConfig.getReadCapacityUnits());
227         assertEquals(5, fromConfig.getWriteCapacityUnits());
228         assertEquals(RetryMode.STANDARD, fromConfig.getRetryPolicy().retryMode());
229         assertEquals(ExpectedTableSchema.MAYBE_LEGACY, fromConfig.getTableRevision());
230     }
231
232     @SuppressWarnings("null")
233     @Test
234     public void testRegionWithAccessKeysWithPrefixWithReadWriteCapacityUnitsWithBufferSettings() throws Exception {
235         DynamoDBConfig fromConfig = DynamoDBConfig.fromConfig(
236                 mapFrom("region", "eu-west-1", "accessKey", "access1", "secretKey", "secret1", "readCapacityUnits", "3",
237                         "writeCapacityUnits", "5", "bufferCommitIntervalMillis", "501", "bufferSize", "112"));
238         assertEquals(Region.EU_WEST_1, fromConfig.getRegion());
239         assertEquals("access1", fromConfig.getCredentials().accessKeyId());
240         assertEquals("secret1", fromConfig.getCredentials().secretAccessKey());
241         assertEquals("openhab-", fromConfig.getTablePrefixLegacy());
242         assertEquals(3, fromConfig.getReadCapacityUnits());
243         assertEquals(5, fromConfig.getWriteCapacityUnits());
244         assertEquals(RetryMode.STANDARD, fromConfig.getRetryPolicy().retryMode());
245         assertEquals(ExpectedTableSchema.MAYBE_LEGACY, fromConfig.getTableRevision());
246     }
247 }