Skip to content

Commit 98cd558

Browse files
committed
Add dotenv dependency and update environment variable loading in Credentials class
1 parent 7eb50ed commit 98cd558

File tree

4 files changed

+62
-25
lines changed

4 files changed

+62
-25
lines changed

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@
201201
<artifactId>slf4j-simple</artifactId>
202202
<version>1.7.36</version>
203203
</dependency>
204+
<!-- https://mvnrepository.com/artifact/io.github.cdimascio/java-dotenv -->
205+
<dependency>
206+
<groupId>io.github.cdimascio</groupId>
207+
<artifactId>java-dotenv</artifactId>
208+
<version>5.2.2</version>
209+
</dependency>
204210

205211
</dependencies>
206212

@@ -250,7 +256,7 @@
250256
<use>false</use>
251257
<source>1.8</source>
252258
<links>
253-
<link>https://docs.oracle.com/en/java/javase/23/docs/api/index.html</link>
259+
<link>https://docs.oracle.com/en/java/javase/23/docs/api/</link>
254260
</links>
255261
<doclint>none</doclint>
256262
</configuration>
@@ -263,7 +269,7 @@
263269
<artifactId>maven-surefire-plugin</artifactId>
264270
<version>2.22.2</version>
265271
<configuration>
266-
<!-- <skipTests>true</skipTests> -->
272+
<skipTests>true</skipTests>
267273
</configuration>
268274
</plugin>
269275

send-report.sh

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
11
#!/bin/bash
2-
2+
# This script temporarily modifies the pom.xml file to enable tests,
3+
# runs the tests, generates a Surefire HTML report, and sends it to Slack.
4+
# It also ensures that the original pom.xml is restored afterward.
5+
# Usage: ./send-report.sh
6+
# Ensure the script is run from the root of the project
7+
# macOS and Linux compatible
38
set -e # Exit immediately if any command fails
49

10+
# Create a temporary file that won't be committed
11+
backup=$(mktemp)
12+
# Function to restore pom.xml and clean up
13+
restore_pom() {
14+
echo "🔄 Restoring original pom.xml..."
15+
cat "$backup" > pom.xml
16+
rm -f "$backup" # Clean up our temp file
17+
echo "✅ Original pom.xml restored."
18+
}
19+
# Set trap to restore pom.xml on exit (normal or error)
20+
trap restore_pom EXIT
21+
22+
echo "🔍 Backing up pom.xml..."
23+
cat pom.xml > "$backup"
24+
25+
echo "🔧 Temporarily modifying pom.xml to enable tests..."
26+
# Cross-platform sed command (works on both macOS and Linux)
27+
if [[ "$OSTYPE" == "darwin"* ]]; then
28+
# macOS/BSD sed
29+
sed -i '' 's/<skipTests>true<\/skipTests>/<skipTests>false<\/skipTests>/g' pom.xml
30+
else
31+
# GNU sed (Linux, including GoCD agents)
32+
sed -i 's/<skipTests>true<\/skipTests>/<skipTests>false<\/skipTests>/g' pom.xml
33+
fi
34+
35+
echo "🔧 Building project..."
36+
mvn clean package
37+
538
echo "🧪 Running tests..."
639
mvn clean test
740

@@ -11,4 +44,8 @@ mvn surefire-report:report-only
1144
echo "📤 Sending test report to Slack..."
1245
mvn compile exec:java -Dexec.mainClass="com.contentstack.sdk.SanityReport"
1346

14-
echo "✅ Done."
47+
# Restore pom.xml and clean up
48+
restore_pom
49+
trap - EXIT # Remove the trap
50+
51+
echo "✅ Done. All tests complete and original pom.xml restored."

src/main/java/com/contentstack/sdk/SanityReport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
public class SanityReport {
1616

17-
private static final String PROPERTIES_FILE = "src/test/resources/test-config.properties";
17+
private static final String PROPERTIES_FILE = "src/test/resources/.env";
1818

1919
public void generateTestSummaryAndSendToSlack(File reportFile) throws IOException, SlackApiException {
2020
Properties properties = loadProperties(PROPERTIES_FILE);

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.contentstack.sdk;
22

3-
import java.io.FileInputStream;
4-
import java.io.IOException;
53
import java.rmi.AccessException;
64
import java.util.Arrays;
7-
import java.util.Properties;
5+
import io.github.cdimascio.dotenv.Dotenv;
86

97
public class Credentials {
10-
private static final Properties properties = new Properties();
8+
9+
static Dotenv env = Dotenv.configure()
10+
.directory("src/test/resources")
11+
.filename(".env") // or ".env" if you rename it
12+
.load();
1113

1214
private static String envChecker() {
1315
String githubActions = System.getenv("GITHUB_ACTIONS");
@@ -18,24 +20,16 @@ private static String envChecker() {
1820
}
1921
}
2022

21-
static {
22-
try (FileInputStream inputStream = new FileInputStream("src/test/resources/test-config.properties")) {
23-
properties.load(inputStream);
24-
} catch (IOException e) {
25-
System.err.println("Error loading properties file: " + e.getMessage());
26-
}
27-
}
28-
29-
public static final String HOST = properties.getProperty("HOST", "cdn.contentstack.io");
30-
public static final String API_KEY = properties.getProperty("API_KEY", "");
31-
public static final String DELIVERY_TOKEN = properties.getProperty("DELIVERY_TOKEN", "");
32-
public static final String ENVIRONMENT = properties.getProperty("ENVIRONMENT", "env1");
33-
public static final String CONTENT_TYPE = properties.getProperty("contentType", "product");
34-
public static final String ENTRY_UID = properties.getProperty("assetUid", "");
35-
public static final String VARIANT_UID = properties.getProperty("variantUid", "");
23+
public static final String HOST = env.get("HOST", "cdn.contentstack.io");
24+
public static final String API_KEY = env.get("API_KEY", "");
25+
public static final String DELIVERY_TOKEN = env.get("DELIVERY_TOKEN", "");
26+
public static final String ENVIRONMENT = env.get("ENVIRONMENT", "env1");
27+
public static final String CONTENT_TYPE = env.get("contentType", "product");
28+
public static final String ENTRY_UID = env.get("assetUid", "");
29+
public static final String VARIANT_UID = env.get("variantUid", "");
3630
public final static String[] VARIANTS_UID;
3731
static {
38-
String variantsUidString = properties.getProperty("variantsUid");
32+
String variantsUidString = env.get("variantsUid");
3933

4034
if (variantsUidString != null && !variantsUidString.trim().isEmpty()) {
4135
VARIANTS_UID = Arrays.stream(variantsUidString.split(","))

0 commit comments

Comments
 (0)