jihoonson commented on a change in pull request #11377: URL: https://github.com/apache/druid/pull/11377#discussion_r667515646
########## File path: core/src/test/java/org/apache/druid/metadata/EnvironmentVariableDynamicConfigProviderTest.java ########## @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.metadata; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.collect.ImmutableMap; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.Map; + +public class EnvironmentVariableDynamicConfigProviderTest +{ + private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); + + @Before + public void setupTest() throws Exception + { + setEnv(ImmutableMap.of("DRUID_USER", "druid", "DRUID_PASSWORD", "123")); + } + + @Test + public void testSerde() throws IOException + { + String providerString = "{\"type\": \"environment\", \"variables\" : {\"testKey\":\"testValue\"}}"; + DynamicConfigProvider provider = JSON_MAPPER.readValue(providerString, DynamicConfigProvider.class); + Assert.assertTrue(provider instanceof EnvironmentVariableDynamicConfigProvider); + Assert.assertEquals("testValue", ((EnvironmentVariableDynamicConfigProvider) provider).getVariables().get("testKey")); + DynamicConfigProvider serde = JSON_MAPPER.readValue(JSON_MAPPER.writeValueAsString(provider), DynamicConfigProvider.class); + Assert.assertEquals(provider, serde); + } + + @Test + public void testGetConfig() throws Exception + { + String providerString = "{\"type\": \"environment\", \"variables\" : {\"user\":\"DRUID_USER\",\"password\":\"DRUID_PASSWORD\"}}"; + DynamicConfigProvider provider = JSON_MAPPER.readValue(providerString, DynamicConfigProvider.class); + Assert.assertTrue(provider instanceof EnvironmentVariableDynamicConfigProvider); + Assert.assertEquals("druid", ((EnvironmentVariableDynamicConfigProvider) provider).getConfig().get("user")); + Assert.assertEquals("123", ((EnvironmentVariableDynamicConfigProvider) provider).getConfig().get("password")); + } + + protected static void setEnv(Map<String, String> newenv) throws Exception Review comment: This method seems to have a couple of issues. 1) This method seems to be copied from the code snippet in https://stackoverflow.com/a/7201825/4127682. If this is true, we cannot use it directly because it violates the [ASF license policy](https://www.apache.org/legal/resolved.html#stackoverflow). You can get some hint from the stack overflow, but should not copy from it. 2) As noted in the stack overflow link, the environment variables should be reset because they are shared by all tests run by the same JVM process. 3) It would be nice to add some comments to help others understand the code. I left some comments for this. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
