Skip to content

Commit 85fb376

Browse files
run ci
1 parent 36f169d commit 85fb376

File tree

2 files changed

+67
-57
lines changed

2 files changed

+67
-57
lines changed

src/test/java/com/contentstack/sdk/Credentials.java

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,75 @@
11
package com.contentstack.sdk;
22

3-
import io.github.cdimascio.dotenv.Dotenv;
4-
import io.github.cdimascio.dotenv.DotenvException;
3+
import lombok.var;
54

65
import java.rmi.AccessException;
76

87
public class Credentials {
9-
static Dotenv env = getEnv();
8+
//static Dotenv env = getEnv();
109

11-
/**
12-
* The provided Java code defines a method named `getEnv()` that attempts to load environment variables from a `.env` file using the `Dotenv` library. If loading the environment variables encounters an exception (specifically, a `DotenvException`), the code takes an alternative path by creating an empty `.env` file in the current working directory.
13-
* <p>
14-
* Here's a breakdown of what the code is doing step by step:
15-
* <p>
16-
* 1. The method `public static Dotenv getEnv()` is defined. It returns an instance of the `Dotenv` class, which is used to manage environment variables loaded from the `.env` file.
17-
* <p>
18-
* 2. Inside the `try` block, the code tries to load environment variables using `Dotenv.load()`. If successful, the loaded environment variables are stored in the `env` variable.
19-
* <p>
20-
* 3. If loading the environment variables from the `.env` file encounters an exception (a `DotenvException`), the code enters the `catch` block.
21-
* <p>
22-
* 4. In the `catch` block, it gets the current working directory using `System.getProperty("user.dir")` and creates a `File` object named `envFile` representing the `.env` file in the current directory.
23-
* <p>
24-
* 5. The code attempts to create an empty `.env` file using `envFile.createNewFile()`.
25-
* <p>
26-
* 6. If there's an error during the file creation process (an `IOException`), an error message is printed to the standard error output, and the exception's stack trace is printed for debugging purposes.
27-
* <p>
28-
* 7. Finally, regardless of whether the environment variables were successfully loaded or a new `.env` file was created, the method returns the `env` variable, which may either contain the loaded environment variables or be `null` if an exception occurred during the loading process.
29-
* <p>
30-
* In summary, this code defines a method that attempts to load environment variables from a `.env` file using the `Dotenv` library. If loading fails due to an exception, it creates an empty `.env` file in the current working directory and then returns the `Dotenv` instance, which may or may not have loaded environment variables depending on whether an exception occurred.
31-
*
32-
* @return Dotenv
33-
*/
34-
public static Dotenv getEnv() {
35-
String currentDirectory = System.getProperty("user.dir");
36-
//File envFile = new File(currentDirectory, "env");
37-
env = Dotenv.configure()
38-
.directory("src/test/resources")
39-
.filename("env") // instead of '.env', use 'env'
40-
.load();
41-
try {
42-
env = Dotenv.load();
43-
} catch (DotenvException ex) {
44-
System.out.println("Could not load from local .env");
45-
// File envFile = new File(currentDirectory, ".env");
46-
// try {
47-
// // Create .env file in the current directory
48-
// envFile.createNewFile();
49-
// } catch (IOException e) {
50-
// System.err.println("An error occurred while creating .env file.");
51-
// e.printStackTrace();
52-
// }
10+
private static String envChecker() {
11+
String githubActions = System.getenv("GITHUB_ACTIONS");
12+
if (githubActions != null && githubActions.equals("true")) {
13+
System.out.println("Tests are running in GitHub Actions environment.");
14+
String mySecretKey = System.getenv("API_KEY");
15+
System.out.println("My Secret Key: " + mySecretKey);
16+
return "GitHub";
17+
} else {
18+
System.out.println("Tests are running in a local environment.");
19+
return "local";
5320
}
54-
return env;
5521
}
5622

23+
// public static Dotenv getEnv() {
24+
// String currentDirectory = System.getProperty("user.dir");
25+
// File envFile = new File(currentDirectory, "env");
26+
// env = Dotenv.configure()
27+
// .directory("src/test/resources")
28+
// .filename("env") // instead of '.env', use 'env'
29+
// .load();
30+
// try {
31+
// env = Dotenv.load();
32+
// } catch (DotenvException ex) {
33+
// System.out.println("Could not load from local .env");
34+
//// File envFile = new File(currentDirectory, ".env");
35+
//// try {
36+
//// // Create .env file in the current directory
37+
//// envFile.createNewFile();
38+
//// } catch (IOException e) {
39+
//// System.err.println("An error occurred while creating .env file.");
40+
//// e.printStackTrace();
41+
//// }
42+
// }
43+
// return env;
44+
// }
5745

58-
public final static String pwd = (env.get("PWD") != null) ? env.get("PWD") : "contentstack-java";
59-
public final static String HOST = (env.get("HOST") != null) ? env.get("HOST") : "cdn.contentstack.io";
60-
public final static String API_KEY = (env.get("API_KEY") != null) ? env.get("API_KEY") : "***REMOVED***";
61-
public final static String DELIVERY_TOKEN = (env.get("DELIVERY_TOKEN") != null) ? env.get("DELIVERY_TOKEN") : "***REMOVED***";
62-
public final static String ENVIRONMENT = (env.get("ENVIRONMENT") != null) ? env.get("ENVIRONMENT") : "env1";
63-
public final static String CONTENT_TYPE = (env.get("contentType") != null) ? env.get("contentType") : "product";
64-
public final static String ENTRY_UID = (env.get("assetUid") != null) ? env.get("assetUid") : "blt884786476373";
6546

66-
private static Stack stack;
47+
// public final static String pwd = (env.get("PWD") != null) ? env.get("PWD") : "contentstack-java";
48+
// public final static String HOST = (env.get("HOST") != null) ? env.get("HOST") : "cdn.contentstack.io";
49+
// public final static String API_KEY = (env.get("API_KEY") != null) ? env.get("API_KEY") : "***REMOVED***";
50+
// public final static String DELIVERY_TOKEN = (env.get("DELIVERY_TOKEN") != null) ? env.get("DELIVERY_TOKEN") : "***REMOVED***";
51+
// public final static String ENVIRONMENT = (env.get("ENVIRONMENT") != null) ? env.get("ENVIRONMENT") : "env1";
52+
// public final static String CONTENT_TYPE = (env.get("contentType") != null) ? env.get("contentType") : "product";
53+
// public final static String ENTRY_UID = (env.get("assetUid") != null) ? env.get("assetUid") : "blt884786476373";
54+
55+
56+
public final static String HOST = "cdn.contentstack.io";
57+
public final static String API_KEY = "***REMOVED***";
58+
public final static String DELIVERY_TOKEN = "***REMOVED***";
59+
public final static String ENVIRONMENT = "env1";
60+
public final static String CONTENT_TYPE = "product";
61+
public final static String ENTRY_UID = "blt884786476373";
62+
63+
private static volatile Stack stack;
6764

6865
private Credentials() throws AccessException {
69-
// Private constructor to prevent direct instantiation
7066
throw new AccessException("Can not access credential access");
7167
}
7268

7369
public static Stack getStack() {
74-
7570
if (stack == null) {
71+
var envCheck = envChecker();
72+
System.out.println(envCheck);
7673
synchronized (Credentials.class) {
7774
if (stack == null) {
7875
try {

src/test/java/com/contentstack/sdk/TestAsset.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,25 @@ class TestAsset {
1212

1313
private final Logger logger = Logger.getLogger(TestAsset.class.getName());
1414
private String assetUid;
15-
private Stack stack = Credentials.getStack();
15+
private final Stack stack = Credentials.getStack();
1616

17+
private String envChecker() {
18+
String githubActions = System.getenv("GITHUB_ACTIONS");
19+
if (githubActions != null && githubActions.equals("true")) {
20+
System.out.println("Tests are running in GitHub Actions environment.");
21+
String mySecretKey = System.getenv("API_KEY");
22+
System.out.println("My Secret Key: " + mySecretKey);
23+
return "GitHub";
24+
} else {
25+
System.out.println("Tests are running in a local environment.");
26+
return "local";
27+
}
28+
}
1729

1830
@Test
1931
@Order(1)
2032
void testNewAssetLibrary() {
33+
envChecker();
2134
AssetLibrary assets = stack.assetLibrary();
2235
assets.fetchAll(new FetchAssetsCallback() {
2336
@Override

0 commit comments

Comments
 (0)