|
| 1 | +package com.contentstack.sdk; |
| 2 | +import java.io.File; |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.Properties; |
| 6 | +import org.jsoup.Jsoup; |
| 7 | +import org.jsoup.nodes.Document; |
| 8 | +import org.jsoup.nodes.Element; |
| 9 | +import org.jsoup.select.Elements; |
| 10 | +import com.slack.api.bolt.App; |
| 11 | +import com.slack.api.methods.SlackApiException; |
| 12 | +import com.slack.api.methods.response.chat.ChatPostMessageResponse; |
| 13 | +import com.slack.api.methods.response.files.FilesUploadV2Response; |
| 14 | + |
| 15 | +public class SanityReport { |
| 16 | + |
| 17 | + private static final String PROPERTIES_FILE = "src/test/resources/test-config.properties"; |
| 18 | + |
| 19 | + public void generateTestSummaryAndSendToSlack(File reportFile) throws IOException, SlackApiException { |
| 20 | + Properties properties = loadProperties(PROPERTIES_FILE); |
| 21 | + |
| 22 | + String slackToken = properties.getProperty("SLACK_BOT_TOKEN"); |
| 23 | + String slackChannelID = properties.getProperty("SLACK_CHANNEL_ID"); |
| 24 | + String signingSecret = properties.getProperty("SLACK_SIGNING_SECRET"); |
| 25 | + String slackChannel = properties.getProperty("SLACK_CHANNEL"); |
| 26 | + |
| 27 | + if (slackToken == null || slackChannelID == null) { |
| 28 | + System.err.println("Missing Slack credentials in properties."); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + if (!reportFile.exists()) { |
| 33 | + System.err.println("Surefire report file not found at: " + reportFile.getAbsolutePath()); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + String message = generateTestSummary(reportFile); |
| 38 | + App app = configureSlackApp(slackToken, signingSecret); |
| 39 | + |
| 40 | + sendMessageToSlack(app, slackChannel, message); |
| 41 | + uploadReportToSlack(app, slackChannelID, reportFile); |
| 42 | + } |
| 43 | + |
| 44 | + private Properties loadProperties(String filePath) { |
| 45 | + Properties properties = new Properties(); |
| 46 | + try (FileInputStream inputStream = new FileInputStream(filePath)) { |
| 47 | + properties.load(inputStream); |
| 48 | + } catch (IOException e) { |
| 49 | + System.err.println("Failed to load properties: " + e.getMessage()); |
| 50 | + } |
| 51 | + return properties; |
| 52 | + } |
| 53 | + |
| 54 | + private App configureSlackApp(String token, String secret) { |
| 55 | + App app = new App(); |
| 56 | + app.config().setSigningSecret(secret); |
| 57 | + app.config().setSingleTeamBotToken(token); |
| 58 | + return app; |
| 59 | + } |
| 60 | + |
| 61 | + private void sendMessageToSlack(App app, String channel, String message) throws IOException, SlackApiException { |
| 62 | + ChatPostMessageResponse response = app.client().chatPostMessage(r -> r |
| 63 | + .channel(channel) |
| 64 | + .text(message) |
| 65 | + ); |
| 66 | + if (response.isOk()) { |
| 67 | + System.out.println("Message sent successfully!"); |
| 68 | + } else { |
| 69 | + System.err.println("Failed to send message: " + response.getError()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void uploadReportToSlack(App app, String channelID, File file) throws IOException, SlackApiException { |
| 74 | + FilesUploadV2Response response = app.client().filesUploadV2(fuvr -> fuvr |
| 75 | + .channel(channelID) |
| 76 | + .initialComment("Here is the report generated") |
| 77 | + .filename(file.getName()) |
| 78 | + .file(file) |
| 79 | + ); |
| 80 | + if (response.isOk()) { |
| 81 | + System.out.println("Report uploaded successfully!"); |
| 82 | + } else { |
| 83 | + System.err.println("Failed to upload report: " + response.getError()); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + private String generateTestSummary(File surefireReportFile) throws IOException { |
| 89 | + Document doc = Jsoup.parse(surefireReportFile, "UTF-8"); |
| 90 | + Elements summaryRows = doc.select("table.table tr.b"); |
| 91 | + Element summaryRow = summaryRows.first(); |
| 92 | + |
| 93 | + int totalTests = 0, errors = 0, failures = 0, skipped = 0, passedTests, totalSuites, failedSuites = 0; |
| 94 | + String duration = "0m 0s"; |
| 95 | + |
| 96 | + if (summaryRow != null) { |
| 97 | + Elements cells = summaryRow.select("td"); |
| 98 | + if (cells.size() >= 6) { |
| 99 | + totalTests = Integer.parseInt(cells.get(0).text()); |
| 100 | + errors = Integer.parseInt(cells.get(1).text()); |
| 101 | + failures = Integer.parseInt(cells.get(2).text()); |
| 102 | + skipped = Integer.parseInt(cells.get(3).text()); |
| 103 | + |
| 104 | + String timeText = cells.get(5).text(); |
| 105 | + if (timeText.contains("s")) { |
| 106 | + double seconds = Double.parseDouble(timeText.replace(" s", "")); |
| 107 | + duration = (int) seconds / 60 + "m " + (int) seconds % 60 + "s"; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + Elements testSuiteRows = doc.select("table:contains(Class) tr"); |
| 113 | + totalSuites = testSuiteRows.size() - 1; |
| 114 | + |
| 115 | + for (Element row : testSuiteRows) { |
| 116 | + Elements errorCells = row.select("td:nth-child(4)"); |
| 117 | + Elements failureCells = row.select("td:nth-child(5)"); |
| 118 | + if (!errorCells.isEmpty() && !failureCells.isEmpty()) { |
| 119 | + try { |
| 120 | + if (Integer.parseInt(errorCells.text()) > 0 || Integer.parseInt(failureCells.text()) > 0) { |
| 121 | + failedSuites++; |
| 122 | + } |
| 123 | + } catch (NumberFormatException ignored) { |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + passedTests = totalTests - failures - errors - skipped; |
| 129 | + |
| 130 | + return "*Java CDA Test Report*\n" |
| 131 | + + "• Total Suites: " + totalSuites + "\n" |
| 132 | + + "• Total Tests: " + totalTests + "\n" |
| 133 | + + "• Passed Tests: " + passedTests + "\n" |
| 134 | + + "• Failed Suites: " + failedSuites + "\n" |
| 135 | + + "• Failed Tests: " + failures + "\n" |
| 136 | + + "• Skipped Tests: " + skipped + "\n" |
| 137 | + + "• Duration: " + duration; |
| 138 | + } |
| 139 | + |
| 140 | + public static void main(String[] args) { |
| 141 | + File reportFile = new File("target/reports/surefire.html"); |
| 142 | + try { |
| 143 | + new SanityReport().generateTestSummaryAndSendToSlack(reportFile); |
| 144 | + } catch (IOException | SlackApiException e) { |
| 145 | + System.err.println("Error: " + e.getMessage()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments