Skip to content

Commit

Permalink
add support for unit tests (via #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
eroshenkoam committed Jun 27, 2020
1 parent 040452a commit 5474ae7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
import io.qameta.allure.model.StepResult;
import io.qameta.allure.model.TestResult;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.eroshenkoam.xcresults.util.ParseUtil.parseDate;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

Expand Down Expand Up @@ -74,11 +74,15 @@ public TestResult format(final ExportMeta meta, final JsonNode node) {
meta.getLabels().forEach((name, value) -> {
result.getLabels().add(new Label().setName(name).setValue(value));
});
if (nonNull(result.getStart())) {
if (Objects.isNull(result.getStart())) {
result.setStart(meta.getStart());
}
if (Objects.nonNull(result.getStart())) {
if (node.has(DURATION)) {
final Double durationText = node.get(DURATION).get(VALUE).asDouble();
result.setStop(result.getStart() + TimeUnit.SECONDS.toMillis(durationText.longValue()));
} else {
}
if (result.getSteps().size() > 0) {
result.setStop(result.getSteps().get(result.getSteps().size() - 1).getStop());
}
}
Expand Down Expand Up @@ -211,16 +215,6 @@ private Optional<String> getActivityTitle(final JsonNode node) {
return Optional.empty();
}

@SuppressWarnings("PMD.SimpleDateFormatNeedsLocale")
private Long parseDate(final String date) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
return format.parse(date).getTime();
} catch (ParseException e) {
return null;
}
}

private class StepContext {

private TestResult result;
Expand Down
50 changes: 39 additions & 11 deletions src/main/java/io/eroshenkoam/xcresults/export/ExportCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import static io.eroshenkoam.xcresults.util.ParseUtil.parseDate;

@CommandLine.Command(
name = "export", mixinStandardHelpOptions = true,
Expand All @@ -26,6 +29,7 @@ public class ExportCommand implements Runnable {
private static final String ACTION_RESULT = "actionResult";

private static final String RUN_DESTINATION = "runDestination";
private static final String START_TIME = "startedTime";

private static final String SUMMARIES = "summaries";
private static final String TESTABLE_SUMMARIES = "testableSummaries";
Expand All @@ -44,9 +48,12 @@ public class ExportCommand implements Runnable {
private static final String SUMMARY_REF = "summaryRef";

private static final String ID = "id";
private static final String TYPE = "_type";
private static final String NAME = "_name";
private static final String VALUE = "_value";
private static final String VALUES = "_values";
private static final String DISPLAY_NAME = "displayName";
private static final String IDENTIFIER = "identifier";

private static final String TEST_REF = "testsRef";

Expand Down Expand Up @@ -95,31 +102,34 @@ private void runUnsafe() throws Exception {
if (action.has(RUN_DESTINATION)) {
meta.label(RUN_DESTINATION, action.get(RUN_DESTINATION).get(DISPLAY_NAME).get(VALUE).asText());
}
if (action.has(START_TIME)) {
meta.setStart(parseDate(action.get(START_TIME).get(VALUE).textValue()));
}
testRefIds.put(action.get(ACTION_RESULT).get(TEST_REF).get(ID).get(VALUE).asText(), meta);
}
}
final Map<String, ExportMeta> testSummaryRefs = new HashMap<>();
final Map<JsonNode, ExportMeta> testSummaries = new HashMap<>();
testRefIds.forEach((testRefId, meta) -> {
final JsonNode testRef = getReference(testRefId);
for (JsonNode summary : testRef.get(SUMMARIES).get(VALUES)) {
for (JsonNode testableSummary : summary.get(TESTABLE_SUMMARIES).get(VALUES)) {
for (JsonNode test : testableSummary.get(TESTS).get(VALUES)) {
getTestSummaryRefs(test).forEach(ref -> {
testSummaryRefs.put(ref, testRefIds.get(testRefId));
getTestSummaries(test).forEach(testSummary -> {
testSummaries.put(testSummary, testRefIds.get(testRefId));
});
}
}
}
});
System.out.println(String.format("Export information about %s test summaries...", testSummaryRefs.size()));
System.out.println(String.format("Export information about %s test summaries...", testSummaries.size()));
final Map<String, String> attachmentsRefs = new HashMap<>();
testSummaryRefs.forEach((testSummaryRef, meta) -> {
final JsonNode testSummary = getReference(testSummaryRef);
exportTestSummary(testSummaryRef, meta, testSummary);
for (final JsonNode activity : testSummary.get(ACTIVITY_SUMMARIES).get(VALUES)) {
attachmentsRefs.putAll(getAttachmentRefs(activity));
testSummaries.forEach((testSummary, meta) -> {
exportTestSummary(meta, testSummary);
if (testSummary.has(ACTIVITY_SUMMARIES)) {
for (final JsonNode activity : testSummary.get(ACTIVITY_SUMMARIES).get(VALUES)) {
attachmentsRefs.putAll(getAttachmentRefs(activity));
}
}

});
System.out.println(String.format("Export information about %s attachments...", attachmentsRefs.size()));
for (Map.Entry<String, String> attachment : attachmentsRefs.entrySet()) {
Expand All @@ -129,9 +139,10 @@ private void runUnsafe() throws Exception {
}
}

private void exportTestSummary(final String uuid, final ExportMeta meta, final JsonNode testSummary) {
private void exportTestSummary(final ExportMeta meta, final JsonNode testSummary) {
Path testSummaryPath = null;
Object formattedResult = null;
final String uuid = UUID.randomUUID().toString();
switch (format) {
case json: {
final String testSummaryFilename = String.format("%s.json", uuid);
Expand Down Expand Up @@ -174,6 +185,23 @@ private Map<String, String> getAttachmentRefs(final JsonNode test) {
return refs;
}

private List<JsonNode> getTestSummaries(final JsonNode test) {
final List<JsonNode> summaries = new ArrayList<>();
if (test.has(SUMMARY_REF)) {
final String ref = test.get(SUMMARY_REF).get(ID).get(VALUE).asText();
summaries.add(getReference(ref));
}
if (test.has(TYPE) && test.get(TYPE).get(NAME).textValue().equals("ActionTestMetadata")) {
summaries.add(test);
}
if (test.has(SUBTESTS)) {
for (final JsonNode subTest : test.get(SUBTESTS).get(VALUES)) {
summaries.addAll(getTestSummaries(subTest));
}
}
return summaries;
}

private List<String> getTestSummaryRefs(final JsonNode test) {
final List<String> refs = new ArrayList<>();
if (test.has(SUMMARY_REF)) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/eroshenkoam/xcresults/export/ExportMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@

public class ExportMeta {

private Long start;
private Map<String, String> labels;

public ExportMeta setStart(final Long startTime) {
this.start = startTime;
return this;
}

public Long getStart() {
return this.start;
}

public ExportMeta label(final String name, final String value) {
getLabels().put(name, value);
return this;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/eroshenkoam/xcresults/util/ParseUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.eroshenkoam.xcresults.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public final class ParseUtil {

private ParseUtil(){
}

@SuppressWarnings("PMD.SimpleDateFormatNeedsLocale")
public static Long parseDate(final String date) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
return format.parse(date).getTime();
} catch (ParseException e) {
return null;
}
}

}

0 comments on commit 5474ae7

Please sign in to comment.