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 EncryptIndexReviser #33663

Merged
merged 2 commits into from
Nov 14, 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
@@ -0,0 +1,59 @@
/*
* 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.encrypt.metadata.reviser.index;

import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.table.EncryptTable;
import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Optional;

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

class EncryptIndexReviserTest {

@Test
void assertReviseWithEmptyColumn() {
assertFalse(new EncryptIndexReviser(mock(EncryptTable.class)).revise("foo_tbl", new IndexMetaData("name"), mock(EncryptRule.class)).isPresent());
}

@Test
void assertReviseWithColumns() {
Optional<IndexMetaData> indexMetaData = new EncryptIndexReviser(mockEncryptTable())
.revise("foo_tbl", new IndexMetaData("name", Arrays.asList("cipher_col", "assisted_col", "other_col")), mock(EncryptRule.class));
assertTrue(indexMetaData.isPresent());
assertThat(indexMetaData.get().getColumns(), is(new LinkedHashSet<>(Arrays.asList("col_1", "col_2", "other_col"))));
}

private EncryptTable mockEncryptTable() {
EncryptTable result = mock(EncryptTable.class);
when(result.isCipherColumn("cipher_col")).thenReturn(true);
when(result.getLogicColumnByCipherColumn("cipher_col")).thenReturn("col_1");
when(result.isAssistedQueryColumn("assisted_col")).thenReturn(true);
when(result.getLogicColumnByAssistedQueryColumn("assisted_col")).thenReturn("col_2");
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.reviser.MetaDataReviseEntry;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -31,45 +30,36 @@
import java.util.Collections;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@SuppressWarnings({"rawtypes", "unchecked"})
@ExtendWith(MockitoExtension.class)
class IndexReviseEngineTest<T extends ShardingSphereRule> {
class IndexReviseEngineTest {

@Mock
private MetaDataReviseEntry<T> metaDataReviseEntry;
private MetaDataReviseEntry metaDataReviseEntry;

@InjectMocks
private IndexReviseEngine<T> indexReviseEngine;
private IndexReviseEngine indexReviseEngine;

@Test
void assertReviseIsPresentIsFalse() {
when(metaDataReviseEntry.getIndexReviser(any(), anyString())).thenReturn(Optional.empty());
Collection<IndexMetaData> indexMetaDataList = Collections.singletonList(new IndexMetaData("index"));
Collection<IndexMetaData> actual = indexReviseEngine.revise("tableName", indexMetaDataList);
assertNotNull(actual);
assertThat(actual.size(), is(1));
assertThat(actual, is(indexMetaDataList));
void assertReviseWithoutIndexReviser() {
when(metaDataReviseEntry.getIndexReviser(any(), eq("foo_tbl"))).thenReturn(Optional.empty());
Collection<IndexMetaData> actual = indexReviseEngine.revise("foo_tbl", Collections.singleton(new IndexMetaData("foo_idx")));
assertThat(actual, is(Collections.singleton(new IndexMetaData("foo_idx"))));
}

@Test
void assertReviseIsPresentIsTrue() {
IndexReviser<T> reviser = mock(IndexReviser.class);
IndexMetaData indexMetaData = new IndexMetaData("index");
doReturn(Optional.of(reviser)).when(metaDataReviseEntry).getIndexReviser(any(), anyString());
when(reviser.revise(anyString(), any(), any())).thenReturn(Optional.of(indexMetaData));
Collection<IndexMetaData> indexMetaDataList = Arrays.asList(new IndexMetaData("index1"), new IndexMetaData("index2"));
Collection<IndexMetaData> actual = indexReviseEngine.revise("tableName", indexMetaDataList);
assertThat(actual.size(), equalTo(1));
assertTrue(actual.contains(indexMetaData));
void assertReviseWithIndexReviser() {
IndexReviser reviser = mock(IndexReviser.class);
when(reviser.revise(eq("foo_tbl"), any(), any())).thenReturn(Optional.of(new IndexMetaData("foo_idx")));
when(metaDataReviseEntry.getIndexReviser(any(), eq("foo_tbl"))).thenReturn(Optional.of(reviser));
Collection<IndexMetaData> actual = indexReviseEngine.revise("foo_tbl", Arrays.asList(new IndexMetaData("idx_0"), new IndexMetaData("idx_1")));
assertThat(actual, is(Collections.singleton(new IndexMetaData("foo_idx"))));
}
}