Skip to content
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

Move percentage number formatting to ftl #301

Merged
merged 1 commit into from
Sep 13, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DurationFormatUtils;

import java.text.*;

/**
* @author kadehar
* @since 4.0
Expand Down Expand Up @@ -38,20 +36,6 @@ public static String formatDuration(Long durationMillis, String format) {
return formattedDuration;
}

public static double formatDouble(double value) {
log.info("Formatting value {}", value);
DecimalFormat df = new DecimalFormat("##.#");
String tmp = df.format(value);
NumberFormat numberFormat = NumberFormat.getInstance();
try {
double result = numberFormat.parse(tmp).doubleValue();
log.info("Formatted value is {}", result);
return result;
} catch (ParseException e) {
return 0.0;
}
}

public static String formatReportLink(String link) {
return link != null && link.endsWith("/") ? link + "allure" : link;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package guru.qa.allure.notifications.util;

import guru.qa.allure.notifications.formatters.Formatters;
import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -13,7 +12,6 @@ public class Percentage {
public double eval(int result, int total) {
log.info("Calculate percentage for result {} by total {}", result,
total);
double value = (result * 100.00) / total;
return Formatters.formatDouble(value);
return (result * 100.00) / total;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<b>${phrases.scenario.duration}: </b>${time}<br/>
<b>${phrases.scenario.totalScenarios}: </b>${total}
<ul>
<#if passed != 0 ><li><b>${phrases.scenario.totalPassed}: </b>${passed} (${passedPercentage} %)</li></#if>
<#if failed != 0 ><li><b>${phrases.scenario.totalFailed}: </b>${failed} (${failedPercentage} %)</li></#if>
<#if passed != 0 ><li><b>${phrases.scenario.totalPassed}: </b>${passed} (${passedPercentage?string("##.#")} %)</li></#if>
<#if failed != 0 ><li><b>${phrases.scenario.totalFailed}: </b>${failed} (${failedPercentage?string("##.#")} %)</li></#if>
<#if broken != 0 ><li><b>${phrases.scenario.totalBroken}: </b>${broken}</li></#if>
<#if unknown != 0 ><li><b>${phrases.scenario.totalUnknown}: </b>${unknown}</li></#if>
<#if skipped != 0 ><li><b>${phrases.scenario.totalSkipped}: </b>${skipped}</li></#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*${phrases.comment}:* ${comm}
*${phrases.scenario.duration}:* ${time}
*${phrases.scenario.totalScenarios}:* ${total}
<#if passed != 0 > *${phrases.scenario.totalPassed}:* ${passed} (${passedPercentage} %)</#if>
<#if failed != 0 > *${phrases.scenario.totalFailed}:* ${failed} (${failedPercentage} %)</#if>
<#if passed != 0 > *${phrases.scenario.totalPassed}:* ${passed} (${passedPercentage?string("##.#")} %)</#if>
<#if failed != 0 > *${phrases.scenario.totalFailed}:* ${failed} (${failedPercentage?string("##.#")} %)</#if>
<#if broken != 0 > *${phrases.scenario.totalBroken}:* ${broken} </#if>
<#if unknown != 0 >*${phrases.scenario.totalUnknown}:* ${unknown} </#if>
<#if skipped != 0 >*${phrases.scenario.totalSkipped}:* ${skipped} </#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
**-${phrases.comment}:** ${comm}
**-${phrases.scenario.duration}:** **${time}**
**-${phrases.scenario.totalScenarios}:** ${total}
<#if passed != 0 > **-${phrases.scenario.totalPassed}:** ${passed} **(${passedPercentage} %)**</#if>
<#if failed != 0 > **-${phrases.scenario.totalFailed}:** ${failed} **(${failedPercentage} %)** </#if>
<#if passed != 0 > **-${phrases.scenario.totalPassed}:** ${passed} **(${passedPercentage?string("##.#")} %)**</#if>
<#if failed != 0 > **-${phrases.scenario.totalFailed}:** ${failed} **(${failedPercentage?string("##.#")} %)** </#if>
<#if broken != 0 > **-${phrases.scenario.totalBroken}:** ${broken} </#if>
<#if unknown != 0 > **-${phrases.scenario.totalUnknown}:** ${unknown} </#if>
<#if skipped != 0 > **-${phrases.scenario.totalSkipped}:** ${skipped} </#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<b>${phrases.comment}: </b>${comm}
<b>${phrases.scenario.duration}: </b>${time}
<b>${phrases.scenario.totalScenarios}: </b>${total}
<#if passed != 0 ><b>${phrases.scenario.totalPassed}: </b>${passed} (${passedPercentage} %)</#if>
<#if failed != 0 ><b>${phrases.scenario.totalFailed}: </b>${failed} (${failedPercentage} %)</#if>
<#if passed != 0 ><b>${phrases.scenario.totalPassed}: </b>${passed} (${passedPercentage?string("##.#")} %)</#if>
<#if failed != 0 ><b>${phrases.scenario.totalFailed}: </b>${failed} (${failedPercentage?string("##.#")} %)</#if>
<#if broken != 0 ><b>${phrases.scenario.totalBroken}: </b>${broken}</#if>
<#if unknown != 0 ><b>${phrases.scenario.totalUnknown}: </b>${unknown}</#if>
<#if skipped != 0 ><b>${phrases.scenario.totalSkipped}: </b>${skipped}</#if>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package guru.qa.allure.notifications.template;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mockConstruction;
Expand All @@ -8,19 +10,28 @@

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mock;
import org.mockito.MockedConstruction;
import org.mockito.junit.jupiter.MockitoExtension;

import freemarker.template.Configuration;
import freemarker.template.Template;
import guru.qa.allure.notifications.config.Config;
import guru.qa.allure.notifications.exceptions.MessageBuildException;
import guru.qa.allure.notifications.json.JSON;
import guru.qa.allure.notifications.model.phrases.Phrases;
import guru.qa.allure.notifications.model.summary.Summary;
import guru.qa.allure.notifications.template.config.TemplateConfig;
import guru.qa.allure.notifications.template.data.MessageData;

Expand Down Expand Up @@ -66,4 +77,31 @@ void shouldCreateMessageFromTemplateFromJar() throws MessageBuildException, IOEx
verify(configuration).getTemplate(RESOURCES_PATH);
}
}

@ParameterizedTest
@CsvSource({
"templates/html.ftl, guru/qa/allure/notifications/template/messages/html.txt",
"templates/markdown.ftl, guru/qa/allure/notifications/template/messages/markdown.txt",
"templates/rocket.ftl, guru/qa/allure/notifications/template/messages/rocket.txt",
"templates/telegram.ftl, guru/qa/allure/notifications/template/messages/telegram.txt"
})
void shouldValidateGeneratedMessageFromTemplate(String templatePath, String expectedMessagePath)
throws IOException, URISyntaxException, MessageBuildException {
Config config = new JSON().parseResource(
"/guru/qa/allure/notifications/template/testConfig.json", Config.class);
Summary summary = new JSON().parseResource(
"/guru/qa/allure/notifications/template/testSummary.json", Summary.class);
ClassLoader classLoader = getClass().getClassLoader();
URL testSuitesUrl = classLoader.getResource("guru/qa/allure/notifications/template/testSuites.json");
String suitesSummaryJson = new String(Files.readAllBytes(Paths.get(testSuitesUrl.toURI())), UTF_8);
Phrases phrases = new JSON().parseResource("/phrases/en.json", Phrases.class);

MessageData testMessageData = new MessageData(config.getBase(), summary, suitesSummaryJson, phrases);
MessageTemplate messageTemplate = new MessageTemplate(testMessageData);

String messageGeneratedFromTemplate = messageTemplate.createMessageFromTemplate(templatePath);
URL expectedMessageUrl = classLoader.getResource(expectedMessagePath);
String expectedMessage = new String(Files.readAllBytes(Paths.get(expectedMessageUrl.toURI())), UTF_8);
assertEquals(expectedMessage, messageGeneratedFromTemplate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h3>Results:</h3>
<b>Environment: </b>Test<br/>
<b>Comment: </b>Test comment<br/>
<b>Duration: </b>00:00:24.011<br/>
<b>Total scenarios: </b>3
<ul>
<li><b>Total passed: </b>2 (66.7 %)</li>
<li><b>Total failed: </b>1 (33.3 %)</li>
</ul>
<b>Number of test suites: </b>2<br/>
<table style="border:1px solid black;">
<tr>
<td>
<b>Name of the test suite: </b>batch-1<br/>
<b>Total scenarios: </b>2<br/>
<li><b>Total passed: </b>1</li>
<li><b>Total failed: </b>1</li>
</td>
</tr>
</table><br/>
<table style="border:1px solid black;">
<tr>
<td>
<b>Name of the test suite: </b>batch-2<br/>
<b>Total scenarios: </b>1<br/>
<li><b>Total passed: </b>1</li>
</td>
</tr>
</table><br/>
<b>Report available at the link:</b> <a href=https://www.example.com>https://www.example.com</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*Results:*
*Environment:* Test
*Comment:* Test comment
*Duration:* 00:00:24.011
*Total scenarios:* 3
*Total passed:* 2 (66.7 %)
*Total failed:* 1 (33.3 %)
*Number of test suites:* 2
*Name of the test suite:* batch-1
> *Total scenarios:* 2
> *Total passed:* 1
> *Total failed:* 1
*Name of the test suite:* batch-2
> *Total scenarios:* 1
> *Total passed:* 1
*Report available at the link:* https://www.example.com
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
**Results:**
**-Environment:** Test
**-Comment:** Test comment
**-Duration:** **00:00:24.011**
**-Total scenarios:** 3
**-Total passed:** 2 **(66.7 %)**
**-Total failed:** 1 **(33.3 %)**
**-Number of test suites:** **2**
**-Name of the test suite:** **batch-1**
**-Total scenarios:** **2**
**-Total passed:** 1
**-Total failed:** 1
**-Name of the test suite:** **batch-2**
**-Total scenarios:** **1**
**-Total passed:** 1
**Report available at the link:** https://www.example.com
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<b>Results:</b>
<b>Environment: </b>Test
<b>Comment: </b>Test comment
<b>Duration: </b>00:00:24.011
<b>Total scenarios: </b>3
<b>Total passed: </b>2 (66.7 %)
<b>Total failed: </b>1 (33.3 %)
<b>Number of test suites: </b>2
<b>Name of the test suite: </b>batch-1
<code>Total scenarios: 2</code>
<code>Total passed: 1</code>
<code>Total failed: 1</code>
<b>Name of the test suite: </b>batch-2
<code>Total scenarios: 1</code>
<code>Total passed: 1</code>
<b>Report available at the link:</b> <a href="https://www.example.com">https://www.example.com</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"base": {
"project": "TEST PROJECT",
"environment": "Test",
"comment": "Test comment",
"reportLink": "https://www.example.com",
"language": "en",
"allureFolder": "build/allure-report/",
"enableChart": true,
"logo": "logo.png",
"durationFormat": "HH:mm:ss.SSS",
"enableSuitesPublishing": "true"
},
"mail": {
"host": "smtp.gmail.com",
"port": "465",
"username": "user",
"password": "password",
"securityProtocol": "SSL",
"from": "test1@gmail.com",
"recipient": "test2@gmail.com"
},
"rocketChat" : {
"url": "https://www.example.com",
"auth_token": "authToken",
"user_id": "userId",
"channel": "channel",
"templatePath": "/templates/rocket.ftl"
},
"discord": {
"botToken": "testToken",
"channelId": "testChannelId",
"templatePath": "/templates/markdown.ftl"
},
"telegram": {
"token": "testToken",
"chat": "testChat",
"replyTo": "user",
"templatePath": "/templates/telegram.ftl"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"total":2,"items":[{"uid":"87b8be25e9c9e007e80824e508a2043b","name":"batch-1","statistic":{"failed":1,"broken":0,"skipped":0,"passed":1,"unknown":0,"total":2}},{"uid":"9c1ca7280386cbcc24a7a39c461dd533","name":"batch-2","statistic":{"failed":0,"broken":0,"skipped":0,"passed":1,"unknown":0,"total":1}}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"reportName":"TEST Report","testRuns":[],"statistic":{"failed":1,"broken":0,"skipped":0,"passed":2,"unknown":0,"total":3},"time":{"start":1723022106448,"stop":1723022130459,"duration":24011,"minDuration":2045,"maxDuration":15512,"sumDuration":21736}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<b>${duration}: </b>${time}<br/>
<b>${totalScenarios}: </b>${total}
<ul>
<#if passed != 0 ><li><b>${totalPassed}: </b>${passed} (${passedPercentage} %)</li></#if>
<#if failed != 0 ><li><b>${totalFailed}: </b>${failed} (${failedPercentage} %)</li></#if>
<#if passed != 0 ><li><b>${totalPassed}: </b>${passed} (${passedPercentage?string("##.#")} %)</li></#if>
<#if failed != 0 ><li><b>${totalFailed}: </b>${failed} (${failedPercentage?string("##.#")} %)</li></#if>
<#if broken != 0 ><li><b>${totalBroken}: </b>${broken}</li></#if>
<#if unknown != 0 ><li><b>${totalUnknown}: </b>${unknown}</li></#if>
<#if skipped != 0 ><li><b>${totalSkipped}: </b>${skipped}</li></#if>
Expand Down