Skip to content

Fixed testcase assertions to be env agnostic #184

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

Draft
wants to merge 6 commits into
base: development
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ local.properties
.settings/
.loadpath
.recommenders
# Ignore test configuration
test-config.properties

# External tool builders
.externalToolBuilders/
Expand Down
38 changes: 25 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dotenv-source.version>3.0.0</dotenv-source.version>
<rxjava-source.version>3.1.10</rxjava-source.version>
<retrofit-source.version>2.11.0</retrofit-source.version>
<loggin.version>5.0.0-alpha.11</loggin.version>
<loggin.version>4.12.0</loggin.version>
<jococo-plugin.version>0.8.5</jococo-plugin.version>
<lombok-source.version>1.18.36</lombok-source.version>
<junit-jupiter.version>5.11.4</junit-jupiter.version>
Expand All @@ -34,7 +34,7 @@
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<json-version>20250107</json-version>
<jacoco-maven-plugin-version>0.8.7</jacoco-maven-plugin-version>
<jacoco-maven-plugin-version>0.8.11</jacoco-maven-plugin-version>
<maven-release-plugin-version>2.5.3</maven-release-plugin-version>
<contentstack-utils-version>1.2.15</contentstack-utils-version>
</properties>
Expand Down Expand Up @@ -122,14 +122,6 @@
<scope>compile</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/io.github.cdimascio/java-dotenv -->
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>java-dotenv</artifactId>
<version>5.2.2</version>
</dependency>


<dependency>
<groupId>io.reactivex.rxjava3</groupId>
<artifactId>rxjava</artifactId>
Expand Down Expand Up @@ -189,6 +181,27 @@
<artifactId>jackson-databind</artifactId>
<version>2.18.2</version>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>bolt</artifactId>
<version>1.44.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -237,8 +250,7 @@
<use>false</use>
<source>1.8</source>
<links>
<link>https://docs.oracle.com/javase/7/docs/api/</link>
<link>https://docs.oracle.com/javase/7/docs/api/</link>
<link>https://docs.oracle.com/en/java/javase/23/docs/api/index.html</link>
</links>
<doclint>none</doclint>
</configuration>
Expand All @@ -251,7 +263,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<skipTests>true</skipTests>
<!-- <skipTests>true</skipTests> -->
</configuration>
</plugin>

Expand Down
14 changes: 14 additions & 0 deletions send-report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -e # Exit immediately if any command fails

echo "🧪 Running tests..."
mvn clean test

echo "📄 Generating Surefire HTML report..."
mvn surefire-report:report-only

echo "📤 Sending test report to Slack..."
mvn compile exec:java -Dexec.mainClass="com.contentstack.sdk.SanityReport"

echo "✅ Done."
2 changes: 1 addition & 1 deletion src/main/java/com/contentstack/sdk/CSHttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private String getParams(HashMap<String, Object> params) {
urlParams += urlParams.equals("?") ? key + "=" + value : "&" + key + "=" + value;
}
} catch (Exception e1) {
e1.printStackTrace();
logger.log(Level.SEVERE, e1.getLocalizedMessage(), e1);
}
}
return urlParams;
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/contentstack/sdk/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ private void setIncludeJSON(JSONObject mainJson, ResultCallBack callBack) {

private void throwException(@Nullable String errorMsg, Exception e, EntryResultCallBack callBack) {
Error error = new Error();
if (errorMsg != null) {
if (!errorMsg.isEmpty()) {
error.setErrorMessage(errorMsg);
} else {
error.setErrorMessage(e.toString());
Expand Down Expand Up @@ -1124,11 +1124,9 @@ public Entry includeMetadata() {
}

/**
* @method variants
* @memberof Entry
* @description The variant header will be added to client
* @returns {Entry}
* @example
* The variant header will be added to client
* @return {Entry}
*
* import contentstack from '@contentstack/delivery-sdk'
*
* Stack stack = contentstack.Stack("apiKey", "deliveryToken",
Expand Down
149 changes: 149 additions & 0 deletions src/main/java/com/contentstack/sdk/SanityReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.contentstack.sdk;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.slack.api.bolt.App;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import com.slack.api.methods.response.files.FilesUploadV2Response;

public class SanityReport {

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

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

String slackToken = properties.getProperty("SLACK_BOT_TOKEN");
String slackChannelID = properties.getProperty("SLACK_CHANNEL_ID");
String signingSecret = properties.getProperty("SLACK_SIGNING_SECRET");
String slackChannel = properties.getProperty("SLACK_CHANNEL");

if (slackToken == null || slackChannelID == null) {
System.err.println("Missing Slack credentials in properties.");
return;
}

if (!reportFile.exists()) {
System.err.println("Surefire report file not found at: " + reportFile.getAbsolutePath());
return;
}

String message = generateTestSummary(reportFile);
App app = configureSlackApp(slackToken, signingSecret);

sendMessageToSlack(app, slackChannel, message);
uploadReportToSlack(app, slackChannelID, reportFile);
}

private Properties loadProperties(String filePath) {
Properties properties = new Properties();
try (FileInputStream inputStream = new FileInputStream(filePath)) {
properties.load(inputStream);
} catch (IOException e) {
System.err.println("Failed to load properties: " + e.getMessage());
}
return properties;
}

private App configureSlackApp(String token, String secret) {
App app = new App();
app.config().setSigningSecret(secret);
app.config().setSingleTeamBotToken(token);
return app;
}

private void sendMessageToSlack(App app, String channel, String message) throws IOException, SlackApiException {
ChatPostMessageResponse response = app.client().chatPostMessage(r -> r
.channel(channel)
.text(message)
);
if (response.isOk()) {
System.out.println("Message sent successfully!");
} else {
System.err.println("Failed to send message: " + response.getError());
}
}

private void uploadReportToSlack(App app, String channelID, File file) throws IOException, SlackApiException {
FilesUploadV2Response response = app.client().filesUploadV2(fuvr -> fuvr
.channel(channelID)
.initialComment("Here is the report generated")
.filename(file.getName())
.file(file)
);
if (response.isOk()) {
System.out.println("Report uploaded successfully!");
} else {
System.err.println("Failed to upload report: " + response.getError());
}

}

private String generateTestSummary(File surefireReportFile) throws IOException {
Document doc = Jsoup.parse(surefireReportFile, "UTF-8");
Elements summaryRows = doc.select("table.table tr.b");
Element summaryRow = summaryRows.first();

int totalTests = 0, errors = 0, failures = 0, skipped = 0, passedTests, totalSuites, failedSuites = 0;
String duration = "0m 0s";

if (summaryRow != null) {
Elements cells = summaryRow.select("td");
if (cells.size() >= 6) {
totalTests = Integer.parseInt(cells.get(0).text());
errors = Integer.parseInt(cells.get(1).text());
failures = Integer.parseInt(cells.get(2).text());
skipped = Integer.parseInt(cells.get(3).text());

String timeText = cells.get(5).text();
if (timeText.contains("s")) {
double seconds = Double.parseDouble(timeText.replace(" s", ""));
duration = (int) seconds / 60 + "m " + (int) seconds % 60 + "s";
}
}
}

Elements testSuiteRows = doc.select("table:contains(Class) tr");
totalSuites = testSuiteRows.size() - 1;

for (Element row : testSuiteRows) {
Elements errorCells = row.select("td:nth-child(4)");
Elements failureCells = row.select("td:nth-child(5)");
if (!errorCells.isEmpty() && !failureCells.isEmpty()) {
try {
if (Integer.parseInt(errorCells.text()) > 0 || Integer.parseInt(failureCells.text()) > 0) {
failedSuites++;
}
} catch (NumberFormatException ignored) {
}
}
}

passedTests = totalTests - failures - errors - skipped;

return "*Java CDA Test Report*\n"
+ "• Total Suites: " + totalSuites + "\n"
+ "• Total Tests: " + totalTests + "\n"
+ "• Passed Tests: " + passedTests + "\n"
+ "• Failed Suites: " + failedSuites + "\n"
+ "• Failed Tests: " + failures + "\n"
+ "• Skipped Tests: " + skipped + "\n"
+ "• Duration: " + duration;
}

public static void main(String[] args) {
File reportFile = new File("target/reports/surefire.html");
try {
new SanityReport().generateTestSummaryAndSendToSlack(reportFile);
} catch (IOException | SlackApiException e) {
System.err.println("Error: " + e.getMessage());
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/contentstack/sdk/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void includeLivePreview() {
if (config.enableLivePreview) {
String urlLivePreview = config.livePreviewHost;
if(config.region != null && !config.region.name().isEmpty()){
if(config.region.name() == "US" ){
if(config.region.name().equals("US") ){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can u please check if the value US is valid or not? For NA region. The known valid values were NA / AWS-NA

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well by default it does take the NA host. This US check is being made in live preview functionality

config.livePreviewHost = urlLivePreview;
}else{
String regionPrefix = config.region.name().toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/contentstack/sdk/Taxonomy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Taxonomy, currently in the Early Access Phase simplifies
* the process of organizing content in your system, making
* it effortless to find and retrieve information.
* @implSpec To implement the taxonomy use below code
* To implement the taxonomy use below code
* <pre>
* {@code
* Stack stack = Contentstack.stack("API_KEY", "DELIVERY_TOKEN", "ENVIRONMENT");
Expand Down
36 changes: 18 additions & 18 deletions src/test/java/com/contentstack/sdk/Credentials.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.contentstack.sdk;

import io.github.cdimascio.dotenv.Dotenv;

import java.io.FileInputStream;
import java.io.IOException;
import java.rmi.AccessException;
import java.util.Arrays;
import java.util.Properties;

public class Credentials {
static Dotenv env = getEnv();
private static final Properties properties = new Properties();

private static String envChecker() {
String githubActions = System.getenv("GITHUB_ACTIONS");
Expand All @@ -17,25 +18,24 @@ private static String envChecker() {
}
}

public static Dotenv getEnv() {
env = Dotenv.configure()
.directory("src/test/resources")
.filename("env") // instead of '.env', use 'env'
.load();

return Dotenv.load();
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 final static String HOST = (env.get("HOST") != null) ? env.get("HOST") : "cdn.contentstack.io";
public final static String API_KEY = (env.get("API_KEY") != null) ? env.get("API_KEY") : "";
public final static String DELIVERY_TOKEN = (env.get("DELIVERY_TOKEN") != null) ? env.get("DELIVERY_TOKEN") : "";
public final static String ENVIRONMENT = (env.get("ENVIRONMENT") != null) ? env.get("ENVIRONMENT") : "env1";
public final static String CONTENT_TYPE = (env.get("contentType") != null) ? env.get("contentType") : "product";
public final static String ENTRY_UID = (env.get("assetUid") != null) ? env.get("assetUid") : "";
public final static String VARIANT_UID = (env.get("variantUid") != null) ? env.get("variantUid") : "";
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 final static String[] VARIANTS_UID;
static {
String variantsUidString = env.get("variantsUid");
String variantsUidString = properties.getProperty("variantsUid");

if (variantsUidString != null && !variantsUidString.trim().isEmpty()) {
VARIANTS_UID = Arrays.stream(variantsUidString.split(","))
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/contentstack/sdk/TaxonomyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class TaxonomyTest {

private final Stack stack = Credentials.getStack();
private final String host = Credentials.HOST;

@Test
void testInstance() {
Expand All @@ -28,7 +29,7 @@ void operationIn() {
Request req = taxonomy.in("taxonomies.color", listOfItems).makeRequest().request();
//Assertions.assertEquals(3, req.headers().size());
Assertions.assertEquals("GET", req.method());
Assertions.assertEquals("cdn.contentstack.io", req.url().host());
Assertions.assertEquals(host, req.url().host());
Assertions.assertEquals("/v3/taxonomies/entries", req.url().encodedPath());
Assertions.assertEquals("query={\"taxonomies.color\":{\"$in\":[\"red\",\"yellow\"]}}", req.url().query());
}
Expand Down
Loading
Loading