From af98b2077cb97e04ad2ca499216346ef8f312080 Mon Sep 17 00:00:00 2001 From: Janne Valkealahti <janne.valkealahti@gmail.com> Date: Sun, 20 Jun 2021 13:51:58 +0100 Subject: [PATCH] Migrate to junit5 - Migrate to assertj - Fixes #204 --- .../local/AbstractLocalDeployerSupport.java | 3 +- .../deployer/spi/local/DebugAddressTests.java | 2 +- ...ppDeployerEnvironmentIntegrationTests.java | 76 ++++---- .../LocalAppDeployerIntegrationTests.java | 179 +++++++++--------- .../spi/local/LocalDeployerSupportTests.java | 35 ++-- .../LocalTaskLauncherIntegrationTests.java | 126 +++++++----- .../local/RandomPortRangeContextTests.java | 4 +- 7 files changed, 229 insertions(+), 196 deletions(-) diff --git a/spring-cloud-deployer-local/src/main/java/org/springframework/cloud/deployer/spi/local/AbstractLocalDeployerSupport.java b/spring-cloud-deployer-local/src/main/java/org/springframework/cloud/deployer/spi/local/AbstractLocalDeployerSupport.java index 817b6da..e11b832 100644 --- a/spring-cloud-deployer-local/src/main/java/org/springframework/cloud/deployer/spi/local/AbstractLocalDeployerSupport.java +++ b/spring-cloud-deployer-local/src/main/java/org/springframework/cloud/deployer/spi/local/AbstractLocalDeployerSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 the original author or authors. + * Copyright 2016-2021 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. @@ -25,7 +25,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/DebugAddressTests.java b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/DebugAddressTests.java index 25be021..d9a5840 100644 --- a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/DebugAddressTests.java +++ b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/DebugAddressTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2020 the original author or authors. + * Copyright 2020-2021 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. diff --git a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalAppDeployerEnvironmentIntegrationTests.java b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalAppDeployerEnvironmentIntegrationTests.java index c703695..9e71ae6 100644 --- a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalAppDeployerEnvironmentIntegrationTests.java +++ b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalAppDeployerEnvironmentIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2018-2021 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. @@ -20,14 +20,14 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.ByteBuffer; +import java.time.Duration; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; -import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -37,10 +37,11 @@ import org.springframework.cloud.deployer.resource.docker.DockerResource; import org.springframework.cloud.deployer.spi.app.AppDeployer; import org.springframework.cloud.deployer.spi.app.AppInstanceStatus; import org.springframework.cloud.deployer.spi.app.AppStatus; +import org.springframework.cloud.deployer.spi.app.DeploymentState; import org.springframework.cloud.deployer.spi.core.AppDefinition; import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest; import org.springframework.cloud.deployer.spi.local.LocalAppDeployerEnvironmentIntegrationTests.Config; -import org.springframework.cloud.deployer.spi.test.AbstractAppDeployerIntegrationTests; +import org.springframework.cloud.deployer.spi.test.AbstractAppDeployerIntegrationJUnit5Tests; import org.springframework.cloud.deployer.spi.test.AbstractIntegrationTests; import org.springframework.cloud.deployer.spi.test.Timeout; import org.springframework.context.annotation.Bean; @@ -48,17 +49,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.web.client.RestTemplate; -import static org.hamcrest.CoreMatchers.anyOf; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.springframework.cloud.deployer.spi.app.DeploymentState.deployed; -import static org.springframework.cloud.deployer.spi.app.DeploymentState.unknown; -import static org.springframework.cloud.deployer.spi.test.EventuallyMatcher.eventually; +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; /** * Integration tests for {@link LocalAppDeployer} not using SAJ. @@ -76,7 +68,7 @@ import static org.springframework.cloud.deployer.spi.test.EventuallyMatcher.even @SpringBootTest(classes = { Config.class, AbstractIntegrationTests.Config.class }, value = { "maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot", "spring.cloud.deployer.local.use-spring-application-json=false" }) -public class LocalAppDeployerEnvironmentIntegrationTests extends AbstractAppDeployerIntegrationTests { +public class LocalAppDeployerEnvironmentIntegrationTests extends AbstractAppDeployerIntegrationJUnit5Tests { private static final String TESTAPP_DOCKER_IMAGE_NAME = "springcloud/spring-cloud-deployer-spi-test-app:latest"; @@ -106,7 +98,7 @@ public class LocalAppDeployerEnvironmentIntegrationTests extends AbstractAppDepl // tweak random dir name on win to be shorter String uuid = UUID.randomUUID().toString(); long l = ByteBuffer.wrap(uuid.toString().getBytes()).getLong(); - return name.getMethodName() + Long.toString(l, Character.MAX_RADIX); + return testName + Long.toString(l, Character.MAX_RADIX); } else { return super.randomName(); @@ -128,8 +120,11 @@ public class LocalAppDeployerEnvironmentIntegrationTests extends AbstractAppDepl String deploymentId = appDeployer().deploy(request); Timeout timeout = deploymentTimeout(); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.deployed); + }); Map<String, AppInstanceStatus> instances = appDeployer().status(deploymentId).getInstances(); String url = null; @@ -146,21 +141,24 @@ public class LocalAppDeployerEnvironmentIntegrationTests extends AbstractAppDepl timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); - assertThat(url, notNullValue()); + assertThat(url).isNotNull(); if (LocalDeployerUtils.isWindows()) { // windows is weird, we may still get Path or PATH - assertThat(env, anyOf(containsString("\"Path\""), containsString("\"PATH\""))); + assertThat(env).containsIgnoringCase("path"); } else { - assertThat(env, containsString("\"PATH\"")); + assertThat(env).contains("\"PATH\""); // we're not using SAJ so it's i.e. // INSTANCE_INDEX not instance.index - assertThat(env, containsString("\"INSTANCE_INDEX\"")); - assertThat(env, containsString("\"SPRING_APPLICATION_INDEX\"")); - assertThat(env, containsString("\"SPRING_CLOUD_APPLICATION_GUID\"")); + assertThat(env).contains("\"INSTANCE_INDEX\""); + assertThat(env).contains("\"SPRING_APPLICATION_INDEX\""); + assertThat(env).contains("\"SPRING_CLOUD_APPLICATION_GUID\""); } } @@ -177,15 +175,21 @@ public class LocalAppDeployerEnvironmentIntegrationTests extends AbstractAppDepl String deploymentId = appDeployer().deploy(request); Timeout timeout = deploymentTimeout(); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.deployed); + }); log.info("Undeploying {}...", deploymentId); timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); } @Test @@ -199,11 +203,11 @@ public class LocalAppDeployerEnvironmentIntegrationTests extends AbstractAppDepl AppDeployer deployer = appDeployer(); String deploymentId = deployer.deploy(request); AppStatus appStatus = deployer.status(deploymentId); - assertTrue(appStatus.getInstances().size() > 0); + assertThat(appStatus.getInstances()).hasSizeGreaterThan(0); for (Entry<String, AppInstanceStatus> instanceStatusEntry : appStatus.getInstances().entrySet()) { Map<String, String> attributes = instanceStatusEntry.getValue().getAttributes(); - assertFalse(attributes.containsKey("stdout")); - assertFalse(attributes.containsKey("stderr")); + assertThat(attributes).doesNotContainKey("stdout"); + assertThat(attributes).doesNotContainKey("stderr"); } deployer.undeploy(deploymentId); } @@ -227,12 +231,12 @@ public class LocalAppDeployerEnvironmentIntegrationTests extends AbstractAppDepl try { String containerId = getCommandOutput("docker ps -q --filter ancestor="+ TESTAPP_DOCKER_IMAGE_NAME); String logOutput = getCommandOutput("docker logs "+ containerId); - assertTrue(logOutput.contains("Listening for transport dt_socket at address: 9999")); + assertThat(logOutput).contains("Listening for transport dt_socket at address: 9999"); } catch (IOException e) { } } else { - assertEquals("deploying", appStatus.toString()); + assertThat(appStatus.toString()).contains("deploying"); } deployer.undeploy(deploymentId); diff --git a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalAppDeployerIntegrationTests.java b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalAppDeployerIntegrationTests.java index 883ce28..a9cc4a6 100644 --- a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalAppDeployerIntegrationTests.java +++ b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalAppDeployerIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 the original author or authors. + * Copyright 2016-2021 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. @@ -33,12 +33,8 @@ import java.util.Map.Entry; import java.util.UUID; import java.util.stream.Collectors; -import org.hamcrest.BaseMatcher; -import org.hamcrest.CoreMatchers; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.Matchers; -import org.junit.Test; +import org.junit.jupiter.api.Test; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -51,7 +47,7 @@ import org.springframework.cloud.deployer.spi.app.DeploymentState; import org.springframework.cloud.deployer.spi.core.AppDefinition; import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest; import org.springframework.cloud.deployer.spi.local.LocalAppDeployerIntegrationTests.Config; -import org.springframework.cloud.deployer.spi.test.AbstractAppDeployerIntegrationTests; +import org.springframework.cloud.deployer.spi.test.AbstractAppDeployerIntegrationJUnit5Tests; import org.springframework.cloud.deployer.spi.test.AbstractIntegrationTests; import org.springframework.cloud.deployer.spi.test.Timeout; import org.springframework.context.annotation.Bean; @@ -59,18 +55,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.web.client.RestTemplate; +import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; -import static org.hamcrest.CoreMatchers.anyOf; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.springframework.cloud.deployer.spi.app.DeploymentState.deployed; -import static org.springframework.cloud.deployer.spi.app.DeploymentState.unknown; -import static org.springframework.cloud.deployer.spi.test.EventuallyMatcher.eventually; /** * Integration tests for {@link LocalAppDeployer}. @@ -87,7 +73,7 @@ import static org.springframework.cloud.deployer.spi.test.EventuallyMatcher.even */ @SpringBootTest(classes = {Config.class, AbstractIntegrationTests.Config.class}, value = { "maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot"}) -public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegrationTests { +public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegrationJUnit5Tests { private static final String TESTAPP_DOCKER_IMAGE_NAME = "springcloud/spring-cloud-deployer-spi-test-app:latest"; @@ -117,7 +103,7 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra // tweak random dir name on win to be shorter String uuid = UUID.randomUUID().toString(); long l = ByteBuffer.wrap(uuid.toString().getBytes()).getLong(); - return name.getMethodName() + Long.toString(l, Character.MAX_RADIX); + return testName + Long.toString(l, Character.MAX_RADIX); } else { return super.randomName(); @@ -139,8 +125,11 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra String deploymentId = appDeployer().deploy(request); Timeout timeout = deploymentTimeout(); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.deployed); + }); Map<String, AppInstanceStatus> instances = appDeployer().status(deploymentId).getInstances(); String url = null; @@ -157,22 +146,25 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); - assertThat(url, notNullValue()); + assertThat(url).isNotNull(); if (LocalDeployerUtils.isWindows()) { // windows is weird, we may still get Path or PATH - assertThat(env, anyOf(containsString("\"Path\""), containsString("\"PATH\""))); + assertThat(env).containsIgnoringCase("path"); } else { - assertThat(env, containsString("\"PATH\"")); + assertThat(env).contains("\"PATH\""); // we're defaulting to SAJ so it's i.e. // instance.index not INSTANCE_INDEX - assertThat(env, containsString("\"instance.index\"")); - assertThat(env, containsString("\"spring.application.index\"")); - assertThat(env, containsString("\"spring.cloud.application.guid\"")); - assertThat(env, containsString("\"spring.cloud.stream.instanceIndex\"")); + assertThat(env).contains("\"instance.index\""); + assertThat(env).contains("\"spring.application.index\""); + assertThat(env).contains("\"spring.cloud.application.guid\""); + assertThat(env).contains("\"spring.cloud.stream.instanceIndex\""); } } @@ -186,10 +178,13 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra log.info("Deploying {}...", request.getDefinition().getName()); String deploymentId = appDeployer().deploy(request); Timeout timeout = deploymentTimeout(); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.deployed); + }); String logContent = appDeployer().getLog(deploymentId); - assertThat(logContent, containsString("Starting DeployerIntegrationTestApplication")); + assertThat(logContent).contains("Starting DeployerIntegrationTestApplication"); } // TODO: remove when these two are forced in tck tests @@ -216,15 +211,21 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra String deploymentId = appDeployer().deploy(request); Timeout timeout = deploymentTimeout(); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.deployed); + }); log.info("Undeploying {}...", deploymentId); timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); } @Test @@ -238,11 +239,11 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra AppDeployer deployer = appDeployer(); String deploymentId = deployer.deploy(request); AppStatus appStatus = deployer.status(deploymentId); - assertTrue(appStatus.getInstances().size() > 0); + assertThat(appStatus.getInstances()).hasSizeGreaterThan(0); for (Entry<String, AppInstanceStatus> instanceStatusEntry : appStatus.getInstances().entrySet()) { Map<String, String> attributes = instanceStatusEntry.getValue().getAttributes(); - assertFalse(attributes.containsKey("stdout")); - assertFalse(attributes.containsKey("stderr")); + assertThat(attributes).doesNotContainKey("stdout"); + assertThat(attributes).doesNotContainKey("stderr"); } deployer.undeploy(deploymentId); } @@ -266,12 +267,12 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra try { String containerId = getCommandOutput("docker ps -q --filter ancestor="+ TESTAPP_DOCKER_IMAGE_NAME); String logOutput = getCommandOutput("docker logs "+ containerId); - assertTrue(logOutput.contains("Listening for transport dt_socket at address: 9999")); + assertThat(logOutput).contains("Listening for transport dt_socket at address: 9999"); } catch (IOException e) { } } else { - assertEquals("deploying", appStatus.toString()); + assertThat(appStatus.toString()).contains("deploying"); } deployer.undeploy(deploymentId); @@ -296,14 +297,14 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra try { String containerId = getCommandOutput("docker ps -q --filter ancestor="+ TESTAPP_DOCKER_IMAGE_NAME); String logOutput = getCommandOutput("docker logs "+ containerId); - assertTrue(logOutput.contains("Listening for transport dt_socket at address: 8888")); + assertThat(logOutput).contains("Listening for transport dt_socket at address: 8888"); String containerPorts = getCommandOutputAll("docker port "+ containerId); - assertTrue(containerPorts.contains("8888/tcp -> 0.0.0.0:8888")); + assertThat(containerPorts).contains("8888/tcp -> 0.0.0.0:8888"); } catch (IOException e) { } } else { - assertEquals("deploying", appStatus.toString()); + assertThat(appStatus.toString()).contains("deploying"); } deployer.undeploy(deploymentId); @@ -332,39 +333,23 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra // Deploy String deploymentId = appDeployer.deploy(request); Timeout timeout = deploymentTimeout(); - assertThat(deploymentId, eventually(hasStatusThat( - appDeployer, - Matchers.<AppStatus>hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer.status(deploymentId).getState()).isEqualTo(DeploymentState.deployed); + }); + timeout = undeploymentTimeout(); // Undeploy appDeployer.undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - appDeployer, - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer.status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); List<Path> afterDirs = getAfterPaths(customWorkDirRoot); - assertThat("Additional working directory not created", afterDirs.size(), CoreMatchers.is(beforeDirs.size()+1)); - - } - - protected Matcher<String> hasStatusThat(final AppDeployer appDeployer, final Matcher<AppStatus> statusMatcher) { - return new BaseMatcher<String>() { - private AppStatus status; - - public boolean matches(Object item) { - this.status = appDeployer.status((String)item); - return statusMatcher.matches(this.status); - } - - public void describeMismatch(Object item, Description mismatchDescription) { - mismatchDescription.appendText("status of ").appendValue(item).appendText(" "); - statusMatcher.describeMismatch(this.status, mismatchDescription); - } - - public void describeTo(Description description) { - statusMatcher.describeTo(description); - } - }; + assertThat(afterDirs).as("Additional working directory not created").hasSize(beforeDirs.size() + 1); } @Test @@ -385,19 +370,25 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra log.info("Deploying {}...", request.getDefinition().getName()); String deploymentId = appDeployer().deploy(request); Timeout timeout = deploymentTimeout(); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(deployed))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.deployed); + }); log.info("Undeploying {}...", deploymentId); timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); List<Path> afterDirs = getAfterPaths(customWorkDirRoot); - assertThat("Additional working directory not created", afterDirs.size(), CoreMatchers.is(beforeDirs.size()+1)); + assertThat(afterDirs.size()).as("Additional working directory not created").isEqualTo(beforeDirs.size() + 1); } @Test @@ -424,8 +415,11 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra Timeout timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); } @Test @@ -452,8 +446,11 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra Timeout timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); } @Test @@ -477,8 +474,11 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra Timeout timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); } @Test @@ -506,8 +506,11 @@ public class LocalAppDeployerIntegrationTests extends AbstractAppDeployerIntegra Timeout timeout = undeploymentTimeout(); appDeployer().undeploy(deploymentId); - assertThat(deploymentId, eventually(hasStatusThat( - Matchers.<AppStatus>hasProperty("state", is(unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(appDeployer().status(deploymentId).getState()).isEqualTo(DeploymentState.unknown); + }); } private List<Path> getAfterPaths(Path customWorkDirRoot) throws IOException { diff --git a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalDeployerSupportTests.java b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalDeployerSupportTests.java index 09970ce..c88e9fb 100644 --- a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalDeployerSupportTests.java +++ b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalDeployerSupportTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2021 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. @@ -21,8 +21,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.cloud.deployer.spi.core.AppDefinition; import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest; @@ -30,10 +30,7 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.test.util.ReflectionTestUtils; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for the AbstractLocalDeployerSupport @@ -45,7 +42,7 @@ public class LocalDeployerSupportTests { private LocalDeployerProperties localDeployerProperties; private AbstractLocalDeployerSupport localDeployerSupport; - @Before + @BeforeEach public void setUp() { localDeployerProperties = new LocalDeployerProperties(); localDeployerSupport = new AbstractLocalDeployerSupport(this.localDeployerProperties) {}; @@ -59,9 +56,9 @@ public class LocalDeployerSupportTests { Map<String, String> environmentVariables = localDeployerSupport.formatApplicationProperties(appDeploymentRequest, envVarsToUse); - assertThat(environmentVariables.size(), is(1)); - assertThat(environmentVariables.keySet(), hasItem(AbstractLocalDeployerSupport.SPRING_APPLICATION_JSON)); - assertThat(environmentVariables.get(AbstractLocalDeployerSupport.SPRING_APPLICATION_JSON), is("{\"test.foo\":\"foo\",\"test.bar\":\"bar\"}")); + assertThat(environmentVariables).hasSize(1); + assertThat(environmentVariables).containsEntry(AbstractLocalDeployerSupport.SPRING_APPLICATION_JSON, + "{\"test.foo\":\"foo\",\"test.bar\":\"bar\"}"); } @Test @@ -78,7 +75,7 @@ public class LocalDeployerSupportTests { deploymentPropertites, commandLineArgs); int portToUse = localDeployerSupport.calcServerPort(appDeploymentRequest, false, new HashMap<>()); - assertThat(portToUse, is(9292)); + assertThat(portToUse).isEqualTo(9292); // test adding to command line args, which has higher precedence than application properties commandLineArgs.add(LocalTaskLauncher.SERVER_PORT_KEY_COMMAND_LINE_ARG + 9191); @@ -86,12 +83,12 @@ public class LocalDeployerSupportTests { deploymentPropertites, commandLineArgs); portToUse = localDeployerSupport.calcServerPort(appDeploymentRequest, false, new HashMap<>()); - assertThat(portToUse, is(9191)); + assertThat(portToUse).isEqualTo(9191); // test using dynamic port assignment portToUse = localDeployerSupport.calcServerPort(appDeploymentRequest, true, new HashMap<>()); - assertThat(portToUse, not(9191)); - assertThat(portToUse, not(9292)); + assertThat(portToUse).isNotEqualTo(9191); + assertThat(portToUse).isNotEqualTo(9292); } @Test @@ -103,8 +100,8 @@ public class LocalDeployerSupportTests { Object requestFactory = ReflectionTestUtils.getField(restTemplate, "requestFactory"); Object connectTimeout = ReflectionTestUtils.getField(requestFactory, "connectTimeout"); Object readTimeout = ReflectionTestUtils.getField(requestFactory, "readTimeout"); - assertThat(connectTimeout, is(1000)); - assertThat(readTimeout, is(1000)); + assertThat(connectTimeout).isEqualTo(1000); + assertThat(readTimeout).isEqualTo(1000); } @Test @@ -116,8 +113,8 @@ public class LocalDeployerSupportTests { Object requestFactory = ReflectionTestUtils.getField(restTemplate, "requestFactory"); Object connectTimeout = ReflectionTestUtils.getField(requestFactory,"connectTimeout"); Object readTimeout = ReflectionTestUtils.getField(requestFactory, "readTimeout"); - assertThat(connectTimeout, is(-1)); - assertThat(readTimeout, is(-1)); + assertThat(connectTimeout).isEqualTo(-1); + assertThat(readTimeout).isEqualTo(-1); } protected AppDeploymentRequest createAppDeploymentRequest() throws MalformedURLException { diff --git a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalTaskLauncherIntegrationTests.java b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalTaskLauncherIntegrationTests.java index 5e20074..dddb261 100644 --- a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalTaskLauncherIntegrationTests.java +++ b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/LocalTaskLauncherIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 the original author or authors. + * Copyright 2016-2021 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. @@ -21,6 +21,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -28,26 +29,23 @@ import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; -import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.system.OutputCaptureRule; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.cloud.deployer.resource.docker.DockerResource; import org.springframework.cloud.deployer.spi.core.AppDefinition; import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest; import org.springframework.cloud.deployer.spi.local.LocalTaskLauncherIntegrationTests.Config; import org.springframework.cloud.deployer.spi.task.LaunchState; import org.springframework.cloud.deployer.spi.task.TaskLauncher; -import org.springframework.cloud.deployer.spi.task.TaskStatus; import org.springframework.cloud.deployer.spi.test.AbstractIntegrationTests; -import org.springframework.cloud.deployer.spi.test.AbstractTaskLauncherIntegrationTests; -import org.springframework.cloud.deployer.spi.test.EventuallyMatcher; +import org.springframework.cloud.deployer.spi.test.AbstractTaskLauncherIntegrationJUnit5Tests; import org.springframework.cloud.deployer.spi.test.Timeout; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -55,12 +53,8 @@ import org.springframework.core.io.Resource; import org.springframework.util.FileSystemUtils; import org.springframework.util.SocketUtils; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.springframework.cloud.deployer.spi.test.EventuallyMatcher.eventually; +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; /** * Integration tests for {@link LocalTaskLauncher}. @@ -78,10 +72,11 @@ import static org.springframework.cloud.deployer.spi.test.EventuallyMatcher.even */ @SpringBootTest(classes = {Config.class, AbstractIntegrationTests.Config.class}, value = { "maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot" }) -public class LocalTaskLauncherIntegrationTests extends AbstractTaskLauncherIntegrationTests { +@ExtendWith(OutputCaptureExtension.class) +public class LocalTaskLauncherIntegrationTests extends AbstractTaskLauncherIntegrationJUnit5Tests { - @Rule - public OutputCaptureRule outputCapture = new OutputCaptureRule(); + // @Rule + // public OutputCaptureRule outputCapture = new OutputCaptureRule(); @Autowired private TaskLauncher taskLauncher; @@ -109,7 +104,7 @@ public class LocalTaskLauncherIntegrationTests extends AbstractTaskLauncherInteg // tweak random dir name on win to be shorter String uuid = UUID.randomUUID().toString(); long l = ByteBuffer.wrap(uuid.toString().getBytes()).getLong(); - return name.getMethodName() + Long.toString(l, Character.MAX_RADIX); + return testName + Long.toString(l, Character.MAX_RADIX); } else { return super.randomName(); @@ -117,22 +112,22 @@ public class LocalTaskLauncherIntegrationTests extends AbstractTaskLauncherInteg } @Test - public void testPassingServerPortViaCommandLineArgs(){ - Map<String, String> appProperties = new HashMap(); + public void testPassingServerPortViaCommandLineArgs(CapturedOutput output){ + Map<String, String> appProperties = new HashMap<>(); appProperties.put("killDelay", "0"); appProperties.put("exitCode", "0"); AppDefinition definition = new AppDefinition(this.randomName(), appProperties); basicLaunchAndValidation(definition, null); - assertTrue(this.outputCapture.toString().contains("Logs will be in")); + assertThat(output).contains("Logs will be in"); } @Test - public void testInheritLoggingAndWorkDir() throws IOException { + public void testInheritLoggingAndWorkDir(CapturedOutput output) throws IOException { - Map<String, String> appProperties = new HashMap(); + Map<String, String> appProperties = new HashMap<>(); appProperties.put("killDelay", "0"); appProperties.put("exitCode", "0"); @@ -153,12 +148,12 @@ public class LocalTaskLauncherIntegrationTests extends AbstractTaskLauncherInteg } basicLaunchAndValidation(definition, deploymentProperties); - assertTrue(this.outputCapture.toString().contains("Logs will be inherited.")); + assertThat(output).contains("Logs will be inherited."); List<Path> afterDirs = Files.walk(customWorkDirRoot, 1) .filter(path -> Files.isDirectory(path)) .collect(Collectors.toList()); - assertThat("Additional working directory not created", afterDirs.size(), is(beforeDirs.size()+1)); + assertThat(afterDirs).as("Additional working directory not created").hasSize(beforeDirs.size() + 1); // clean up if test passed FileSystemUtils.deleteRecursively(customWorkDirRoot); @@ -177,10 +172,14 @@ public class LocalTaskLauncherIntegrationTests extends AbstractTaskLauncherInteg Timeout timeout = deploymentTimeout(); - assertThat(launchId1, eventually(hasStatusThat( - Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.complete))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher().status(launchId1).getState()).isEqualTo(LaunchState.complete); + }); + String logContent = taskLauncher().getLog(launchId1); - assertThat(logContent, containsString("Starting DeployerIntegrationTestApplication")); + assertThat(logContent).contains("Starting DeployerIntegrationTestApplication"); } @Test @@ -196,36 +195,54 @@ public class LocalTaskLauncherIntegrationTests extends AbstractTaskLauncherInteg Timeout timeout = deploymentTimeout(); - assertThat(launchId1, eventually(hasStatusThat( - Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.complete))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher().status(launchId1).getState()).isEqualTo(LaunchState.complete); + }); String launchId2 = taskLauncher().launch(request); - assertThat(launchId2, not(is(launchId1))); + assertThat(launchId2).isNotEqualTo(launchId1); timeout = deploymentTimeout(); - assertThat(launchId2, eventually(hasStatusThat( - Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.complete))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher().status(launchId2).getState()).isEqualTo(LaunchState.complete); + }); - assertThat(launchId1, eventually(hasStatusThat( - Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher().status(launchId1).getState()).isEqualTo(LaunchState.unknown); + }); String launchId3 = taskLauncher().launch(request); - assertThat(launchId3, not(is(launchId1))); - assertThat(launchId3, not(is(launchId2))); + assertThat(launchId3).isNotEqualTo(launchId1); + assertThat(launchId3).isNotEqualTo(launchId2); timeout = deploymentTimeout(); - assertThat(launchId3, eventually(hasStatusThat( - Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.complete))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher().status(launchId3).getState()).isEqualTo(LaunchState.complete); + }); - assertThat(launchId1, eventually(hasStatusThat( - Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher().status(launchId1).getState()).isEqualTo(LaunchState.unknown); + }); - assertThat(launchId2, eventually(hasStatusThat( - Matchers.<TaskStatus>hasProperty("state", Matchers.is(LaunchState.unknown))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher().status(launchId2).getState()).isEqualTo(LaunchState.unknown); + }); taskLauncher().destroy(definition.getName()); } @@ -241,13 +258,26 @@ public class LocalTaskLauncherIntegrationTests extends AbstractTaskLauncherInteg this.log.info("Launching {}...", request.getDefinition().getName()); String launchId = this.taskLauncher().launch(request); - assertThat(taskLauncher.getRunningTaskExecutionCount(), eventually(is(1))); Timeout timeout = this.deploymentTimeout(); - - Assert.assertThat(launchId, EventuallyMatcher.eventually(this.hasStatusThat(Matchers.hasProperty("state", Matchers.is(LaunchState.complete))), timeout.maxAttempts, timeout.pause)); + await().pollInterval(Duration.ofMillis(1)) + .atMost(Duration.ofSeconds(30)) + .untilAsserted(() -> { + assertThat(taskLauncher.getRunningTaskExecutionCount()).isEqualTo(1); + }); + + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher().status(launchId).getState()).isEqualTo(LaunchState.complete); + }); this.taskLauncher().destroy(definition.getName()); - assertThat(taskLauncher.getRunningTaskExecutionCount(), eventually(is(0))); + + await().pollInterval(Duration.ofMillis(timeout.pause)) + .atMost(Duration.ofMillis(timeout.maxAttempts * timeout.pause)) + .untilAsserted(() -> { + assertThat(taskLauncher.getRunningTaskExecutionCount()).isEqualTo(0); + }); } @Configuration diff --git a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/RandomPortRangeContextTests.java b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/RandomPortRangeContextTests.java index af331e1..2e6fc2f 100644 --- a/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/RandomPortRangeContextTests.java +++ b/spring-cloud-deployer-local/src/test/java/org/springframework/cloud/deployer/spi/local/RandomPortRangeContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2018-2021 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. @@ -15,7 +15,7 @@ */ package org.springframework.cloud.deployer.spi.local; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -- GitLab