Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Commit

Permalink
Replace System Rules with System Lambda
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
stefanbirkner committed Oct 8, 2020
1 parent f362498 commit c225ff9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<artifactId>system-lambda</artifactId>
<version>1.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,21 +31,18 @@ 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<String> keys = Arrays.asList("One", "Two", "Three");
Assertions.assertThat(keys.toString()).isEqualTo("[One, Two, Three]");
}

@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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())
Expand All @@ -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();
}
}

0 comments on commit c225ff9

Please sign in to comment.