Skip to content

Commit

Permalink
Add test cases on ResourceMetaData (#33245)
Browse files Browse the repository at this point in the history
* Add test cases on ResourceMetaData

* Add test cases on StorageNode
  • Loading branch information
terrymanu authored Oct 14, 2024
1 parent fd599fb commit 44771f4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public ResourceMetaData(final Map<String, DataSource> dataSources) {
.collect(Collectors.toMap(each -> each, StorageNode::new, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Map<String, DataSourcePoolProperties> dataSourcePoolPropsMap = dataSources.entrySet().stream().collect(
Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
storageUnits = new LinkedHashMap<>();
for (Entry<String, StorageNode> entry : storageUnitNodeMap.entrySet()) {
storageUnits.put(entry.getKey(), new StorageUnit(entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName())));
}
storageUnits = storageUnitNodeMap.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> new StorageUnit(entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName())),
(a, b) -> b, LinkedHashMap::new));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.infra.metadata.database.resource;

import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ResourceMetaDataTest {

@Test
void assertGetAllInstanceDataSourceNames() {
assertThat(new ResourceMetaData(Collections.singletonMap("foo", new MockedDataSource())).getAllInstanceDataSourceNames(), is(Collections.singletonList("foo")));
}

@Test
void assertGetNotExistedDataSources() {
assertTrue(new ResourceMetaData(Collections.singletonMap("foo", new MockedDataSource())).getNotExistedDataSources(Collections.singleton("foo")).isEmpty());
}

@Test
void assertGetDataSourceMap() {
assertThat(new ResourceMetaData(Collections.singletonMap("foo", new MockedDataSource())).getDataSourceMap().size(), is(1));
assertTrue(new ResourceMetaData(Collections.singletonMap("foo", new MockedDataSource())).getDataSourceMap().containsKey("foo"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.infra.metadata.database.resource.node;

import org.junit.jupiter.api.Test;

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

class StorageNodeTest {

@Test
void assertEquals() {
assertThat(new StorageNode("localhost", 3306, "root"), is(new StorageNode("LOCALHOST", 3306, "ROOT")));
}

@Test
void assertNotEqualsWithDifferentClassType() {
assertThat(new StorageNode("localhost", 3306, "root"), not(new Object()));
}

@Test
void assertNotEqualsWithDifferentName() {
assertThat(new StorageNode("localhost", 3306, "root"), not(new StorageNode("12.0.0.1", 3306, "ROOT")));
}

@Test
void assertHashcode() {
assertThat(new StorageNode("localhost", 3306, "root").hashCode(), is(new StorageNode("LOCALHOST", 3306, "ROOT").hashCode()));
}

@Test
void assertToString() {
assertThat(new StorageNode("localhost", 3306, "root").toString(), is("localhost_3306_root"));
}
}

0 comments on commit 44771f4

Please sign in to comment.