|
| 1 | +package io.apicurio.tests.dbupgrade; |
| 2 | + |
| 3 | +import io.apicurio.registry.rest.client.RegistryClientFactory; |
| 4 | +import io.apicurio.registry.test.utils.KafkaTestContainerManager; |
| 5 | +import io.apicurio.registry.types.ArtifactType; |
| 6 | +import io.apicurio.registry.utils.IoUtil; |
| 7 | +import io.apicurio.registry.utils.tests.SimpleDisplayName; |
| 8 | +import io.apicurio.tests.ApicurioRegistryBaseIT; |
| 9 | +import io.apicurio.tests.utils.Constants; |
| 10 | +import io.apicurio.tests.utils.CustomTestsUtils; |
| 11 | +import io.apicurio.tests.utils.TestSeparator; |
| 12 | +import io.quarkus.test.common.QuarkusTestResource; |
| 13 | +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; |
| 14 | +import io.quarkus.test.junit.QuarkusIntegrationTest; |
| 15 | +import org.junit.jupiter.api.DisplayNameGeneration; |
| 16 | +import org.junit.jupiter.api.Tag; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.jupiter.api.TestInstance; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.testcontainers.containers.GenericContainer; |
| 22 | +import org.testcontainers.containers.Network; |
| 23 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 24 | + |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.stream.Collectors; |
| 29 | + |
| 30 | +import static io.apicurio.tests.dbupgrade.UpgradeTestsDataInitializer.PREPARE_AVRO_GROUP; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | + |
| 33 | +@DisplayNameGeneration(SimpleDisplayName.class) |
| 34 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 35 | +@Tag(Constants.DB_UPGRADE) |
| 36 | +@Tag(Constants.KAFKA_SQL) |
| 37 | +@QuarkusTestResource(value = KafkaTestContainerManager.class, restrictToAnnotatedClass = true) |
| 38 | +@QuarkusTestResource(value = KafkaSqlAvroUpgraderIT.KafkaSqlAvroUpgraderInitializer.class, restrictToAnnotatedClass = true) |
| 39 | +@QuarkusIntegrationTest |
| 40 | +public class KafkaSqlAvroUpgraderIT extends ApicurioRegistryBaseIT implements TestSeparator, Constants { |
| 41 | + |
| 42 | + static final Logger logger = LoggerFactory.getLogger(KafkaSqlLogCompactionIT.class); |
| 43 | + protected static CustomTestsUtils.ArtifactData avroData; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void cleanArtifacts() throws Exception { |
| 47 | + //Don't clean artifacts for this test |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testStorageUpgradeAvroUpgraderKafkaSql() throws Exception { |
| 52 | + //The check must be retried so the kafka storage has been bootstrapped |
| 53 | + retry(() -> assertEquals(3, registryClient.listArtifactsInGroup(PREPARE_AVRO_GROUP).getCount())); |
| 54 | + |
| 55 | + var searchResults = registryClient.listArtifactsInGroup(PREPARE_AVRO_GROUP); |
| 56 | + |
| 57 | + var avros = searchResults.getArtifacts().stream() |
| 58 | + .filter(ar -> ar.getType().equals(ArtifactType.AVRO)) |
| 59 | + .collect(Collectors.toList()); |
| 60 | + |
| 61 | + System.out.println("Avro artifacts are " + avros.size()); |
| 62 | + assertEquals(1, avros.size()); |
| 63 | + var avroMetadata = registryClient.getArtifactMetaData(avros.get(0).getGroupId(), avros.get(0).getId()); |
| 64 | + var content = registryClient.getContentByGlobalId(avroMetadata.getGlobalId()); |
| 65 | + |
| 66 | + //search with canonicalize |
| 67 | + var versionMetadata = registryClient.getArtifactVersionMetaDataByContent(avros.get(0).getGroupId(), avros.get(0).getId(), true, null, content); |
| 68 | + assertEquals(avroData.meta.getContentId(), versionMetadata.getContentId()); |
| 69 | + |
| 70 | + String test1content = ApicurioRegistryBaseIT.resourceToString("artifactTypes/" + "avro/multi-field_v1.json"); |
| 71 | + |
| 72 | + //search with canonicalize |
| 73 | + versionMetadata = registryClient.getArtifactVersionMetaDataByContent(avros.get(0).getGroupId(), avros.get(0).getId(), true, null, IoUtil.toStream(test1content)); |
| 74 | + assertEquals(avroData.meta.getContentId(), versionMetadata.getContentId()); |
| 75 | + |
| 76 | + //search without canonicalize |
| 77 | + versionMetadata = registryClient.getArtifactVersionMetaDataByContent(avros.get(0).getGroupId(), avros.get(0).getId(), false, null, IoUtil.toStream(test1content)); |
| 78 | + assertEquals(avroData.meta.getContentId(), versionMetadata.getContentId()); |
| 79 | + |
| 80 | + //create one more avro artifact and verify |
| 81 | + String test2content = ApicurioRegistryBaseIT.resourceToString("artifactTypes/" + "avro/multi-field_v2.json"); |
| 82 | + avroData = CustomTestsUtils.createArtifact(registryClient, PREPARE_AVRO_GROUP, ArtifactType.AVRO, test2content); |
| 83 | + versionMetadata = registryClient.getArtifactVersionMetaDataByContent(PREPARE_AVRO_GROUP, avroData.meta.getId(), true, null, IoUtil.toStream(test2content)); |
| 84 | + assertEquals(avroData.meta.getContentId(), versionMetadata.getContentId()); |
| 85 | + |
| 86 | + //assert total num of artifacts |
| 87 | + assertEquals(4, registryClient.listArtifactsInGroup(PREPARE_AVRO_GROUP).getCount()); |
| 88 | + } |
| 89 | + |
| 90 | + public static class KafkaSqlAvroUpgraderInitializer implements QuarkusTestResourceLifecycleManager { |
| 91 | + private GenericContainer genericContainer; |
| 92 | + @Override |
| 93 | + public Map<String, String> start() { |
| 94 | + if (!Boolean.parseBoolean(System.getProperty("cluster.tests"))) { |
| 95 | + |
| 96 | + String bootstrapServers = System.getProperty("bootstrap.servers.internal"); |
| 97 | + startOldRegistryVersion("quay.io/apicurio/apicurio-registry-kafkasql:2.3.0.Final", bootstrapServers); |
| 98 | + |
| 99 | + try { |
| 100 | + var registryClient = RegistryClientFactory.create("http://localhost:8081/"); |
| 101 | + UpgradeTestsDataInitializer.prepareAvroCanonicalHashUpgradeData(registryClient); |
| 102 | + } catch (Exception e) { |
| 103 | + logger.warn("Error filling old registry with information: ", e); |
| 104 | + } |
| 105 | + } |
| 106 | + return Collections.emptyMap(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int order() { |
| 111 | + return 10000; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void stop() { |
| 116 | + if (genericContainer != null && genericContainer.isRunning()) { |
| 117 | + genericContainer.stop(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void startOldRegistryVersion(String registryImage, String bootstrapServers) { |
| 122 | + genericContainer = new GenericContainer<>(registryImage) |
| 123 | + .withEnv(Map.of("KAFKA_BOOTSTRAP_SERVERS", bootstrapServers, "QUARKUS_HTTP_PORT", "8081")) |
| 124 | + .withExposedPorts(8081) |
| 125 | + .withNetwork(Network.SHARED); |
| 126 | + |
| 127 | + genericContainer.setPortBindings(List.of("8081:8081")); |
| 128 | + |
| 129 | + genericContainer.waitingFor(Wait.forHttp("/apis/registry/v2/search/artifacts").forStatusCode(200)); |
| 130 | + genericContainer.start(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments