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