Skip to content

Commit

Permalink
Add AuthorityRuleConfigurationRepositoryTupleSwapperIT (#31093)
Browse files Browse the repository at this point in the history
* Add AuthorityRuleConfigurationRepositoryTupleSwapperIT

* Add AuthorityRuleConfigurationRepositoryTupleSwapperIT
  • Loading branch information
terrymanu authored May 1, 2024
1 parent dd1b1d8 commit 2d38ee2
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.broadcast.yaml;
package org.apache.shardingsphere.broadcast.it;

import org.apache.shardingsphere.broadcast.yaml.swapper.BroadcastRuleConfigurationRepositoryTupleSwapper;
import org.apache.shardingsphere.infra.util.yaml.datanode.RepositoryTuple;
Expand All @@ -30,7 +30,7 @@
class BroadcastRuleConfigurationRepositoryTupleSwapperIT extends RepositoryTupleSwapperIT {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.broadcast.yaml;
package org.apache.shardingsphere.broadcast.it;

import org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
Expand Down

This file was deleted.

6 changes: 6 additions & 0 deletions kernel/authority/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-it-yaml</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.authority.it;

import org.apache.shardingsphere.authority.yaml.swapper.AuthorityRuleConfigurationRepositoryTupleSwapper;
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 AuthorityRuleConfigurationRepositoryTupleSwapperIT extends RepositoryTupleSwapperIT {

AuthorityRuleConfigurationRepositoryTupleSwapperIT() {
super("yaml/authority-rule.yaml", new AuthorityRuleConfigurationRepositoryTupleSwapper(), true);
}

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

private void assertAuthority(final RepositoryTuple actual) {
assertThat(actual.getKey(), is("authority"));
assertThat(actual.getValue(), is("authenticators:\n fixture:\n type: FIXTURE\ndefaultAuthenticator: fixture\nprivilege:\n type: ALL_PERMITTED\n"
+ "users:\n- admin: true\n password: root\n user: root@%\n- admin: false\n password: sharding\n user: sharding@\n"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.authority.it;

import org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.yaml.config.YamlUserConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationIT;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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;

class AuthorityRuleConfigurationYamlIT extends YamlRuleConfigurationIT {

AuthorityRuleConfigurationYamlIT() {
super("yaml/authority-rule.yaml");
}

@Override
protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
assertAuthorityRule((YamlAuthorityRuleConfiguration) actual.getRules().iterator().next());
}

private void assertAuthorityRule(final YamlAuthorityRuleConfiguration actual) {
assertUsers(new ArrayList<>(actual.getUsers()));
assertPrivilege(actual.getPrivilege());
assertAuthenticators(actual.getAuthenticators());
assertDefaultAuthenticator(actual.getDefaultAuthenticator());
}

private void assertUsers(final List<YamlUserConfiguration> actual) {
assertThat(actual.size(), is(2));
assertThat(actual.get(0).getUser(), is("root@%"));
assertThat(actual.get(0).getPassword(), is("root"));
assertTrue(actual.get(0).isAdmin());
assertThat(actual.get(1).getUser(), is("sharding@"));
assertThat(actual.get(1).getPassword(), is("sharding"));
assertFalse(actual.get(1).isAdmin());
}

private void assertPrivilege(final YamlAlgorithmConfiguration actual) {
assertThat(actual.getType(), is("ALL_PERMITTED"));
assertTrue(actual.getProps().isEmpty());
}

private void assertAuthenticators(final Map<String, YamlAlgorithmConfiguration> actual) {
assertThat(actual.size(), is(1));
assertThat(actual.get("fixture").getType(), is("FIXTURE"));
assertTrue(actual.get("fixture").getProps().isEmpty());
}

private void assertDefaultAuthenticator(final String actual) {
assertThat(actual, is("fixture"));
}
}

This file was deleted.

32 changes: 32 additions & 0 deletions kernel/authority/core/src/test/resources/yaml/authority-rule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# 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.
#

rules:
- !AUTHORITY
authenticators:
fixture:
type: FIXTURE
defaultAuthenticator: fixture
privilege:
type: ALL_PERMITTED
users:
- admin: true
password: root
user: root@%
- admin: false
password: sharding
user: sharding@
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ public abstract class RepositoryTupleSwapperIT {
@SuppressWarnings("rawtypes")
private final RepositoryTupleSwapper swapper;

private final boolean isGlobalRule;

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

@Test
Expand All @@ -74,15 +77,20 @@ void assertSwapToObject() throws IOException {

@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());
Collection<RepositoryTuple> repositoryTuples = getRepositoryTuples().stream().map(each -> new RepositoryTuple(getRepositoryTupleKey(each), 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 getRepositoryTupleKey(final RepositoryTuple tuple) {
return isGlobalRule
? String.format("/metadata/rules/%s/versions/0", swapper.getRuleTypeName())
: String.format("/metadata/foo_db/rules/%s/%s/versions/0", swapper.getRuleTypeName(), tuple.getKey());
}

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

0 comments on commit 2d38ee2

Please sign in to comment.