|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.config.specs; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.JsonNode; |
| 11 | +import com.google.common.collect.ImmutableMap; |
| 12 | +import io.airbyte.commons.json.Jsons; |
| 13 | +import io.airbyte.config.ConfigSchema; |
| 14 | +import io.airbyte.config.DockerImageSpec; |
| 15 | +import io.airbyte.config.StandardDestinationDefinition; |
| 16 | +import io.airbyte.protocol.models.ConnectorSpecification; |
| 17 | +import java.util.List; |
| 18 | +import java.util.UUID; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +class CombinedConnectorCatalogGeneratorTest { |
| 23 | + |
| 24 | + private static final UUID DEF_ID1 = UUID.randomUUID(); |
| 25 | + private static final UUID DEF_ID2 = UUID.randomUUID(); |
| 26 | + private static final String CONNECTOR_NAME1 = "connector1"; |
| 27 | + private static final String CONNECTOR_NAME2 = "connector2"; |
| 28 | + private static final String DOCUMENTATION_URL = "https://www.example.com"; |
| 29 | + private static final String DOCKER_REPOSITORY1 = "airbyte/connector1"; |
| 30 | + private static final String DOCKER_REPOSITORY2 = "airbyte/connector2"; |
| 31 | + private static final String DOCKER_TAG1 = "0.1.0"; |
| 32 | + private static final String DOCKER_TAG2 = "0.2.0"; |
| 33 | + |
| 34 | + private CombinedConnectorCatalogGenerator catalogGenerator; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setup() { |
| 38 | + catalogGenerator = new CombinedConnectorCatalogGenerator(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void testMergeSpecsIntoDefinitions() { |
| 43 | + final StandardDestinationDefinition destinationDefinition1 = new StandardDestinationDefinition() |
| 44 | + .withDestinationDefinitionId(DEF_ID1) |
| 45 | + .withDockerRepository(DOCKER_REPOSITORY1) |
| 46 | + .withDockerImageTag(DOCKER_TAG1) |
| 47 | + .withName(CONNECTOR_NAME1) |
| 48 | + .withDocumentationUrl(DOCUMENTATION_URL) |
| 49 | + .withSpec(new ConnectorSpecification()); |
| 50 | + final StandardDestinationDefinition destinationDefinition2 = new StandardDestinationDefinition() |
| 51 | + .withDestinationDefinitionId(DEF_ID2) |
| 52 | + .withDockerRepository(DOCKER_REPOSITORY2) |
| 53 | + .withDockerImageTag(DOCKER_TAG2) |
| 54 | + .withName(CONNECTOR_NAME2) |
| 55 | + .withDocumentationUrl(DOCUMENTATION_URL) |
| 56 | + .withSpec(new ConnectorSpecification()); |
| 57 | + final DockerImageSpec destinationSpec1 = new DockerImageSpec().withDockerImage(DOCKER_REPOSITORY1 + ":" + DOCKER_TAG1) |
| 58 | + .withSpec(new ConnectorSpecification().withConnectionSpecification(Jsons.jsonNode(ImmutableMap.of( |
| 59 | + "foo1", |
| 60 | + "bar1")))); |
| 61 | + final DockerImageSpec destinationSpec2 = new DockerImageSpec().withDockerImage(DOCKER_REPOSITORY2 + ":" + DOCKER_TAG2) |
| 62 | + .withSpec(new ConnectorSpecification().withConnectionSpecification(Jsons.jsonNode(ImmutableMap.of( |
| 63 | + "foo2", |
| 64 | + "bar2")))); |
| 65 | + |
| 66 | + final List<JsonNode> definitions = List.of(Jsons.jsonNode(destinationDefinition1), Jsons.jsonNode(destinationDefinition2)); |
| 67 | + final List<JsonNode> specs = List.of(Jsons.jsonNode(destinationSpec1), Jsons.jsonNode(destinationSpec2)); |
| 68 | + |
| 69 | + catalogGenerator.mergeSpecsIntoDefinitions(definitions, specs, ConfigSchema.STANDARD_DESTINATION_DEFINITION); |
| 70 | + |
| 71 | + final StandardDestinationDefinition expectedDefinition1 = new StandardDestinationDefinition() |
| 72 | + .withDestinationDefinitionId(DEF_ID1) |
| 73 | + .withDockerRepository(DOCKER_REPOSITORY1) |
| 74 | + .withDockerImageTag(DOCKER_TAG1) |
| 75 | + .withName(CONNECTOR_NAME1) |
| 76 | + .withDocumentationUrl(DOCUMENTATION_URL) |
| 77 | + .withSpec(destinationSpec1.getSpec()); |
| 78 | + |
| 79 | + final StandardDestinationDefinition expectedDefinition2 = new StandardDestinationDefinition() |
| 80 | + .withDestinationDefinitionId(DEF_ID2) |
| 81 | + .withDockerRepository(DOCKER_REPOSITORY2) |
| 82 | + .withDockerImageTag(DOCKER_TAG2) |
| 83 | + .withName(CONNECTOR_NAME2) |
| 84 | + .withDocumentationUrl(DOCUMENTATION_URL) |
| 85 | + .withSpec(destinationSpec2.getSpec()); |
| 86 | + |
| 87 | + assertEquals(Jsons.jsonNode(expectedDefinition1), definitions.get(0)); |
| 88 | + assertEquals(Jsons.jsonNode(expectedDefinition2), definitions.get(1)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void testMergeSpecsIntoDefinitionsThrowsOnMissingSpec() { |
| 93 | + final StandardDestinationDefinition destinationDefinition1 = new StandardDestinationDefinition() |
| 94 | + .withDestinationDefinitionId(DEF_ID1) |
| 95 | + .withDockerRepository(DOCKER_REPOSITORY1) |
| 96 | + .withDockerImageTag(DOCKER_TAG1) |
| 97 | + .withName(CONNECTOR_NAME1) |
| 98 | + .withDocumentationUrl(DOCUMENTATION_URL) |
| 99 | + .withSpec(new ConnectorSpecification()); |
| 100 | + final List<JsonNode> definitions = List.of(Jsons.jsonNode(destinationDefinition1)); |
| 101 | + final List<JsonNode> specs = List.of(); |
| 102 | + |
| 103 | + assertThrows(UnsupportedOperationException.class, |
| 104 | + () -> catalogGenerator.mergeSpecsIntoDefinitions(definitions, specs, ConfigSchema.STANDARD_DESTINATION_DEFINITION)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testMergeSpecsIntoDefinitionsThrowsOnInvalidFormat() { |
| 109 | + final JsonNode invalidDefinition = Jsons.jsonNode(ImmutableMap.of("dockerRepository", DOCKER_REPOSITORY1, "dockerImageTag", DOCKER_TAG1)); |
| 110 | + final DockerImageSpec destinationSpec = new DockerImageSpec().withDockerImage(DOCKER_REPOSITORY1 + ":" + DOCKER_TAG1) |
| 111 | + .withSpec(new ConnectorSpecification().withConnectionSpecification(Jsons.jsonNode(ImmutableMap.of( |
| 112 | + "foo1", |
| 113 | + "bar1")))); |
| 114 | + |
| 115 | + final List<JsonNode> definitions = List.of(Jsons.jsonNode(invalidDefinition)); |
| 116 | + final List<JsonNode> specs = List.of(Jsons.jsonNode(destinationSpec)); |
| 117 | + |
| 118 | + assertThrows(RuntimeException.class, |
| 119 | + () -> catalogGenerator.mergeSpecsIntoDefinitions(definitions, specs, ConfigSchema.STANDARD_DESTINATION_DEFINITION)); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments