-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add carousel attachment feature (via #52)
- Loading branch information
1 parent
330ea77
commit f85583e
Showing
13 changed files
with
299 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
version=3.18-SNAPSHOT | ||
# Graal VM | ||
graalVersion=19.2.0 | ||
graalVersion=23.0.1 |
20 changes: 20 additions & 0 deletions
20
src/main/java/io/eroshenkoam/xcresults/carousel/Carousel.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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.eroshenkoam.xcresults.carousel; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class Carousel implements Serializable { | ||
|
||
private final List<CarouselImage> images; | ||
|
||
public Carousel(final List<CarouselImage> images) { | ||
this.images = Optional.ofNullable(images).orElseGet(ArrayList::new); | ||
} | ||
|
||
public List<CarouselImage> getImages() { | ||
return images; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/eroshenkoam/xcresults/carousel/CarouselImage.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.eroshenkoam.xcresults.carousel; | ||
|
||
import java.io.Serializable; | ||
|
||
public class CarouselImage implements Serializable { | ||
|
||
private final String name; | ||
private final String base64; | ||
|
||
public CarouselImage(final String name, final String base64) { | ||
this.name = name; | ||
this.base64 = base64; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getBase64() { | ||
return base64; | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/io/eroshenkoam/xcresults/carousel/CarouselPostProcessor.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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.eroshenkoam.xcresults.carousel; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import freemarker.template.TemplateException; | ||
import io.eroshenkoam.xcresults.export.ExportPostProcessor; | ||
import io.eroshenkoam.xcresults.util.FreemarkerUtil; | ||
import io.qameta.allure.model.Attachment; | ||
import io.qameta.allure.model.ExecutableItem; | ||
import io.qameta.allure.model.TestResult; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
import static io.eroshenkoam.xcresults.util.FormatUtil.getAttachmentFileName; | ||
|
||
public class CarouselPostProcessor implements ExportPostProcessor { | ||
|
||
private final ObjectMapper mapper = new ObjectMapper() | ||
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); | ||
|
||
private final String templatePath; | ||
|
||
public CarouselPostProcessor(final String templatePath) { | ||
this.templatePath = templatePath; | ||
} | ||
|
||
@Override | ||
public void processTestResults(final Path outputPath, final Map<Path, TestResult> testResults) { | ||
System.out.println("Carousel attachment feature enabled"); | ||
Optional.ofNullable(templatePath) | ||
.map(t -> String.format("Carousel template: %s", t)) | ||
.ifPresent(System.out::println); | ||
testResults.forEach((path, result) -> processTestResult(outputPath, path, result)); | ||
} | ||
|
||
private void processTestResult(final Path outputPath, final Path path, final TestResult testResult) { | ||
final List<Attachment> attachments = getAttachment(testResult, (a) -> a.getName().endsWith(".jpeg")); | ||
final List<CarouselImage> carouselImages = attachments.stream() | ||
.map(a -> convert(outputPath, a)) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.collect(Collectors.toList()); | ||
if (carouselImages.size() == 0) { | ||
return; | ||
} | ||
try { | ||
final Carousel carousel = new Carousel(carouselImages); | ||
final Map<String, Object> data = Map.of("carousel", carousel); | ||
final String carouselContent = Objects.nonNull(templatePath) | ||
? FreemarkerUtil.render(Path.of(templatePath), data) | ||
: FreemarkerUtil.render("templates/carousel.ftl", data); | ||
final Path carouselPath = outputPath.resolve(getAttachmentFileName("html")); | ||
Files.write(carouselPath, carouselContent.getBytes(StandardCharsets.UTF_8)); | ||
testResult.getAttachments().add(new Attachment() | ||
.setName("Carousel") | ||
.setSource(carouselPath.getFileName().toString())); | ||
mapper.writeValue(path.toFile(), testResult); | ||
} catch (IOException | TemplateException e) { | ||
System.out.println("Can not create carousel attachment: " + e.getMessage()); | ||
} | ||
} | ||
|
||
private List<Attachment> getAttachment(final ExecutableItem item, final Predicate<Attachment> filter) { | ||
final List<Attachment> attachments = new ArrayList<>(); | ||
item.getAttachments().stream() | ||
.filter(filter) | ||
.forEach(attachments::add); | ||
if (Objects.nonNull(item.getSteps())) { | ||
item.getSteps().forEach(step -> attachments.addAll(getAttachment(step, filter))); | ||
} | ||
return attachments; | ||
} | ||
|
||
private Optional<CarouselImage> convert(final Path outputPath, final Attachment attachment) { | ||
try { | ||
final byte[] bytes = Files.readAllBytes(outputPath.resolve(attachment.getSource())); | ||
final String content = "data:image/jpeg;base64, " + Base64.getEncoder().encodeToString(bytes); | ||
return Optional.of(new CarouselImage(attachment.getName(), content)); | ||
} catch (IOException e) { | ||
return Optional.empty(); | ||
} | ||
|
||
} | ||
|
||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/io/eroshenkoam/xcresults/export/ExportPostProcessor.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.eroshenkoam.xcresults.export; | ||
|
||
import io.qameta.allure.model.TestResult; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
public interface ExportPostProcessor { | ||
|
||
void processTestResults(Path outputPath, Map<Path, TestResult> testResults); | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/io/eroshenkoam/xcresults/util/FreemarkerUtil.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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.eroshenkoam.xcresults.util; | ||
|
||
import freemarker.template.Configuration; | ||
import freemarker.template.Template; | ||
import freemarker.template.TemplateException; | ||
import freemarker.template.TemplateExceptionHandler; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
public class FreemarkerUtil { | ||
|
||
private FreemarkerUtil() { | ||
} | ||
|
||
public static String render(final Path templatePath, final Map<String, Object> data) | ||
throws IOException, TemplateException { | ||
final Template template = new Template( | ||
templatePath.getFileName().toString(), Files.readString(templatePath), getDefaultConfiguration() | ||
); | ||
return render(template, data); | ||
} | ||
|
||
public static String render(final String templateName, final Map<String, Object> data) | ||
throws IOException, TemplateException { | ||
return render(getDefaultConfiguration().getTemplate(templateName), data); | ||
} | ||
|
||
public static Configuration getDefaultConfiguration() { | ||
Configuration configuration = new Configuration(Configuration.VERSION_2_3_30); | ||
configuration.setClassForTemplateLoading(FreemarkerUtil.class, "/"); | ||
configuration.setDefaultEncoding("UTF-8"); | ||
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); | ||
configuration.setLogTemplateExceptions(false); | ||
configuration.setWrapUncheckedExceptions(true); | ||
configuration.setFallbackOnNullLoopVariable(false); | ||
return configuration; | ||
} | ||
|
||
private static String render(final Template template, final Map<String, Object> data) | ||
throws IOException, TemplateException { | ||
try (StringWriter result = new StringWriter()) { | ||
template.process(data, result); | ||
return result.toString(); | ||
} | ||
} | ||
|
||
} |
Binary file not shown.
9 changes: 9 additions & 0 deletions
9
src/main/resources/META-INF/native-image/org.freemarker/freemarker/resource-config.json
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"resources": { | ||
"includes": [ | ||
{ | ||
"pattern": ".*\\.properties" | ||
} | ||
] | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/resources/META-INF/native-image/resource-config.json
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"resources": { | ||
"includes": [ | ||
{ | ||
"pattern": "templates/.*\\.ftl" | ||
} | ||
] | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.