Fix NPE when local leader is null (#297)

This commit is contained in:
Gytis Trikleris
2018-12-14 17:19:38 +01:00
committed by Ryan Baxter
parent ad155a60d2
commit f335da672c
2 changed files with 63 additions and 1 deletions

View File

@@ -68,7 +68,7 @@ public class LeadershipController {
}
public Optional<Leader> getLocalLeader() {
return Optional.of(localLeader);
return Optional.ofNullable(localLeader);
}
public synchronized void update() {

View File

@@ -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();
}
}