|
| 1 | +/* |
| 2 | + * Copyright 2023 Flamingock (https://www.flamingock.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.flamingock.importer.mongock.dynamodb; |
| 17 | + |
| 18 | +import io.flamingock.api.annotations.EnableFlamingock; |
| 19 | +import io.flamingock.api.annotations.Stage; |
| 20 | +import io.flamingock.community.dynamodb.driver.DynamoDBAuditStore; |
| 21 | +import io.flamingock.importer.dynamodb.MongockDynamoDBAuditEntry; |
| 22 | +import io.flamingock.internal.common.core.audit.AuditEntry; |
| 23 | +import io.flamingock.internal.common.core.error.FlamingockException; |
| 24 | +import io.flamingock.internal.core.builder.FlamingockFactory; |
| 25 | +import io.flamingock.internal.core.runner.Runner; |
| 26 | +import io.flamingock.support.mongock.annotations.MongockSupport; |
| 27 | +import org.junit.jupiter.api.Assertions; |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Disabled; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.testcontainers.containers.GenericContainer; |
| 33 | +import org.testcontainers.junit.jupiter.Container; |
| 34 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 35 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 36 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 37 | +import software.amazon.awssdk.regions.Region; |
| 38 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 39 | +import software.amazon.awssdk.services.dynamodb.model.ScanRequest; |
| 40 | +import software.amazon.awssdk.services.dynamodb.model.ScanResponse; |
| 41 | + |
| 42 | +import java.net.URI; |
| 43 | +import java.time.Instant; |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.HashMap; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +import static io.flamingock.internal.util.constants.CommunityPersistenceConstants.DEFAULT_AUDIT_STORE_NAME; |
| 49 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 50 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 51 | + |
| 52 | +@Testcontainers |
| 53 | +@MongockSupport(targetSystem = "dynamodb-target-system") |
| 54 | +@EnableFlamingock(stages = {@Stage(location = "io.flamingock.importer.mongock.dynamodb.changes")}) |
| 55 | +public class DynamoDBImporterTest { |
| 56 | + |
| 57 | + @Container |
| 58 | + public static final GenericContainer<?> dynamoDBContainer = new GenericContainer<>("amazon/dynamodb-local:latest") |
| 59 | + .withExposedPorts(8000); |
| 60 | + |
| 61 | + public static final String MONGOCK_CHANGE_LOGS = "mongockChangeLogs"; |
| 62 | + |
| 63 | + |
| 64 | + private static DynamoDbClient client; |
| 65 | + private DynamoDBTestHelper mongockChangeLogsHelper; |
| 66 | + |
| 67 | + @BeforeAll |
| 68 | + static void beforeAll() { |
| 69 | + dynamoDBContainer.start(); |
| 70 | + |
| 71 | + String endpoint = String.format("http://%s:%d", |
| 72 | + dynamoDBContainer.getHost(), |
| 73 | + dynamoDBContainer.getMappedPort(8000)); |
| 74 | + client = DynamoDbClient.builder() |
| 75 | + .endpointOverride(URI.create(endpoint)) |
| 76 | + .region(Region.US_EAST_1) |
| 77 | + .credentialsProvider( |
| 78 | + StaticCredentialsProvider.create( |
| 79 | + AwsBasicCredentials.create("dummy", "dummy") |
| 80 | + ) |
| 81 | + ) |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | + @BeforeEach |
| 86 | + void setUp() { |
| 87 | + mongockChangeLogsHelper = new DynamoDBTestHelper(client, MONGOCK_CHANGE_LOGS); |
| 88 | + mongockChangeLogsHelper.ensureTableExists(); |
| 89 | + mongockChangeLogsHelper.resetTable(); |
| 90 | + |
| 91 | + new DynamoDBTestHelper(client, DEFAULT_AUDIT_STORE_NAME).ensureTableExists(); |
| 92 | + new DynamoDBTestHelper(client, DEFAULT_AUDIT_STORE_NAME).resetTable(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @Disabled("restore when https://trello.com/c/4gEQ8Wb4/458-mongock-legacy-targetsystem done") |
| 97 | + void testImportDynamoDBChangeLogs() { |
| 98 | + List<MongockDynamoDBAuditEntry> entries = Arrays.asList( |
| 99 | + new MongockDynamoDBAuditEntry( |
| 100 | + "exec-1", |
| 101 | + "client-initializer", |
| 102 | + "author1", |
| 103 | + String.valueOf(Instant.now().toEpochMilli()), |
| 104 | + "EXECUTED", |
| 105 | + "EXECUTION", |
| 106 | + "io.flamingock.changelog.Class1", |
| 107 | + "method1", |
| 108 | + new HashMap<String, String>() {{ |
| 109 | + put("meta1", "value1"); |
| 110 | + }}.toString(), |
| 111 | + 123L, |
| 112 | + "host1", |
| 113 | + null, |
| 114 | + true |
| 115 | + ), |
| 116 | + new MongockDynamoDBAuditEntry( |
| 117 | + "exec-1", |
| 118 | + "client-updater", |
| 119 | + "author1", |
| 120 | + String.valueOf(Instant.now().toEpochMilli()), |
| 121 | + "EXECUTED", |
| 122 | + "EXECUTION", |
| 123 | + "io.flamingock.changelog.Class2", |
| 124 | + "method1", |
| 125 | + new HashMap<String, String>() {{ |
| 126 | + put("meta1", "value1"); |
| 127 | + }}.toString(), |
| 128 | + 123L, |
| 129 | + "host1", |
| 130 | + null, |
| 131 | + true |
| 132 | + ) |
| 133 | + ); |
| 134 | + |
| 135 | + mongockChangeLogsHelper.insertChangeEntries(entries); |
| 136 | + |
| 137 | + Runner flamingock = FlamingockFactory.getCommunityBuilder() |
| 138 | + .setAuditStore(new DynamoDBAuditStore(client)) |
| 139 | + .build(); |
| 140 | + |
| 141 | + flamingock.run(); |
| 142 | + |
| 143 | + List<AuditEntry> auditLog = new DynamoDBTestHelper(client, DEFAULT_AUDIT_STORE_NAME).getAuditEntriesSorted(); |
| 144 | + assertEquals(6, auditLog.size()); |
| 145 | + |
| 146 | + for (AuditEntry entry : auditLog) { |
| 147 | + System.out.println("executionId: " + entry.getExecutionId()); |
| 148 | + System.out.println("stageId: " + entry.getStageId()); |
| 149 | + System.out.println("taskId: " + entry.getTaskId()); |
| 150 | + System.out.println("author: " + entry.getAuthor()); |
| 151 | + System.out.println("createdAt: " + entry.getCreatedAt()); |
| 152 | + System.out.println("state: " + entry.getState()); |
| 153 | + System.out.println("type: " + entry.getType()); |
| 154 | + System.out.println("className: " + entry.getClassName()); |
| 155 | + System.out.println("methodName: " + entry.getMethodName()); |
| 156 | + System.out.println("executionMillis: " + entry.getExecutionMillis()); |
| 157 | + System.out.println("executionHostname: " + entry.getExecutionHostname()); |
| 158 | + System.out.println("metadata: " + entry.getMetadata()); |
| 159 | + System.out.println("systemChange: " + entry.getSystemChange()); |
| 160 | + System.out.println("errorTrace: " + entry.getErrorTrace()); |
| 161 | + System.out.println("txStrategy: " + entry.getTxType()); |
| 162 | + System.out.println("targetSystemId: " + entry.getTargetSystemId()); |
| 163 | + System.out.println("order: " + entry.getOrder()); |
| 164 | + System.out.println("-----"); |
| 165 | + } |
| 166 | + |
| 167 | + AuditEntry entry1 = auditLog.stream() |
| 168 | + .filter(e -> "client-updater".equals(e.getTaskId())) |
| 169 | + .findFirst() |
| 170 | + .orElseThrow(() -> new AssertionError("Entry with changeId 'client-updater' not found")); |
| 171 | + |
| 172 | + |
| 173 | + assertEquals("client-updater", entry1.getTaskId()); |
| 174 | + assertEquals("author1", entry1.getAuthor()); |
| 175 | + assertEquals("exec-1", entry1.getExecutionId()); |
| 176 | + assertTrue(entry1.getSystemChange()); |
| 177 | + |
| 178 | + ScanResponse scanResponse = client.scan( |
| 179 | + ScanRequest.builder().tableName(DEFAULT_AUDIT_STORE_NAME).build() |
| 180 | + ); |
| 181 | + assertTrue(scanResponse.count() > 0, "Audit table should not be empty"); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + @Disabled("restore when https://trello.com/c/4gEQ8Wb4/458-mongock-legacy-targetsystem done") |
| 186 | + void failIfEmptyOrigin() { |
| 187 | + Runner flamingock = FlamingockFactory.getCommunityBuilder() |
| 188 | + .setAuditStore(new DynamoDBAuditStore(client)) |
| 189 | + .build(); |
| 190 | + |
| 191 | + Assertions.assertThrows(FlamingockException.class, flamingock::run); |
| 192 | + } |
| 193 | +} |
0 commit comments