Skip to content

Commit

Permalink
Refactor AlterTrafficRuleExecutorTest Test Case
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasahsan123 committed May 12, 2024
1 parent a8174ae commit c574a0a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.traffic.distsql.handler.fixture;

import org.apache.shardingsphere.infra.algorithm.loadbalancer.core.LoadBalanceAlgorithm;

import java.util.List;

public class DistSQLLoadBalanceAlgorithmFixture implements LoadBalanceAlgorithm {

@Override
public String getType() {
return "DISTSQL.FIXTURE";
}

@Override
public String getTargetName(String groupName, List<String> availableTargetNames) {
return "Load Balance Algorithm Fixture";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.apache.shardingsphere.traffic.distsql.handler.update;

import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine;
import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
Expand All @@ -34,10 +37,9 @@
import java.util.Collections;
import java.util.Properties;

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.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -47,36 +49,27 @@ class AlterTrafficRuleExecutorTest {
void assertExecuteWithNotExistRuleName() {
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment(
"rule_name_3", Arrays.asList("olap", "order_by"), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
AlterTrafficRuleExecutor executor = new AlterTrafficRuleExecutor();
TrafficRule rule = mock(TrafficRule.class);
when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
executor.setRule(rule);
assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment))));
AlterTrafficRuleStatement sqlStatement = new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment));
DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager());
assertThrows(MissingRequiredRuleException.class, engine::executeUpdate);
}

@Test
void assertExecuteWithInvalidAlgorithmType() {
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment(
"rule_name_1", Arrays.asList("olap", "order_by"), new AlgorithmSegment("invalid", new Properties()), new AlgorithmSegment("invalid", new Properties()));
AlterTrafficRuleExecutor executor = new AlterTrafficRuleExecutor();
TrafficRule rule = mock(TrafficRule.class);
when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
executor.setRule(rule);
assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment))));
AlterTrafficRuleStatement sqlStatement = new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment));
DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager());
assertThrows(ServiceProviderNotFoundException.class, engine::executeUpdate);
}

@Test
void assertExecuteWithLoadBalancerCannotBeNull() {
TrafficRuleSegment trafficRuleSegment = new TrafficRuleSegment("rule_name_1", Arrays.asList("olap", "order_by"),
new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), null);
AlterTrafficRuleExecutor executor = new AlterTrafficRuleExecutor();
TrafficRule rule = mock(TrafficRule.class);
when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
executor.setRule(rule);
TrafficRuleConfiguration actual = executor.buildToBeAlteredRuleConfiguration(new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment)));
assertThat(actual.getTrafficStrategies().size(), is(2));
assertThat(actual.getTrafficAlgorithms().size(), is(2));
assertThat(actual.getLoadBalancers().size(), is(1));
AlterTrafficRuleStatement sqlStatement = new AlterTrafficRuleStatement(Collections.singleton(trafficRuleSegment));
DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager());
assertDoesNotThrow(engine::executeUpdate);
}

@Test
Expand All @@ -85,18 +78,20 @@ void assertExecute() {
"rule_name_1", Arrays.asList("olap", "order_by"), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
TrafficRuleSegment trafficRuleSegment2 = new TrafficRuleSegment(
"rule_name_2", Collections.emptyList(), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
AlterTrafficRuleExecutor executor = new AlterTrafficRuleExecutor();
AlterTrafficRuleStatement sqlStatement = new AlterTrafficRuleStatement(Arrays.asList(trafficRuleSegment1, trafficRuleSegment2));
DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager());
assertDoesNotThrow(engine::executeUpdate);
}

@SuppressWarnings({"rawtypes", "unchecked"})
private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
TrafficRule rule = mock(TrafficRule.class);
GlobalRuleDefinitionExecutor executor = mock(GlobalRuleDefinitionExecutor.class);
when(executor.getRuleClass()).thenReturn(TrafficRule.class);
when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
executor.setRule(rule);
TrafficRuleConfiguration actual =
executor.buildToBeAlteredRuleConfiguration(new AlterTrafficRuleStatement(Arrays.asList(trafficRuleSegment1, trafficRuleSegment2)));
assertThat(actual.getTrafficStrategies().size(), is(2));
assertThat(actual.getTrafficAlgorithms().size(), is(2));
assertThat(actual.getLoadBalancers().size(), is(2));
assertThat(actual.getTrafficStrategies().iterator().next().getName(), is("rule_name_1"));
assertNotNull(actual.getTrafficAlgorithms().get("rule_name_1_distsql.fixture"));
assertNotNull(actual.getLoadBalancers().get("rule_name_2_distsql.fixture"));
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(executor.getRuleClass())).thenReturn(rule);
return result;
}

private TrafficRuleConfiguration createTrafficRuleConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

org.apache.shardingsphere.traffic.distsql.handler.fixture.DistSQLLoadBalanceAlgorithmFixture

0 comments on commit c574a0a

Please sign in to comment.