From 5a51b5853e42325cb323eab40350c20027a022d0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 5 Dec 2020 07:27:20 -0800 Subject: [PATCH] Restore HazelcastHealthIndicatorTests Restore `HazelcastHealthIndicatorTests` which was accidentally replaced with a version from 2.4.x. See gh-24337 --- .../HazelcastHealthIndicatorTests.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/hazelcast/HazelcastHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/hazelcast/HazelcastHealthIndicatorTests.java index c32d78418d..ad91180052 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/hazelcast/HazelcastHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/hazelcast/HazelcastHealthIndicatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -16,14 +16,16 @@ package org.springframework.boot.actuate.hazelcast; -import com.hazelcast.core.Endpoint; +import java.io.IOException; + import com.hazelcast.core.HazelcastException; import com.hazelcast.core.HazelcastInstance; -import com.hazelcast.transaction.TransactionalTask; import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Status; +import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory; +import org.springframework.core.io.ClassPathResource; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -38,28 +40,27 @@ import static org.mockito.Mockito.mock; */ class HazelcastHealthIndicatorTests { - private final HazelcastInstance hazelcast = mock(HazelcastInstance.class); - @Test - void hazelcastUp() { - Endpoint endpoint = mock(Endpoint.class); - given(this.hazelcast.getName()).willReturn("hz0-instance"); - given(this.hazelcast.getLocalEndpoint()).willReturn(endpoint); - given(endpoint.getUuid()).willReturn("7581bb2f-879f-413f-b574-0071d7519eb0"); - given(this.hazelcast.executeTransaction(any())).willAnswer((invocation) -> { - TransactionalTask task = invocation.getArgument(0); - return task.execute(null); - }); - Health health = new HazelcastHealthIndicator(this.hazelcast).health(); - assertThat(health.getStatus()).isEqualTo(Status.UP); - assertThat(health.getDetails()).containsOnlyKeys("name", "uuid").containsEntry("name", "hz0-instance") - .containsEntry("uuid", "7581bb2f-879f-413f-b574-0071d7519eb0"); + void hazelcastUp() throws IOException { + HazelcastInstance hazelcast = new HazelcastInstanceFactory(new ClassPathResource("hazelcast.xml")) + .getHazelcastInstance(); + try { + Health health = new HazelcastHealthIndicator(hazelcast).health(); + assertThat(health.getStatus()).isEqualTo(Status.UP); + assertThat(health.getDetails()).containsOnlyKeys("name", "uuid").containsEntry("name", + "actuator-hazelcast"); + assertThat(health.getDetails().get("uuid")).asString().isNotEmpty(); + } + finally { + hazelcast.shutdown(); + } } @Test void hazelcastDown() { - given(this.hazelcast.executeTransaction(any())).willReturn(new HazelcastException()); - Health health = new HazelcastHealthIndicator(this.hazelcast).health(); + HazelcastInstance hazelcast = mock(HazelcastInstance.class); + given(hazelcast.executeTransaction(any())).willThrow(new HazelcastException()); + Health health = new HazelcastHealthIndicator(hazelcast).health(); assertThat(health.getStatus()).isEqualTo(Status.DOWN); }