Skip to content

Commit

Permalink
NO ISSUE: Improving stream assertions in tests (apache#6068)
Browse files Browse the repository at this point in the history
* First cleanup for assertj assertions on streams

* More stream assertions cleanup

* Making the test behaves correctly
  • Loading branch information
pibizza authored and rgdoliveira committed Sep 5, 2024
1 parent c7c74be commit ee6a195
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ void testNestedImports(VariantTestConf conf) {
LOG.debug("{}", message);
}
LOG.debug("{}", evaluateModelCDecision);
assertThat(evaluateModelCDecision.getDecisionResults()).size().isEqualTo(3);
evaluateModelCDecision.getDecisionResults().forEach(dmnDecisionResult -> assertThat(dmnDecisionResult.getEvaluationStatus()).isEqualTo(SUCCEEDED));
assertThat(evaluateModelCDecision.getDecisionResults()).hasSize(3);
assertThat(evaluateModelCDecision.getDecisionResults()).allMatch(dmnDecisionResult -> dmnDecisionResult.getEvaluationStatus().equals(SUCCEEDED));
}

@ParameterizedTest
Expand Down Expand Up @@ -415,13 +415,11 @@ void wrongComparisonOps(VariantTestConf conf) {
assertThat(dmnModel.hasErrors()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).isFalse();
assertThat(dmnModel.getMessages()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).hasSize(4);
assertThat(dmnModel.getMessages(DMNMessage.Severity.WARN)).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).hasSize(4);
assertThat(dmnModel.getMessages(DMNMessage.Severity.WARN)
.stream()
.filter(m -> m.getSourceId().equals("_d72d6fab-1e67-4fe7-9c12-54800d6fe294") ||
assertThat(dmnModel.getMessages(DMNMessage.Severity.WARN)).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()))
.filteredOn(m -> m.getSourceId().equals("_d72d6fab-1e67-4fe7-9c12-54800d6fe294") ||
m.getSourceId().equals("_2390dd99-094d-4f97-aecc-9cccb697ce05") ||
m.getSourceId().equals("_0c292d34-498e-4b08-ae99-3c694197b69f") ||
m.getSourceId().equals("_21c7d800-b806-4b2e-9a10-00828de7f2d2"))
.count()).as(DMNRuntimeUtil.formatMessages(dmnModel.getMessages())).isEqualTo(4L);
m.getSourceId().equals("_21c7d800-b806-4b2e-9a10-00828de7f2d2")).hasSize(4);
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ void isInUnnamedImportFalse() {
final DMNModelImpl importingModel = (DMNModelImpl)runtime.getModel("http://www.trisotech.com/dmn/definitions/_f79aa7a4-f9a3-410a-ac95-bea496edabgc",
"Importing named Model");
assertThat(importingModel).isNotNull();
importedModel.getDecisions().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isFalse());
importedModel.getBusinessKnowledgeModels().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isFalse());
importedModel.getDecisionServices().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isFalse());
importedModel.getInputs().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isFalse());
importedModel.getItemDefinitions().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isFalse());
assertThat(importedModel.getDecisions()).noneMatch(node -> isInUnnamedImport(node, importingModel));
assertThat(importedModel.getBusinessKnowledgeModels()).noneMatch(node -> isInUnnamedImport(node, importingModel));
assertThat(importedModel.getDecisionServices()).noneMatch(node -> isInUnnamedImport(node, importingModel));
assertThat(importedModel.getInputs()).noneMatch(node -> isInUnnamedImport(node, importingModel));
assertThat(importedModel.getItemDefinitions()).noneMatch(node -> isInUnnamedImport(node, importingModel));
}

@Test
Expand All @@ -80,11 +80,11 @@ void addIfNotPresentTrue() throws IOException {
try (InputStream is = importedModelFileResource.openStream()) {
String xml = new String(is.readAllBytes(), StandardCharsets.UTF_8);
Definitions definitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(xml);
definitions.getDecisionService().forEach(definition -> assertThat(added(definition)).isTrue());
definitions.getBusinessContextElement().forEach(definition -> assertThat(added(definition)).isTrue());
definitions.getDrgElement().forEach(definition -> assertThat(added(definition)).isTrue());
definitions.getImport().forEach(definition -> assertThat(added(definition)).isTrue());
definitions.getItemDefinition().forEach(definition -> assertThat(added(definition)).isTrue());
assertThat(definitions.getDecisionService()).allMatch(definition -> added(definition));
assertThat(definitions.getBusinessContextElement()).allMatch(definition -> added(definition));
assertThat(definitions.getDrgElement()).allMatch(definition -> added(definition));
assertThat(definitions.getImport()).allMatch(definition -> added(definition));
assertThat(definitions.getItemDefinition()).allMatch(definition -> added(definition));
}
}

Expand Down Expand Up @@ -114,11 +114,11 @@ private void commonIsInUnnamedImportTrue(String importingModelRef, String import
"-f9a3-410a-ac95-bea496edabgc",
"Importing empty-named Model");
assertThat(importingModel).isNotNull();
importedModel.getDecisions().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isTrue());
importedModel.getBusinessKnowledgeModels().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isTrue());
importedModel.getDecisionServices().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isTrue());
importedModel.getInputs().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isTrue());
importedModel.getItemDefinitions().forEach(node -> assertThat(isInUnnamedImport(node, importingModel)).isTrue());
assertThat(importedModel.getDecisions()).allMatch(node -> isInUnnamedImport(node, importingModel));
assertThat(importedModel.getBusinessKnowledgeModels()).allMatch(node -> isInUnnamedImport(node, importingModel));
assertThat(importedModel.getDecisionServices()).allMatch(node -> isInUnnamedImport(node, importingModel));
assertThat(importedModel.getInputs()).allMatch(node -> isInUnnamedImport(node, importingModel));
assertThat(importedModel.getItemDefinitions()).allMatch(node -> isInUnnamedImport(node, importingModel));
}

private void commonAddIfNotPresentFalse(String importingModelRef, String importedModelRef) throws IOException {
Expand All @@ -136,12 +136,11 @@ private void commonAddIfNotPresentFalse(String importingModelRef, String importe
try (InputStream is = importedModelFileResource.openStream()) {
String importedXml = new String(is.readAllBytes(), StandardCharsets.UTF_8);
Definitions importedDefinitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(importedXml);
importedDefinitions.getDecisionService().forEach(definition -> assertThat(added(importingDefinitions.getDecisionService(), definition)).isFalse());
importedDefinitions.getBusinessContextElement().forEach(definition -> assertThat(added(importingDefinitions.getBusinessContextElement(), definition)).isFalse());
importedDefinitions.getDrgElement().forEach(definition -> assertThat(added(importingDefinitions.getDrgElement(), definition)).isFalse());
importedDefinitions.getImport().forEach(definition -> assertThat(added(importingDefinitions.getImport(),
definition)).isFalse());
importedDefinitions.getItemDefinition().forEach(definition -> assertThat(added(importingDefinitions.getItemDefinition(), definition)).isFalse());
assertThat(importedDefinitions.getDecisionService()).noneMatch(definition -> added(importingDefinitions.getDecisionService(), definition));
assertThat(importedDefinitions.getBusinessContextElement()).noneMatch(definition -> added(importingDefinitions.getBusinessContextElement(), definition));
assertThat(importedDefinitions.getDrgElement()).noneMatch(definition -> added(importingDefinitions.getDrgElement(), definition));
assertThat(importedDefinitions.getImport()).noneMatch(definition -> added(importingDefinitions.getImport(), definition));
assertThat(importedDefinitions.getItemDefinition()).noneMatch(definition -> added(importingDefinitions.getItemDefinition(), definition));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void addDecisionSameModel() {
decisionNodeList.add(toAdd);
});

decisionNodeList.forEach(decisionNode -> {
assertThat(decisionNodeList).allSatisfy(decisionNode -> {
assertThat(model.getDecisionByName(decisionNode.getName()))
.isNotNull()
.isEqualTo(decisionNode);
Expand All @@ -72,7 +72,7 @@ void addDecisionWithoutIdSameModel() {
decisionNodeList.add(toAdd);
});

decisionNodeList.forEach(decisionNode ->
assertThat(decisionNodeList).allSatisfy(decisionNode ->
assertThat(model.getDecisionByName(decisionNode.getName()))
.isNotNull()
.isEqualTo(decisionNode));
Expand All @@ -86,7 +86,7 @@ void addDecisionWithoutNameSameModel() {
model.addDecision(toAdd);
decisionNodeList.add(toAdd);
});
decisionNodeList.forEach(decisionNode ->
assertThat(decisionNodeList).allSatisfy(decisionNode ->
assertThat(model.getDecisionById(decisionNode.getId()))
.isNotNull()
.isEqualTo(decisionNode));
Expand All @@ -104,7 +104,7 @@ void addDecisionImportedModel() {
decisionNodeList.add(toAdd);
});

decisionNodeList.forEach(decisionNode -> {
assertThat(decisionNodeList).allSatisfy(decisionNode -> {
assertThat(model.getDecisionByName(String.format("%s.%s", importedNameSpace, decisionNode.getName())))
.isNotNull()
.isEqualTo(decisionNode);
Expand All @@ -126,7 +126,7 @@ void addDecisionWithoutIdImportedModel() {
decisionNodeList.add(toAdd);
});

decisionNodeList.forEach(decisionNode ->
assertThat(decisionNodeList).allSatisfy(decisionNode ->
assertThat(model.getDecisionByName(String.format("%s.%s", importedNameSpace, decisionNode.getName())))
.isNotNull()
.isEqualTo(decisionNode));
Expand All @@ -143,7 +143,7 @@ void addDecisionWithoutNameImportedModel() {
model.addDecision(toAdd);
decisionNodeList.add(toAdd);
});
decisionNodeList.forEach(decisionNode ->
assertThat(decisionNodeList).allSatisfy(decisionNode ->
assertThat(model.getDecisionById(String.format("%s#%s", importedNameSpace, decisionNode.getId())))
.isNotNull()
.isEqualTo(decisionNode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1035,11 +1035,11 @@ void collectionOfCollectionOfCollection(VariantTestConf conf) {
Map<String, Object> allProperties = outputSet.allFEELProperties();
List<List<List<FEELPropertyAccessible>>> colCOut = (List<List<List<FEELPropertyAccessible>>>) allProperties.get("Decision-1");
List<FEELPropertyAccessible> personOutList = colCOut.stream().flatMap(colB -> colB.stream()).flatMap(colA -> colA.stream()).collect(Collectors.toList());
personOutList.stream().forEach(person -> assertPersonInDeepCol(person));
assertThat(personOutList).allSatisfy(person -> assertPersonInDeepCol(person));
} else {
List<List<List<Map<String, Object>>>> colCOut = (List<List<List<Map<String, Object>>>>)dmnResult.getContext().get("Decision-1");
List<Map<String, Object>> personOutList = colCOut.stream().flatMap(colB -> colB.stream()).flatMap(colA -> colA.stream()).collect(Collectors.toList());
personOutList.stream().forEach(person -> assertPersonMapInDeepCol(person));
assertThat(personOutList).allSatisfy(person -> assertPersonMapInDeepCol(person));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
package org.kie.dmn.feel.lang.ast.forexpressioniterators;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -61,18 +58,17 @@ void hasNextDescendantTest() {
void nextAscendantTest() {
BigDecimal start = BigDecimal.valueOf(1);
BigDecimal end = BigDecimal.valueOf(3);
List<BigDecimal> expected = Arrays.asList(BigDecimal.valueOf(1), BigDecimal.valueOf(2), BigDecimal.valueOf(3));
BigDecimalRangeIterator iterator = new BigDecimalRangeIterator(start, end);
IntStream.range(0, 3).forEach(i -> assertThat(expected.get(i)).isEqualTo(iterator.next()));

assertThat(iterator).toIterable().containsExactly(BigDecimal.valueOf(1), BigDecimal.valueOf(2), BigDecimal.valueOf(3));
}

@Test
void nextDescendantTest() {
BigDecimal start = BigDecimal.valueOf(3);
BigDecimal end = BigDecimal.valueOf(1);
List<BigDecimal> expected = Arrays.asList(BigDecimal.valueOf(3), BigDecimal.valueOf(2), BigDecimal.valueOf(1));
BigDecimalRangeIterator iterator = new BigDecimalRangeIterator(start, end);
IntStream.range(0, 3).forEach(i -> assertThat(expected.get(i)).isEqualTo(iterator.next()));
assertThat(iterator).toIterable().containsExactly(BigDecimal.valueOf(3), BigDecimal.valueOf(2), BigDecimal.valueOf(1));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -62,19 +59,21 @@ void hasNextValueLocalDateTest() {
void getNextValueBigDecimalTest() {
BigDecimal start = BigDecimal.valueOf(1);
BigDecimal end = BigDecimal.valueOf(3);
List<BigDecimal> expected = Arrays.asList(BigDecimal.valueOf(1), BigDecimal.valueOf(2), BigDecimal.valueOf(3));
ForIteration iteration = new ForIteration("iteration", start, end);
iteration.hasNextValue();
IntStream.range(0, 3).forEach(i -> assertThat(expected.get(i)).isEqualTo(iteration.getNextValue()));
assertThat(iteration.hasNextValue()).isTrue();
assertThat(iteration.getNextValue()).isEqualTo(BigDecimal.valueOf(1));
assertThat(iteration.getNextValue()).isEqualTo(BigDecimal.valueOf(2));
assertThat(iteration.getNextValue()).isEqualTo(BigDecimal.valueOf(3));
}

@Test
void getNextValueLocalDateTest() {
LocalDate start = LocalDate.of(2021, 1, 3);
LocalDate end = LocalDate.of(2021, 1, 1);
List<LocalDate> expected = Arrays.asList(LocalDate.of(2021, 1, 3), LocalDate.of(2021, 1, 2), LocalDate.of(2021, 1, 1));
ForIteration iteration = new ForIteration("iteration", start, end);
iteration.hasNextValue();
IntStream.range(0, 3).forEach(i -> assertThat(expected.get(i)).isEqualTo(iteration.getNextValue()));
assertThat(iteration.hasNextValue()).isTrue();
assertThat(iteration.getNextValue()).isEqualTo(LocalDate.of(2021, 1, 3));
assertThat(iteration.getNextValue()).isEqualTo(LocalDate.of(2021, 1, 2));
assertThat(iteration.getNextValue()).isEqualTo(LocalDate.of(2021, 1, 1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
package org.kie.dmn.feel.lang.ast.forexpressioniterators;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -65,16 +62,14 @@ void hasNextDescendantTest() {

@Test
void nextAscendantTest() {
List<LocalDate> expected = Arrays.asList(LocalDate.of(2021, 1, 1), LocalDate.of(2021, 1, 2), LocalDate.of(2021, 1, 3));
LocalDateRangeIterator iterator = new LocalDateRangeIterator(before, after);
IntStream.range(0, 3).forEach(i -> assertThat(expected.get(i)).isEqualTo(iterator.next()));
assertThat(iterator).toIterable().containsExactly(LocalDate.of(2021, 1, 1), LocalDate.of(2021, 1, 2), LocalDate.of(2021, 1, 3));
}

@Test
void nextDescendantTest() {
List<LocalDate> expected = Arrays.asList(LocalDate.of(2021, 1, 3), LocalDate.of(2021, 1, 2), LocalDate.of(2021, 1, 1));
LocalDateRangeIterator iterator = new LocalDateRangeIterator(after, before);
IntStream.range(0, 3).forEach(i -> assertThat(expected.get(i)).isEqualTo(iterator.next()));
assertThat(iterator).toIterable().containsExactly(LocalDate.of(2021, 1, 3), LocalDate.of(2021, 1, 2), LocalDate.of(2021, 1, 1));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -68,20 +65,18 @@ void hasNextDescendantTest() {

@Test
void nextAscendantTest() {
List<ZonedDateTime> expected = Arrays.asList(getZonedDateTime(2021, 1, 1, 10, 15),
ZonedDateTimeRangeIterator iterator = new ZonedDateTimeRangeIterator(before, after);
assertThat(iterator).toIterable().containsExactly(getZonedDateTime(2021, 1, 1, 10, 15),
getZonedDateTime(2021, 1, 2, 10, 15),
getZonedDateTime(2021, 1, 3, 10, 15));
ZonedDateTimeRangeIterator iterator = new ZonedDateTimeRangeIterator(before, after);
IntStream.range(0, 3).forEach(i -> assertThat(expected.get(i)).isEqualTo(iterator.next()));
}

@Test
void nextDescendantTest() {
List<ZonedDateTime> expected = Arrays.asList(getZonedDateTime(2021, 1, 3, 10, 15),
ZonedDateTimeRangeIterator iterator = new ZonedDateTimeRangeIterator(after, before);
assertThat(iterator).toIterable().containsExactly(getZonedDateTime(2021, 1, 3, 10, 15),
getZonedDateTime(2021, 1, 2, 10, 15),
getZonedDateTime(2021, 1, 1, 10, 15));
ZonedDateTimeRangeIterator iterator = new ZonedDateTimeRangeIterator(after, before);
IntStream.range(0, 3).forEach(i -> assertThat(expected.get(i)).isEqualTo(iterator.next()));
}


Expand Down
Loading

0 comments on commit ee6a195

Please sign in to comment.