From 98cd55872999567d594462a44e4bcc457453b431 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Tue, 20 May 2025 13:25:23 +0530 Subject: [PATCH] Add dotenv dependency and update environment variable loading in Credentials class --- pom.xml | 10 ++++- send-report.sh | 41 ++++++++++++++++++- .../com/contentstack/sdk/SanityReport.java | 2 +- .../com/contentstack/sdk/Credentials.java | 34 +++++++-------- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 579e0db..1d0006f 100644 --- a/pom.xml +++ b/pom.xml @@ -201,6 +201,12 @@ slf4j-simple 1.7.36 + + + io.github.cdimascio + java-dotenv + 5.2.2 + @@ -250,7 +256,7 @@ false 1.8 - https://docs.oracle.com/en/java/javase/23/docs/api/index.html + https://docs.oracle.com/en/java/javase/23/docs/api/ none @@ -263,7 +269,7 @@ maven-surefire-plugin 2.22.2 - + true diff --git a/send-report.sh b/send-report.sh index 14ec36b..e404380 100755 --- a/send-report.sh +++ b/send-report.sh @@ -1,7 +1,40 @@ #!/bin/bash - +# This script temporarily modifies the pom.xml file to enable tests, +# runs the tests, generates a Surefire HTML report, and sends it to Slack. +# It also ensures that the original pom.xml is restored afterward. +# Usage: ./send-report.sh +# Ensure the script is run from the root of the project +# macOS and Linux compatible set -e # Exit immediately if any command fails +# Create a temporary file that won't be committed +backup=$(mktemp) +# Function to restore pom.xml and clean up +restore_pom() { + echo "๐Ÿ”„ Restoring original pom.xml..." + cat "$backup" > pom.xml + rm -f "$backup" # Clean up our temp file + echo "โœ… Original pom.xml restored." +} +# Set trap to restore pom.xml on exit (normal or error) +trap restore_pom EXIT + +echo "๐Ÿ” Backing up pom.xml..." +cat pom.xml > "$backup" + +echo "๐Ÿ”ง Temporarily modifying pom.xml to enable tests..." +# Cross-platform sed command (works on both macOS and Linux) +if [[ "$OSTYPE" == "darwin"* ]]; then + # macOS/BSD sed + sed -i '' 's/true<\/skipTests>/false<\/skipTests>/g' pom.xml +else + # GNU sed (Linux, including GoCD agents) + sed -i 's/true<\/skipTests>/false<\/skipTests>/g' pom.xml +fi + +echo "๐Ÿ”ง Building project..." +mvn clean package + echo "๐Ÿงช Running tests..." mvn clean test @@ -11,4 +44,8 @@ mvn surefire-report:report-only echo "๐Ÿ“ค Sending test report to Slack..." mvn compile exec:java -Dexec.mainClass="com.contentstack.sdk.SanityReport" -echo "โœ… Done." +# Restore pom.xml and clean up +restore_pom +trap - EXIT # Remove the trap + +echo "โœ… Done. All tests complete and original pom.xml restored." \ No newline at end of file diff --git a/src/main/java/com/contentstack/sdk/SanityReport.java b/src/main/java/com/contentstack/sdk/SanityReport.java index b61a015..bf2fc92 100644 --- a/src/main/java/com/contentstack/sdk/SanityReport.java +++ b/src/main/java/com/contentstack/sdk/SanityReport.java @@ -14,7 +14,7 @@ public class SanityReport { - private static final String PROPERTIES_FILE = "src/test/resources/test-config.properties"; + private static final String PROPERTIES_FILE = "src/test/resources/.env"; public void generateTestSummaryAndSendToSlack(File reportFile) throws IOException, SlackApiException { Properties properties = loadProperties(PROPERTIES_FILE); diff --git a/src/test/java/com/contentstack/sdk/Credentials.java b/src/test/java/com/contentstack/sdk/Credentials.java index e6dce57..b54a525 100644 --- a/src/test/java/com/contentstack/sdk/Credentials.java +++ b/src/test/java/com/contentstack/sdk/Credentials.java @@ -1,13 +1,15 @@ package com.contentstack.sdk; -import java.io.FileInputStream; -import java.io.IOException; import java.rmi.AccessException; import java.util.Arrays; -import java.util.Properties; +import io.github.cdimascio.dotenv.Dotenv; public class Credentials { - private static final Properties properties = new Properties(); + + static Dotenv env = Dotenv.configure() + .directory("src/test/resources") + .filename(".env") // or ".env" if you rename it + .load(); private static String envChecker() { String githubActions = System.getenv("GITHUB_ACTIONS"); @@ -18,24 +20,16 @@ private static String envChecker() { } } - static { - try (FileInputStream inputStream = new FileInputStream("src/test/resources/test-config.properties")) { - properties.load(inputStream); - } catch (IOException e) { - System.err.println("Error loading properties file: " + e.getMessage()); - } - } - - public static final String HOST = properties.getProperty("HOST", "cdn.contentstack.io"); - public static final String API_KEY = properties.getProperty("API_KEY", ""); - public static final String DELIVERY_TOKEN = properties.getProperty("DELIVERY_TOKEN", ""); - public static final String ENVIRONMENT = properties.getProperty("ENVIRONMENT", "env1"); - public static final String CONTENT_TYPE = properties.getProperty("contentType", "product"); - public static final String ENTRY_UID = properties.getProperty("assetUid", ""); - public static final String VARIANT_UID = properties.getProperty("variantUid", ""); + public static final String HOST = env.get("HOST", "cdn.contentstack.io"); + public static final String API_KEY = env.get("API_KEY", ""); + public static final String DELIVERY_TOKEN = env.get("DELIVERY_TOKEN", ""); + public static final String ENVIRONMENT = env.get("ENVIRONMENT", "env1"); + public static final String CONTENT_TYPE = env.get("contentType", "product"); + public static final String ENTRY_UID = env.get("assetUid", ""); + public static final String VARIANT_UID = env.get("variantUid", ""); public final static String[] VARIANTS_UID; static { - String variantsUidString = properties.getProperty("variantsUid"); + String variantsUidString = env.get("variantsUid"); if (variantsUidString != null && !variantsUidString.trim().isEmpty()) { VARIANTS_UID = Arrays.stream(variantsUidString.split(","))