-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify message text generation (#317)
- Loading branch information
Showing
24 changed files
with
98 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
...ations-api/src/main/java/guru/qa/allure/notifications/template/config/TemplateConfig.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ class EmailTests { | |
private static final String CC = "[email protected], [email protected]"; | ||
private static final String BCC = "[email protected]"; | ||
private static final String PROJECT = "LETTER PROJECT"; | ||
private static final String EMPTY_TEMPLATE_PATH = "/guru/qa/allure/notifications/template/emptyTemplate.ftl"; | ||
private static final String EMPTY_TEMPLATE_PATH = "/template/emptyTemplate.ftl"; | ||
private static final String TEXT_FROM_TEMPLATE = "for test purposes"; | ||
private static final byte[] IMG = new byte[1]; | ||
|
||
|
102 changes: 27 additions & 75 deletions
102
...cations-api/src/test/java/guru/qa/allure/notifications/template/MessageTemplateTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,60 @@ | ||
package guru.qa.allure.notifications.template; | ||
|
||
import static java.lang.ClassLoader.getSystemResource; | ||
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; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
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.Optional; | ||
import java.util.stream.Stream; | ||
|
||
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 org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class MessageTemplateTests { | ||
public static final String FILE_NAME = "testTemplate.ftl"; | ||
|
||
public static final String FILESYSTEM_PATH = "guru/qa/allure/notifications/template/" + FILE_NAME; | ||
public static final String RESOURCES_PATH = "/templates/" + FILE_NAME; | ||
|
||
@Mock private MessageData messageData; | ||
@Mock private Configuration configuration; | ||
@Mock private Template template; | ||
|
||
@Test | ||
void shouldCreateMessageFromTemplateFromFileSystem() throws MessageBuildException, IOException { | ||
try (MockedConstruction<TemplateConfig> configMock = mockConstruction(TemplateConfig.class, | ||
(mock, context) -> when(mock.configure(any())).thenReturn(configuration))) { | ||
String fileSystemPath = ClassLoader.getSystemResource(FILESYSTEM_PATH).getFile(); | ||
when(configuration.getTemplate(FILE_NAME)).thenReturn(template); | ||
when(messageData.getValues()).thenReturn(new HashMap<>()); | ||
MessageTemplate messageTemplate = new MessageTemplate(messageData); | ||
messageTemplate.createMessageFromTemplate(fileSystemPath); | ||
|
||
TemplateConfig templateConfig = configMock.constructed().get(0); | ||
verify(templateConfig).configure(argThat(f -> | ||
new File(fileSystemPath).toString().equals(f.get().toString()))); | ||
verify(configuration).getTemplate(FILE_NAME); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldCreateMessageFromTemplateFromJar() throws MessageBuildException, IOException { | ||
try (MockedConstruction<TemplateConfig> configMock = mockConstruction(TemplateConfig.class, | ||
(mock, context) -> when(mock.configure(any())).thenReturn(configuration))) { | ||
when(configuration.getTemplate(RESOURCES_PATH)).thenReturn(template); | ||
when(messageData.getValues()).thenReturn(new HashMap<>()); | ||
MessageTemplate messageTemplate = new MessageTemplate(messageData); | ||
messageTemplate.createMessageFromTemplate(RESOURCES_PATH); | ||
|
||
TemplateConfig templateConfig = configMock.constructed().get(0); | ||
verify(templateConfig).configure(Optional.empty()); | ||
verify(configuration).getTemplate(RESOURCES_PATH); | ||
} | ||
static Stream<Arguments> testData() { | ||
return Stream.of( | ||
arguments("templates/html.ftl", "messages/html.txt"), | ||
arguments("templates/markdown.ftl", "messages/markdown.txt"), | ||
arguments("templates/rocket.ftl", "messages/rocket.txt"), | ||
arguments("templates/telegram.ftl", "messages/telegram.txt"), | ||
arguments("template/testTemplateAsResource.ftl", "messages/customHtml.txt"), | ||
arguments(getSystemResource("template/testTemplateAsFile.ftl").getFile(), "messages/customHtml.txt") | ||
); | ||
} | ||
|
||
@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", | ||
"guru/qa/allure/notifications/template/testTemplate.ftl, guru/qa/allure/notifications/template/messages/customHtml.txt" | ||
}) | ||
@MethodSource("testData") | ||
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); | ||
|
||
Config config = new JSON().parseResource("/data/testConfig.json", Config.class); | ||
Summary summary = new JSON().parseResource("/data/testSummary.json", Summary.class); | ||
String suitesSummaryJson = readResource("data/testSuites.json"); | ||
Phrases phrases = new JSON().parseResource("/phrases/en.json", Phrases.class); | ||
|
||
MessageData testMessageData = new MessageData(config.getBase(), summary, suitesSummaryJson, phrases); | ||
MessageTemplate messageTemplate = new MessageTemplate(testMessageData); | ||
MessageData messageData = new MessageData(config.getBase(), summary, suitesSummaryJson, phrases); | ||
|
||
String messageGeneratedFromTemplate = messageTemplate.createMessageFromTemplate(templatePath); | ||
URL expectedMessageUrl = classLoader.getResource(expectedMessagePath); | ||
String expectedMessage = new String(Files.readAllBytes(Paths.get(expectedMessageUrl.toURI())), UTF_8); | ||
String messageGeneratedFromTemplate = MessageTemplate.createMessageFromTemplate(messageData, templatePath); | ||
String expectedMessage = readResource(expectedMessagePath); | ||
assertEquals(expectedMessage, messageGeneratedFromTemplate); | ||
} | ||
|
||
private String readResource(String name) throws IOException, URISyntaxException { | ||
URL resourceUrl = getClass().getClassLoader().getResource(name); | ||
return new String(Files.readAllBytes(Paths.get(resourceUrl.toURI())), UTF_8); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.