cgivre commented on a change in pull request #2270: URL: https://github.com/apache/drill/pull/2270#discussion_r693204668
########## File path: contrib/storage-http/src/test/java/org/apache/drill/exec/store/http/TestURLParameters.java ########## @@ -0,0 +1,121 @@ +/* + * 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.drill.exec.store.http; + +import okhttp3.HttpUrl; +import org.apache.drill.common.map.CaseInsensitiveMap; +import org.apache.drill.exec.store.http.util.SimpleHttp; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestURLParameters { + + @Test + public void testUrlParameters() { + // Http client setup + HttpUrl githubSingleParam = HttpUrl.parse("https://github.com/orgs/{org}/repos"); + CaseInsensitiveMap<String> filters = CaseInsensitiveMap.newHashMap(); + filters.put("org", "apache"); + filters.put("param1", "value1"); + filters.put("param2", "value2"); + assertEquals("https://github.com/orgs/apache/repos", SimpleHttp.mapURLParameters(githubSingleParam, filters)); + + + HttpUrl githubMultiParam = HttpUrl.parse("https://github.com/orgs/{org}/{repos}"); + CaseInsensitiveMap<String> filters2 = CaseInsensitiveMap.newHashMap(); + filters2.put("org", "apache"); + filters2.put("param1", "value1"); + filters2.put("repos", "drill"); + assertEquals("https://github.com/orgs/apache/drill", SimpleHttp.mapURLParameters(githubMultiParam, filters2)); + + HttpUrl githubNoParam = HttpUrl.parse("https://github.com/orgs/org/repos"); + CaseInsensitiveMap<String> filters3 = CaseInsensitiveMap.newHashMap(); + + filters3.put("org", "apache"); + filters3.put("param1", "value1"); + filters3.put("repos", "drill"); + assertEquals("https://github.com/orgs/org/repos", SimpleHttp.mapURLParameters(githubNoParam, filters3)); + } + + @Test + public void testParamAtEnd() { + HttpUrl pokemonUrl = HttpUrl.parse("https://pokeapi.co/api/v2/pokemon/{pokemon_name}"); + CaseInsensitiveMap<String> filters = CaseInsensitiveMap.newHashMap(); + filters.put("pokemon_name", "Misty"); + filters.put("param1", "value1"); + filters.put("repos", "drill"); + assertEquals("https://pokeapi.co/api/v2/pokemon/Misty", SimpleHttp.mapURLParameters(pokemonUrl, filters)); + } + + @Test + public void testUpperCase() { + HttpUrl githubSingleParam = HttpUrl.parse("https://github.com/orgs/{ORG}/repos"); + CaseInsensitiveMap<String> filters = CaseInsensitiveMap.newHashMap(); + filters.put("org", "apache"); + filters.put("param1", "value1"); + filters.put("param2", "value2"); + assertEquals("https://github.com/orgs/apache/repos", SimpleHttp.mapURLParameters(githubSingleParam, filters)); + } + + @Test + public void testURLDefaultParameters() { + HttpUrl githubSingleParam = HttpUrl.parse("https://github.com/orgs/{org=apache}/repos"); + CaseInsensitiveMap<String> filters = CaseInsensitiveMap.newHashMap(); + filters.put("param1", "value1"); + filters.put("param2", "value2"); + assertEquals("https://github.com/orgs/apache/repos", SimpleHttp.mapURLParameters(githubSingleParam, filters)); + } + + @Test + public void testMixedCase() { + // Since SQL is case-insensitive, + HttpUrl githubSingleParam = HttpUrl.parse("https://github.com/orgs/{ORG}/{org}/repos"); + CaseInsensitiveMap<String> filters = CaseInsensitiveMap.newHashMap(); + filters.put("org", "apache"); + filters.put("ORG", "linux"); + filters.put("param1", "value1"); + filters.put("param2", "value2"); + assertEquals("https://github.com/orgs/linux/linux/repos", SimpleHttp.mapURLParameters(githubSingleParam, filters)); + } + + @Test + public void testDuplicateParameters() { + HttpUrl pokemonUrl = HttpUrl.parse("https://pokeapi.co/api/{pokemon_name}/pokemon/{pokemon_name}"); + CaseInsensitiveMap<String> filters = CaseInsensitiveMap.newHashMap(); + filters.put("pokemon_name", "Misty"); + filters.put("param1", "value1"); + filters.put("repos", "drill"); + assertEquals("https://pokeapi.co/api/Misty/pokemon/Misty", SimpleHttp.mapURLParameters(pokemonUrl, filters)); + } + + @Test + public void testDefaultParametersWithDifferentDatatypes() { + HttpUrl pokemonUrl = HttpUrl.parse("https://pokeapi.co/api/{boolean=true}/{int=1234}"); + CaseInsensitiveMap<String> filters = CaseInsensitiveMap.newHashMap(); + assertEquals("https://pokeapi.co/api/true/1234", SimpleHttp.mapURLParameters(pokemonUrl,filters)); + } + + @Test + public void testDefaultParameterExtractor() { + HttpUrl pokemonUrl = HttpUrl.parse("https://pokeapi.co/api/{pokemon_name=Misty}"); Review comment: @paul-rogers I added a unit test for this. Upon reflecting on it a bit, it would seem to me that the "correct" behavior would be to throw an exception in this case. Having a blank default rather defeats the purpose of having a default at all, so my thinking here, was either specify a default value or don't. Does that make sense? In any event, I think I've addressed all your comments. Thanks for the review! -- 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]
