|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * 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, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.teleport.v2.spanner.migrations.utils; |
| 17 | + |
| 18 | +import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.CONTACT_POINTS; |
| 19 | +import static com.datastax.oss.driver.api.core.config.DefaultDriverOption.RETRY_POLICY_CLASS; |
| 20 | +import static com.google.common.truth.Truth.assertThat; |
| 21 | +import static org.junit.Assert.assertThrows; |
| 22 | +import static org.mockito.Mockito.mockStatic; |
| 23 | + |
| 24 | +import com.datastax.oss.driver.api.core.config.DriverConfigLoader; |
| 25 | +import com.datastax.oss.driver.api.core.config.OptionsMap; |
| 26 | +import com.google.common.collect.ImmutableMap; |
| 27 | +import com.google.common.io.Resources; |
| 28 | +import com.typesafe.config.ConfigException; |
| 29 | +import java.io.FileNotFoundException; |
| 30 | +import java.net.MalformedURLException; |
| 31 | +import java.net.URL; |
| 32 | +import java.util.AbstractMap.SimpleEntry; |
| 33 | +import java.util.List; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Before; |
| 36 | +import org.junit.Test; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.mockito.MockedStatic; |
| 39 | +import org.mockito.junit.MockitoJUnitRunner; |
| 40 | + |
| 41 | +/** Test class for {@link CassandraDriverConfigLoader}. */ |
| 42 | +@RunWith(MockitoJUnitRunner.class) |
| 43 | +public class CassandraDriverConfigLoaderTest { |
| 44 | + MockedStatic mockFileReader; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void initialize() { |
| 48 | + mockFileReader = mockStatic(JarFileReader.class); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testCassandraDriverConfigLoaderBasic() |
| 53 | + throws FileNotFoundException, MalformedURLException { |
| 54 | + String testGcsPath = "gs://smt-test-bucket/cassandraConfig.conf"; |
| 55 | + URL testUrl = Resources.getResource("test-cassandra-config.conf"); |
| 56 | + mockFileReader |
| 57 | + .when(() -> JarFileReader.saveFilesLocally(testGcsPath)) |
| 58 | + .thenReturn(new URL[] {testUrl}); |
| 59 | + DriverConfigLoader driverConfigLoader = CassandraDriverConfigLoader.loadFile(testGcsPath); |
| 60 | + assertThat( |
| 61 | + driverConfigLoader |
| 62 | + .getInitialConfig() |
| 63 | + .getProfiles() |
| 64 | + .get("default") |
| 65 | + .getStringList(CONTACT_POINTS)) |
| 66 | + .isEqualTo(List.of("127.0.0.1:9042", "127.0.0.2:9042")); |
| 67 | + ; |
| 68 | + assertThat( |
| 69 | + driverConfigLoader |
| 70 | + .getInitialConfig() |
| 71 | + .getProfiles() |
| 72 | + .get("default") |
| 73 | + .getString(RETRY_POLICY_CLASS)) |
| 74 | + .isEqualTo("DefaultRetryPolicy"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testCassandraDriverConfigLoadError() |
| 79 | + throws FileNotFoundException, MalformedURLException { |
| 80 | + String testGcsPathNotFound = "gs://smt-test-bucket/cassandraConfigNotFound.conf"; |
| 81 | + String testGcsPathList = |
| 82 | + "gs://smt-test-bucket/cassandraConfig1.conf,gs://smt-test-bucket/cassandraConfig2.conf"; |
| 83 | + |
| 84 | + URL testUrl = Resources.getResource("test-cassandra-config-parse-err.conf"); |
| 85 | + mockFileReader |
| 86 | + .when(() -> JarFileReader.saveFilesLocally(testGcsPathNotFound)) |
| 87 | + .thenReturn(new URL[] {}); |
| 88 | + mockFileReader |
| 89 | + .when(() -> JarFileReader.saveFilesLocally(testGcsPathList)) |
| 90 | + .thenReturn( |
| 91 | + new URL[] { |
| 92 | + Resources.getResource("test-cassandra-config.conf"), |
| 93 | + Resources.getResource("test-cassandra-config.conf") |
| 94 | + }); |
| 95 | + assertThrows( |
| 96 | + FileNotFoundException.class, |
| 97 | + () -> CassandraDriverConfigLoader.loadFile(testGcsPathNotFound)); |
| 98 | + assertThrows( |
| 99 | + IllegalArgumentException.class, |
| 100 | + () -> CassandraDriverConfigLoader.loadFile(testGcsPathList)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testCassandraDriverConfigParseError() |
| 105 | + throws FileNotFoundException, MalformedURLException { |
| 106 | + String testGcsPath = "gs://smt-test-bucket/cassandraConfig.conf"; |
| 107 | + URL testUrl = Resources.getResource("test-cassandra-config-parse-err.conf"); |
| 108 | + mockFileReader |
| 109 | + .when(() -> JarFileReader.saveFilesLocally(testGcsPath)) |
| 110 | + .thenReturn(new URL[] {testUrl}); |
| 111 | + assertThrows( |
| 112 | + ConfigException.Parse.class, () -> CassandraDriverConfigLoader.loadFile(testGcsPath)); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testOptionsMapConversion() throws FileNotFoundException { |
| 117 | + |
| 118 | + String testGcsPath = "gs://smt-test-bucket/cassandraConfig.conf"; |
| 119 | + URL testUrl = Resources.getResource("test-cassandra-config.conf"); |
| 120 | + mockFileReader |
| 121 | + .when(() -> JarFileReader.saveFilesLocally(testGcsPath)) |
| 122 | + .thenReturn(new URL[] {testUrl}); |
| 123 | + DriverConfigLoader driverConfigLoaderDirect = CassandraDriverConfigLoader.loadFile(testGcsPath); |
| 124 | + OptionsMap optionsMap = CassandraDriverConfigLoader.getOptionsMapFromFile(testGcsPath); |
| 125 | + DriverConfigLoader driverConfigLoaderFromOptionsMap = |
| 126 | + CassandraDriverConfigLoader.fromOptionsMap(optionsMap); |
| 127 | + ImmutableMap<String, ImmutableMap<String, String>> directLoadMap = |
| 128 | + driverConfigMap(driverConfigLoaderDirect); |
| 129 | + ImmutableMap<String, ImmutableMap<String, String>> fromOptionsMap = |
| 130 | + driverConfigMap(driverConfigLoaderFromOptionsMap); |
| 131 | + |
| 132 | + assertThat(directLoadMap).isEqualTo(fromOptionsMap); |
| 133 | + |
| 134 | + assertThrows( |
| 135 | + IllegalArgumentException.class, |
| 136 | + () -> { |
| 137 | + OptionsMap optionsMapToLoad = new OptionsMap(); |
| 138 | + CassandraDriverConfigLoader.putInOptionsMap( |
| 139 | + optionsMapToLoad, "default", new SimpleEntry<>("Unsupported", "Unsupported")); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + private static ImmutableMap<String, ImmutableMap<String, String>> driverConfigMap( |
| 144 | + DriverConfigLoader driverConfigLoaderDirect) { |
| 145 | + ImmutableMap.Builder<String, ImmutableMap<String, String>> driverConfigMap = |
| 146 | + ImmutableMap.builder(); |
| 147 | + driverConfigLoaderDirect |
| 148 | + .getInitialConfig() |
| 149 | + .getProfiles() |
| 150 | + .forEach( |
| 151 | + (profile, options) -> { |
| 152 | + ImmutableMap.Builder<String, String> profileMapBuilder = ImmutableMap.builder(); |
| 153 | + options |
| 154 | + .entrySet() |
| 155 | + .forEach( |
| 156 | + e -> profileMapBuilder.put(e.getKey().toString(), e.getValue().toString())); |
| 157 | + driverConfigMap.put(profile, profileMapBuilder.build()); |
| 158 | + }); |
| 159 | + return driverConfigMap.build(); |
| 160 | + } |
| 161 | + |
| 162 | + @After |
| 163 | + public void cleanup() { |
| 164 | + mockFileReader.close(); |
| 165 | + mockFileReader = null; |
| 166 | + } |
| 167 | +} |
0 commit comments