Skip to content

Commit

Permalink
Add test cases on ShadowTableHintDataSourceMappingsRetriever (#33557)
Browse files Browse the repository at this point in the history
* Refactor ShadowDMLStatementDataSourceMappingsRetriever

* Add test cases on ShadowTableHintDataSourceMappingsRetriever
  • Loading branch information
terrymanu authored Nov 5, 2024
1 parent 0f6e2ef commit 472dc42
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
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());
}
}

0 comments on commit 472dc42

Please sign in to comment.