From 173b47b803af8588a120d2efe058c046505a3e94 Mon Sep 17 00:00:00 2001 From: Liren Tu Date: Tue, 20 Jul 2021 11:08:08 -0700 Subject: [PATCH] Remove duplicated seed repository --- .../io/airbyte/server/SeedRepository.java | 107 --------------- .../io/airbyte/server/SeedRepositoryTest.java | 128 ------------------ 2 files changed, 235 deletions(-) delete mode 100644 airbyte-server/src/main/java/io/airbyte/server/SeedRepository.java delete mode 100644 airbyte-server/src/test/java/io/airbyte/server/SeedRepositoryTest.java diff --git a/airbyte-server/src/main/java/io/airbyte/server/SeedRepository.java b/airbyte-server/src/main/java/io/airbyte/server/SeedRepository.java deleted file mode 100644 index b815dd193718..000000000000 --- a/airbyte-server/src/main/java/io/airbyte/server/SeedRepository.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 Airbyte - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package io.airbyte.server; - -import com.fasterxml.jackson.databind.JsonNode; -import io.airbyte.commons.io.IOs; -import io.airbyte.commons.json.Jsons; -import io.airbyte.config.helpers.YamlListToStandardDefinitions; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.stream.Collectors; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.DefaultParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; - -/** - * This class takes in a yaml file with a list of objects. It then then assigns each object a uuid - * based on its name attribute. The uuid is written as a field in the object with the key specified - * as the id-name. It then writes each object to its own file in the specified output directory. - * Each file's name is the generated uuid. The goal is that a user should be able to add objects to - * the database seed without having to generate uuids themselves. The output files should be - * compatible with our file system database (config persistence). - */ -public class SeedRepository { - - private static final Options OPTIONS = new Options(); - private static final Option ID_NAME_OPTION = new Option("id", "id-name", true, "field name of the id"); - private static final Option INPUT_PATH_OPTION = new Option("i", "input-path", true, "path to input file"); - private static final Option OUTPUT_PATH_OPTION = new Option("o", "output-path", true, "path to where files will be output"); - - static { - ID_NAME_OPTION.setRequired(true); - INPUT_PATH_OPTION.setRequired(true); - OUTPUT_PATH_OPTION.setRequired(true); - OPTIONS.addOption(ID_NAME_OPTION); - OPTIONS.addOption(INPUT_PATH_OPTION); - OPTIONS.addOption(OUTPUT_PATH_OPTION); - } - - private static CommandLine parse(final String[] args) { - final CommandLineParser parser = new DefaultParser(); - final HelpFormatter helpFormatter = new HelpFormatter(); - - try { - return parser.parse(OPTIONS, args); - } catch (final ParseException e) { - helpFormatter.printHelp("", OPTIONS); - throw new IllegalArgumentException(e); - } - } - - public static void main(final String[] args) throws IOException { - final CommandLine parsed = parse(args); - final String idName = parsed.getOptionValue(ID_NAME_OPTION.getOpt()); - final Path inputPath = Path.of(parsed.getOptionValue(INPUT_PATH_OPTION.getOpt())); - final Path outputPath = Path.of(parsed.getOptionValue(OUTPUT_PATH_OPTION.getOpt())); - - new SeedRepository().run(idName, inputPath, outputPath); - } - - public void run(final String idName, final Path input, final Path output) throws IOException { - final var jsonNode = YamlListToStandardDefinitions.verifyAndConvertToJsonNode(idName, IOs.readFile(input)); - final var elementsIter = jsonNode.elements(); - - // clean output directory. - for (final Path file : Files.list(output).collect(Collectors.toList())) { - Files.delete(file); - } - - // write to output directory. - while (elementsIter.hasNext()) { - final JsonNode element = Jsons.clone(elementsIter.next()); - IOs.writeFile( - output, - element.get(idName).asText() + ".json", - Jsons.toPrettyString(element)); - } - } - -} diff --git a/airbyte-server/src/test/java/io/airbyte/server/SeedRepositoryTest.java b/airbyte-server/src/test/java/io/airbyte/server/SeedRepositoryTest.java deleted file mode 100644 index e03455038b02..000000000000 --- a/airbyte-server/src/test/java/io/airbyte/server/SeedRepositoryTest.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 Airbyte - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package io.airbyte.server; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.google.common.collect.ImmutableMap; -import io.airbyte.commons.io.IOs; -import io.airbyte.commons.json.Jsons; -import io.airbyte.commons.yaml.Yamls; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.UUID; -import java.util.stream.Collectors; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class SeedRepositoryTest { - - private static final String CONFIG_ID = "configId"; - private static final JsonNode OBJECT = Jsons.jsonNode(ImmutableMap.builder() - .put(CONFIG_ID, UUID.randomUUID()) - .put("name", "barker") - .put("description", "playwright") - .build()); - - private Path input; - private Path output; - - @BeforeEach - void setup() throws IOException { - input = Files.createTempDirectory("test_input").resolve("input.yaml"); - output = Files.createTempDirectory("test_output"); - - writeSeedList(OBJECT); - } - - @Test - void testWrite() throws IOException { - new SeedRepository().run(CONFIG_ID, input, output); - final JsonNode actual = Jsons.deserialize(IOs.readFile(output, OBJECT.get(CONFIG_ID).asText() + ".json")); - assertEquals(OBJECT, actual); - } - - @Test - void testOverwrites() throws IOException { - new SeedRepository().run(CONFIG_ID, input, output); - final JsonNode actual = Jsons.deserialize(IOs.readFile(output, OBJECT.get(CONFIG_ID).asText() + ".json")); - assertEquals(OBJECT, actual); - - final JsonNode clone = Jsons.clone(OBJECT); - ((ObjectNode) clone).put("description", "revolutionary"); - writeSeedList(clone); - - new SeedRepository().run(CONFIG_ID, input, output); - final JsonNode actualAfterOverwrite = Jsons.deserialize(IOs.readFile(output, OBJECT.get(CONFIG_ID).asText() + ".json")); - assertEquals(clone, actualAfterOverwrite); - } - - @Test - void testFailsOnDuplicateId() { - final JsonNode object = Jsons.clone(OBJECT); - ((ObjectNode) object).put("name", "howard"); - - writeSeedList(OBJECT, object); - final SeedRepository seedRepository = new SeedRepository(); - assertThrows(IllegalArgumentException.class, () -> seedRepository.run(CONFIG_ID, input, output)); - } - - @Test - void testFailsOnDuplicateName() { - final JsonNode object = Jsons.clone(OBJECT); - ((ObjectNode) object).put(CONFIG_ID, UUID.randomUUID().toString()); - - writeSeedList(OBJECT, object); - final SeedRepository seedRepository = new SeedRepository(); - assertThrows(IllegalArgumentException.class, () -> seedRepository.run(CONFIG_ID, input, output)); - } - - @Test - void testPristineOutputDir() throws IOException { - IOs.writeFile(output, "blah.json", "{}"); - assertEquals(1, Files.list(output).count()); - - new SeedRepository().run(CONFIG_ID, input, output); - - // verify the file that the file that was already in the directory is gone. - assertEquals(1, Files.list(output).count()); - assertEquals(OBJECT.get(CONFIG_ID).asText() + ".json", Files.list(output).collect(Collectors.toList()).get(0).getFileName().toString()); - } - - private void writeSeedList(JsonNode... seeds) { - final JsonNode seedList = Jsons.jsonNode(new ArrayList<>()); - for (JsonNode seed : seeds) { - ((ArrayNode) seedList).add(seed); - } - IOs.writeFile(input, Yamls.serialize(seedList)); - } - -}