Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
feat(action-impl): soft assert for jsonAssertAction (#1176)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbenyoussef authored Nov 27, 2023
1 parent f81508d commit dde0c5c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

public class JsonAssertAction implements Action {

Expand All @@ -27,8 +28,8 @@ public class JsonAssertAction implements Action {
private final Map<String, Object> mapExpectedResults;

public JsonAssertAction(Logger logger,
@Input("document") String document,
@Input("expected") Map<String, Object> mapExpectedResults) {
@Input("document") String document,
@Input("expected") Map<String, Object> mapExpectedResults) {
this.logger = logger;
this.document = document;
this.mapExpectedResults = mapExpectedResults;
Expand All @@ -47,7 +48,8 @@ public ActionExecutionResult execute() {
try {
ReadContext json = JsonPath.parse(document, Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS));

boolean matchesOk = mapExpectedResults.entrySet().stream().allMatch(entry -> {
AtomicBoolean matchesOk = new AtomicBoolean(true);
mapExpectedResults.entrySet().stream().forEach(entry -> {
String path = entry.getKey();
Object expected = entry.getValue();
Object actualValue = json.read(path);
Expand All @@ -73,14 +75,16 @@ public ActionExecutionResult execute() {
}
}
}
if (!result) {
if (result) {
logger.info("On path [" + path + "], found [" + actualValue + "]");
} else {
logger.error("On path [" + path + "], found [" + actualValue + "], expected was [" + expected + "]");
matchesOk.set(false);
}
return result;
}
);

if (!matchesOk) {
if (!matchesOk.get()) {
return ActionExecutionResult.ko();
}
return ActionExecutionResult.ok();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class HttpAction {
public static ActionExecutionResult httpCall(Logger logger, Supplier<ResponseEntity<String>> caller) {
try {
ResponseEntity<String> response = caller.get();
logger.info("HTTP call status :" + response.getStatusCode().value());
return ActionExecutionResult.ok(toOutputs(response));
}
catch (ResourceAccessException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static java.util.Optional.ofNullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.chutneytesting.action.TestLogger;
import com.chutneytesting.action.assertion.JsonCompareAction.COMPARE_MODE;
Expand Down Expand Up @@ -162,6 +164,30 @@ void should_execute_a_successful_assertions_on_comparing_expected_value_as_strin
assertThat(result.status).isEqualTo(Success);
}

@Test
void should_execute_all_assertions_after_assertion_fail() {
Map<String, Object> expected = new HashMap<>();
expected.put("$.something.value", 2);
expected.put("$.something_else.value", 5);
expected.put("$.a_thing.type", "my_type");
expected.put("$.a_thing.not.existing", null);

// Given
String fakeActualResult = "{\"something\":{\"value\":3},\"something_else\":{\"value\":5},\"a_thing\":{\"type\":\"my_type\"}}";
Logger logger = mock(Logger.class);

// When
JsonAssertAction jsonAssertAction = new JsonAssertAction(logger, fakeActualResult, expected);
ActionExecutionResult result = jsonAssertAction.execute();

// Then
assertThat(result.status).isEqualTo(Failure);
verify(logger, times(1)).error("On path [$.something.value], found [3], expected was [2]");
verify(logger, times(1)).info("On path [$.something_else.value], found [5]");
verify(logger, times(1)).info("On path [$.a_thing.type], found [my_type]");
verify(logger, times(1)).info("On path [$.a_thing.not.existing], found [null]");
}

@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@DisplayName("Assert json using placeholders")
Expand Down

0 comments on commit dde0c5c

Please sign in to comment.