001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import static org.junit.Assert.assertEquals;
021import static org.mockito.ArgumentMatchers.any;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.mockStatic;
024import static org.mockito.Mockito.when;
025
026import java.net.URI;
027import java.util.concurrent.CompletableFuture;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseConfiguration;
031import org.apache.hadoop.hbase.testclassification.ClientTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.junit.After;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.mockito.ArgumentCaptor;
039import org.mockito.MockedStatic;
040
041@Category({ ClientTests.class, SmallTests.class })
042public class TestConnectionFactoryApplyURIQueries {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestConnectionFactoryApplyURIQueries.class);
047
048  private Configuration conf;
049
050  private MockedStatic<ConnectionRegistryFactory> mockedConnectionRegistryFactory;
051
052  private ConnectionRegistry registry;
053
054  @Before
055  public void setUp() {
056    conf = HBaseConfiguration.create();
057    mockedConnectionRegistryFactory = mockStatic(ConnectionRegistryFactory.class);
058    registry = mock(ConnectionRegistry.class);
059    mockedConnectionRegistryFactory
060      .when(() -> ConnectionRegistryFactory.create(any(), any(), any())).thenReturn(registry);
061    when(registry.getClusterId()).thenReturn(CompletableFuture.completedFuture("cluster"));
062  }
063
064  @After
065  public void tearDown() {
066    mockedConnectionRegistryFactory.closeOnDemand();
067  }
068
069  @Test
070  public void testApplyURIQueries() throws Exception {
071    ConnectionFactory.createConnection(new URI("hbase+rpc://server:16010?a=1&b=2&c"), conf);
072    ArgumentCaptor<Configuration> captor = ArgumentCaptor.forClass(Configuration.class);
073    mockedConnectionRegistryFactory
074      .verify(() -> ConnectionRegistryFactory.create(any(), captor.capture(), any()));
075    Configuration c = captor.getValue();
076    assertEquals("1", c.get("a"));
077    assertEquals("2", c.get("b"));
078    assertEquals("", c.get("c"));
079  }
080}