Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for save configured env vars #90

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
kornys marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
Expand Down Expand Up @@ -113,6 +116,22 @@ protected Map<String, Object> loadConfigurationFile() {
}
}

/***
* Saves all set environment variables into yaml file
*
* @param testLogDir dir where to store file with set env vars
* @throws IOException ioException
*/
public void saveConfigurationFile(String testLogDir) throws IOException {
Path logPath = Path.of(testLogDir);
Files.createDirectories(logPath);

LinkedHashMap<String, String> toSave = new LinkedHashMap<>(values);

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.writerWithDefaultPrettyPrinter().writeValue(logPath.resolve("config.yaml").toFile(), toSave);
}

/**
* Method which logs environment variables parsed by {@link TestEnvironmentVariables}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
import io.skodjob.testframe.environment.TestEnvironmentVariables;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class TestEnvironmentVariablesTest {

@Test
void testGetEnvVariablesCorrectly() {
assertEquals(MyEnvs.MY_ENV, "this");
assertEquals(MyEnvs.SECOND_ENV, "23");
assertTrue(Files.exists(Paths.get(System.getProperty("user.dir"))
.resolve("target").resolve("config.yaml")));
}

public static class MyEnvs {
private static final Map<String, String> ENVS_MAP = Map.of(
"MY_ENV", "this",
"THIRD_ENV", "that"
"MY_ENV", "this",
"THIRD_ENV", "that"
);

public static final TestEnvironmentVariables ENVIRONMENT_VARIABLES = new TestEnvironmentVariables(ENVS_MAP);
Expand All @@ -27,6 +34,12 @@ public static class MyEnvs {

static {
ENVIRONMENT_VARIABLES.logEnvironmentVariables();
try {
ENVIRONMENT_VARIABLES.saveConfigurationFile(Paths.get(System.getProperty("user.dir"))
.resolve("target").toAbsolutePath().toString());
} catch (IOException e) {
fail("Env vars not saved");
}
}
}
}
Loading