From c225ff983a7198d3999343fd0c4254914efa40e6 Mon Sep 17 00:00:00 2001 From: Stefan Birkner Date: Fri, 9 Oct 2020 00:53:57 +0200 Subject: [PATCH] Replace System Rules with System Lambda System Lambda is more specific. It only wraps the part of the code that reads the environment variables. In addition System Lambda is independent from the test framework and no obstacle for moving to another test framework (e.g. JUnit Lambda). --- pom.xml | 4 ++-- .../tokens/AbstractAccessTokenRefresherTest.java | 13 +++++-------- .../stups/tokens/AccessTokenBuilderTest.java | 15 ++++++--------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index f6b38fd..ee51189 100644 --- a/pom.xml +++ b/pom.xml @@ -134,8 +134,8 @@ com.github.stefanbirkner - system-rules - 1.19.0 + system-lambda + 1.1.0 test diff --git a/src/test/java/org/zalando/stups/tokens/AbstractAccessTokenRefresherTest.java b/src/test/java/org/zalando/stups/tokens/AbstractAccessTokenRefresherTest.java index c6ace0d..0b55b49 100644 --- a/src/test/java/org/zalando/stups/tokens/AbstractAccessTokenRefresherTest.java +++ b/src/test/java/org/zalando/stups/tokens/AbstractAccessTokenRefresherTest.java @@ -15,13 +15,13 @@ */ package org.zalando.stups.tokens; +import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; + import java.util.Arrays; import java.util.List; import org.assertj.core.api.Assertions; -import org.junit.Rule; import org.junit.Test; -import org.junit.contrib.java.lang.system.EnvironmentVariables; import org.mockito.Mockito; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,9 +31,6 @@ public class AbstractAccessTokenRefresherTest { private static final String FIXED_TOKENS = "one=DSAFDASASDFDA,two=DSFADFADFADFADFA,three=ADAFDAFADFAFEWRDFADFASDF"; private static final String EXPECTED_MESSAGE = "No token available for tokenId 'four'. Tokens are available for the following tokenIds [one, two, three]"; - @Rule - public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); - @Test public void testListToString() { List keys = Arrays.asList("One", "Two", "Three"); @@ -41,11 +38,11 @@ public void testListToString() { } @Test - public void testExceptionMessage() { - environmentVariables.set("OAUTH2_ACCESS_TOKENS", FIXED_TOKENS); + public void testExceptionMessage() throws Exception { TokenRefresherConfiguration configuration = Mockito.mock(TokenRefresherConfiguration.class); AbstractAccessTokenRefresher refresher = new TestTokenRefresher(configuration); - refresher.initializeFixedTokensFromEnvironment(); + withEnvironmentVariable("OAUTH2_ACCESS_TOKENS", FIXED_TOKENS) + .execute(refresher::initializeFixedTokensFromEnvironment); String availableTokenIds = refresher.getAvailableTokenIds(); Assertions.assertThat(availableTokenIds).isEqualTo("[one, two, three]"); try { diff --git a/src/test/java/org/zalando/stups/tokens/AccessTokenBuilderTest.java b/src/test/java/org/zalando/stups/tokens/AccessTokenBuilderTest.java index 42b574c..ba446db 100644 --- a/src/test/java/org/zalando/stups/tokens/AccessTokenBuilderTest.java +++ b/src/test/java/org/zalando/stups/tokens/AccessTokenBuilderTest.java @@ -15,6 +15,7 @@ */ package org.zalando.stups.tokens; +import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable; import static org.assertj.core.api.Assertions.assertThat; import java.io.File; @@ -27,7 +28,6 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.contrib.java.lang.system.EnvironmentVariables; import org.junit.rules.TemporaryFolder; import org.mockito.Mockito; import org.mockito.internal.util.io.IOUtil; @@ -49,9 +49,6 @@ public class AccessTokenBuilderTest { @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); - @Rule - public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); - @Before public void createCredentials() throws IOException { File tempDir = tempFolder.newFolder(); @@ -150,9 +147,9 @@ public void noEnvironmentSet() { @Test public void notAnUri_OAUTH2_ACCESS_TOKEN_URL() { - environmentVariables.set("OAUTH2_ACCESS_TOKEN_URL", "::::"); try { - Tokens.createAccessTokens(); + withEnvironmentVariable("OAUTH2_ACCESS_TOKEN_URL", "::::") + .execute(Tokens::createAccessTokens); Assertions.fail("Not expected to reach this point"); } catch (Exception e) { assertThat(e.getMessage()) @@ -161,9 +158,9 @@ public void notAnUri_OAUTH2_ACCESS_TOKEN_URL() { } @Test - public void usinEnvCreatesBuilder() { - environmentVariables.set("OAUTH2_ACCESS_TOKEN_URL", "https://somwhere.test/tokens"); - AccessTokensBuilder builder = Tokens.createAccessTokens(); + public void usinEnvCreatesBuilder() throws Exception { + AccessTokensBuilder builder = withEnvironmentVariable("OAUTH2_ACCESS_TOKEN_URL", "https://somwhere.test/tokens") + .execute(Tokens::createAccessTokens); Assertions.assertThat(builder).isNotNull(); } }