2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.dynamodb.internal;
15 import static org.junit.jupiter.api.Assertions.*;
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;
25 import java.util.Optional;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.io.TempDir;
31 import software.amazon.awssdk.core.retry.RetryMode;
32 import software.amazon.awssdk.core.retry.RetryPolicy;
33 import software.amazon.awssdk.regions.Region;
37 * @author Sami Salonen - Initial contribution
41 public class DynamoDBConfigTest {
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];
51 return Collections.unmodifiableMap(config);
54 public @TempDir @NonNullByDefault({}) File folder;
57 public void testEmpty() throws Exception {
58 assertNull(DynamoDBConfig.fromConfig(new HashMap<>()));
62 public void testInvalidRegion() throws Exception {
63 assertNull(DynamoDBConfig.fromConfig(Collections.singletonMap("region", "foobie")));
67 public void testRegionOnly() throws Exception {
68 assertNull(DynamoDBConfig.fromConfig(Collections.singletonMap("region", "eu-west-1")));
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());
86 @SuppressWarnings("null")
88 public void testRegionWithProfilesConfigFile() throws Exception {
89 Path credsFile = Files.createFile(Paths.get(folder.getPath(), "creds"));
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);
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());
106 @SuppressWarnings("null")
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);
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());
127 public void testEmptyConfiguration() throws Exception {
128 assertNull(DynamoDBConfig.fromConfig(mapFrom()));
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);
139 assertNull(DynamoDBConfig.fromConfig(mapFrom("region", "eu-west-1", "profilesConfigFile",
140 credsFile.toFile().getAbsolutePath(), "profile", "fooprofile")));
144 public void testRegionWithProfilesConfigFileMissingProfile() throws Exception {
145 Path credsFile = Files.createFile(Paths.get(folder.getPath(), "creds"));
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);
151 assertNull(DynamoDBConfig.fromConfig(
152 mapFrom("region", "eu-west-1", "profilesConfigFile", credsFile.toAbsolutePath().toString())));
155 @SuppressWarnings("null")
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
171 @SuppressWarnings("null")
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());
187 @SuppressWarnings("null")
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());
203 @SuppressWarnings("null")
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
219 @SuppressWarnings("null")
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());
234 @SuppressWarnings("null")
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());