Skip to content

Instantly share code, notes, and snippets.

@itudoben
Created July 20, 2022 13:18
Show Gist options
  • Save itudoben/c1faad00ba931c6c761142e4627d9df1 to your computer and use it in GitHub Desktop.
Save itudoben/c1faad00ba931c6c761142e4627d9df1 to your computer and use it in GitHub Desktop.
Test should pass
package io.siren.federate.integrationtests.testframework
import org.junit.Assert
import spock.lang.Specification
import java.lang.reflect.Field
import static java.lang.System.getenv
final class TestConfigurationTest extends Specification {
def "Fail when properties not set"() {
when:
TestConfiguration tc = TestConfiguration.getTestConfiguration()
then:
Exception ioEx = thrown()
ioEx.message.contains('The following environment variables are required: ELASTICSEARCH_VERSION, FEDERATE_VERSION')
}
def "Can set env variables"() {
given:
Map<String, String> envMap = getEditableMapOfVariables()
when:
envMap.put('ELASTICSEARCH_VERSION', 'some version')
envMap.put('FEDERATE_VERSION', 'some version')
then:
Assert.assertEquals(System.getenv('ELASTICSEARCH_VERSION'), 'some version')
Assert.assertEquals(System.getenv('FEDERATE_VERSION'), 'some version')
}
def "Can make TestConfiguration fail after creation"() {
given:
Map<String, String> envMap = getEditableMapOfVariables()
when:
envMap.put('ELASTICSEARCH_VERSION', 'some version')
envMap.put('FEDERATE_VERSION', 'some version')
TestConfiguration tc = TestConfiguration.getTestConfiguration()
envMap.remove('ELASTICSEARCH_VERSION')
envMap.remove('FEDERATE_VERSION')
then:
Assert.assertNull(System.getenv('ELASTICSEARCH_VERSION'))
Assert.assertNull(System.getenv('FEDERATE_VERSION'))
Assert.assertNotNull(TestConfiguration.getSirenFederateVersion())
}
// Using https://github.com/stefanbirkner/system-lambda/blob/18e1f1ed60d1b4346371cdf09ad21c172f438309/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java#L1337
private static Map<String, String> getEditableMapOfVariables() {
Class<?> classOfMap = getenv().getClass();
try {
return getFieldValue(classOfMap, getenv(), "m");
}
catch (IllegalAccessException e) {
throw new RuntimeException("System Rules cannot access the field"
+ " 'm' of the map System.getenv().", e);
}
catch (NoSuchFieldException e) {
throw new RuntimeException("System Rules expects System.getenv() to"
+ " have a field 'm' but it has not.", e);
}
}
private static Map<String, String> getFieldValue(
Class<?> klass,
Object object,
String name
) throws NoSuchFieldException, IllegalAccessException {
Field field = klass.getDeclaredField(name);
field.setAccessible(true);
return (Map<String, String>) field.get(object);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment