|
26 | 26 | import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; |
27 | 27 | import com.amazonaws.services.dynamodbv2.local.main.ServerRunner; |
28 | 28 | import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer; |
| 29 | +import java.lang.reflect.Field; |
29 | 30 | import java.net.URI; |
30 | 31 | import java.net.URISyntaxException; |
31 | | -import java.util.Set; |
| 32 | +import java.util.Arrays; |
32 | 33 | import org.junit.jupiter.api.extension.AfterAllCallback; |
33 | 34 | import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 35 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
34 | 36 | import org.junit.jupiter.api.extension.ExtensionContext; |
35 | 37 | import org.junit.jupiter.api.extension.ParameterContext; |
36 | 38 | import org.junit.jupiter.api.extension.ParameterResolutionException; |
|
47 | 49 | /** |
48 | 50 | * Setups the ddb instance. |
49 | 51 | */ |
50 | | -public class DynamoDbExtension |
51 | | - extends DataStoreExtension |
52 | | - implements BeforeAllCallback, AfterAllCallback, ParameterResolver { |
| 52 | +public class DynamoDbExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, ParameterResolver { |
53 | 53 |
|
54 | 54 | private static final Logger LOGGER = LoggerFactory.getLogger(DynamoDbExtension.class); |
55 | | - |
56 | | - @Override |
57 | | - protected Class<?> namespaceClass() { |
58 | | - return DynamoDbExtension.class; |
59 | | - } |
60 | | - |
| 55 | + private static final ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(DynamoDbExtension.class); |
61 | 56 |
|
62 | 57 | private String randomPort() { |
63 | 58 | return String.valueOf((int) (Math.random() * 10000 + 1000)); |
64 | 59 | } |
65 | 60 |
|
| 61 | + private ClassInstanceManager classInstanceManager(final ExtensionContext context) { |
| 62 | + return context.getStore(namespace) |
| 63 | + .getOrComputeIfAbsent(ClassInstanceManager.class, |
| 64 | + k -> new ClassInstanceManager(), |
| 65 | + ClassInstanceManager.class); |
| 66 | + } |
| 67 | + |
66 | 68 | @Override |
67 | 69 | public void beforeAll(final ExtensionContext context) throws Exception { |
68 | 70 | LOGGER.info("Setting in memory DynamoDB local instance"); |
69 | | - String port = randomPort(); |
70 | | - DynamoDBProxyServer server = ServerRunner.createServerFromCommandLineArgs( |
| 71 | + final ClassInstanceManager classInstanceManager = classInstanceManager(context); |
| 72 | + final String port = randomPort(); |
| 73 | + final DynamoDBProxyServer server = ServerRunner.createServerFromCommandLineArgs( |
71 | 74 | new String[]{"-inMemory", "-port", port}); |
72 | 75 | server.start(); |
73 | | - withStore(context, s -> { |
74 | | - AmazonDynamoDB client = getAmazonDynamoDb(port); |
75 | | - s.put(DynamoDBProxyServer.class, server); |
76 | | - s.put(AmazonDynamoDB.class, client); |
77 | | - s.put(DynamoDbClient.class, getDynamoDbClient(port)); |
78 | | - s.put(DynamoDBMapper.class, new DynamoDBMapper(client)); |
79 | | - }); |
| 76 | + AmazonDynamoDB client = getAmazonDynamoDb(port); |
| 77 | + classInstanceManager.put(DynamoDBProxyServer.class, server); |
| 78 | + classInstanceManager.put(AmazonDynamoDB.class, client); |
| 79 | + classInstanceManager.put(DynamoDbClient.class, getDynamoDbClient(port)); |
| 80 | + classInstanceManager.put(DynamoDBMapper.class, new DynamoDBMapper(client)); |
80 | 81 | } |
81 | 82 |
|
82 | 83 | @Override |
83 | 84 | public void afterAll(final ExtensionContext context) { |
84 | 85 | LOGGER.info("Tearing down in memory DynamoDB local instance"); |
85 | | - withStore(context, s -> { |
| 86 | + final ClassInstanceManager classInstanceManager = context.getStore(namespace).remove(ClassInstanceManager.class, ClassInstanceManager.class); |
| 87 | + if (classInstanceManager == null) { |
| 88 | + LOGGER.error("No class instance manager found"); |
| 89 | + return; |
| 90 | + } |
| 91 | + classInstanceManager.remove(DynamoDBProxyServer.class).ifPresent(o -> { |
86 | 92 | try { |
87 | | - s.remove(DynamoDBProxyServer.class, DynamoDBProxyServer.class).stop(); |
| 93 | + o.stop(); |
88 | 94 | } catch (Exception e) { |
89 | | - LOGGER.error("Failed to tear down DynamoDBProxyServer", e); |
| 95 | + LOGGER.error("Error stopping DynamoDB proxy server", e); |
90 | 96 | } |
91 | | - s.remove(DynamoDBMapper.class); |
92 | | - s.remove(AmazonDynamoDB.class); |
93 | | - s.remove(DynamoDbClient.class, DynamoDbClient.class).close(); |
94 | 97 | }); |
| 98 | + classInstanceManager.remove(DynamoDBMapper.class); |
| 99 | + classInstanceManager.remove(AmazonDynamoDB.class); |
| 100 | + classInstanceManager.remove(DynamoDbClient.class).ifPresent(DynamoDbClient::close); |
| 101 | + classInstanceManager.clear(); |
95 | 102 | } |
96 | 103 |
|
97 | 104 | @Override |
98 | 105 | public boolean supportsParameter(final ParameterContext parameterContext, |
99 | 106 | final ExtensionContext extensionContext) throws ParameterResolutionException { |
100 | 107 | final Class<?> type = parameterContext.getParameter().getType(); |
101 | | - return parameterContext.isAnnotated(DataStore.class) && extensionContext.getStore(namespace).get(type) != null; |
| 108 | + final ClassInstanceManager classInstanceManager = classInstanceManager(extensionContext); |
| 109 | + return parameterContext.isAnnotated(DataStore.class) && classInstanceManager.hasInstance(type); |
102 | 110 | } |
103 | 111 |
|
104 | 112 | @Override |
105 | 113 | public Object resolveParameter(final ParameterContext parameterContext, |
106 | 114 | final ExtensionContext extensionContext) throws ParameterResolutionException { |
107 | 115 | final Class<?> type = parameterContext.getParameter().getType(); |
108 | | - return extensionContext.getStore(namespace).get(type); |
| 116 | + final ClassInstanceManager classInstanceManager = classInstanceManager(extensionContext); |
| 117 | + return classInstanceManager.get(type).orElseGet(() -> { |
| 118 | + LOGGER.error("No instance found for type {}", type.getSimpleName()); |
| 119 | + return null; |
| 120 | + }); |
109 | 121 | } |
110 | 122 |
|
111 | 123 | private AmazonDynamoDB getAmazonDynamoDb(String port) { |
@@ -136,4 +148,47 @@ private DynamoDbClient getDynamoDbClient(final String port) { |
136 | 148 | } |
137 | 149 | } |
138 | 150 |
|
| 151 | + @Override |
| 152 | + public void beforeEach(final ExtensionContext context) { |
| 153 | + final ClassInstanceManager classInstanceManager = classInstanceManager(context); |
| 154 | + context.getRequiredTestInstances().getAllInstances().forEach(instance -> { |
| 155 | + Arrays.stream(instance.getClass().getDeclaredFields()) |
| 156 | + .filter(f -> f.isAnnotationPresent(DataStore.class)) |
| 157 | + .forEach(field -> { |
| 158 | + setValueForField(classInstanceManager, instance, field); |
| 159 | + }); |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + private void setValueForField(final ClassInstanceManager classInstanceManager, |
| 164 | + final Object o, |
| 165 | + final Field field) { |
| 166 | + final Object value = classInstanceManager.get(field.getType()) |
| 167 | + .orElseThrow(() -> new IllegalArgumentException("Unable to find DynamoDB extension value of type " + field.getType())); // Check the store to see we have this type. |
| 168 | + LOGGER.info("Setting field {}:{}", field.getName(), field.getType().getSimpleName()); |
| 169 | + enableSettingTheField(field); |
| 170 | + try { |
| 171 | + field.set(o, value); |
| 172 | + } catch (IllegalAccessException e) { |
| 173 | + LOGGER.error("Unable to set the field value for {}", field.getName(), e); |
| 174 | + LOGGER.error("Continuing, but expect nothing good will happen next."); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * This allows us to set the field directly. It will fail if the security manager in play disallows it. |
| 180 | + * We can talk about justifications all we want, but really we know Java is not Smalltalk. Meta programming |
| 181 | + * is limited here. So... we try to do the right thing. |
| 182 | + * |
| 183 | + * @param field to change accessibility for. |
| 184 | + */ |
| 185 | + protected void enableSettingTheField(final Field field) { |
| 186 | + try { |
| 187 | + field.setAccessible(true); |
| 188 | + } catch (RuntimeException re) { |
| 189 | + LOGGER.error("Unable to change accessibility for field due to private var or security manager: {}", |
| 190 | + field.getName()); |
| 191 | + LOGGER.error("The setting will likely fail. Consider changing that type to protected.", re); |
| 192 | + } |
| 193 | + } |
139 | 194 | } |
0 commit comments