From 43e29f3651dec38a4228bc5c48bfb5a470504487 Mon Sep 17 00:00:00 2001 From: jessika Date: Sat, 21 Oct 2023 18:45:45 -0300 Subject: [PATCH 1/4] Add more unit tests for IndexReviseEngine (#28536) --- .../reviser/index/IndexReviseEngineTest.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java new file mode 100644 index 0000000000000..3981a6dafaad1 --- /dev/null +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java @@ -0,0 +1,89 @@ +/* + * 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.infra.metadata.database.schema.reviser.index; + +import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.metadata.database.schema.reviser.MetaDataReviseEntry; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Arrays; +import java.util.Collection; +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.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.when; + +public class IndexReviseEngineTest { + + @Mock + private T rule; + + @Mock + private MetaDataReviseEntry metaDataReviseEntry; + + @InjectMocks + private IndexReviseEngine indexReviseEngine; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void assertReviseIsPresentIsFalse() { + when(metaDataReviseEntry.getIndexReviser(any(), anyString())).thenReturn(Optional.empty()); + + Collection indexMetaDataCollection = Collections.singletonList(new IndexMetaData("index")); + Collection actual = indexReviseEngine.revise("tableName", indexMetaDataCollection); + + Assertions.assertNotNull(actual); + assertThat(actual.size(), is(1)); + } + + @Test + void assertReviseIsPresentIsTrue() { + IndexReviser 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 indexMetaDataCollection = Arrays.asList(new IndexMetaData("index1"), new IndexMetaData("index2")); + + Collection response = indexReviseEngine.revise("tableName", indexMetaDataCollection); + + assertThat(response.size(), equalTo(1)); + assertThat(response.contains(indexMetaData), equalTo(Boolean.TRUE)); + } + +} \ No newline at end of file From a83534531e1468365dcf37a5f81846866a281215 Mon Sep 17 00:00:00 2001 From: jessika Date: Sat, 21 Oct 2023 19:01:12 -0300 Subject: [PATCH 2/4] Add more validation (#28536) --- .../schema/reviser/index/IndexReviseEngineTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java index 3981a6dafaad1..66834b0db2cc1 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java @@ -34,8 +34,10 @@ import java.util.Optional; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.equalToObject; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +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.mock; @@ -67,6 +69,7 @@ void assertReviseIsPresentIsFalse() { Assertions.assertNotNull(actual); assertThat(actual.size(), is(1)); + assertThat(actual, equalToObject(indexMetaDataCollection)); } @Test @@ -80,10 +83,10 @@ void assertReviseIsPresentIsTrue() { Collection indexMetaDataCollection = Arrays.asList(new IndexMetaData("index1"), new IndexMetaData("index2")); - Collection response = indexReviseEngine.revise("tableName", indexMetaDataCollection); + Collection actual = indexReviseEngine.revise("tableName", indexMetaDataCollection); - assertThat(response.size(), equalTo(1)); - assertThat(response.contains(indexMetaData), equalTo(Boolean.TRUE)); + assertThat(actual.size(), equalTo(1)); + assertTrue(actual.contains(indexMetaData)); } } \ No newline at end of file From 081c239417c827adf5c92e212c0968b6fe076628 Mon Sep 17 00:00:00 2001 From: jessika Date: Sun, 22 Oct 2023 23:22:17 -0300 Subject: [PATCH 3/4] fix codestyle: add space in the end (#28536) --- .../database/schema/reviser/index/IndexReviseEngineTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java index 66834b0db2cc1..a543b1e50f686 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java @@ -89,4 +89,4 @@ void assertReviseIsPresentIsTrue() { assertTrue(actual.contains(indexMetaData)); } -} \ No newline at end of file +} From 7855250d174949a701a57a7fe210c28e3fb8eda4 Mon Sep 17 00:00:00 2001 From: jessika Date: Thu, 14 Mar 2024 21:47:31 -0300 Subject: [PATCH 4/4] fix codestyle: remove the spaces inside the method (#28536) --- .../schema/reviser/index/IndexReviseEngineTest.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java index a543b1e50f686..85a486ec79d6a 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/index/IndexReviseEngineTest.java @@ -44,7 +44,7 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; -public class IndexReviseEngineTest { +class IndexReviseEngineTest { @Mock private T rule; @@ -63,10 +63,8 @@ void setUp() { @Test void assertReviseIsPresentIsFalse() { when(metaDataReviseEntry.getIndexReviser(any(), anyString())).thenReturn(Optional.empty()); - Collection indexMetaDataCollection = Collections.singletonList(new IndexMetaData("index")); Collection actual = indexReviseEngine.revise("tableName", indexMetaDataCollection); - Assertions.assertNotNull(actual); assertThat(actual.size(), is(1)); assertThat(actual, equalToObject(indexMetaDataCollection)); @@ -75,16 +73,11 @@ void assertReviseIsPresentIsFalse() { @Test void assertReviseIsPresentIsTrue() { IndexReviser 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 indexMetaDataCollection = Arrays.asList(new IndexMetaData("index1"), new IndexMetaData("index2")); - Collection actual = indexReviseEngine.revise("tableName", indexMetaDataCollection); - assertThat(actual.size(), equalTo(1)); assertTrue(actual.contains(indexMetaData)); }