提交 e7cc1a77 编写于 作者: K KomachiSion

Add unit test for YamlOrchestrationEncryptDataSourceFactory

上级 16777afe
/*
* 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.shardingjdbc.orchestration.api.yaml;
import lombok.SneakyThrows;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationEncryptDataSource;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
public class YamlOrchestrationEncryptDataSourceFactoryTest {
@Test
public void assertCreateDataSourceByYamlFile() throws URISyntaxException, IOException {
File yamlFile = new File(OrchestrationEncryptDataSource.class.getResource("/yaml/unit/encryptWithRegistryCenter.yaml").toURI());
DataSource dataSource = YamlOrchestrationEncryptDataSourceFactory.createDataSource(yamlFile);
assertThat(dataSource, instanceOf(OrchestrationEncryptDataSource.class));
}
@Test
public void assertCreateDataSourceByYamlFileWithDataSource() throws URISyntaxException, IOException {
File yamlFile = new File(OrchestrationEncryptDataSource.class.getResource("/yaml/unit/encryptWithRegistryCenter.yaml").toURI());
DataSource dataSource = YamlOrchestrationEncryptDataSourceFactory.createDataSource(getDataSource(), yamlFile);
assertThat(dataSource, instanceOf(OrchestrationEncryptDataSource.class));
}
@Test
public void assertCreateDataSourceByYamlFileWithoutRule() throws URISyntaxException, IOException {
File yamlFile = new File(OrchestrationEncryptDataSource.class.getResource("/yaml/unit/noRule.yaml").toURI());
DataSource dataSource = YamlOrchestrationEncryptDataSourceFactory.createDataSource(getDataSource(), yamlFile);
assertThat(dataSource, instanceOf(OrchestrationEncryptDataSource.class));
}
@Test
public void assertCreateDataSourceByYamlBytes() throws IOException {
DataSource dataSource = YamlOrchestrationEncryptDataSourceFactory.createDataSource(readBytesFromYamlFile());
assertThat(dataSource, instanceOf(OrchestrationEncryptDataSource.class));
}
@Test
public void assertCreateDataSourceByYamlBytesWithDataSource() throws IOException {
DataSource dataSource = YamlOrchestrationEncryptDataSourceFactory.createDataSource(getDataSource() ,readBytesFromYamlFile());
assertThat(dataSource, instanceOf(OrchestrationEncryptDataSource.class));
}
@SneakyThrows
private byte[] readBytesFromYamlFile() {
File yamlFile = new File(OrchestrationEncryptDataSource.class.getResource("/yaml/unit/encryptWithRegistryCenter.yaml").toURI());
byte[] result = new byte[(int) yamlFile.length()];
try (InputStream inputStream = new FileInputStream(yamlFile)) {
inputStream.read(result);
}
return result;
}
private DataSource getDataSource() {
BasicDataSource result = new BasicDataSource();
result.setDriverClassName("org.h2.Driver");
result.setUrl("jdbc:h2:mem:ds_encrypt;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
result.setUsername("sa");
result.setPassword("");
return result;
}
}
......@@ -61,7 +61,7 @@ public class OrchestrationEncryptDataSourceTest {
private OrchestrationConfiguration getOrchestrationConfiguration() {
RegistryCenterConfiguration registryCenterConfiguration = new RegistryCenterConfiguration("TestRegistryCenter");
registryCenterConfiguration.setNamespace("test_ms");
registryCenterConfiguration.setNamespace("test_encrypt");
registryCenterConfiguration.setServerLists("localhost:3181");
return new OrchestrationConfiguration("test", registryCenterConfiguration, true);
}
......
#
# 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.
#
orchestration:
name: test
overwrite: true
registry:
type: TestRegistryCenter
namespace: test_encrypt
serverLists: localhost:3181
dataSource: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:ds_encrypt;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
encryptRule:
encryptors:
order_encryptor:
type: aes
qualifiedColumns: t_order.user_id
props:
aes.key.value: 123456
#
# 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.
#
orchestration:
name: test
overwrite: true
registry:
type: TestRegistryCenter
namespace: test_encrypt
serverLists: localhost:3181
dataSource: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:ds_encrypt;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册