Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases on ShadowTableHintDataSourceMappingsRetriever #33557

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ private ShadowColumnDataSourceMappingsRetriever createShadowDataSourceMappingsRe
if (queryContext.getSqlStatementContext() instanceof SelectStatementContext) {
return new ShadowSelectStatementDataSourceMappingsRetriever((SelectStatementContext) queryContext.getSqlStatementContext(), queryContext.getParameters(), tableAliasAndNameMappings);
}
throw new UnsupportedOperationException(String.format("unsupported SQL statement context `%s`", queryContext.getSqlStatementContext().getClass().getName()));
return null;
}

@Override
public Map<String, String> retrieve(final ShadowRule rule) {
Collection<String> shadowTables = rule.filterShadowTables(tableAliasAndNameMappings.values());
Map<String, String> result = tableHintDataSourceMappingsRetriever.retrieve(rule, shadowTables);
return result.isEmpty() ? shadowColumnDataSourceMappingsRetriever.retrieve(rule, shadowTables) : result;
return result.isEmpty() && null != shadowColumnDataSourceMappingsRetriever ? shadowColumnDataSourceMappingsRetriever.retrieve(rule, shadowTables) : result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import org.apache.shardingsphere.shadow.route.determiner.HintShadowAlgorithmDeterminer;
import org.apache.shardingsphere.shadow.route.retriever.dml.table.ShadowTableDataSourceMappingsRetriever;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.spi.hint.HintShadowAlgorithm;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;

/**
* Shadow table hint data source mappings retriever.
Expand All @@ -50,11 +48,8 @@ public Map<String, String> retrieve(final ShadowRule rule, final Collection<Stri

@SuppressWarnings("unchecked")
private boolean isMatchDefaultAlgorithm(final ShadowRule rule) {
Optional<ShadowAlgorithm> defaultAlgorithm = rule.getDefaultShadowAlgorithm();
if (defaultAlgorithm.isPresent() && defaultAlgorithm.get() instanceof HintShadowAlgorithm<?>) {
return HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>) defaultAlgorithm.get(), new ShadowCondition(), rule, isShadow);
}
return false;
return rule.getDefaultShadowAlgorithm()
.filter(optional -> HintShadowAlgorithmDeterminer.isShadow((HintShadowAlgorithm<Comparable<?>>) optional, new ShadowCondition(), rule, isShadow)).isPresent();
}

private Map<String, String> findShadowDataSourceMappingsBySQLHints(final ShadowRule rule, final Collection<String> shadowTables) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.shadow.route.retriever.dml.table.hint;

import org.apache.shardingsphere.shadow.algorithm.shadow.hint.SQLHintShadowAlgorithm;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
import org.apache.shardingsphere.shadow.spi.hint.HintShadowAlgorithm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class ShadowTableHintDataSourceMappingsRetrieverTest {

@Mock
private ShadowRule rule;

@Test
void assertRetrieveWithDefaultAlgorithmAndWithShadow() {
HintShadowAlgorithm<?> shadowAlgorithm = new SQLHintShadowAlgorithm();
when(rule.getDefaultShadowAlgorithm()).thenReturn(Optional.of(shadowAlgorithm));
when(rule.getAllShadowDataSourceMappings()).thenReturn(Collections.singletonMap("prod_ds", "shadow_ds"));
Map<String, String> actual = new ShadowTableHintDataSourceMappingsRetriever(ShadowOperationType.HINT_MATCH, true).retrieve(rule, Collections.emptyList());
assertThat(actual, is(Collections.singletonMap("prod_ds", "shadow_ds")));
}

@Test
void assertRetrieveWithDefaultAlgorithmAndWithNotShadow() {
HintShadowAlgorithm<?> shadowAlgorithm = new SQLHintShadowAlgorithm();
when(rule.getDefaultShadowAlgorithm()).thenReturn(Optional.of(shadowAlgorithm));
Map<String, String> actual = new ShadowTableHintDataSourceMappingsRetriever(ShadowOperationType.HINT_MATCH, false).retrieve(rule, Collections.emptyList());
assertTrue(actual.isEmpty());
}

@SuppressWarnings("unchecked")
@Test
void assertRetrieveWithSQLHintsAndWithShadow() {
HintShadowAlgorithm<?> shadowAlgorithm = new SQLHintShadowAlgorithm();
when(rule.getHintShadowAlgorithms("foo_tbl")).thenReturn(Collections.singleton((HintShadowAlgorithm<Comparable<?>>) shadowAlgorithm));
when(rule.getShadowDataSourceMappings("foo_tbl")).thenReturn(Collections.singletonMap("prod_ds", "shadow_ds"));
Map<String, String> actual = new ShadowTableHintDataSourceMappingsRetriever(ShadowOperationType.HINT_MATCH, true).retrieve(rule, Collections.singleton("foo_tbl"));
assertThat(actual, is(Collections.singletonMap("prod_ds", "shadow_ds")));
}

@SuppressWarnings("unchecked")
@Test
void assertRetrieveWithSQLHintsAndWithNotShadow() {
HintShadowAlgorithm<?> shadowAlgorithm = new SQLHintShadowAlgorithm();
when(rule.getHintShadowAlgorithms("foo_tbl")).thenReturn(Collections.singleton((HintShadowAlgorithm<Comparable<?>>) shadowAlgorithm));
Map<String, String> actual = new ShadowTableHintDataSourceMappingsRetriever(ShadowOperationType.HINT_MATCH, false).retrieve(rule, Collections.singleton("foo_tbl"));
assertTrue(actual.isEmpty());
}

@Test
void assertRetrieveWithEmptyTableAndWithoutDefaultAlgorithm() {
Map<String, String> actual = new ShadowTableHintDataSourceMappingsRetriever(ShadowOperationType.HINT_MATCH, true).retrieve(rule, Collections.emptyList());
assertTrue(actual.isEmpty());
}
}