Skip to content

Commit e1e7af4

Browse files
committed
rft allure http attachments
1 parent 0ddcbb4 commit e1e7af4

42 files changed

Lines changed: 667 additions & 1094 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Usage example:
7272
```
7373
.filter(new AllureLoggerFilter())
7474
```
75-
You can specify custom template:
75+
You can specify custom templateName:
7676
```
7777
.filter(new AllureLoggerFilter().withTemplate("/templates/custom_template.ftl"))
7878
```
@@ -93,7 +93,7 @@ Usage example:
9393
```
9494
.addInterceptor(new AllureLoggingInterceptor())
9595
```
96-
You can specify custom template:
96+
You can specify custom templateName:
9797
```
9898
.addInterceptor(new AllureLoggingInterceptor().withTemplate("/templates/custom_template.ftl"))
9999
```

allure-attachments/build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
description = 'Allure attachments'
2+
3+
apply from: "${gradleScriptDir}/maven-publish.gradle"
4+
apply from: "${gradleScriptDir}/bintray.gradle"
5+
apply plugin: 'maven'
6+
7+
dependencies {
8+
compile project(':allure-java-commons')
9+
10+
compile 'org.freemarker:freemarker'
11+
compile 'commons-io:commons-io'
12+
compile 'org.apache.commons:commons-lang3'
13+
14+
testCompile 'org.assertj:assertj-core'
15+
testCompile 'junit:junit'
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.qameta.allure.attachment;
2+
3+
/**
4+
* @author charlie (Dmitry Baev).
5+
*/
6+
public interface AttachmentContent {
7+
8+
String getContent();
9+
10+
String getContentType();
11+
12+
String getFileExtension();
13+
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.qameta.allure.attachment;
2+
3+
/**
4+
* Marker interface for complex Allure attachments.
5+
*
6+
* @author charlie (Dmitry Baev).
7+
*/
8+
public interface AttachmentData {
9+
10+
String getName();
11+
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.qameta.allure.attachment;
2+
3+
/**
4+
* @param <T> the type of attachment data.
5+
* @author charlie (Dmitry Baev).
6+
*/
7+
public interface AttachmentProcessor<T extends AttachmentData> {
8+
9+
void addAttachment(T attachmentData, AttachmentRenderer<T> renderer);
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.qameta.allure.attachment;
2+
3+
/**
4+
* @author charlie (Dmitry Baev).
5+
*/
6+
public class AttachmentRenderException extends RuntimeException {
7+
8+
public AttachmentRenderException(final String message, final Throwable cause) {
9+
super(message, cause);
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.qameta.allure.attachment;
2+
3+
/**
4+
* @param <T> the type of attachment data
5+
* @author charlie (Dmitry Baev).
6+
*/
7+
public interface AttachmentRenderer<T extends AttachmentData> {
8+
9+
AttachmentContent render(T attachmentData) throws AttachmentRenderException;
10+
11+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.qameta.allure.attachment;
2+
3+
/**
4+
* @author charlie (Dmitry Baev).
5+
*/
6+
public class DefaultAttachmentContent implements AttachmentContent {
7+
8+
private final String content;
9+
10+
private final String contentType;
11+
12+
private final String fileExtension;
13+
14+
public DefaultAttachmentContent(final String content,
15+
final String contentType,
16+
final String fileExtension) {
17+
this.content = content;
18+
this.contentType = contentType;
19+
this.fileExtension = fileExtension;
20+
}
21+
22+
@Override
23+
public String getContent() {
24+
return content;
25+
}
26+
27+
@Override
28+
public String getContentType() {
29+
return contentType;
30+
}
31+
32+
@Override
33+
public String getFileExtension() {
34+
return fileExtension;
35+
}
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.qameta.allure.attachment;
2+
3+
import io.qameta.allure.Allure;
4+
5+
/**
6+
* @author charlie (Dmitry Baev).
7+
*/
8+
public class DefaultAttachmentProcessor implements AttachmentProcessor<AttachmentData> {
9+
10+
@Override
11+
public void addAttachment(final AttachmentData attachmentData,
12+
final AttachmentRenderer<AttachmentData> renderer) {
13+
final AttachmentContent content = renderer.render(attachmentData);
14+
Allure.addAttachment(
15+
attachmentData.getName(),
16+
content.getContentType(),
17+
content.getContent(),
18+
content.getFileExtension()
19+
);
20+
}
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.qameta.allure.attachment;
2+
3+
import freemarker.template.Configuration;
4+
import freemarker.template.Template;
5+
6+
import java.io.StringWriter;
7+
import java.io.Writer;
8+
import java.util.Collections;
9+
10+
/**
11+
* @author charlie (Dmitry Baev).
12+
*/
13+
public class FreemarkerAttachmentRenderer implements AttachmentRenderer<AttachmentData> {
14+
15+
private final Configuration configuration;
16+
17+
private final String templateName;
18+
19+
public FreemarkerAttachmentRenderer(final String templateName) {
20+
this.templateName = templateName;
21+
this.configuration = new Configuration(Configuration.VERSION_2_3_23);
22+
this.configuration.setLocalizedLookup(false);
23+
this.configuration.setTemplateUpdateDelayMilliseconds(0);
24+
this.configuration.setClassLoaderForTemplateLoading(getClass().getClassLoader(), "tpl");
25+
}
26+
27+
public DefaultAttachmentContent render(final AttachmentData data) {
28+
try (Writer writer = new StringWriter()) {
29+
final Template template = configuration.getTemplate(templateName);
30+
template.process(Collections.singletonMap("data", data), writer);
31+
return new DefaultAttachmentContent(writer.toString(), "text/html", ".html");
32+
} catch (Exception e) {
33+
throw new AttachmentRenderException("Could't render http attachment file", e);
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)