|
| 1 | +package com.phocassoftware.graphql.database.manager.test; |
| 2 | + |
| 3 | +import static com.phocassoftware.graphql.database.manager.test.DynamoDbInitializer.*; |
| 4 | + |
| 5 | +import java.lang.reflect.Parameter; |
| 6 | +import java.util.stream.Stream; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.phocassoftware.graphql.database.manager.Database; |
| 10 | +import com.phocassoftware.graphql.database.manager.VirtualDatabase; |
| 11 | +import com.phocassoftware.graphql.database.manager.dynamo.DynamoDbManager; |
| 12 | +import com.phocassoftware.graphql.database.manager.test.TestDatabaseProvider.ServerWrapper; |
| 13 | +import com.phocassoftware.graphql.database.manager.test.annotations.*; |
| 14 | + |
| 15 | +import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; |
| 16 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 17 | + |
| 18 | +public class ArgumentProvider { |
| 19 | + |
| 20 | + private final String uniqueId; |
| 21 | + private final ServerWrapper wrapper; |
| 22 | + private final String organisationId; |
| 23 | + private final Parameter parameter; |
| 24 | + private final Boolean withHistory; |
| 25 | + private final boolean hashed; |
| 26 | + private final String classPath; |
| 27 | + private final ObjectMapper objectMapper; |
| 28 | + |
| 29 | + public ArgumentProvider( |
| 30 | + String uniqueId, |
| 31 | + ServerWrapper wrapper, |
| 32 | + String organisationId, |
| 33 | + Parameter parameter, |
| 34 | + Boolean withHistory, |
| 35 | + boolean hashed, |
| 36 | + String classPath, |
| 37 | + ObjectMapper objectMapper |
| 38 | + ) { |
| 39 | + this.uniqueId = uniqueId; |
| 40 | + var databaseOrganisation = parameter.getAnnotation(DatabaseOrganisation.class); |
| 41 | + organisationId = databaseOrganisation != null ? databaseOrganisation.value() : organisationId; |
| 42 | + |
| 43 | + this.wrapper = wrapper; |
| 44 | + this.organisationId = organisationId; |
| 45 | + this.parameter = parameter; |
| 46 | + this.withHistory = withHistory; |
| 47 | + this.hashed = hashed; |
| 48 | + this.classPath = classPath; |
| 49 | + this.objectMapper = objectMapper; |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + public DynamoDbManager getDynamoDbManager() { |
| 54 | + try { |
| 55 | + var client = wrapper.clientAsync(); |
| 56 | + final var databaseNames = parameter.getAnnotation(DatabaseNames.class); |
| 57 | + String[] tables; |
| 58 | + String historyTable = null; |
| 59 | + if (databaseNames != null) { |
| 60 | + tables = Stream.of(databaseNames.value()).map(name -> name + "_" + uniqueId).toArray(String[]::new); |
| 61 | + for (final String table : tables) { |
| 62 | + createTable(client, table); |
| 63 | + if (withHistory) { |
| 64 | + historyTable = table + "_history"; |
| 65 | + createHistoryTable(client, historyTable); |
| 66 | + } |
| 67 | + } |
| 68 | + } else { |
| 69 | + tables = new String[] { "table" }; |
| 70 | + historyTable = "table_history"; |
| 71 | + } |
| 72 | + |
| 73 | + final var globalEnabledAnnotation = parameter.getAnnotation(GlobalEnabled.class); |
| 74 | + |
| 75 | + var globalEnabled = true; |
| 76 | + if (globalEnabledAnnotation != null) { |
| 77 | + globalEnabled = globalEnabledAnnotation.value(); |
| 78 | + } |
| 79 | + |
| 80 | + return getDatabaseManager(client, tables, historyTable, globalEnabled, hashed, classPath, "parallelIndex", objectMapper); |
| 81 | + } catch (Exception e) { |
| 82 | + throw new RuntimeException(e); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public HistoryProcessor getHistoryProcessor() { |
| 87 | + return new HistoryProcessor(wrapper.client(), wrapper.streamClient(), parameter, organisationId); |
| 88 | + } |
| 89 | + |
| 90 | + public DynamoDbAsyncClient getClientAsync() { |
| 91 | + return wrapper.clientAsync(); |
| 92 | + } |
| 93 | + |
| 94 | + public DynamoDbClient getClient() { |
| 95 | + return wrapper.client(); |
| 96 | + } |
| 97 | + |
| 98 | + public Database getDatabase() { |
| 99 | + return getEmbeddedDatabase(getDynamoDbManager(), organisationId); |
| 100 | + } |
| 101 | + |
| 102 | + public VirtualDatabase getVirtualDatabase() { |
| 103 | + return new VirtualDatabase(getDatabase()); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments