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

feat(java): ✨ made boyka config path configurable #609

Merged
merged 14 commits into from
Sep 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import static com.google.common.reflect.TypeToken.of;
import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;
import static com.google.gson.JsonParser.parseString;
import static java.util.Objects.requireNonNull;
import static org.apache.logging.log4j.LogManager.getLogger;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -43,7 +43,7 @@
* @author Wasiq Bhamla
* @since 17-Feb-2022
*/
public class JsonUtil {
public final class JsonUtil {
private static final Gson GSON;
private static final Logger LOGGER = getLogger ();

Expand All @@ -64,15 +64,15 @@ public class JsonUtil {
*/
public static <T> T fromFile (final String filePath, final Class<T> objectClass) {
LOGGER.traceEntry ("filePath: {}, objectClass: {}", filePath, objectClass);
final var path = requireNonNull (SettingUtils.class.getClassLoader ()
.getResource (filePath), NO_JSON_FILE_FOUND.getMessageText ());
T result = null;
try (final var reader = new FileReader (path.getPath ())) {
try (final var reader = new FileReader (filePath)) {
result = GSON.fromJson (reader, of (objectClass).getType ());
} catch (final FileNotFoundException e) {
handleAndThrow (NO_JSON_FILE_FOUND, e, filePath);
} catch (final JsonSyntaxException e) {
handleAndThrow (JSON_SYNTAX_ERROR, e);
} catch (final IOException e) {
handleAndThrow (ERROR_READING_FILE, e, path.getPath ());
handleAndThrow (ERROR_READING_FILE, e, filePath);
}
return LOGGER.traceExit (result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
import static com.github.wasiqb.boyka.utils.ErrorHandler.throwError;
import static com.github.wasiqb.boyka.utils.JsonUtil.fromFile;
import static java.lang.String.join;
import static java.lang.System.getProperty;
import static java.lang.System.getenv;
import static java.util.Optional.ofNullable;
import static org.apache.logging.log4j.LogManager.getLogger;

import java.nio.file.Path;
import java.util.Map;

import com.github.wasiqb.boyka.config.FrameworkSetting;
Expand Down Expand Up @@ -64,7 +68,13 @@ public static <T> T getSetting (final Map<String, T> settings, final String key)
public static FrameworkSetting loadSetting () {
LOGGER.traceEntry ();
if (frameworkSetting == null) {
frameworkSetting = fromFile ("boyka-config.json", FrameworkSetting.class);
final var defaultPath = Path.of (getProperty ("user.dir"), "src/test/resources")
.toString ();
final var configDirectory = ofNullable (getenv ("BOYKA_CONFIG_PATH")).orElse (
ofNullable (getProperty ("boyka.config.path")).orElse (defaultPath));
final var configPath = Path.of (configDirectory, "boyka-config.json")
.toString ();
frameworkSetting = fromFile (configPath, FrameworkSetting.class);
}
return LOGGER.traceExit (frameworkSetting);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@

package com.github.wasiqb.boyka.testng.others;

import static com.github.wasiqb.boyka.utils.JsonUtil.fromFile;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.System.getProperty;

import java.nio.file.Path;

import com.github.wasiqb.boyka.config.FrameworkSetting;
import com.github.wasiqb.boyka.exception.FrameworkError;
import com.github.wasiqb.boyka.utils.JsonUtil;
import org.testng.annotations.Test;

Expand All @@ -26,6 +32,16 @@
* @since 8/22/2022
*/
public class JsonUtilTest {
/**
* This method verifies the Error when file is not found.
*/
@Test (expectedExceptions = FrameworkError.class)
public void testFromFile () {
final var configPath = Path.of (getProperty ("user.dir"), "boyka-config.json")
.toString ();
fromFile (configPath, FrameworkSetting.class);
}

/**
* This method verifies the output of JSON util toString() method.
*/
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"@lerna/child-process": "^7.3.0",
"@lerna/version": "^6.6.2",
"@types/node": "^18.17.15",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",
"commitlint": "^17.7.1",
"eslint": "^8.49.0",
"eslint-config-google": "^0.14.0",
Expand Down
Loading