From 8848cfa063628daaccb1e43c9f00b476eda40267 Mon Sep 17 00:00:00 2001 From: Karthick Balathandayuthapani <78250271+captainbkarthick@users.noreply.github.com> Date: Thu, 20 Oct 2022 15:20:50 +0530 Subject: [PATCH] [ISSUE #21569] Add Tests for ShardingAutoTableAlgorithmUtil Class (#21603) * [ISSUE #21569] Add Tests for ShardingAutoTableAlgorithmUtil Class * [ISSUE #21569] Fix checkstyle errors * [ISSUE #21569] Change the ShardingAutoTableAlgorithmUtilTest class as final * [ISSUE #21569] remove empty spaces and final modifier for method variables * [ISSUE #21569] Change assertEquals to assertThat in tests Co-authored-by: Karthick B --- .../ShardingAutoTableAlgorithmUtilTest.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/ShardingAutoTableAlgorithmUtilTest.java diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/ShardingAutoTableAlgorithmUtilTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/ShardingAutoTableAlgorithmUtilTest.java new file mode 100644 index 0000000000000..272be69b13bf8 --- /dev/null +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/ShardingAutoTableAlgorithmUtilTest.java @@ -0,0 +1,68 @@ +/* + * 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.sharding.algorithm.sharding; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Optional; +import org.apache.shardingsphere.infra.datanode.DataNodeInfo; +import org.junit.Before; +import org.junit.Test; + +public final class ShardingAutoTableAlgorithmUtilTest { + + private Collection collection; + + private DataNodeInfo dataNodeInfo; + + @Before + public void setup() { + collection = new ArrayList<>(); + collection.add("PREFIX----SUFFIX"); + collection.add("PREFIXSUFFIXSTRING"); + collection.add("PREFIX----------"); + String prefix = "PREFIX"; + int suffixMinLength = 10; + char paddingChar = '-'; + dataNodeInfo = new DataNodeInfo(prefix, suffixMinLength, paddingChar); + } + + @Test + public void assertFindMatchedTargetNameForValidInputs() { + Optional output = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(collection, "SUFFIX", dataNodeInfo); + assertTrue(output.isPresent()); + assertThat("PREFIX----SUFFIX", is(output.get())); + Optional output1 = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(collection, "SUFFIXSTRING", dataNodeInfo); + assertTrue(output1.isPresent()); + assertThat("PREFIXSUFFIXSTRING", is(output1.get())); + Optional output2 = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(collection, "", dataNodeInfo); + assertTrue(output2.isPresent()); + assertThat("PREFIX----------", is(output2.get())); + } + + @Test + public void assertFindMatchedTargetNameNonExistingInput() { + Optional output = ShardingAutoTableAlgorithmUtil.findMatchedTargetName(collection, "NONEXISTINGSUFFIX", dataNodeInfo); + assertFalse(output.isPresent()); + } +}