Fix NPE when local leader is null (#297)
This commit is contained in:
committed by
Ryan Baxter
parent
ad155a60d2
commit
f335da672c
@@ -68,7 +68,7 @@ public class LeadershipController {
|
||||
}
|
||||
|
||||
public Optional<Leader> getLocalLeader() {
|
||||
return Optional.of(localLeader);
|
||||
return Optional.ofNullable(localLeader);
|
||||
}
|
||||
|
||||
public synchronized void update() {
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed 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.springframework.cloud.kubernetes.leader;
|
||||
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.integration.leader.Candidate;
|
||||
import org.springframework.integration.leader.event.LeaderEventPublisher;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Gytis Trikleris
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LeadershipControllerTest {
|
||||
|
||||
@Mock
|
||||
private Candidate mockCandidate;
|
||||
|
||||
@Mock
|
||||
private LeaderProperties mockLeaderProperties;
|
||||
|
||||
@Mock
|
||||
private LeaderEventPublisher mockLeaderEventPublisher;
|
||||
|
||||
@Mock
|
||||
private KubernetesClient mockKubernetesClient;
|
||||
|
||||
private LeadershipController leadershipController;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
leadershipController = new LeadershipController(mockCandidate, mockLeaderProperties, mockLeaderEventPublisher,
|
||||
mockKubernetesClient);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetEmptyLocalLeader() {
|
||||
assertThat(leadershipController.getLocalLeader().isPresent()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user