From b37eea1d6d2125b22387495959d4ad21e1353c97 Mon Sep 17 00:00:00 2001 From: Anurag Rajawat Date: Wed, 14 Sep 2022 14:04:26 +0530 Subject: [PATCH] refactor (jkube-kit-vertx): Migrate all tests from JUnit4 to JUnit5 (#1572) Signed-off-by: Anurag Rajawat --- jkube-kit/jkube-kit-vertx/pom.xml | 10 +- .../VertxHealthCheckEnricherTest.java | 964 +++++++++--------- .../VertxGeneratorIsApplicableTest.java | 50 +- .../vertx/generator/VertxGeneratorTest.java | 26 +- .../generator/VertxPortsExtractorTest.java | 23 +- 5 files changed, 523 insertions(+), 550 deletions(-) diff --git a/jkube-kit/jkube-kit-vertx/pom.xml b/jkube-kit/jkube-kit-vertx/pom.xml index 949bf16646..f6bc6b84cd 100644 --- a/jkube-kit/jkube-kit-vertx/pom.xml +++ b/jkube-kit/jkube-kit-vertx/pom.xml @@ -43,8 +43,14 @@ - junit - junit + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.jupiter + junit-jupiter-params + test org.assertj diff --git a/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/enricher/VertxHealthCheckEnricherTest.java b/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/enricher/VertxHealthCheckEnricherTest.java index 1913de63ba..3162d92e23 100644 --- a/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/enricher/VertxHealthCheckEnricherTest.java +++ b/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/enricher/VertxHealthCheckEnricherTest.java @@ -20,7 +20,9 @@ import java.util.Properties; import java.util.TreeMap; import java.util.stream.Collectors; +import java.util.stream.Stream; +import io.fabric8.kubernetes.api.model.ExecAction; import io.fabric8.kubernetes.client.utils.Serialization; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; @@ -31,27 +33,31 @@ import com.fasterxml.jackson.core.JsonProcessingException; import io.fabric8.kubernetes.api.model.HTTPHeader; import io.fabric8.kubernetes.api.model.Probe; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.jupiter.params.provider.Arguments.arguments; /** * @author Clement Escoffier */ -public class VertxHealthCheckEnricherTest { +class VertxHealthCheckEnricherTest { private JKubeEnricherContext context; private TreeMap plexusMavenConfig; private Map jKubePluginConfiguration; private Properties properties; - @Before - public void setUp() throws Exception { + @BeforeEach + void setUp() { plexusMavenConfig = new TreeMap<>(); jKubePluginConfiguration = new HashMap<>(); properties = new Properties(); @@ -64,57 +70,214 @@ public void setUp() throws Exception { .build(); } + @Nested + @DisplayName("with TCP health type") + class TCPHealthType { + @Test + @DisplayName("and port using properties, should enable probes with configured port") + void andPortUsingProperties_shouldConfigureProbesWithPort() { + properties.put("vertx.health.type", "tcp"); + properties.put("vertx.health.port", "1234"); + properties.put("vertx.health.readiness.port", "1235"); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertTCPSocket(livenessProbe, 1234); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertTCPSocket(readinessProbe, 1235); + } + + @Test + @DisplayName("and port using config, should enable probes with configured port") + void andPortUsingConfig_shouldConfigureProbesWithPort() throws Exception { + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + final String config = "{\"type\":\"tcp\",\"liveness\":{\"port\":\"1234\"},\"readiness\":{\"port\":\"1235\"}}"; + jKubePluginConfiguration.putAll(createFakeConfig(config)); + plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertTCPSocket(livenessProbe, 1234); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertTCPSocket(readinessProbe, 1235); + } + + @Test + @DisplayName("and port name using properties, should enable probes with configured port name") + void andPortNameUsingProperties_shouldConfigureProbesWithPortName() { + properties.put("vertx.health.type", "tcp"); + properties.put("vertx.health.port-name", "health"); + properties.put("vertx.health.readiness.port-name", "ready"); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket.port.strVal", "health"); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket.port.strVal", "ready"); + } + + @Test + @DisplayName("and port name using config, should enable probes with configured port name") + void andPortNameUsingConfig_shouldConfigureProbesWithPortName() throws Exception { + final String config = "{\"type\":\"tcp\",\"liveness\":{\"port-name\":\"health\"},\"readiness\":{\"port-name\":\"ready\"}}"; + jKubePluginConfiguration.putAll(createFakeConfig(config)); + plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket.port.strVal", "health"); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket.port.strVal", "ready"); + } + + @Test + @DisplayName("and without liveness probe using properties, should disable liveness probe") + void andWithoutLivenessProbeUsingProperties_shouldDisableLiveness() { + properties.put("vertx.health.type", "tcp"); + properties.put("vertx.health.readiness.port", "1235"); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNull(); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertTCPSocket(readinessProbe, 1235); + } + + @Test + @DisplayName("and without liveness probe using config, should disable liveness probe") + void andWithoutLivenessProbeUsingConfig_shouldDisableLiveness() throws Exception { + final String config = "{\"type\":\"tcp\",\"readiness\":{\"port\":\"1235\"}}"; + jKubePluginConfiguration.putAll(createFakeConfig(config)); + plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNull(); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertTCPSocket(readinessProbe, 1235); + } + + @Test + @DisplayName("and negative readiness port using properties, should disable readiness probe") + void andNegativeReadinessPortUsingProperties_shouldDisableReadiness() { + properties.put("vertx.health.type", "tcp"); + properties.put("vertx.health.port", "1337"); + properties.put("vertx.health.readiness.port", "-1"); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertTCPSocket(livenessProbe, 1337); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNull(); + } + + @Test + @DisplayName("and negative readiness port using config, should disable readiness probe") + void andNegativeReadinessPortUsingConfig_shouldDisableReadiness() throws Exception { + final String config = "{\"type\":\"tcp\",\"liveness\":{\"port\":\"1235\"},\"readiness\":{\"port\":\"-1\"}}"; + jKubePluginConfiguration.putAll(createFakeConfig(config)); + plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertTCPSocket(livenessProbe, 1235); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNull(); + } + + @Test + @DisplayName("port and port name using properties, should throw exception") + void portAndPortNameUsingProperties_shouldThrowException() { + properties.put("vertx.health.type", "tcp"); + properties.put("vertx.health.port", "1235"); + properties.put("vertx.health.port-name", "health"); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + assertThatIllegalArgumentException() + .isThrownBy(enricher::getLivenessProbe) + .withMessageContaining("Invalid health check configuration"); + + assertThatIllegalArgumentException() + .isThrownBy(enricher::getReadinessProbe) + .withMessageStartingWith("Invalid health check configuration"); + } + + @Test + @DisplayName("port and port-name using config, should throw exception") + void portAndPortNameUsingConfig_shouldThrowException() throws Exception { + final String config = "{\"type\":\"tcp\"," + + "\"liveness\":{\"port\":\"1234\",\"port-name\":\"foo\"}," + + "\"readiness\":{\"port\":\"1235\",\"port-name\":\"foo\"}}"; + jKubePluginConfiguration.putAll(createFakeConfig(config)); + plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); + + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + assertThatIllegalArgumentException() + .isThrownBy(enricher::getLivenessProbe) + .withMessageStartingWith("Invalid health check configuration"); + + assertThatIllegalArgumentException() + .isThrownBy(enricher::getReadinessProbe) + .withMessageStartingWith("Invalid health check configuration"); + } + + } + @Test - public void testDefaultConfiguration() { + @DisplayName("with empty configuration, should disable probes") + void withEmptyConfig_shouldDisableProbes() { VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); + assertNoLivenessReadinessProbes(enricher); } @Test - public void testDefaultConfiguration_Enabled() { + @DisplayName("default configuration with path, should enable probes with configured path") + void defaultConfigurationWithPath_shouldEnableProbes() { properties.put("vertx.health.path", "/ping"); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertNull(probe.getHttpGet().getHost()); - assertThat(probe.getHttpGet().getScheme()).isEqualTo("HTTP"); - assertThat(probe.getHttpGet().getPort().getIntVal().intValue()).isEqualTo(8080); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/ping"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertHTTPGet(livenessProbe, "HTTP", 8080, "/ping"); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getScheme()).isEqualTo("HTTP"); - assertNull(probe.getHttpGet().getHost()); - assertThat(probe.getHttpGet().getPort().getIntVal().intValue()).isEqualTo(8080); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/ping"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertHTTPGet(readinessProbe, "HTTP", 8080, "/ping"); } @Test - public void testDifferentPathForLivenessAndReadiness() { + @DisplayName("with different paths using properties, should enable probes with different paths") + void differentPathForLivenessAndReadinessProbes() { properties.put("vertx.health.path", "/ping"); properties.put("vertx.health.readiness.path", "/ready"); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertNull(probe.getHttpGet().getHost()); - assertThat(probe.getHttpGet().getScheme()).isEqualTo("HTTP"); - assertThat(probe.getHttpGet().getPort().getIntVal().intValue()).isEqualTo(8080); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/ping"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertHTTPGet(livenessProbe, "HTTP", 8080, "/ping"); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getScheme()).isEqualTo("HTTP"); - assertNull(probe.getHttpGet().getHost()); - assertThat(probe.getHttpGet().getPort().getIntVal().intValue()).isEqualTo(8080); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/ready"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertHTTPGet(readinessProbe, "HTTP", 8080, "/ready"); } @SuppressWarnings("unchecked") @@ -143,414 +306,225 @@ private Map createFakeConfig(String config) throws JsonProcessin } @Test - public void testWithCustomConfigurationComingFromProcessorConf() throws Exception { + @DisplayName("with custom configuration coming from processor, should enable probes with config") + void withCustomConfigurationComingFromProcessorConf() throws Exception { String configString = "{\"path\":\"health\",\"port\":\"1234\",\"scheme\":\"https\"}"; plexusMavenConfig.putAll(createFakeConfigLikeMaven(configString)); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertNull(probe.getHttpGet().getHost()); - assertThat(probe.getHttpGet().getScheme()).isEqualTo("HTTPS"); - assertThat(probe.getHttpGet().getPort().getIntVal().intValue()).isEqualTo(1234); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/health"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertHTTPGet(livenessProbe, "HTTPS", 1234, "/health"); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getScheme()).isEqualTo("HTTPS"); - assertNull(probe.getHttpGet().getHost()); - assertThat(probe.getHttpGet().getPort().getIntVal().intValue()).isEqualTo(1234); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/health"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertHTTPGet(readinessProbe, "HTTPS", 1234, "/health"); } @Test - public void testWithCustomConfigurationForLivenessAndReadinessComingFromConf() throws Exception { + @DisplayName("with custom configuration for liveness and readiness probe coming from config, should enable probes with config") + void withCustomConfigurationForLivenessAndReadinessComingFromConf() throws Exception { final String config = "{\"path\":\"health\",\"port\":\"1234\",\"scheme\":\"https\",\"readiness\":{\"path\":\"/ready\"}}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + + Probe livenessProbe = enricher.getLivenessProbe(); + assertHTTPGet(livenessProbe, "HTTPS", 1234, "/health"); - assertThat(enricher.getLivenessProbe()) - .hasFieldOrPropertyWithValue("httpGet.host", null) - .hasFieldOrPropertyWithValue("httpGet.scheme", "HTTPS") - .hasFieldOrPropertyWithValue("httpGet.port.intVal", 1234) - .hasFieldOrPropertyWithValue("httpGet.path", "/health"); - assertThat(enricher.getReadinessProbe()) - .hasFieldOrPropertyWithValue("httpGet.host", null) - .hasFieldOrPropertyWithValue("httpGet.scheme", "HTTPS") - .hasFieldOrPropertyWithValue("httpGet.port.intVal", 1234) - .hasFieldOrPropertyWithValue("httpGet.path", "/ready"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertHTTPGet(readinessProbe, "HTTPS", 1234, "/ready"); } @Test - public void testCustomConfiguration() { + @DisplayName("with custom configuration coming from properties, should enable probes with config") + void withCustomConfigurationFromProperties() { properties.put("vertx.health.path", "/health"); properties.put("vertx.health.port", " 8081 "); properties.put("vertx.health.scheme", " https"); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertNull(probe.getHttpGet().getHost()); - assertThat(probe.getHttpGet().getScheme()).isEqualToIgnoringCase("https"); - assertThat(probe.getHttpGet().getPort().getIntVal().intValue()).isEqualTo(8081); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/health"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertHTTPGet(livenessProbe, "HTTPS", 8081, "/health"); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getScheme()).isEqualToIgnoringCase("https"); - assertNull(probe.getHttpGet().getHost()); - assertThat(probe.getHttpGet().getPort().getIntVal().intValue()).isEqualTo(8081); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/health"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertHTTPGet(readinessProbe, "HTTPS", 8081, "/health"); } @Test - public void testWithHttpHeaders() throws Exception { + @DisplayName("with HTTP headers, should enable probes with headers") + void withHttpHeaders() throws Exception { final String config = "{\"path\":\"health\",\"headers\":{\"X-Header\":\"X\",\"Y-Header\":\"Y\"}}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/health"); - assertThat(probe.getHttpGet().getPort().getIntVal()).isEqualTo(8080); - assertThat(probe.getHttpGet().getHttpHeaders()).hasSize(2) + Probe livenessProbe = enricher.getLivenessProbe(); + assertHTTPGet(livenessProbe, "HTTP", 8080, "/health"); + assertThat(livenessProbe.getHttpGet().getHttpHeaders()).hasSize(2) .contains(new HTTPHeader("X-Header", "X"), new HTTPHeader("Y-Header", "Y")); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getPath()).isEqualTo( "/health"); - assertThat(probe.getHttpGet().getPort().getIntVal()).isEqualTo(8080); - assertThat(probe.getHttpGet().getHttpHeaders()).hasSize(2) + Probe readinessProbe = enricher.getReadinessProbe(); + assertHTTPGet(readinessProbe, "HTTP", 8080, "/health"); + assertThat(readinessProbe.getHttpGet().getHttpHeaders()).hasSize(2) .contains(new HTTPHeader("X-Header", "X"), new HTTPHeader("Y-Header", "Y")); } @Test - public void testDisabledUsingEmptyPath() { - properties.put("vertx.health.path", ""); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); - } - - @Test - public void testDisabledUsingNegativePort() { + @DisplayName("with negative port using property, should disable probes") + void withNegativePort_shouldDisableProbes() { properties.put("vertx.health.port", " -1 "); properties.put("vertx.health.path", " /ping "); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); + assertNoLivenessReadinessProbes(enricher); } @Test - public void testDisabledUsingInvalidPort() { + @DisplayName("with invalid port using property, should throw exception") + void withInvalidPort_shouldThrowException() { properties.put("vertx.health.port", "not an integer"); properties.put("vertx.health.path", " /ping "); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - try { - enricher.getLivenessProbe(); - fail("Illegal configuration not detected"); - } catch (Exception e) { - // OK. - } + assertThatExceptionOfType(NumberFormatException.class) + .isThrownBy(enricher::getLivenessProbe) + .withMessageContaining("not an integer"); - try { - enricher.getReadinessProbe(); - fail("Illegal configuration not detected"); - } catch (Exception e) { - // OK. - } + assertThatExceptionOfType(NumberFormatException.class) + .isThrownBy(enricher::getReadinessProbe) + .withMessageContaining("not an integer"); } @Test - public void testDisabledUsingPortName() { + @DisplayName("with port name, should disable probes ") + void withPortName_shouldDisableProbes() { properties.put("vertx.health.port-name", " health "); properties.put("vertx.health.path", " /ping "); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertThat(probe.getHttpGet().getPort().getStrVal()).isEqualToIgnoringCase("health"); - probe = enricher.getReadinessProbe(); - assertThat(probe.getHttpGet().getPort().getStrVal()).isEqualToIgnoringCase("health"); - } - - @Test - public void testDisabledUsingNegativePortUsingConfiguration() throws Exception { - final String config = "{\"path\":\"/ping\",\"port\":\"-1\"}"; - jKubePluginConfiguration.putAll(createFakeConfig(config)); - plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe.getHttpGet().getPort().getStrVal()).isEqualToIgnoringCase("health"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe.getHttpGet().getPort().getStrVal()).isEqualToIgnoringCase("health"); } - @Test - public void testReadinessDisabled() { - properties.put("vertx.health.readiness.path", ""); - properties.put("vertx.health.path", "/ping"); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getPath()).isEqualTo("/ping"); - probe = enricher.getReadinessProbe(); - assertNull(probe); - } - - @Test - public void testReadinessDisabledUsingConfig() throws Exception { - final String config = "{\"readiness\":{\"path\":\"\"},\"path\":\"/ping\"}"; - jKubePluginConfiguration.putAll(createFakeConfig(config)); - plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); - + @DisplayName("using invalid properties") + @ParameterizedTest(name = "{index}: with ''{0}'' should disable probes") + @MethodSource("invalidProperties") + void withInvalidProperties_shouldDisableProbes(String testDesc, String property, String propVal) { + // Given + properties.put(property, propVal); + // When VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getPath()).isEqualTo("/ping"); - probe = enricher.getReadinessProbe(); - assertNull(probe); + // Then + assertNoLivenessReadinessProbes(enricher); } - @Test - public void testLivenessDisabledAndReadinessEnabled() { - properties.put("vertx.health.readiness.path", "/ping"); - properties.put("vertx.health.path", ""); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getPath()).isEqualTo("/ping"); - + static Stream invalidProperties() { + return Stream.of( + arguments("empty health path", "vertx.health.path", ""), + arguments("TCP health type", "vertx.health.type", "tcp"), + arguments("exec health type", "vertx.health.type", "exec")); } - @Test - public void testLivenessDisabledAndReadinessEnabledUsingConfig() throws Exception { - final String config = "{\"readiness\":{\"path\":\"/ping\"},\"path\":\"\"}"; + @DisplayName("using invalid config") + @ParameterizedTest(name = "{index}: with ''{0}'' should disable probes") + @MethodSource("invalidConfigs") + void withInvalidConfigs_shouldDisableProbes(String testDesc, String config) throws Exception { + // Given jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); + // When VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getHttpGet().getPath()).isEqualTo("/ping"); - } - - @Test - public void testTCPSocketUsingUserProperties() { - properties.put("vertx.health.type", "tcp"); - properties.put("vertx.health.port", "1234"); - properties.put("vertx.health.readiness.port", "1235"); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertThat(probe.getTcpSocket().getPort().getIntVal().intValue()).isEqualTo(1234); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getTcpSocket().getPort().getIntVal().intValue()).isEqualTo(1235); + // Then + assertNoLivenessReadinessProbes(enricher); } - @Test - public void testTCPSocketUsingConfig() throws Exception { - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - final String config = "{\"type\":\"tcp\",\"liveness\":{\"port\":\"1234\"},\"readiness\":{\"port\":\"1235\"}}"; - jKubePluginConfiguration.putAll(createFakeConfig(config)); - plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); - - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertThat(probe.getTcpSocket().getPort().getIntVal().intValue()).isEqualTo(1234); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getTcpSocket().getPort().getIntVal().intValue()).isEqualTo(1235); + static Stream invalidConfigs() { + return Stream.of( + arguments("negative port", "{\"path\":\"/ping\",\"port\":\"-1\"}"), + arguments("TCP health type", "{\"type\":\"tcp\"}"), + arguments("exec health type", "{\"type\":\"exec\"}")); } @Test - public void testTCPSocketUsingUserPropertiesAndPortName() { - properties.put("vertx.health.type", "tcp"); - properties.put("vertx.health.port-name", "health"); - properties.put("vertx.health.readiness.port-name", "ready"); + @DisplayName("with empty readiness and non-empty health path using properties, should disable readiness probe") + void withEmptyReadinessAndNonEmptyHealthPathUsingProperties_shouldDisableReadiness() { + properties.put("vertx.health.readiness.path", ""); + properties.put("vertx.health.path", "/ping"); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertThat(probe.getTcpSocket().getPort().getStrVal()).isEqualTo( "health"); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getTcpSocket().getPort().getStrVal()).isEqualTo( "ready"); - } - - @Test - public void testTCPSocketUsingConfigAndPortName() throws Exception { - final String config = "{\"type\":\"tcp\",\"liveness\":{\"port-name\":\"health\"},\"readiness\":{\"port-name\":\"ready\"}}"; - jKubePluginConfiguration.putAll(createFakeConfig(config)); - plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .hasFieldOrPropertyWithValue("httpGet.path", "/ping"); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertThat(probe.getTcpSocket().getPort().getStrVal()).isEqualTo( "health"); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getTcpSocket().getPort().getStrVal()).isEqualTo( "ready"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNull(); } @Test - public void testTCPSocketUsingUserPropertiesLivenessDisabled() { - properties.put("vertx.health.type", "tcp"); - properties.put("vertx.health.readiness.port", "1235"); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertEquals(1235, probe.getTcpSocket().getPort().getIntVal().intValue()); - } - - @Test - public void testTCPSocketUsingConfigLivenessDisabled() throws Exception { - final String config = "{\"type\":\"tcp\",\"readiness\":{\"port\":\"1235\"}}"; + @DisplayName("with empty readiness and non-empty health path using config, should disable readiness probe") + void withEmptyReadinessAndNonEmptyHealthPathUsingConfig_shouldDisableReadiness() throws Exception { + final String config = "{\"readiness\":{\"path\":\"\"},\"path\":\"/ping\"}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertEquals(1235, probe.getTcpSocket().getPort().getIntVal().intValue()); - } - - @Test - public void testTCPSocketUsingUserPropertiesReadinessDisabled() { - properties.put("vertx.health.type", "tcp"); - properties.put("vertx.health.port", "1337"); - properties.put("vertx.health.readiness.port", "0"); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .hasFieldOrPropertyWithValue("httpGet.path", "/ping"); - Probe probe = enricher.getLivenessProbe(); - assertEquals(1337, probe.getTcpSocket().getPort().getIntVal().intValue()); - assertNotNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNull(); } @Test - public void testTCPSocketUsingConfigReadinessDisabled() throws Exception { - final String config = "{\"type\":\"tcp\",\"liveness\":{\"port\":\"1235\"},\"readiness\":{\"port\":\"-1\"}}"; - jKubePluginConfiguration.putAll(createFakeConfig(config)); - plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); + @DisplayName("with empty health path and non-empty readiness path using properties, should disable liveness and enable readiness probe") + void withEmptyHealthAndNonEmptyReadinessPathUsingProperties_shouldDisableLivenessAndEnableReadiness() { + properties.put("vertx.health.readiness.path", "/ping"); + properties.put("vertx.health.path", ""); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertEquals(1235, probe.getTcpSocket().getPort().getIntVal().intValue()); - assertNotNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); - } - - @Test(expected = IllegalArgumentException.class) - public void testTCPSocketUsingUserPropertiesIllegal() { - properties.put("vertx.health.type", "tcp"); - properties.put("vertx.health.port", "1235"); - properties.put("vertx.health.port-name", "health"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNull(); - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - enricher.getLivenessProbe(); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .hasFieldOrPropertyWithValue("httpGet.path", "/ping"); } @Test - public void testTCPSocketUsingConfigIllegal() throws Exception { - final String config = "{\"type\":\"tcp\"," + - "\"liveness\":{\"port\":\"1234\",\"port-name\":\"foo\"}," + - "\"readiness\":{\"port\":\"1235\",\"port-name\":\"foo\"}}"; + @DisplayName("with empty health path and non-empty readiness path using config, should disable liveness and enable readiness probe") + void withEmptyHealthAndNonEmptyReadinessPathUsingConfig_shouldDisableLivenessAndEnableReadiness() throws Exception { + final String config = "{\"readiness\":{\"path\":\"/ping\"},\"path\":\"\"}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - try { - enricher.getLivenessProbe(); - fail("Illegal configuration not detected"); - } catch (Exception e) { - // OK. - } - - try { - enricher.getReadinessProbe(); - fail("Illegal configuration not detected"); - } catch (Exception e) { - // OK. - } - } - - - @Test - public void testTCPSocketUsingUserPropertiesDisabled() { - properties.put("vertx.health.type", "tcp"); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); - } - - @Test - public void testTCPSocketUsingConfigDisabled() throws Exception { - final String config = "{\"type\":\"tcp\"}"; - jKubePluginConfiguration.putAll(createFakeConfig(config)); - plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNull(); - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .hasFieldOrPropertyWithValue("httpGet.path", "/ping"); } @Test - public void testExecUsingConfig() throws Exception { + @DisplayName("with exec type using config, should enable probes with configured exec") + void withExecTypeUsingConfig_shouldConfigureProbesWithExec() throws Exception { final String config = "{\"type\":\"exec\"," + "\"command\": {\"arg\":[\"/bin/sh\", \"-c\",\"touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600\"]}" + "}"; @@ -559,16 +533,24 @@ public void testExecUsingConfig() throws Exception { VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertThat(probe.getExec().getCommand()).hasSize(3); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getExec().getCommand()).hasSize(3); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .extracting(Probe::getExec) + .extracting(ExecAction::getCommand) + .asList() + .hasSize(3); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .extracting(Probe::getExec) + .extracting(ExecAction::getCommand) + .asList() + .hasSize(3); } @Test - public void testExecUsingConfigLivenessDisabled() throws Exception { + @DisplayName("with exec type and readiness using config, should disable liveness probe") + void withExecTypeReadinessUsingConfig_shouldDisableLiveness() throws Exception { final String config = "{\"type\":\"exec\"," + "\"readiness\":{" + "\"command\": {\"arg\":[\"/bin/sh\", \"-c\",\"touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600\"]}}," + @@ -579,15 +561,19 @@ public void testExecUsingConfigLivenessDisabled() throws Exception { VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertThat(probe.getExec().getCommand()).hasSize(3); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNull(); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .extracting(Probe::getExec) + .extracting(ExecAction::getCommand).asList() + .hasSize(3); } @Test - public void testExecUsingConfigReadinessDisabled() throws Exception { + @DisplayName("with exec type and liveness using config, should disable readiness probe") + void withExecTypeLivenessUsingConfig_shouldDisableReadiness() throws Exception { final String config = "{\"type\":\"exec\"," + "\"liveness\":{" + "\"command\": {\"arg\":[\"/bin/sh\", \"-c\",\"touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600\"]}}," + @@ -598,112 +584,77 @@ public void testExecUsingConfigReadinessDisabled() throws Exception { VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertThat(probe.getExec().getCommand()).hasSize(3); - assertNotNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); - } - - @Test - public void testExecUsingUserPropertiesDisabled() { - properties.put("vertx.health.type", "exec"); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .extracting(Probe::getExec) + .extracting(ExecAction::getCommand).asList() + .hasSize(3); - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNull(); } @Test - public void testExecUsingConfigDisabled() throws Exception { - final String config = "{\"type\":\"exec\"}"; - jKubePluginConfiguration.putAll(createFakeConfig(config)); - plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); - - VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); - } - - @Test - public void testUnknownTypeUsingUserProperties() { + @DisplayName("with invalid type using properties, should throw exception") + void withInvalidTypeUsingProperties_shouldThrowException() { properties.put("vertx.health.type", "not a valid type"); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - try { - enricher.getLivenessProbe(); - fail("Illegal configuration not detected"); - } catch (Exception e) { - // OK. - } + assertThatIllegalArgumentException() + .isThrownBy(enricher::getLivenessProbe) + .withMessageContaining("Invalid health check configuration"); - try { - enricher.getReadinessProbe(); - fail("Illegal configuration not detected"); - } catch (Exception e) { - // OK. - } + assertThatIllegalArgumentException() + .isThrownBy(enricher::getReadinessProbe) + .withMessageContaining("Invalid health check configuration"); } - @Test - public void testUnknownTypeUsingConfig() throws Exception { + @DisplayName("with invalid type using config, should throw exception") + void withInvalidTypeUsingConfig_shouldThrowException() throws Exception { final String config = "{\"type\":\"not a valid type\"}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - try { - enricher.getLivenessProbe(); - fail("Illegal configuration not detected"); - } catch (Exception e) { - // OK. - } + assertThatIllegalArgumentException() + .isThrownBy(enricher::getLivenessProbe) + .withMessageContaining("Invalid health check configuration"); - try { - enricher.getReadinessProbe(); - fail("Illegal configuration not detected"); - } catch (Exception e) { - // OK. - } + assertThatIllegalArgumentException() + .isThrownBy(enricher::getReadinessProbe) + .withMessageContaining("Invalid health check configuration"); } @Test - public void testNotApplicableProject() { + @DisplayName("with no project, vertx enricher should not be applicable") + void witNoPlugin_enricherShouldNotBeApplicable() { VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); context.getProject().setPlugins(Collections.emptyList()); - Probe probe = enricher.getLivenessProbe(); - assertNull(probe); - probe = enricher.getReadinessProbe(); - assertNull(probe); + assertNoLivenessReadinessProbes(enricher); } @Test - public void testApplicableProjectWithGradle() { + @DisplayName("with gradle project, vertx enricher should be applicable") + void withGradlePlugin_enricherShouldBeApplicable() { properties.put("vertx.health.path", "/ping"); context = context.toBuilder() .project(createNewJavaProjectWithVertxPlugin("io.vertx", "io.vertx.vertx-plugin")) .build(); VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - - Probe probe = enricher.getLivenessProbe(); - assertThat(probe).isNotNull(); - probe = enricher.getReadinessProbe(); - assertThat(probe).isNotNull(); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull(); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull(); } @Test - public void testThatWeCanUSeDifferentTypesForLivenessAndReadiness() throws Exception { + @DisplayName("with different types for liveness and readiness probes, should enable probes with configured types") + void withDifferentTypesForLivenessAndReadiness_shouldConfigureProbesWithConfiguredTypes() throws Exception { final String config = "{\"liveness\":{" + "\"type\":\"exec\",\"command\":{\"arg\":\"ls\"}" + "},\"readiness\":{\"path\":\"/ping\"}}"; @@ -712,16 +663,20 @@ public void testThatWeCanUSeDifferentTypesForLivenessAndReadiness() throws Excep VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getLivenessProbe(); - assertNotNull(probe); - assertNotNull(probe.getExec()); - probe = enricher.getReadinessProbe(); - assertNotNull(probe); - assertNotNull(probe.getHttpGet()); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .extracting(Probe::getExec) + .isNotNull(); + + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .extracting(Probe::getHttpGet) + .isNotNull(); } @Test - public void testThatSpecificConfigOverrideGenericUserProperties() throws Exception { + @DisplayName("with specific config, should override generic user properties") + void specificConfigOverrideGenericUserProperties() throws Exception { final String config = "{\"liveness\":{" + "\"type\":\"exec\",\"command\":{\"arg\":\"ls\"}" + "},\"readiness\":{\"port\":\"1337\"}}"; @@ -733,21 +688,22 @@ public void testThatSpecificConfigOverrideGenericUserProperties() throws Excepti VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getReadinessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getTcpSocket()).isNotNull(); - assertThat(probe.getHttpGet()).isNull(); - assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1337); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe.getHttpGet()).isNull(); + assertTCPSocket(readinessProbe, 1337); - probe = enricher.getLivenessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getTcpSocket()).isNull(); - assertThat(probe.getExec()).isNotNull(); - assertThat(probe.getExec().getCommand().iterator().next()).isEqualTo("ls"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket", null) + .extracting(Probe::getExec).isNotNull() + .extracting(ExecAction::getCommand).asList() + .singleElement() + .isEqualTo("ls"); } @Test - public void testThatGenericConfigOverrideGenericUserProperties() throws Exception { + @DisplayName("with generic config, should override generic user properties") + void withGenericConfig_shouldOverrideGenericUserProperties() throws Exception { final String config = "{\"type\":\"exec\",\"command\":{\"arg\":\"ls\"}}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); @@ -756,21 +712,26 @@ public void testThatGenericConfigOverrideGenericUserProperties() throws Exceptio VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getReadinessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getTcpSocket()).isNull(); - assertThat(probe.getExec()).isNotNull(); - assertThat(probe.getExec().getCommand().iterator().next()).isEqualTo("ls"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket", null) + .extracting(Probe::getExec).isNotNull() + .extracting(ExecAction::getCommand).asList() + .singleElement() + .isEqualTo("ls"); - probe = enricher.getLivenessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getTcpSocket()).isNull(); - assertThat(probe.getExec()).isNotNull(); - assertThat(probe.getExec().getCommand().iterator().next()).isEqualTo("ls"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket", null) + .extracting(Probe::getExec).isNotNull() + .extracting(ExecAction::getCommand).asList() + .singleElement() + .isEqualTo("ls"); } @Test - public void testThatSpecificConfigOverrideSpecificUserProperties() throws Exception { + @DisplayName("with specific config, should override specific user properties") + void withSpecificConfig_shouldOverrideSpecificUserProperties() throws Exception { final String config = "{\"liveness\":{\"type\":\"http\",\"path\":\"/ping\"},\"readiness\":{\"port\":\"1337\"}}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); @@ -780,21 +741,21 @@ public void testThatSpecificConfigOverrideSpecificUserProperties() throws Except VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getReadinessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getTcpSocket()).isNotNull(); - assertThat(probe.getHttpGet()).isNull(); - assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1337); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe.getHttpGet()).isNull(); + assertTCPSocket(readinessProbe, 1337); - probe = enricher.getLivenessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getHttpGet()).isNotNull(); - assertThat(probe.getTcpSocket()).isNull(); - assertThat(probe.getHttpGet().getPath()).isEqualTo("/ping"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket", null) + .extracting(Probe::getHttpGet) + .isNotNull() + .hasFieldOrPropertyWithValue("path", "/ping"); } @Test - public void testThatSpecificUserPropertiesOverrideGenericConfig() throws Exception { + @DisplayName("with specific user properties, should override generic config") + void withSpecificUserProperties_shouldOverrideGenericConfig() throws Exception { final String config = "{\"path\":\"/ping\",\"type\":\"http\"}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); @@ -804,21 +765,21 @@ public void testThatSpecificUserPropertiesOverrideGenericConfig() throws Excepti VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getReadinessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getTcpSocket()).isNotNull(); - assertThat(probe.getHttpGet()).isNull(); - assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1234); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe.getHttpGet()).isNull(); + assertTCPSocket(readinessProbe, 1234); - probe = enricher.getLivenessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getHttpGet()).isNotNull(); - assertThat(probe.getTcpSocket()).isNull(); - assertThat(probe.getHttpGet().getPort().getIntVal()).isEqualTo(1235); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .hasFieldOrPropertyWithValue("tcpSocket", null) + .extracting(Probe::getHttpGet) + .isNotNull() + .hasFieldOrPropertyWithValue("port.intVal", 1235); } @Test - public void testThatSpecificConfigOverrideGenericConfig() throws Exception { + @DisplayName("with specific config, should override generic config") + void withSpecificConfig_shouldOverrideGenericConfig() throws Exception { final String config = "{\"liveness\":{\"path\":\"/live\"}," + "\"readiness\":{\"path\":\"/ping\",\"port-name\":\"ready\"}," + "\"path\":\"/health\",\"port-name\":\"health\"}"; @@ -827,21 +788,24 @@ public void testThatSpecificConfigOverrideGenericConfig() throws Exception { VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getReadinessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getHttpGet()).isNotNull(); - assertThat(probe.getHttpGet().getPort().getStrVal()).isEqualTo("ready"); - assertThat(probe.getHttpGet().getPath()).isEqualTo("/ping"); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe).isNotNull() + .extracting(Probe::getHttpGet) + .isNotNull() + .hasFieldOrPropertyWithValue("port.strVal", "ready") + .hasFieldOrPropertyWithValue("path", "/ping"); - probe = enricher.getLivenessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getHttpGet()).isNotNull(); - assertThat(probe.getHttpGet().getPort().getStrVal()).isEqualTo("health"); - assertThat(probe.getHttpGet().getPath()).isEqualTo("/live"); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe).isNotNull() + .extracting(Probe::getHttpGet) + .isNotNull() + .hasFieldOrPropertyWithValue("port.strVal", "health") + .hasFieldOrPropertyWithValue("path", "/live"); } @Test - public void testThatSpecificUserPropertiesOverrideGenericUserProperties() throws Exception { + @DisplayName("with specific user properties, should override generic user properties") + void withSpecificUserProperties_shouldOverrideGenericUserProperties() throws Exception { final String config = "{\"path\":\"/ping\",\"type\":\"http\"}"; jKubePluginConfiguration.putAll(createFakeConfig(config)); plexusMavenConfig.putAll(createFakeConfigLikeMaven(config)); @@ -853,17 +817,13 @@ public void testThatSpecificUserPropertiesOverrideGenericUserProperties() throws VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context); - Probe probe = enricher.getReadinessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getTcpSocket()).isNotNull(); - assertThat(probe.getHttpGet()).isNull(); - assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1234); + Probe readinessProbe = enricher.getReadinessProbe(); + assertThat(readinessProbe.getHttpGet()).isNull(); + assertTCPSocket(readinessProbe, 1234); - probe = enricher.getLivenessProbe(); - assertThat(probe).isNotNull(); - assertThat(probe.getHttpGet()).isNull(); - assertThat(probe.getTcpSocket()).isNotNull(); - assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1236); + Probe livenessProbe = enricher.getLivenessProbe(); + assertThat(livenessProbe.getHttpGet()).isNull(); + assertTCPSocket(livenessProbe, 1236); } private JavaProject createNewJavaProjectWithVertxPlugin(String vertxGroup, String vertxArtifact) { @@ -881,4 +841,26 @@ private JavaProject createNewJavaProjectWithVertxPlugin(String vertxGroup, Strin .build(); } + private void assertNoLivenessReadinessProbes(VertxHealthCheckEnricher enricher) { + assertThat(enricher) + .returns(null, VertxHealthCheckEnricher::getLivenessProbe) + .returns(null, VertxHealthCheckEnricher::getReadinessProbe); + } + + private void assertHTTPGet(Probe probe, String scheme, int port, String path) { + assertThat(probe).isNotNull() + .extracting(Probe::getHttpGet) + .hasFieldOrPropertyWithValue("host", null) + .hasFieldOrPropertyWithValue("scheme", scheme) + .hasFieldOrPropertyWithValue("port.intVal", port) + .hasFieldOrPropertyWithValue("path", path); + } + + private void assertTCPSocket(Probe probe, int port) { + assertThat(probe).isNotNull() + .extracting(Probe::getTcpSocket) + .isNotNull() + .hasFieldOrPropertyWithValue("port.intVal", port); + } + } diff --git a/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxGeneratorIsApplicableTest.java b/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxGeneratorIsApplicableTest.java index f43fd6ded9..420f7a7466 100644 --- a/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxGeneratorIsApplicableTest.java +++ b/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxGeneratorIsApplicableTest.java @@ -17,58 +17,44 @@ import org.eclipse.jkube.kit.common.Dependency; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.Plugin; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mockito; -import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.params.provider.Arguments.arguments; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@RunWith(Parameterized.class) -public class VertxGeneratorIsApplicableTest { +class VertxGeneratorIsApplicableTest { private JavaProject project; private GeneratorContext context; - @Before - public void setUp() { + @BeforeEach + void setUp() { project = mock(JavaProject.class, Mockito.RETURNS_DEEP_STUBS); context = mock(GeneratorContext.class, Mockito.RETURNS_DEEP_STUBS); when(context.getProject()).thenReturn(project); } - @Parameterized.Parameters(name = "{0}") - public static Collection data() { - return Arrays.asList( - new Object[] { "Vertx generator SHOULD NOT be applicable if there is no plugin nor dependency", Collections.emptyList(), Collections.emptyList(), false}, - new Object[] { "Vertx generator SHOULD be applicable if there is vertx maven plugin", Collections.singletonList(Plugin.builder().groupId("io.reactiverse").artifactId("vertx-maven-plugin").build()), Collections.emptyList(), true}, - new Object[] { "Vertx generator SHOULD be applicable if there is vertx gradle plugin", Collections.singletonList(Plugin.builder().groupId("io.vertx").artifactId("io.vertx.vertx-plugin").build()), Collections.emptyList(), true}, - new Object[] { "Vertx generator SHOULD be applicable if there is dependency with io.vertx groupId", Collections.emptyList(), Collections.singletonList(Dependency.builder().groupId("io.vertx").build()), true} + static Stream data() { + return Stream.of( + arguments("Vertx generator SHOULD NOT be applicable if there is no plugin nor dependency", Collections.emptyList(), Collections.emptyList(), false), + arguments("Vertx generator SHOULD be applicable if there is vertx maven plugin", Collections.singletonList(Plugin.builder().groupId("io.reactiverse").artifactId("vertx-maven-plugin").build()), Collections.emptyList(), true), + arguments("Vertx generator SHOULD be applicable if there is vertx gradle plugin", Collections.singletonList(Plugin.builder().groupId("io.vertx").artifactId("io.vertx.vertx-plugin").build()), Collections.emptyList(), true), + arguments("Vertx generator SHOULD be applicable if there is dependency with io.vertx groupId", Collections.emptyList(), Collections.singletonList(Dependency.builder().groupId("io.vertx").build()), true) ); } - @Parameterized.Parameter - public String testDescription; - - @Parameterized.Parameter(1) - public List pluginList; - - @Parameterized.Parameter(2) - public List dependencyList; - - @Parameterized.Parameter(3) - public boolean expectedValue; - - - @Test - public void testIsApplicable() { + @ParameterizedTest(name = "{0}") + @MethodSource("data") + void isApplicable(String testDescription, List pluginList, List dependencyList, boolean expectedValue) { // Given when(project.getPlugins()).thenReturn(pluginList); when(project.getDependencies()).thenReturn(dependencyList); diff --git a/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxGeneratorTest.java b/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxGeneratorTest.java index feb57c4e2b..283a02f9fe 100644 --- a/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxGeneratorTest.java +++ b/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxGeneratorTest.java @@ -24,10 +24,9 @@ import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.generator.api.GeneratorContext; import org.eclipse.jkube.kit.common.util.JKubeProjectUtil; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.mockito.MockedStatic; import static org.assertj.core.api.Assertions.assertThat; @@ -39,10 +38,7 @@ /** * @author Clement Escoffier */ -public class VertxGeneratorTest { - - @Rule - public TemporaryFolder folder = new TemporaryFolder(); +class VertxGeneratorTest { private JavaProject project; @@ -50,12 +46,12 @@ public class VertxGeneratorTest { private Dependency dropwizard; private Dependency core; - @Before - public void init() throws IOException { + @BeforeEach + void init(@TempDir File folder) throws IOException { dropwizard = Dependency.builder().groupId("io.vertx").artifactId("vertx-dropwizard-metrics").version("3.4.2") - .type("jar").scope("compile").file(folder.newFile("vertx-dropwizard-metrics.jar")).build(); + .type("jar").scope("compile").file(File.createTempFile("vertx-dropwizard-metrics", ".jar", folder)).build(); core = Dependency.builder().groupId("io.vertx").artifactId("vertx-core").version("3.4.2").type("jar") - .scope("compile").file(folder.newFile("vertx-core.jar")).build(); + .scope("compile").file(File.createTempFile("vertx-core", ".jar", folder)).build(); project = mock(JavaProject.class, RETURNS_DEEP_STUBS); KitLogger logger = mock(KitLogger.class, RETURNS_DEEP_STUBS); context = GeneratorContext.builder() @@ -65,7 +61,7 @@ public void init() throws IOException { } @Test - public void testDefaultOptions() { + void defaultOptions() { // Given when(project.getOutputDirectory()).thenReturn(new File("target/tmp/target")); // When @@ -75,7 +71,7 @@ public void testDefaultOptions() { } @Test - public void testWithMetrics() { + void withMetrics() { // Given when(project.getDependencies()).thenReturn(Arrays.asList(dropwizard, core)); // When @@ -89,7 +85,7 @@ public void testWithMetrics() { } @Test - public void testWithInfinispanClusterManager() { + void withInfinispanClusterManager() { try (MockedStatic mockedJKubeProjectUtil = mockStatic(JKubeProjectUtil.class)) { // Given mockedJKubeProjectUtil diff --git a/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxPortsExtractorTest.java b/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxPortsExtractorTest.java index 80c6a08013..b5ab7d0ae2 100644 --- a/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxPortsExtractorTest.java +++ b/jkube-kit/jkube-kit-vertx/src/test/java/org/eclipse/jkube/vertx/generator/VertxPortsExtractorTest.java @@ -20,24 +20,26 @@ import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.Plugin; import org.eclipse.jkube.kit.common.PrefixedLogger; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.jkube.kit.common.util.FileUtil.getAbsolutePath; -import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -public class VertxPortsExtractorTest { - PrefixedLogger log; +class VertxPortsExtractorTest { + private PrefixedLogger log; - @Before + @BeforeEach public void setUp() { log = mock(PrefixedLogger.class,RETURNS_DEEP_STUBS); } @Test - public void testVertxConfigPathFromProject() { + @DisplayName("extract vertx configuration path from project, should return configured port") + void extract_vertxConfigPathFromProject_shouldReturnConfiguredPort() { Map vertxConfig = new HashMap<>(); vertxConfig.put("vertxConfig", getAbsolutePath(VertxPortsExtractorTest.class.getResource("/config.json"))); @@ -51,17 +53,18 @@ public void testVertxConfigPathFromProject() { .build(); Map result = new VertxPortsExtractor(log).extract(project); - assertEquals((Integer) 80, result.get("http.port")); + assertThat(result).containsEntry("http.port", 80); } @Test - public void testNoVertxConfiguration() { + @DisplayName("extract with no vertx configuration, ports should be empty") + void extract_withNoVertxConfiguration_shouldBeEmpty() { JavaProject project = JavaProject.builder() .plugins(Collections.singletonList(Plugin.builder().groupId(Constants.VERTX_MAVEN_PLUGIN_GROUP) .artifactId(Constants.VERTX_MAVEN_PLUGIN_ARTIFACT).version("testversion").configuration(Collections.emptyMap()) .build())) .build(); Map result = new VertxPortsExtractor(log).extract(project); - assertEquals(0, result.size()); + assertThat(result).isEmpty(); } }