Skip to content

Commit

Permalink
Examples update
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Feb 12, 2024
1 parent fb11cb0 commit 4a8a2b2
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 197 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.epam.reportportal.example.cucumber.util;

public enum Attachment {
CMD(".cmd"),
CSS(".css"),
CSV(".csv"),
HAR(".har"),
HTML(".html"),
JPG(".jpg"),
JS(".js"),
JSON(".json"),
PDF(".pdf"),
PHP(".php"),
PNG(".png"),
TAR(".tar"),
TAR_GZ(".tar.gz"),
TXT(".txt"),
XML(".xml"),
ZIP(".zip");
CMD("cmd"),
CSS("css"),
CSV("csv"),
HAR("har"),
HTML("html"),
JPG("jpg"),
JS("js"),
JSON("json"),
PDF("pdf"),
PHP("php"),
PNG("png"),
TAR("tar"),
TAR_GZ("tar.gz"),
TXT("txt"),
XML("xml"),
ZIP("zip");

private String extension;
private final String extension;

public String getExtension() {
return extension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import java.io.OutputStream;

public class AttachmentHelper {

public static final String FILE_NAME = "file";

public static File getFileFromResources(String name, String extension) {
public static File getFileFromResources(String path, String name, String extension) {
File file = null;
try {
file = File.createTempFile("rp-test", extension);
ByteSource source = Utils.getFileAsByteSource(new File(String.format("files/%s%s", name, extension)));
ByteSource source = Utils.getFileAsByteSource(new File(String.format("%s/%s.%s", path, name, extension)));
try (InputStream is = source.openStream()) {
try (OutputStream os = java.nio.file.Files.newOutputStream(file.toPath())) {
Utils.copyStreams(is, os);
Expand All @@ -26,4 +25,8 @@ public static File getFileFromResources(String name, String extension) {
}
return file;
}

public static File getFileFromResources(String name, String extension) {
return getFileFromResources("src/test/resources/files", name, extension);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.epam.reportportal.example.cucumber2.util;

public enum Attachment {
CMD(".cmd"),
CSS(".css"),
CSV(".csv"),
HAR(".har"),
HTML(".html"),
JPG(".jpg"),
JS(".js"),
JSON(".json"),
PDF(".pdf"),
PHP(".php"),
PNG(".png"),
TAR(".tar"),
TAR_GZ(".tar.gz"),
TXT(".txt"),
XML(".xml"),
ZIP(".zip");
CMD("cmd"),
CSS("css"),
CSV("csv"),
HAR("har"),
HTML("html"),
JPG("jpg"),
JS("js"),
JSON("json"),
PDF("pdf"),
PHP("php"),
PNG("png"),
TAR("tar"),
TAR_GZ("tar.gz"),
TXT("txt"),
XML("xml"),
ZIP("zip");

private String extension;
private final String extension;

public String getExtension() {
return extension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import java.io.OutputStream;

public class AttachmentHelper {

public static final String FILE_NAME = "file";

public static File getFileFromResources(String name, String extension) {
public static File getFileFromResources(String path, String name, String extension) {
File file = null;
try {
file = File.createTempFile("rp-test", extension);
ByteSource source = Utils.getFileAsByteSource(new File(String.format("files/%s%s", name, extension)));
ByteSource source = Utils.getFileAsByteSource(new File(String.format("%s/%s.%s", path, name, extension)));
try (InputStream is = source.openStream()) {
try (OutputStream os = java.nio.file.Files.newOutputStream(file.toPath())) {
Utils.copyStreams(is, os);
Expand All @@ -26,4 +25,8 @@ public static File getFileFromResources(String name, String extension) {
}
return file;
}

public static File getFileFromResources(String name, String extension) {
return getFileFromResources("src/test/resources/files", name, extension);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@
package com.epam.reportportal.example.cucumber4.util;

public enum Attachment {
CMD(".cmd"),
CSS(".css"),
CSV(".csv"),
HAR(".har"),
HTML(".html"),
JPG(".jpg"),
JS(".js"),
JSON(".json"),
PDF(".pdf"),
PHP(".php"),
PNG(".png"),
TAR(".tar"),
TAR_GZ(".tar.gz"),
TXT(".txt"),
XML(".xml"),
ZIP(".zip");
CMD("cmd"),
CSS("css"),
CSV("csv"),
HAR("har"),
HTML("html"),
JPG("jpg"),
JS("js"),
JSON("json"),
PDF("pdf"),
PHP("php"),
PNG("png"),
TAR("tar"),
TAR_GZ("tar.gz"),
TXT("txt"),
XML("xml"),
ZIP("zip");

private String extension;
private final String extension;

public String getExtension() {
return extension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
public class AttachmentHelper {
public static final String FILE_NAME = "file";

public static File getFileFromResources(String name, String extension) {
public static File getFileFromResources(String path, String name, String extension) {
File file = null;
try {
file = File.createTempFile("rp-test", extension);
ByteSource source = Utils.getFileAsByteSource(new File(String.format("files/%s%s", name, extension)));
ByteSource source = Utils.getFileAsByteSource(new File(String.format("%s/%s.%s", path, name, extension)));
try (InputStream is = source.openStream()) {
try (OutputStream os = java.nio.file.Files.newOutputStream(file.toPath())) {
Utils.copyStreams(is, os);
Expand All @@ -41,4 +41,8 @@ public static File getFileFromResources(String name, String extension) {
}
return file;
}

public static File getFileFromResources(String name, String extension) {
return getFileFromResources("src/test/resources/files", name, extension);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.epam.reportportal.service.tree.TestItemTree;
import com.epam.ta.reportportal.ws.model.FinishTestItemRQ;
import com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ;
import com.google.common.collect.Sets;
import org.junit.AfterClass;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -19,6 +18,7 @@

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import static java.util.Optional.ofNullable;
Expand All @@ -35,8 +35,7 @@ public class SaucelabsAttributeTest {
public TestRule rule = new TestWatcher() {
@Override
protected void finished(Description description) {
ofNullable(ItemTreeUtils.retrieveLeaf(
description,
ofNullable(ItemTreeUtils.retrieveLeaf(description,
ParallelRunningContext.getCurrent().getItemTree()
)).ifPresent(testItemLeaves::add);
}
Expand All @@ -56,9 +55,8 @@ private static void addSaucelabsAttribute() {
FinishTestItemRQ request = new FinishTestItemRQ();
request.setEndTime(Calendar.getInstance().getTime());
request.setStatus("PASSED");
request.setAttributes(Sets.newHashSet(new ItemAttributesRQ("SLID", "0586c1c90fcd4a499591109692426d54")));
ItemTreeReporter.finishItem(
ReportPortalListener.getReportPortal().getClient(),
request.setAttributes(Collections.singleton(new ItemAttributesRQ("SLID", "0586c1c90fcd4a499591109692426d54")));
ItemTreeReporter.finishItem(ReportPortalListener.getReportPortal().getClient(),
request,
ParallelRunningContext.getCurrent().getItemTree().getLaunchId(),
testItemLeaves.get(0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.epam.reportportal.example.junit.logging;

import com.epam.reportportal.example.junit.LoggingUtils;
import com.epam.reportportal.utils.files.ByteSource;
import com.epam.reportportal.utils.files.Utils;
import com.epam.reportportal.example.junit.util.AttachmentHelper;
import org.junit.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static org.junit.Assert.assertTrue;

Expand All @@ -29,16 +26,10 @@ public void before() {
}

@Test
public void test1() throws IOException {
public void test1() {
LOGGER.info("Inside AfterAndBeforeMethodTest test ");
// Report launch log
File file = File.createTempFile("rp-test", ".xml");
ByteSource source = Utils.getFileAsByteSource(new File("logback.xml"));
try (InputStream is = source.openStream()) {
try (OutputStream os = java.nio.file.Files.newOutputStream(file.toPath())) {
Utils.copyStreams(is, os);
}
}
File file = AttachmentHelper.getFileFromResources("src/test/resources", "logback", "xml");
int n = 5;
while (n-- > 0) {
LoggingUtils.log(file, "LAUNCH LOG MESSAGE WITH ATTACHMENT");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.epam.reportportal.example.junit.logging;

import com.epam.reportportal.example.junit.LoggingUtils;
import com.epam.reportportal.example.junit.util.AttachmentHelper;
import com.epam.reportportal.service.ReportPortal;
import com.epam.reportportal.utils.files.ByteSource;
import com.epam.reportportal.utils.files.Utils;
import com.google.common.io.Resources;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import java.util.Date;

/**
Expand All @@ -20,36 +19,28 @@
*/
public class JsonLoggingTest {

public static final String JSON_FILE_PATH = "xml/file.json";
public static final String JSON_FILE_PATH = "src/test/resources/xml";
private static final Logger LOGGER = LoggerFactory.getLogger(JsonLoggingTest.class);

@Test
public void logJsonBase64() throws IOException {
/* here we are logging some binary data as BASE64 string */
ReportPortal.emitLog("LAUNCH LOG MESAGE", "error", new Date());

File file = File.createTempFile("rp-test", ".css");
ByteSource source = Utils.getFileAsByteSource(new File("files/file.css"));
try (InputStream is = source.openStream()) {
try (OutputStream os = Files.newOutputStream(file.toPath())) {
Utils.copyStreams(is, os);
}
}
ReportPortal.emitLog("LAUNCH LOG MESAGE WITH ATTACHMENT", "error", new Date(), file);
LoggingUtils.log(Resources.asByteSource(Resources.getResource(JSON_FILE_PATH)).read(), "I'm logging content via BASE64");
ReportPortal.emitLog("LAUNCH LOG MESSAGE", "error", new Date());

File file = AttachmentHelper.getFileFromResources(JSON_FILE_PATH, "file", "json");
ReportPortal.emitLog("LAUNCH LOG MESSAGE WITH ATTACHMENT", "error", new Date(), file);
LOGGER.info("RP_MESSAGE#FILE#{}#{}", file.getAbsolutePath(), "I'm logging content via temp file");
LOGGER.info(
"RP_MESSAGE#BASE64#{}#{}",
Base64.getEncoder().encodeToString(Utils.getFileAsByteSource(file).read()),
"I'm logging content via BASE64"
);
}

@Test
public void logJsonFile() throws IOException {
public void logJsonFile() {
/* here we are logging some binary data as file (useful for selenium) */
File file = File.createTempFile("rp-test", ".json");
ByteSource source = Utils.getFileAsByteSource(new File(JSON_FILE_PATH));
try (InputStream is = source.openStream()) {
try (OutputStream os = Files.newOutputStream(file.toPath())) {
Utils.copyStreams(is, os);
}
}

File file = AttachmentHelper.getFileFromResources(JSON_FILE_PATH, "file", "json");
for (int i = 0; i < 1; i++) {
LOGGER.info("RP_MESSAGE#FILE#{}#{}", file.getAbsolutePath(), "I'm logging content via temp file");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.epam.reportportal.example.junit.logging;

import com.epam.reportportal.example.junit.LoggingUtils;
import com.epam.reportportal.example.junit.MagicRandomizer;
import com.google.common.io.Resources;
import com.epam.reportportal.utils.files.Utils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.Base64;

/**
* Logs image
Expand All @@ -26,11 +27,15 @@ public void logImageBase64() throws IOException {
/* 50 percents. So we should have approximately same count of lucky and unlucky pugs */
boolean happy = MagicRandomizer.checkYourLucky(30);
String image = getImageResource(happy);
LoggingUtils.log(Resources.asByteSource(Resources.getResource(image)).read(), "Pug is " + (happy ? "HAPPY" : "NOT HAPPY"));
LOGGER.info(
"RP_MESSAGE#BASE64#{}#{}",
Base64.getEncoder().encodeToString(Utils.getFileAsByteSource(new File(image)).read()),
"Pug is " + (happy ? "HAPPY" : "NOT HAPPY")
);
}
}

private String getImageResource(boolean lucky) {
return "pug/" + (lucky ? "lucky.jpg" : "unlucky.jpg");
return "src/test/resources/pug/" + (lucky ? "lucky.jpg" : "unlucky.jpg");
}
}
Loading

0 comments on commit 4a8a2b2

Please sign in to comment.