/* * 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.shardingsphere.spring.boot.governance.type; import lombok.SneakyThrows; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.shardingsphere.driver.governance.internal.datasource.GovernanceShardingSphereDataSource; import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts; import org.apache.shardingsphere.spring.boot.governance.registry.TestGovernanceRepository; import org.apache.shardingsphere.spring.boot.governance.util.EmbedTestingServer; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import javax.sql.DataSource; import java.io.IOException; import java.lang.reflect.Field; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Collectors; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = GovernanceSpringBootRegistryReplicaQueryTest.class) @SpringBootApplication @ActiveProfiles("registry-replica-query") public class GovernanceSpringBootRegistryReplicaQueryTest { private static final String DATA_SOURCE_FILE = "yaml/replica-query-databases.yaml"; private static final String RULE_FILE = "yaml/replica-query-rule.yaml"; @Resource private DataSource dataSource; @BeforeClass public static void init() { EmbedTestingServer.start(); TestGovernanceRepository repository = new TestGovernanceRepository(); repository.persist("/metadata/logic_db/datasource", readYAML(DATA_SOURCE_FILE)); repository.persist("/metadata/logic_db/rule", readYAML(RULE_FILE)); repository.persist("/props", "{}\n"); repository.persist("/states/datanodes", ""); } @Test public void assertWithReplicaQueryDataSource() throws NoSuchFieldException, IllegalAccessException { assertTrue(dataSource instanceof GovernanceShardingSphereDataSource); Field field = GovernanceShardingSphereDataSource.class.getDeclaredField("metaDataContexts"); field.setAccessible(true); MetaDataContexts metaDataContexts = (MetaDataContexts) field.get(dataSource); for (DataSource each : metaDataContexts.getDefaultMetaData().getResource().getDataSources().values()) { assertThat(((BasicDataSource) each).getMaxTotal(), is(16)); assertThat(((BasicDataSource) each).getUsername(), is("sa")); } } @SneakyThrows({URISyntaxException.class, IOException.class}) private static String readYAML(final String yamlFile) { return Files.readAllLines(Paths.get(ClassLoader.getSystemResource(yamlFile).toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining()); } }