Skip to content

Commit

Permalink
Add RepositoryTupleSwapperIT (#31092)
Browse files Browse the repository at this point in the history
* Add RepositoryTupleSwapperIT

* Add RepositoryTupleSwapperIT
  • Loading branch information
terrymanu authored May 1, 2024
1 parent 2796c1c commit dd1b1d8
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.broadcast.yaml;

import org.apache.shardingsphere.broadcast.yaml.swapper.BroadcastRuleConfigurationRepositoryTupleSwapper;
import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
import org.apache.shardingsphere.test.it.yaml.RepositoryTupleSwapperIT;

import java.util.Collection;
import java.util.Iterator;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class BroadcastRuleConfigurationRepositoryTupleSwapperIT extends RepositoryTupleSwapperIT {

BroadcastRuleConfigurationRepositoryTupleSwapperIT() {
super("yaml/broadcast-rule.yaml", new BroadcastRuleConfigurationRepositoryTupleSwapper());
}

@Override
protected void assertRepositoryTuples(final Collection<RepositoryTuple> actualRepositoryTuples) {
assertThat(actualRepositoryTuples.size(), is(1));
Iterator<RepositoryTuple> iterator = actualRepositoryTuples.iterator();
assertTables(iterator.next());
}

private void assertTables(final RepositoryTuple actual) {
assertThat(actual.getKey(), is("tables"));
assertThat(actual.getValue(), is("- foo_tbl\n- bar_tbl\n"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationIT;

import java.util.ArrayList;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

Expand All @@ -36,7 +38,8 @@ protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
}

private void assertBroadcastRule(final YamlBroadcastRuleConfiguration actual) {
assertThat(actual.getTables().size(), is(1));
assertThat(actual.getTables().iterator().next(), is("t_address"));
assertThat(actual.getTables().size(), is(2));
assertThat(new ArrayList<>(actual.getTables()).get(0), is("foo_tbl"));
assertThat(new ArrayList<>(actual.getTables()).get(1), is("bar_tbl"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#

rules:
- !BROADCAST
tables:
- t_address
- !BROADCAST
tables:
- foo_tbl
- bar_tbl
5 changes: 5 additions & 0 deletions test/it/yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<artifactId>shardingsphere-infra-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-mode-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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.test.it.yaml;

import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import org.apache.shardingsphere.mode.spi.RepositoryTupleSwapper;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;

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;

public abstract class RepositoryTupleSwapperIT {

private final File yamlFile;

@SuppressWarnings("rawtypes")
private final RepositoryTupleSwapper swapper;

@SuppressWarnings("rawtypes")
public RepositoryTupleSwapperIT(final String yamlFileName, final RepositoryTupleSwapper swapper) {
URL url = Thread.currentThread().getContextClassLoader().getResource(yamlFileName);
assertNotNull(url);
yamlFile = new File(url.getFile());
this.swapper = swapper;
}

@Test
void assertSwapToRepositoryTuples() throws IOException {
assertRepositoryTuples(getRepositoryTuples());
}

@SuppressWarnings("unchecked")
private Collection<RepositoryTuple> getRepositoryTuples() throws IOException {
YamlRootConfiguration yamlRootConfig = YamlEngine.unmarshal(yamlFile, YamlRootConfiguration.class);
assertThat(yamlRootConfig.getRules().size(), is(1));
return (Collection<RepositoryTuple>) swapper.swapToRepositoryTuples(yamlRootConfig.getRules().iterator().next());
}

protected abstract void assertRepositoryTuples(Collection<RepositoryTuple> actualRepositoryTuples);

@Test
void assertSwapToObject() throws IOException {
assertThat(getActualYamlContent(), is(getExpectedYamlContent()));
}

@SuppressWarnings("unchecked")
private String getActualYamlContent() throws IOException {
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuples().stream()
.map(each -> new RepositoryTuple(String.format("/metadata/foo_db/rules/%s/%s/versions/0", swapper.getRuleTypeName(), each.getKey()), each.getValue())).collect(Collectors.toList());
Optional<YamlRuleConfiguration> actualYamlRuleConfig = swapper.swapToObject(repositoryTuples);
assertTrue(actualYamlRuleConfig.isPresent());
YamlRootConfiguration yamlRootConfig = new YamlRootConfiguration();
yamlRootConfig.setRules(Collections.singletonList(actualYamlRuleConfig.get()));
return YamlEngine.marshal(yamlRootConfig);
}

private String getExpectedYamlContent() throws IOException {
return Files.readAllLines(yamlFile.toPath()).stream().filter(each -> !each.contains("#") && !each.isEmpty()).collect(Collectors.joining(System.lineSeparator())) + System.lineSeparator();
}
}

0 comments on commit dd1b1d8

Please sign in to comment.