Skip to content

Commit

Permalink
GEODE-6745 Added unit test coverage for MemberConfigManager and refac…
Browse files Browse the repository at this point in the history
…tored (apache#3620)


Co-authored-by: Peter Tran <[email protected]>
Co-authored-by: Joris Melchior <[email protected]>
  • Loading branch information
2 people authored and jdeppe-pivotal committed May 28, 2019
1 parent 350e6d2 commit 5ee7a2e
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class MemberInformation implements Serializable {
private int httpServicePort;
private String httpServiceBindAddress;
private boolean isServer;
private List<CacheServerInfo> cacheServerList;
private List<CacheServerInfo> cacheServerList = new ArrayList<>();
private int clientCount;
private double cpuUsage;
private Set<String> hostedRegions;
Expand Down Expand Up @@ -229,9 +229,6 @@ public List<CacheServerInfo> getCacheServeInfo() {
public void addCacheServerInfo(CacheServerInfo cacheServerInfo) {
if (cacheServerInfo == null)
return;
if (cacheServerList == null) {
cacheServerList = new ArrayList<CacheServerInfo>();
}
cacheServerList.add(cacheServerInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.NotImplementedException;

import org.apache.geode.annotations.VisibleForTesting;
import org.apache.geode.cache.configuration.CacheConfig;
import org.apache.geode.cache.execute.Execution;
import org.apache.geode.cache.execute.FunctionService;
import org.apache.geode.cache.execute.ResultCollector;
import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.distributed.internal.DistributionManager;
import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
import org.apache.geode.distributed.internal.membership.MembershipManager;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.management.configuration.MemberConfig;
Expand Down Expand Up @@ -61,13 +65,7 @@ public void delete(MemberConfig config, CacheConfig existing) {

@Override
public List<MemberConfig> list(MemberConfig filter, CacheConfig existing) {

Set<DistributedMember> distributedMembers =
cache.getDistributionManager().getDistributionManagerIds()
.stream().filter(internalDistributedMember -> (filter.getId() == null
|| filter.getId().equals(internalDistributedMember.getName())))
.map(DistributedMember.class::cast).collect(Collectors.toSet());

Set<DistributedMember> distributedMembers = getDistributedMembers(filter);
if (distributedMembers.size() == 0) {
return Collections.emptyList();
}
Expand All @@ -77,68 +75,72 @@ public List<MemberConfig> list(MemberConfig filter, CacheConfig existing) {
return generateMemberConfigs(memberInformation);
}

@VisibleForTesting
Set<DistributedMember> getDistributedMembers(MemberConfig filter) {
Set<InternalDistributedMember> distributionManagerIds =
cache.getDistributionManager().getDistributionManagerIds();
if (filter.getId() != null) {
distributionManagerIds = distributionManagerIds.stream().filter(
internalDistributedMember -> (filter.getId().equals(internalDistributedMember.getName())))
.collect(Collectors.toSet());
}

return distributionManagerIds.stream()
.map(DistributedMember.class::cast).collect(Collectors.toSet());
}

private ArrayList<MemberInformation> getMemberInformation(
Set<DistributedMember> distributedMembers) {
Execution execution = FunctionService.onMembers(distributedMembers);
ResultCollector<?, ?> resultCollector = execution.execute(new GetMemberInformationFunction());
return (ArrayList<MemberInformation>) resultCollector.getResult();
}

private List<MemberConfig> generateMemberConfigs(ArrayList<MemberInformation> memberInformation) {

@VisibleForTesting
List<MemberConfig> generateMemberConfigs(ArrayList<MemberInformation> memberInformation) {
final String coordinatorId = getCoordinatorId();
List<MemberConfig> memberConfigs = new ArrayList<>();
for (MemberInformation memberInfo : memberInformation) {
MemberConfig member = generateMemberConfig(coordinatorId, memberInfo);
memberConfigs.add(member);
}

return memberConfigs;
return memberInformation.stream().map(
memberInfo -> generateMemberConfig(coordinatorId, memberInfo)).collect(Collectors.toList());
}

private MemberConfig generateMemberConfig(String coordinatorId, MemberInformation memberInfo) {
@VisibleForTesting
MemberConfig generateMemberConfig(String coordinatorId, MemberInformation memberInfo) {
MemberConfig member = new MemberConfig();
member.setId(memberInfo.getName());
member.setHost(memberInfo.getHost());
member.setPid(memberInfo.getProcessId());
member.setStatus(memberInfo.getStatus());
member.setInitialHeap(memberInfo.getInitHeapSize());
member.setMaxHeap(memberInfo.getMaxHeapSize());
member.setGroups(Arrays.asList(memberInfo.getGroups().split(",")));
member.setCoordinator(memberInfo.getId().equals(coordinatorId));
member.setUsedHeap(memberInfo.getHeapUsage());
member.setLogFile(memberInfo.getLogFilePath());
member.setWorkingDirectory(memberInfo.getWorkingDirPath());
member.setClientConnections(memberInfo.getClientCount());

if (memberInfo.isServer() && memberInfo.getCacheServeInfo() != null) {
if (memberInfo.isServer()) {
for (CacheServerInfo info : memberInfo.getCacheServeInfo()) {
MemberConfig.CacheServerConfig csConfig = new MemberConfig.CacheServerConfig();
csConfig.setPort(info.getPort());
csConfig.setMaxConnections(info.getMaxConnections());
csConfig.setMaxThreads(info.getMaxThreads());
member.addCacheServer(csConfig);
MemberConfig.CacheServerConfig cacheServerConfig = new MemberConfig.CacheServerConfig();
cacheServerConfig.setPort(info.getPort());
cacheServerConfig.setMaxConnections(info.getMaxConnections());
cacheServerConfig.setMaxThreads(info.getMaxThreads());
member.addCacheServer(cacheServerConfig);
}
member.setLocator(false);
member.setGroups(Arrays.asList(memberInfo.getGroups().split(",")));
member.setClientConnections(memberInfo.getClientCount());
} else {
member.setPort(memberInfo.getLocatorPort());
member.setLocator(true);
}
return member;
}

private String getCoordinatorId() {
final MembershipManager membershipManager =
cache.getDistributionManager().getMembershipManager();
if (membershipManager == null) {
return null;
}

final DistributedMember coordinator = membershipManager.getCoordinator();
if (coordinator == null) {
return null;
}

return coordinator.getId();
@VisibleForTesting
String getCoordinatorId() {
return Optional.ofNullable(cache.getDistributionManager())
.map(DistributionManager::getMembershipManager)
.map(MembershipManager::getCoordinator)
.map(DistributedMember::getId)
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
* 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.management.internal.configuration.mutators;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.distributed.internal.DistributionManager;
import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
import org.apache.geode.distributed.internal.membership.MembershipManager;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.management.configuration.MemberConfig;
import org.apache.geode.management.internal.cli.domain.CacheServerInfo;
import org.apache.geode.management.internal.cli.domain.MemberInformation;

public class MemberConfigManagerTest {
private MemberConfigManager memberConfigManager;
private DistributionManager distributionManager;
private MembershipManager membershipManager;
private DistributedMember coordinator;
private CacheServerInfo cacheServerInfo;
private MemberConfig filter;
private InternalDistributedMember internalDistributedMemberMatch;
private InternalDistributedMember internalDistributedMember;

@Before
public void setUp() throws Exception {
InternalCache cache = mock(InternalCache.class);
memberConfigManager = new MemberConfigManager(cache);
distributionManager = mock(DistributionManager.class);
membershipManager = mock(MembershipManager.class);
coordinator = mock(DistributedMember.class);
cacheServerInfo = mock(CacheServerInfo.class);
filter = new MemberConfig();
internalDistributedMember = mock(InternalDistributedMember.class);
internalDistributedMemberMatch = mock(InternalDistributedMember.class);

when(internalDistributedMember.getName()).thenReturn("no-match");
when(cache.getDistributionManager()).thenReturn(distributionManager);
}

@Test
public void getDistributedMembersNoFilter() {
Set<InternalDistributedMember> internalDistributedMembers = new HashSet<>();
internalDistributedMembers.add(internalDistributedMember);
internalDistributedMembers.add(internalDistributedMember);
when(distributionManager.getDistributionManagerIds()).thenReturn(internalDistributedMembers);

assertThat(memberConfigManager.getDistributedMembers(filter).size())
.isEqualTo(internalDistributedMembers.size());
}

@Test
public void getDistributedMembersReturnsNothing() {
filter.setId("some-id-no-one-else-has");
Set<InternalDistributedMember> internalDistributedMembers = new HashSet<>();

internalDistributedMembers.add(internalDistributedMember);
internalDistributedMembers.add(internalDistributedMember);
when(distributionManager.getDistributionManagerIds()).thenReturn(internalDistributedMembers);

assertThat(memberConfigManager.getDistributedMembers(filter).size())
.isEqualTo(0);
}

@Test
public void getDistributedMembersReturnsSomeMatches() {
String sharedId = "shared-id";
filter.setId(sharedId);
Set<InternalDistributedMember> internalDistributedMembers = new HashSet<>();
when(internalDistributedMemberMatch.getName()).thenReturn(sharedId);
internalDistributedMembers.add(internalDistributedMemberMatch);
internalDistributedMembers.add(internalDistributedMember);
when(distributionManager.getDistributionManagerIds()).thenReturn(internalDistributedMembers);

assertThat(memberConfigManager.getDistributedMembers(filter).size())
.isEqualTo(1);
}

@Test
public void generatesMemberConfigs() {
ArrayList<MemberInformation> emptyArrayList = new ArrayList<>();
assertThat(memberConfigManager.generateMemberConfigs(emptyArrayList)).isNotNull();

ArrayList<MemberInformation> smallArrayList = new ArrayList<>();
MemberInformation someMemberInfo = new MemberInformation();
someMemberInfo.setGroups("hello");
someMemberInfo.setId("world");
smallArrayList.add(someMemberInfo);
MemberInformation someOtherMemberInfo = new MemberInformation();
someOtherMemberInfo.setId("hello");
someOtherMemberInfo.setGroups("world");
smallArrayList.add(someOtherMemberInfo);
assertThat(memberConfigManager.generateMemberConfigs(smallArrayList).size())
.isEqualTo(smallArrayList.size());
}

@Test
public void generateMemberConfig() {
MemberInformation memberInformation = new MemberInformation();
memberInformation.setId("some-id");
String someName = "some-name";
memberInformation.setName(someName);
String someHost = "some-host";
memberInformation.setHost(someHost);
int somePid = 7;
memberInformation.setProcessId(somePid);
String someStatus = "some-status";
memberInformation.setStatus(someStatus);
long someInitHeapSize = 234L;
memberInformation.setInitHeapSize(someInitHeapSize);
long someMaxHeapSize = 523L;
memberInformation.setMaxHeapSize(someMaxHeapSize);
long someHeapUsage = 123L;
memberInformation.setHeapUsage(someHeapUsage);
String somePath = "somePath";
memberInformation.setLogFilePath(somePath);
memberInformation.setWorkingDirPath(somePath);

MemberConfig memberConfig =
memberConfigManager.generateMemberConfig("coordinatorId", memberInformation);
assertThat(memberConfig.getId()).isEqualTo(someName);
assertThat(memberConfig.getHost()).isEqualTo(someHost);
assertThat(memberConfig.getPid()).isEqualTo(somePid);
assertThat(memberConfig.getStatus()).isEqualTo(someStatus);
assertThat(memberConfig.getInitialHeap()).isEqualTo(someInitHeapSize);
assertThat(memberConfig.getMaxHeap()).isEqualTo(someMaxHeapSize);
assertThat(memberConfig.getUsedHeap()).isEqualTo(someHeapUsage);
assertThat(memberConfig.getLogFile()).isEqualTo(somePath);
assertThat(memberConfig.getWorkingDirectory()).isEqualTo(somePath);
assertThat(memberConfig.isCoordinator()).isFalse();
}

@Test
public void generateServerMemberConfig() {
int somePort = 5000;
when(cacheServerInfo.getPort()).thenReturn(somePort);
int someConnectionNumber = 10;
when(cacheServerInfo.getMaxConnections()).thenReturn(someConnectionNumber);
int someThreadNumber = 5;
when(cacheServerInfo.getMaxThreads()).thenReturn(someThreadNumber);

MemberInformation memberInformation = new MemberInformation();
memberInformation.addCacheServerInfo(cacheServerInfo);
memberInformation.setServer(true);
String someGroup1 = "something1";
String someGroup2 = "something2";
memberInformation.setGroups(someGroup1 + "," + someGroup2);
String memberId = "memberId";
memberInformation.setId(memberId);

String coordinatorId = "coordinatorId";
MemberConfig memberConfig =
memberConfigManager.generateMemberConfig(coordinatorId, memberInformation);

assertThat(memberConfig.isLocator()).isFalse();
assertThat(memberConfig.isCoordinator()).isFalse();
MemberConfig.CacheServerConfig cacheServerConfig = memberConfig.getCacheServers().get(0);
assertThat(cacheServerConfig).isNotNull();
assertThat(cacheServerConfig.getPort()).isEqualTo(somePort);
assertThat(cacheServerConfig.getMaxConnections()).isEqualTo(someConnectionNumber);
assertThat(cacheServerConfig.getMaxThreads()).isEqualTo(someThreadNumber);
assertThat(memberConfig.getGroups()).contains(someGroup1, someGroup2);
}

@Test
public void generateLocatorMemberConfig() {
MemberInformation memberInformation = new MemberInformation();
int someLocatorPort = 180;
memberInformation.setLocatorPort(someLocatorPort);
String memberId = "memberId";
memberInformation.setId(memberId);

String coordinatorId = "coordinatorId";
MemberConfig memberConfig =
memberConfigManager.generateMemberConfig(coordinatorId, memberInformation);
assertThat(memberConfig.getPort()).isEqualTo(someLocatorPort);
assertThat(memberConfig.isLocator()).isTrue();
assertThat(memberConfig.isCoordinator()).isFalse();
}


@Test
public void generateCoordinatorMemberConfig() {
MemberInformation memberInformation = new MemberInformation();
String coordinatorId = "coordinatorId";
memberInformation.setId(coordinatorId);

MemberConfig memberConfig =
memberConfigManager.generateMemberConfig(coordinatorId, memberInformation);
assertThat(memberConfig.isCoordinator()).isTrue();
}

@Test
public void getsCoordinatorId() {
when(distributionManager.getMembershipManager()).thenReturn(membershipManager);
when(membershipManager.getCoordinator()).thenReturn(coordinator);
String coordinatorId = "some-id";
when(coordinator.getId()).thenReturn(coordinatorId);

assertThat(memberConfigManager.getCoordinatorId()).isEqualTo(coordinatorId);
}

@Test
public void getCoordinatorIdReturnsNullWhenMembershipManagerIsNull() {
when(distributionManager.getMembershipManager()).thenReturn(null);

assertThat(memberConfigManager.getCoordinatorId()).isNull();
}

@Test
public void getCoordinatorIdReturnsNullWhenCoordinatorIsNull() {
when(distributionManager.getMembershipManager()).thenReturn(membershipManager);
when(membershipManager.getCoordinator()).thenReturn(null);

assertThat(memberConfigManager.getCoordinatorId()).isNull();
}
}
Loading

0 comments on commit 5ee7a2e

Please sign in to comment.