Skip to content

Commit

Permalink
Add FailureState to Result
Browse files Browse the repository at this point in the history
  • Loading branch information
drdavella committed May 23, 2024
1 parent 78f6bf0 commit 289cb1d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/main/java/io/codemodder/codetf/CodeTFResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class CodeTFResult {
private final String summary;
private final String description;
private final DetectionTool detectionTool;
private final FailureState failureState;
private final Set<String> failedFiles;
private final List<CodeTFReference> references;
private final Map<String, String> properties;
Expand All @@ -24,15 +25,17 @@ public CodeTFResult(
@JsonProperty(value = "summary", index = 2) final String summary,
@JsonProperty(value = "description", index = 3) final String description,
@JsonProperty(value = "detectionTool", index = 4) final DetectionTool detectionTool,
@JsonProperty(value = "failedFiles", index = 5) final Set<String> failedFiles,
@JsonProperty(value = "references", index = 6) final List<CodeTFReference> references,
@JsonProperty(value = "properties", index = 7) final Map<String, String> properties,
@JsonProperty(value = "changeset", index = 8) final List<CodeTFChangesetEntry> changeset,
@JsonProperty(value = "unfixed", index = 9) final List<UnfixedFinding> unfixedFindings) {
@JsonProperty(value = "failureState", index = 5) final FailureState failureState,
@JsonProperty(value = "failedFiles", index = 6) final Set<String> failedFiles,
@JsonProperty(value = "references", index = 7) final List<CodeTFReference> references,
@JsonProperty(value = "properties", index = 8) final Map<String, String> properties,
@JsonProperty(value = "changeset", index = 9) final List<CodeTFChangesetEntry> changeset,
@JsonProperty(value = "unfixed", index = 10) final List<UnfixedFinding> unfixedFindings) {
this.codemod = CodeTFValidator.requireNonBlank(codemod);
this.summary = CodeTFValidator.requireNonBlank(summary);
this.description = CodeTFValidator.requireNonBlank(description);
this.detectionTool = detectionTool;
this.failureState = failureState;
this.failedFiles = CodeTFValidator.toImmutableCopyOrEmptyOnNull(failedFiles);
this.references = CodeTFValidator.toImmutableCopyOrEmptyOnNull(references);
this.properties = CodeTFValidator.toImmutableCopyOrEmptyOnNull(properties);
Expand All @@ -46,7 +49,7 @@ public CodeTFResult(
final String summary,
final String description,
final List<CodeTFChangesetEntry> changeset) {
this(codemod, summary, description, null, null, null, null, changeset, null);
this(codemod, summary, description, null, null, null, null, null, changeset, null);
}

public String getCodemod() {
Expand All @@ -65,6 +68,10 @@ public DetectionTool getDetectionTool() {
return detectionTool;
}

public FailureState getFailureState() {
return failureState;
}

public Set<String> getFailedFiles() {
return failedFiles;
}
Expand Down Expand Up @@ -108,6 +115,8 @@ public static class Builder {
private final CodeTFResult originalResult;
private String updatedSummary;
private String updatedDescription;

private FailureState failureState;
private List<CodeTFReference> updatedReferences;
private DetectionTool detectionTool;
private List<CodeTFChangesetEntry> changeset;
Expand All @@ -131,6 +140,11 @@ public Builder withDescription(final String description) {
return this;
}

public Builder withFailureState(final FailureState failureState) {
this.failureState = failureState;
return this;
}

public Builder withReferences(final List<CodeTFReference> references) {
Objects.requireNonNull(references);
this.updatedReferences = references;
Expand Down Expand Up @@ -171,6 +185,7 @@ public CodeTFResult build() {
updatedSummary != null ? updatedSummary : originalResult.getSummary(),
updatedDescription != null ? updatedDescription : originalResult.getDescription(),
detectionTool != null ? detectionTool : originalResult.getDetectionTool(),
failureState != null ? failureState : originalResult.getFailureState(),
originalResult.getFailedFiles(),
updatedReferences != null ? updatedReferences : originalResult.getReferences(),
originalResult.getProperties(),
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/io/codemodder/codetf/FailureState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.codemodder.codetf;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class FailureState {
private final boolean failed;
private final String reason;
private final String exception;

@JsonCreator
public FailureState(
@JsonProperty("failed") final boolean failed,
@JsonProperty("reason") final String reason,
@JsonProperty("exception") final String exception) {
this.failed = failed;
this.reason = reason;
this.exception = exception;
}

public boolean failed() {
return failed;
}

public String reason() {
return reason;
}

public String exception() {
return exception;
}
}
11 changes: 11 additions & 0 deletions src/test/java/io/codemodder/codetf/CodeTFResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void it_creates_result() {
"Hardened object deserialization calls against attack",
"Lengthier description about deserialization risks, protections, etc...",
null,
null,
Set.of("/foo/failed.java"),
List.of(
new CodeTFReference(
Expand Down Expand Up @@ -61,6 +62,7 @@ void it_creates_result_with_detection_tool() {
"Hardened object deserialization calls against attack",
"Lengthier description about deserialization risks, protections, etc...",
tool,
null,
Set.of("/foo/failed.java"),
List.of(
new CodeTFReference(
Expand Down Expand Up @@ -143,6 +145,7 @@ void it_has_changeset_with_ai() {
"Hardened object deserialization calls against attack",
"Lengthier description about deserialization risks, protections, etc...",
null,
null,
Set.of("/foo/failed.java"),
List.of(
new CodeTFReference(
Expand All @@ -154,4 +157,12 @@ void it_has_changeset_with_ai() {

assertTrue(result.usesAi());
}

@Test
void it_has_failure_state() {
FailureState state = new FailureState(true, "reason", "exception");
assertTrue(state.failed());
assertEquals("reason", state.reason());
assertEquals("exception", state.exception());
}
}

0 comments on commit 289cb1d

Please sign in to comment.