Skip to content

Commit

Permalink
[incubator-kie-issues#1411] Ban usage of Junit assertions inside DMN
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele-Cardosi committed Sep 3, 2024
1 parent d1eda49 commit 668d0ca
Show file tree
Hide file tree
Showing 28 changed files with 612 additions and 562 deletions.
12 changes: 12 additions & 0 deletions kie-dmn/kie-dmn-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.societegenerale.commons</groupId>
<artifactId>arch-unit-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>sonarcloud-analysis</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class DMNMessagesAPITest {

Expand Down Expand Up @@ -72,7 +72,7 @@ void apiUsage() {

@Test
void apiUsageSnippetForDocumentation() {
assertThrows(IllegalStateException.class, () -> {
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> {
KieServices ks = KieServices.Factory.get();

ReleaseId releaseId = ks.newReleaseId("org.kie", "dmn-test-" + UUID.randomUUID(), "1.0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
import org.slf4j.LoggerFactory;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.kie.dmn.core.util.DMNTestUtil.getAndAssertModelNoErrors;
import static org.kie.dmn.core.util.DynamicTypeUtils.entry;
import static org.kie.dmn.core.util.DynamicTypeUtils.mapOf;
Expand Down Expand Up @@ -3136,7 +3136,7 @@ void functionDefinitionParameterTrailingSpace(boolean useExecModelCompiler) {
@MethodSource("params")
void evaluateByNameWithEmptyParam(boolean useExecModelCompiler) {
init(useExecModelCompiler);
assertThrows(IllegalArgumentException.class, () -> {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("simple-item-def.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn/itemdef", "simple-item-def");
assertThat(dmnModel).isNotNull();
Expand All @@ -3154,7 +3154,7 @@ void evaluateByNameWithEmptyParam(boolean useExecModelCompiler) {
@MethodSource("params")
void evaluateByIdWithEmptyParam(boolean useExecModelCompiler) {
init(useExecModelCompiler);
assertThrows(IllegalArgumentException.class, () -> {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("simple-item-def.dmn", this.getClass());
final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn/itemdef", "simple-item-def");
assertThat(dmnModel).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import org.kie.dmn.core.util.DMNRuntimeUtil;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class DMNContextEvaluatorTest {

Expand Down Expand Up @@ -74,8 +72,8 @@ void dateToDateTime() {
DMNResultImpl result = createResult(dmnModel, context );
DMNExpressionEvaluator evaluator = ed.getEvaluator();
EvaluatorResult evaluated = evaluator.evaluate(runtime, result);
assertNotNull(evaluated);
assertEquals(EvaluatorResult.ResultType.SUCCESS, evaluated.getResultType());
assertThat(evaluated).isNotNull();
assertThat(EvaluatorResult.ResultType.SUCCESS).isEqualTo(evaluated.getResultType());
}

private DMNResultImpl createResult(DMNModel model, DMNContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.kie.dmn.model.v1_5.TInformationRequirement;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

class DMNCompilerImplTest {

Expand Down Expand Up @@ -70,7 +70,7 @@ void getRootElement() {

InformationRequirement informationRequirement = new TInformationRequirement();
elementReference.setParent(informationRequirement);
assertThrows(RuntimeException.class, () -> DMNCompilerImpl.getRootElement(elementReference));
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> DMNCompilerImpl.getRootElement(elementReference));

informationRequirement.setParent(parent);
retrieved = DMNCompilerImpl.getRootElement(elementReference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
import org.kie.dmn.model.api.NamedElement;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.kie.dmn.core.compiler.UnnamedImportUtils.addIfNotPresent;
import static org.kie.dmn.core.compiler.UnnamedImportUtils.isInUnnamedImport;

Expand Down Expand Up @@ -68,26 +65,26 @@ 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 -> assertFalse(isInUnnamedImport(node, importingModel)));
importedModel.getBusinessKnowledgeModels().forEach(node -> assertFalse(isInUnnamedImport(node, importingModel)));
importedModel.getDecisionServices().forEach(node -> assertFalse(isInUnnamedImport(node, importingModel)));
importedModel.getInputs().forEach(node -> assertFalse(isInUnnamedImport(node, importingModel)));
importedModel.getItemDefinitions().forEach(node -> assertFalse(isInUnnamedImport(node, importingModel)));
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());
}

@Test
void addIfNotPresentTrue() throws IOException {
URL importedModelFileResource = Thread.currentThread().getContextClassLoader().getResource(
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
assertNotNull(importedModelFileResource);
assertThat(importedModelFileResource).isNotNull();
try (InputStream is = importedModelFileResource.openStream()) {
String xml = new String(is.readAllBytes(), StandardCharsets.UTF_8);
Definitions definitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(xml);
definitions.getDecisionService().forEach(definition -> assertTrue(added(definition)));
definitions.getBusinessContextElement().forEach(definition -> assertTrue(added(definition)));
definitions.getDrgElement().forEach(definition -> assertTrue(added(definition)));
definitions.getImport().forEach(definition -> assertTrue(added(definition)));
definitions.getItemDefinition().forEach(definition -> assertTrue(added(definition)));
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());
}
}

Expand Down Expand Up @@ -117,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 -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getBusinessKnowledgeModels().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getDecisionServices().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getInputs().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
importedModel.getItemDefinitions().forEach(node -> assertTrue(isInUnnamedImport(node, importingModel)));
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());
}

private void commonAddIfNotPresentFalse(String importingModelRef, String importedModelRef) throws IOException {
Expand All @@ -135,16 +132,16 @@ private void commonAddIfNotPresentFalse(String importingModelRef, String importe
Definitions importingDefinitions = importingModel.getDefinitions();
URL importedModelFileResource = Thread.currentThread().getContextClassLoader().getResource(
"valid_models/DMNv1_5/Imported_Model_Unamed.dmn");
assertNotNull(importedModelFileResource);
assertThat(importedModelFileResource).isNotNull();
try (InputStream is = importedModelFileResource.openStream()) {
String importedXml = new String(is.readAllBytes(), StandardCharsets.UTF_8);
Definitions importedDefinitions = DMNMarshallerFactory.newDefaultMarshaller().unmarshal(importedXml);
importedDefinitions.getDecisionService().forEach(definition -> assertFalse(added(importingDefinitions.getDecisionService(), definition)));
importedDefinitions.getBusinessContextElement().forEach(definition -> assertFalse(added(importingDefinitions.getBusinessContextElement(), definition)));
importedDefinitions.getDrgElement().forEach(definition -> assertFalse(added(importingDefinitions.getDrgElement(), definition)));
importedDefinitions.getImport().forEach(definition -> assertFalse(added(importingDefinitions.getImport(),
definition)));
importedDefinitions.getItemDefinition().forEach(definition -> assertFalse(added(importingDefinitions.getItemDefinition(), definition)));
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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.kie.dmn.feel.lang.EvaluationContext;
import org.kie.dmn.feel.lang.FEELDialect;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

class DMNContextFEELCtxWrapperTest extends BaseDMNContextTest {

Expand All @@ -56,23 +56,23 @@ void contextWithEntriesAndMetadata() {

@Test
void pushScopeException() {
assertThrows(UnsupportedOperationException.class, () -> {
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> {
DMNContextFEELCtxWrapper ctx = new DMNContextFEELCtxWrapper(new EvaluationContextMock(Collections.emptyMap()));
ctx.pushScope("scopeName", "scopeNamespace");
});
}

@Test
void popScopeException() {
assertThrows(UnsupportedOperationException.class, () -> {
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> {
DMNContextFEELCtxWrapper ctx = new DMNContextFEELCtxWrapper(new EvaluationContextMock(Collections.emptyMap()));
ctx.popScope();
});
}

@Test
void scopeNamespaceException() {
assertThrows(UnsupportedOperationException.class, () -> {
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> {
DMNContextFEELCtxWrapper ctx = new DMNContextFEELCtxWrapper(new EvaluationContextMock(Collections.emptyMap()));
ctx.scopeNamespace();
});
Expand Down
Loading

0 comments on commit 668d0ca

Please sign in to comment.