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

process zip file in-memory #13

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
8 changes: 6 additions & 2 deletions plugin-junit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ apply from: scriptsUrl + '/release-fat.gradle'
apply from: scriptsUrl + '/signing.gradle'

repositories {
mavenLocal()
mavenCentral { url "https://repo1.maven.org/maven2" }
if (!releaseMode) {
maven { url 'https://jitpack.io' }
}
mavenLocal()
mavenCentral { url "https://repo1.maven.org/maven2" }
}

dependencyManagement {
Expand All @@ -55,7 +55,11 @@ dependencies {
}
implementation('org.hibernate:hibernate-core:5.4.18.Final')
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.7'
compile 'org.apache.commons:commons-compress:1.27.1'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
testImplementation "org.mockito:mockito-junit-jupiter:3.4.6"
}

spotbugs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.epam.ta.reportportal.dao.LaunchRepository;
import java.io.InputStream;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -32,6 +34,7 @@
*/
public class XmlImportStrategy extends AbstractImportStrategy {

private static final Logger log = LoggerFactory.getLogger(XmlImportStrategy.class);
private final XunitParseService xunitParseService;

public XmlImportStrategy(ApplicationEventPublisher eventPublisher,
Expand All @@ -51,7 +54,7 @@ public String importLaunch(MultipartFile file, String projectName, LaunchImportR
updateStartTime(launchUuid, parseResults.getStartTime());
return launchUuid;
} catch (Exception e) {
e.printStackTrace();
log.error("Error during launch import", e);
Optional.ofNullable(launchUuid).ifPresent(this::updateBrokenLaunch);
throw new ReportPortalException(ErrorType.IMPORT_FILE_ERROR, cleanMessage(e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.dao.LaunchRepository;
import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -43,6 +43,7 @@ public class ZipImportStrategy extends AbstractImportStrategy {
private static final Predicate<ZipEntry> isFile = zipEntry -> !zipEntry.isDirectory();
private static final Predicate<ZipEntry> isXml =
zipEntry -> zipEntry.getName().endsWith(XML_EXTENSION);
private static final Logger log = LoggerFactory.getLogger(ZipImportStrategy.class);

private final XunitParseService xunitParseService;

Expand All @@ -56,50 +57,43 @@ public ZipImportStrategy(ApplicationEventPublisher eventPublisher,
public String importLaunch(MultipartFile file, String projectName, LaunchImportRQ rq) {
//copy of the launch's id to use it in catch block if something goes wrong
String savedLaunchUuid = null;
File zip = transferToTempFile(file);

try (ZipFile zipFile = new ZipFile(zip)) {
try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(file.getInputStream(),
StandardCharsets.UTF_8.toString(), true, true)) {
String launchUuid = startLaunch(getLaunchName(file, ZIP_EXTENSION), projectName, rq);
savedLaunchUuid = launchUuid;
List<ParseResults> parseResults = zipFile.stream().filter(isFile.and(isXml))
.map(zipEntry ->
xunitParseService.call(getEntryStream(zipFile, zipEntry), launchUuid,
projectName,
isSkippedNotIssue(rq.getAttributes()))).collect(Collectors.toList());
List<ParseResults> parseResults = new ArrayList<>();
ZipEntry zipEntry;

while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (isFile.test(zipEntry) && isXml.test(zipEntry)) {

ByteArrayInputStream bais = getZipEntryInputStream(zipInputStream, zipEntry.getSize());

parseResults.add(xunitParseService.call(
bais,
launchUuid,
projectName,
isSkippedNotIssue(rq.getAttributes())));
}
}

ParseResults results = processResults(parseResults);
finishLaunch(launchUuid, projectName, results);
updateStartTime(launchUuid, results.getStartTime());
return launchUuid;
} catch (Exception e) {
e.printStackTrace();
log.error("Error during launch import", e);
updateBrokenLaunch(savedLaunchUuid);
throw new ReportPortalException(ErrorType.IMPORT_FILE_ERROR, cleanMessage(e));
} finally {
try {
Files.deleteIfExists(zip.getAbsoluteFile().toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}

private InputStream getEntryStream(ZipFile file, ZipEntry zipEntry) {
try {
return file.getInputStream(zipEntry);
} catch (IOException e) {
throw new ReportPortalException(ErrorType.IMPORT_FILE_ERROR, e.getMessage());
}
}
private ByteArrayInputStream getZipEntryInputStream(ZipArchiveInputStream zipInputStream,
long zipEntrySize) throws IOException {
byte[] bytes = new byte[(int) zipEntrySize];

private File transferToTempFile(MultipartFile file) {
try {
File tmp = File.createTempFile(file.getOriginalFilename(),
"." + FilenameUtils.getExtension(file.getOriginalFilename())
);
file.transferTo(tmp);
return tmp;
} catch (IOException e) {
throw new ReportPortalException("Error during transferring multipart file.", e);
}
zipInputStream.readNBytes(bytes, 0, (int) zipEntrySize);
return new ByteArrayInputStream(bytes);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.epam.reportportal.extension.importing.service;

import static com.epam.reportportal.extension.utils.FileUtils.getMultipartFile;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import com.epam.reportportal.extension.importing.model.LaunchImportRQ;
import com.epam.ta.reportportal.dao.LaunchRepository;
import com.epam.ta.reportportal.entity.launch.Launch;
import java.io.IOException;
import java.time.Instant;
import java.util.Optional;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@ExtendWith(MockitoExtension.class)
class ZipImportStrategyTest {

@Mock
private ApplicationEventPublisher applicationEventPublisher;

@Mock
private LaunchRepository launchRepository;

@InjectMocks
ZipImportStrategy zipImportStrategy;

@Test
void importLaunch() throws IOException {
CommonsMultipartFile multipartFile = getMultipartFile("test_reports.zip");

LaunchImportRQ rq = new LaunchImportRQ();
rq.setStartTime(Instant.now());

when(launchRepository.findByUuid(anyString())).thenReturn(Optional.of(new Launch()));

zipImportStrategy.importLaunch(multipartFile, "test-1", rq);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.epam.reportportal.extension.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class FileUtils {

public static CommonsMultipartFile getMultipartFile(String path) throws IOException {
File file = new ClassPathResource(path).getFile();
FileItem fileItem =
new DiskFileItem("mainFile", Files.probeContentType(file.toPath()), false, file.getName(),
(int) file.length(), file.getParentFile()
);
IOUtils.copy(new FileInputStream(file), fileItem.getOutputStream());
return new CommonsMultipartFile(fileItem);
}
}
Binary file added plugin-junit/src/test/resources/test_reports.zip
Binary file not shown.
Loading