kirklund commented on a change in pull request #6013: URL: https://github.com/apache/geode/pull/6013#discussion_r581368726
########## File path: geode-wan/src/test/java/org/apache/geode/cache/client/internal/locator/wan/WanLocatorDiscovererTest.java ########## @@ -0,0 +1,95 @@ +/* + * 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.geode.cache.client.internal.locator.wan; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.mockito.quality.Strictness; + +import org.apache.geode.distributed.internal.DistributionConfig; +import org.apache.geode.distributed.internal.DistributionConfigImpl; +import org.apache.geode.internal.admin.remote.DistributionLocatorId; + +public class WanLocatorDiscovererTest { + private LocatorMembershipListener locatorMembershipListener; + private DistributionConfigImpl config; + private final ConcurrentMap<Integer, Set<String>> allServerLocatorsInfo = + new ConcurrentHashMap<>(); + private final ConcurrentMap<Integer, Set<DistributionLocatorId>> allLocatorsInfo = + new ConcurrentHashMap<>(); + private String locators; + + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); + + @Before + public void setUp() { + locatorMembershipListener = mock(LocatorMembershipListener.class); + DistributionLocatorId loc1 = new DistributionLocatorId(40401, "127.0.0.1", null, "loc1"); + DistributionLocatorId loc2 = new DistributionLocatorId(40402, "127.0.0.2", null, "loc2"); + DistributionLocatorId loc3 = new DistributionLocatorId(40403, "127.0.0.3", null, "loc3"); + DistributionLocatorId loc4 = new DistributionLocatorId(40404, "127.0.0.4", null, "loc4"); + Set<DistributionLocatorId> locatorSet = new HashSet<>(); + locatorSet.add(loc1); + locatorSet.add(loc2); + locatorSet.add(loc3); + locatorSet.add(loc4); + allLocatorsInfo.put(1, locatorSet); + Set<String> serverLocatorSet = new HashSet<>(); + serverLocatorSet.add(loc1.toString()); + serverLocatorSet.add(loc2.toString()); + serverLocatorSet.add(loc3.toString()); + serverLocatorSet.add(loc4.toString()); + allServerLocatorsInfo.put(1, serverLocatorSet); + config = mock(DistributionConfigImpl.class); + locators = loc1.marshal(); + + } + + @Test + public void test_discover() { + when(locatorMembershipListener.getAllLocatorsInfo()).thenReturn(allLocatorsInfo); + + when(config.getStartLocator()).thenReturn(DistributionConfig.DEFAULT_START_LOCATOR); + when(config.getName()).thenReturn("loc1"); + when(config.getBindAddress()).thenReturn("127.0.0.1"); + when(config.getDistributedSystemId()).thenReturn(1); + when(config.getLocators()).thenReturn(locators); + when(config.getRemoteLocators()).thenReturn(""); + + WanLocatorDiscovererImpl test_wan = new WanLocatorDiscovererImpl(); + test_wan.discover(40401, config, locatorMembershipListener, null); + + assertThat(allLocatorsInfo.get(1).size()).isEqualTo(4); Review comment: These size assertions provide much better failure messages if you change them to: ``` assertThat(allLocatorsInfo.get(1)).hasSize(4); ``` Then if the size is wrong, it'll actually list out all the elements which helps debug what went wrong. ########## File path: geode-core/src/test/java/org/apache/geode/internal/admin/remote/DistributionLocatorIdJUnitTest.java ########## @@ -46,4 +48,25 @@ public void testEquals() throws UnknownHostException { } + @Test + public void testEquals_and_DetailCompare() throws UnknownHostException { + DistributionLocatorId dLI1 = new DistributionLocatorId(40404, "127.0.0.1", null); + DistributionLocatorId dLI2 = + new DistributionLocatorId(40404, "127.0.0.1", "127.0.1.0", "member2");; + DistributionLocatorId dLI3 = new DistributionLocatorId(40404, "127.0.0.1", null, "member3"); + DistributionLocatorId dLI4 = new DistributionLocatorId(dLI3.marshal()); + + assertTrue(dLI1.equals(dLI2)); Review comment: Can you please update the test to use AssertJ instead of JUnit Asserts? If these assertions ever fail, they will provide much better details for fixing the code if they're changed to: ``` assertThat(dLI1).isEqualTo(dLI2); ``` ########## File path: geode-core/src/test/java/org/apache/geode/internal/admin/remote/DistributionLocatorIdJUnitTest.java ########## @@ -46,4 +48,25 @@ public void testEquals() throws UnknownHostException { } + @Test + public void testEquals_and_DetailCompare() throws UnknownHostException { + DistributionLocatorId dLI1 = new DistributionLocatorId(40404, "127.0.0.1", null); Review comment: Let's change these variable names to something more description than an abbreviation: ``` DistributionLocatorId distributionLocatorId1 = new DistributionLocatorId(40404, "127.0.0.1", null); ``` ########## File path: geode-wan/src/test/java/org/apache/geode/cache/client/internal/locator/wan/LocatorHelperTest.java ########## @@ -0,0 +1,184 @@ +/* + * 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.geode.cache.client.internal.locator.wan; + +import static org.apache.geode.internal.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.mockito.quality.Strictness; + +import org.apache.geode.internal.admin.remote.DistributionLocatorId; + +public class LocatorHelperTest { + private LocatorMembershipListener locatorMembershipListener; + private final ConcurrentMap<Integer, Set<String>> allServerLocatorsInfo = + new ConcurrentHashMap<>(); + private final ConcurrentMap<Integer, Set<DistributionLocatorId>> allLocatorsInfo = + new ConcurrentHashMap<>(); + + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); + + @Before + public void setUp() { + locatorMembershipListener = mock(LocatorMembershipListener.class); + DistributionLocatorId loc1 = new DistributionLocatorId(40401, "127.0.0.1", null, "loc1"); + DistributionLocatorId loc2 = new DistributionLocatorId(40402, "127.0.0.2", null, "loc2"); + DistributionLocatorId loc3 = new DistributionLocatorId(40403, "127.0.0.3", null, "loc3"); + DistributionLocatorId loc4 = new DistributionLocatorId(40404, "127.0.0.4", null, "loc4"); + Set<DistributionLocatorId> locatorSet = new HashSet<>(); + locatorSet.add(loc1); + locatorSet.add(loc2); + locatorSet.add(loc3); + locatorSet.add(loc4); + allLocatorsInfo.put(1, locatorSet); + Set<String> serverLocatorSet = new HashSet<>(); + serverLocatorSet.add(loc1.toString()); + serverLocatorSet.add(loc2.toString()); + serverLocatorSet.add(loc3.toString()); + serverLocatorSet.add(loc4.toString()); + allServerLocatorsInfo.put(1, serverLocatorSet); + } + + @Test + public void testAddLocator_addToList() { + when(locatorMembershipListener.getAllLocatorsInfo()).thenReturn(allLocatorsInfo); + when(locatorMembershipListener.getAllServerLocatorsInfo()).thenReturn(allServerLocatorsInfo); + + DistributionLocatorId locator = new DistributionLocatorId(40405, "127.0.0.5", null, "loc5"); + + assertTrue(LocatorHelper.addLocator(1, locator, locatorMembershipListener, null)); + assertThat(allLocatorsInfo.get(1).size()).isEqualTo(5); + assertThat(allServerLocatorsInfo.get(1).size()).isEqualTo(5); + verify(locatorMembershipListener, times(1)).locatorJoined(1, locator, null); + + } + + @Test + public void testAddLocator_replaceInList() { + when(locatorMembershipListener.getAllLocatorsInfo()).thenReturn(allLocatorsInfo); + when(locatorMembershipListener.getAllServerLocatorsInfo()).thenReturn(allServerLocatorsInfo); + + DistributionLocatorId locator = new DistributionLocatorId(40405, "127.0.0.5", null, "loc4"); + + assertTrue(LocatorHelper.addLocator(1, locator, locatorMembershipListener, null)); + assertThat(allLocatorsInfo.get(1).size()).isEqualTo(4); + assertThat(allServerLocatorsInfo.get(1).size()).isEqualTo(4); + verify(locatorMembershipListener, times(1)).locatorJoined(1, locator, null); + + } + + @Test + public void testAddLocator_noUpdate() { + when(locatorMembershipListener.getAllLocatorsInfo()).thenReturn(allLocatorsInfo); + + DistributionLocatorId locator = new DistributionLocatorId(40404, "127.0.0.4", null, "loc4"); + + assertFalse(LocatorHelper.addLocator(1, locator, locatorMembershipListener, null)); + assertThat(allLocatorsInfo.get(1).size()).isEqualTo(4); + assertThat(allServerLocatorsInfo.get(1).size()).isEqualTo(4); + verify(locatorMembershipListener, times(0)).locatorJoined(1, locator, null); + + } + + @Test + public void testAddLocator_modifyMemberName() { + when(locatorMembershipListener.getAllLocatorsInfo()).thenReturn(allLocatorsInfo); + + DistributionLocatorId locator = new DistributionLocatorId(40404, "127.0.0.4", null, "loc5"); + + assertTrue(LocatorHelper.addLocator(1, locator, locatorMembershipListener, null)); + assertThat(allLocatorsInfo.get(1).size()).isEqualTo(4); + assertThat(allServerLocatorsInfo.get(1).size()).isEqualTo(4); + verify(locatorMembershipListener, times(1)).locatorJoined(1, locator, null); + + } + + @Test + public void testAddExchangedLocators_noUpdate() { + Map<Integer, Set<DistributionLocatorId>> responseLocatorsInfo = + new HashMap<>(); + + DistributionLocatorId loc1 = new DistributionLocatorId(40401, "127.0.0.1", null, "loc1"); + DistributionLocatorId loc2 = new DistributionLocatorId(40402, "127.0.0.2", null, "loc2"); + DistributionLocatorId loc3 = new DistributionLocatorId(40403, "127.0.0.3", null, "loc3"); + DistributionLocatorId loc4 = new DistributionLocatorId(40404, "127.0.0.4", null, "loc4"); + Set<DistributionLocatorId> responseSet = new HashSet<>(); + responseSet.add(loc1); + responseSet.add(loc2); + responseSet.add(loc3); + responseSet.add(loc4); + responseLocatorsInfo.put(1, responseSet); + when(locatorMembershipListener.getAllLocatorsInfo()).thenReturn(allLocatorsInfo); + + assertFalse( + LocatorHelper.addExchangedLocators(responseLocatorsInfo, locatorMembershipListener)); + assertThat(allLocatorsInfo.get(1).size()).isEqualTo(4); + assertThat(allServerLocatorsInfo.get(1).size()).isEqualTo(4); + + } + + @Test + public void testAddExchangedLocators_initialUpdate() { + ConcurrentMap<Integer, Set<DistributionLocatorId>> emptyLocatorList = + new ConcurrentHashMap<>(); + ConcurrentMap<Integer, Set<String>> emptyServerLocatorList = + new ConcurrentHashMap<>(); + + Map<Integer, Set<DistributionLocatorId>> responseLocatorsInfo = + new HashMap<>(); + + DistributionLocatorId loc1 = new DistributionLocatorId(40401, "127.0.0.1", null, "loc1"); + DistributionLocatorId loc2 = new DistributionLocatorId(40402, "127.0.0.2", null, "loc2"); + DistributionLocatorId loc3 = new DistributionLocatorId(40403, "127.0.0.3", null, "loc3"); + DistributionLocatorId loc4 = new DistributionLocatorId(40404, "127.0.0.4", null, "loc4"); + Set<DistributionLocatorId> responseSet = new HashSet<>(); + responseSet.add(loc1); + responseSet.add(loc2); + responseSet.add(loc3); + responseSet.add(loc4); + responseLocatorsInfo.put(1, responseSet); + when(locatorMembershipListener.getAllLocatorsInfo()).thenReturn(emptyLocatorList); + when(locatorMembershipListener.getAllServerLocatorsInfo()).thenReturn(emptyServerLocatorList); + + assertTrue( Review comment: Let's use keep the assertions more consistent and use AssertJ here too: ``` assertThat(LocatorHelper.addExchangedLocators(responseLocatorsInfo, locatorMembershipListener)).isTrue(); ``` ########## File path: geode-core/src/test/java/org/apache/geode/internal/admin/remote/DistributionLocatorIdJUnitTest.java ########## @@ -46,4 +48,25 @@ public void testEquals() throws UnknownHostException { } + @Test + public void testEquals_and_DetailCompare() throws UnknownHostException { + DistributionLocatorId dLI1 = new DistributionLocatorId(40404, "127.0.0.1", null); + DistributionLocatorId dLI2 = + new DistributionLocatorId(40404, "127.0.0.1", "127.0.1.0", "member2");; Review comment: Please delete the extra semi-colon at the end of this line. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
