|
| 1 | +package org.embulk.output.bigquery_java; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | + |
| 7 | +import com.google.cloud.bigquery.FieldList; |
| 8 | +import com.google.cloud.bigquery.PolicyTags; |
| 9 | +import com.google.cloud.bigquery.StandardSQLTypeName; |
| 10 | +import java.io.IOException; |
| 11 | +import java.lang.reflect.Field; |
| 12 | +import java.lang.reflect.InvocationTargetException; |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.function.Function; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import org.embulk.config.ConfigSource; |
| 20 | +import org.embulk.input.file.LocalFileInputPlugin; |
| 21 | +import org.embulk.output.bigquery_java.config.PluginTask; |
| 22 | +import org.embulk.parser.csv.CsvParserPlugin; |
| 23 | +import org.embulk.spi.FileInputPlugin; |
| 24 | +import org.embulk.spi.OutputPlugin; |
| 25 | +import org.embulk.spi.ParserPlugin; |
| 26 | +import org.embulk.spi.Schema; |
| 27 | +import org.embulk.spi.type.Types; |
| 28 | +import org.embulk.test.EmbulkTests; |
| 29 | +import org.embulk.test.TestingEmbulk; |
| 30 | +import org.embulk.util.config.ConfigMapper; |
| 31 | +import org.embulk.util.config.ConfigMapperFactory; |
| 32 | +import org.junit.Rule; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.rules.TemporaryFolder; |
| 35 | + |
| 36 | +public class TestBigqueryClient { |
| 37 | + protected static final ConfigMapperFactory CONFIG_MAPPER_FACTORY = |
| 38 | + ConfigMapperFactory.builder().addDefaultModules().build(); |
| 39 | + |
| 40 | + protected static final ConfigMapper CONFIG_MAPPER = CONFIG_MAPPER_FACTORY.createConfigMapper(); |
| 41 | + |
| 42 | + private static final String BASIC_RESOURCE_PATH = |
| 43 | + "/java/org/embulk/output/bigquery_java/bigquery_client/"; |
| 44 | + |
| 45 | + private static ConfigSource loadYamlResource(TestingEmbulk embulk, String fileName) { |
| 46 | + return embulk.loadYamlResource(BASIC_RESOURCE_PATH + fileName); |
| 47 | + } |
| 48 | + |
| 49 | + @Rule |
| 50 | + public TestingEmbulk embulk = |
| 51 | + TestingEmbulk.builder() |
| 52 | + .registerPlugin(OutputPlugin.class, "bigquery_java", BigqueryJavaOutputPlugin.class) |
| 53 | + .registerPlugin(FileInputPlugin.class, "file", LocalFileInputPlugin.class) |
| 54 | + .registerPlugin(ParserPlugin.class, "csv", CsvParserPlugin.class) |
| 55 | + .build(); |
| 56 | + |
| 57 | + @Rule public TemporaryFolder testFolder = new TemporaryFolder(); |
| 58 | + |
| 59 | + private com.google.cloud.bigquery.Schema invokeTakeoverBuildSchema( |
| 60 | + Function<ConfigSource, ConfigSource> setupConfig, |
| 61 | + Function<com.google.cloud.bigquery.Field.Builder, com.google.cloud.bigquery.Field> |
| 62 | + setupField0, |
| 63 | + Function<com.google.cloud.bigquery.Field.Builder, com.google.cloud.bigquery.Field> |
| 64 | + setupField1) |
| 65 | + throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, |
| 66 | + InvocationTargetException { |
| 67 | + ConfigSource testConfig = EmbulkTests.config("EMBULK_OUTPUT_BIGQUERY_TEST_CONFIG"); |
| 68 | + TestBigqueryJavaOutputPlugin.TestTask testTask = |
| 69 | + CONFIG_MAPPER.map(testConfig, TestBigqueryJavaOutputPlugin.TestTask.class); |
| 70 | + ConfigSource config = loadYamlResource(embulk, "takeover.yml"); |
| 71 | + config.set("json_keyfile", testTask.getJsonKeyfile()); |
| 72 | + config.set("dataset", testTask.getDataset()); |
| 73 | + config.set("table", testTask.getTable()); |
| 74 | + PluginTask task = CONFIG_MAPPER.map(setupConfig.apply(config), PluginTask.class); |
| 75 | + Schema schema = Schema.builder().add("c0", Types.LONG).add("c1", Types.STRING).build(); |
| 76 | + BigqueryClient bigqueryClient = new BigqueryClient(task, schema); |
| 77 | + Field field = BigqueryClient.class.getDeclaredField("cachedSrcFields"); |
| 78 | + field.setAccessible(true); |
| 79 | + List<com.google.cloud.bigquery.Field> fieldList = new ArrayList<>(); |
| 80 | + fieldList.add( |
| 81 | + setupField0.apply( |
| 82 | + com.google.cloud.bigquery.Field.newBuilder("c0", StandardSQLTypeName.INT64))); |
| 83 | + fieldList.add( |
| 84 | + setupField1.apply( |
| 85 | + com.google.cloud.bigquery.Field.newBuilder("c1", StandardSQLTypeName.STRING))); |
| 86 | + field.set(bigqueryClient, FieldList.of(fieldList)); |
| 87 | + Method method = BigqueryClient.class.getDeclaredMethod("buildSchema", Schema.class, List.class); |
| 88 | + method.setAccessible(true); |
| 89 | + return (com.google.cloud.bigquery.Schema) |
| 90 | + method.invoke(bigqueryClient, schema, task.getColumnOptions().orElse(null)); |
| 91 | + } |
| 92 | + |
| 93 | + private com.google.cloud.bigquery.Schema invokeRetainDescriptionBuildSchema( |
| 94 | + String mode, Boolean retainColumnDescriptions, String d0, String d1) |
| 95 | + throws IOException, NoSuchFieldException, InvocationTargetException, IllegalAccessException, |
| 96 | + NoSuchMethodException { |
| 97 | + return invokeTakeoverBuildSchema( |
| 98 | + configSource -> |
| 99 | + configSource |
| 100 | + .set("mode", mode) |
| 101 | + .set("retain_column_descriptions", retainColumnDescriptions), |
| 102 | + builder -> builder.setDescription(d0).build(), |
| 103 | + builder -> builder.setDescription(d1).build()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testRetainDescriptionTrue() |
| 108 | + throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, |
| 109 | + InvocationTargetException, IOException { |
| 110 | + com.google.cloud.bigquery.Schema schema = |
| 111 | + invokeRetainDescriptionBuildSchema("replace", true, "prev_c0", "prev_c1"); |
| 112 | + assertEquals("c0", schema.getFields().get(0).getDescription()); |
| 113 | + assertEquals("prev_c1", schema.getFields().get(1).getDescription()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testRetainDescriptionFalse() |
| 118 | + throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, |
| 119 | + InvocationTargetException, IOException { |
| 120 | + com.google.cloud.bigquery.Schema schema = |
| 121 | + invokeRetainDescriptionBuildSchema("replace", false, "prev_c0", "prev_c1"); |
| 122 | + assertEquals("c0", schema.getFields().get(0).getDescription()); |
| 123 | + assertNull(schema.getFields().get(1).getDescription()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testRetainDescriptionTrueButNotModeReplace() |
| 128 | + throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, |
| 129 | + InvocationTargetException, IOException { |
| 130 | + com.google.cloud.bigquery.Schema schema = |
| 131 | + invokeRetainDescriptionBuildSchema("insert", true, "prev_c0", "prev_c1"); |
| 132 | + assertEquals("c0", schema.getFields().get(0).getDescription()); |
| 133 | + assertNull(schema.getFields().get(1).getDescription()); |
| 134 | + } |
| 135 | + |
| 136 | + private com.google.cloud.bigquery.Schema invokeRetainPolicyTagsBuildSchema( |
| 137 | + String mode, Boolean retainPolicyTags, String[] tags0, String[] tags1) |
| 138 | + throws IOException, NoSuchFieldException, InvocationTargetException, IllegalAccessException, |
| 139 | + NoSuchMethodException { |
| 140 | + List<String> n0 = Arrays.stream(tags0).collect(Collectors.toList()); |
| 141 | + List<String> n1 = Arrays.stream(tags1).collect(Collectors.toList()); |
| 142 | + PolicyTags p0 = PolicyTags.newBuilder().setNames(n0).build(); |
| 143 | + PolicyTags p1 = PolicyTags.newBuilder().setNames(n1).build(); |
| 144 | + return invokeTakeoverBuildSchema( |
| 145 | + configSource -> |
| 146 | + configSource.set("mode", mode).set("retain_column_policy_tags", retainPolicyTags), |
| 147 | + builder -> builder.setPolicyTags(p0).build(), |
| 148 | + builder -> builder.setPolicyTags(p1).build()); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testRetainColumnPolicyTagsTrue() |
| 153 | + throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, |
| 154 | + InvocationTargetException, IOException { |
| 155 | + com.google.cloud.bigquery.Schema schema = |
| 156 | + invokeRetainPolicyTagsBuildSchema( |
| 157 | + "replace", true, new String[] {"c0"}, new String[] {"c10", "c11"}); |
| 158 | + assertArrayEquals( |
| 159 | + new String[] {"c0"}, |
| 160 | + schema.getFields().get(0).getPolicyTags().getNames().toArray(new String[0])); |
| 161 | + assertArrayEquals( |
| 162 | + new String[] {"c10", "c11"}, |
| 163 | + schema.getFields().get(1).getPolicyTags().getNames().toArray(new String[0])); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void testRetainColumnPolicyTagsFalse() |
| 168 | + throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, |
| 169 | + InvocationTargetException, IOException { |
| 170 | + com.google.cloud.bigquery.Schema schema = |
| 171 | + invokeRetainPolicyTagsBuildSchema( |
| 172 | + "replace", false, new String[] {"c0"}, new String[] {"c10", "c11"}); |
| 173 | + assertNull(schema.getFields().get(0).getPolicyTags()); |
| 174 | + assertNull(schema.getFields().get(1).getPolicyTags()); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testRetainColumnPolicyTagsTrueButNotModeReplace() |
| 179 | + throws NoSuchMethodException, NoSuchFieldException, IllegalAccessException, |
| 180 | + InvocationTargetException, IOException { |
| 181 | + com.google.cloud.bigquery.Schema schema = |
| 182 | + invokeRetainPolicyTagsBuildSchema( |
| 183 | + "insert", true, new String[] {"c0"}, new String[] {"c10", "c11"}); |
| 184 | + assertNull(schema.getFields().get(0).getPolicyTags()); |
| 185 | + assertNull(schema.getFields().get(1).getPolicyTags()); |
| 186 | + } |
| 187 | +} |
0 commit comments