Skip to content

Commit 387d573

Browse files
committed
[#761] Module jdmn-runtime-api: Add extra test coverage for listeners
1 parent 32cc879 commit 387d573

File tree

8 files changed

+161
-40
lines changed

8 files changed

+161
-40
lines changed

dmn-runtime-api/src/main/java/com/gs/dmn/runtime/listener/AbstractTraceEventListener.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ public void startRule(DRGElement element, Rule rule) {
3030

3131
@Override
3232
public void matchRule(DRGElement element, Rule rule) {
33+
if (this.ruleNode == null) {
34+
return;
35+
}
36+
3337
this.ruleNode.setMatched(true);
3438
}
3539

3640
@Override
3741
public void endRule(DRGElement element, Rule rule, Object result) {
42+
if (this.ruleNode == null) {
43+
return;
44+
}
45+
3846
this.ruleNode.setResult(result);
3947
if (!this.elementNodeStack.empty()) {
4048
DRGElementNode top = this.elementNodeStack.peek();
@@ -46,6 +54,10 @@ public void endRule(DRGElement element, Rule rule, Object result) {
4654

4755
@Override
4856
public void matchColumn(Rule rule, int columnIndex, Object result) {
57+
if (this.ruleNode == null) {
58+
return;
59+
}
60+
4961
ColumnNode columnNode = new ColumnNode(columnIndex, result);
5062
this.ruleNode.addColumnNode(columnNode);
5163
}

dmn-runtime-api/src/main/java/com/gs/dmn/runtime/listener/LoggingEventListener.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ public LoggingEventListener(Logger logger) {
2323

2424
@Override
2525
public void startDRGElement(DRGElement element, Arguments arguments) {
26+
if (element == null) {
27+
return;
28+
}
29+
2630
logger.debug("Start {} '{}' with inputs '{}'", element.getElementKind().getDisplayName(), element.getName(), arguments);
2731
}
2832

2933
@Override
3034
public void endDRGElement(DRGElement element, Arguments arguments, Object output, long duration) {
35+
if (element == null) {
36+
return;
37+
}
38+
3139
logger.debug("End {} '{}' with output '{}' in {}ms", element.getElementKind().getDisplayName(), element.getName(), output, duration);
3240
}
3341

@@ -41,6 +49,10 @@ public void matchRule(DRGElement element, Rule rule) {
4149

4250
@Override
4351
public void endRule(DRGElement element, Rule rule, Object output) {
52+
if (rule == null) {
53+
return;
54+
}
55+
4456
logger.debug("Rule {} fired with output '{}'", rule.getIndex(), output);
4557
}
4658

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.gs.dmn.runtime.listener;
2+
3+
import com.gs.dmn.runtime.annotation.DRGElementKind;
4+
import com.gs.dmn.runtime.annotation.ExpressionKind;
5+
import com.gs.dmn.runtime.annotation.HitPolicy;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
public abstract class AbstractListenerTest {
11+
protected EventListener listener;
12+
13+
@Test
14+
public void testEvents() {
15+
DRGElement element = new DRGElement("namespace", "name", "label", DRGElementKind.DECISION, ExpressionKind.LITERAL_EXPRESSION, HitPolicy.UNIQUE, 5);
16+
Rule rule = new Rule(1, "annotation");
17+
Arguments arguments = new Arguments();
18+
19+
listener.startDRGElement(element, arguments);
20+
listener.endDRGElement(element, arguments, null, 0);
21+
listener.startRule(element, rule);
22+
listener.matchRule(element, rule);
23+
listener.endRule(element, rule, null);
24+
listener.matchColumn(rule, 1, null);
25+
26+
assertTrue(true);
27+
}
28+
29+
@Test
30+
public void testEventsWithNull() {
31+
listener.startDRGElement(null, null);
32+
listener.endDRGElement(null, null, null, 0);
33+
listener.startRule(null, null);
34+
listener.matchRule(null, null);
35+
listener.endRule(null, null, null);
36+
listener.matchColumn(null, 0, null);
37+
38+
assertTrue(true);
39+
}
40+
}

dmn-runtime-api/src/test/java/com/gs/dmn/runtime/listener/CompositeListenerTest.java

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,11 @@
1212
*/
1313
package com.gs.dmn.runtime.listener;
1414

15-
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.BeforeEach;
1616

17-
import static org.junit.jupiter.api.Assertions.assertTrue;
18-
19-
public class CompositeListenerTest {
20-
private final EventListener listener = new CompositeListener();
21-
22-
@Test
23-
public void testStartDRGElement() {
24-
listener.startDRGElement(null, null);
25-
assertTrue(true);
26-
}
27-
28-
@Test
29-
public void testEndDRGElement() {
30-
listener.endDRGElement(null, null, null, 0);
31-
assertTrue(true);
32-
}
33-
34-
@Test
35-
public void testStartRule() {
36-
listener.startRule(null, null);
37-
assertTrue(true);
38-
}
39-
40-
@Test
41-
public void testMatchRule() {
42-
listener.matchRule(null, null);
43-
assertTrue(true);
44-
}
45-
46-
@Test
47-
public void testEndRule() {
48-
listener.endRule(null, null, null);
49-
assertTrue(true);
50-
}
51-
52-
@Test
53-
public void testMatchColumn() {
54-
listener.matchColumn(null, 0, null);
55-
assertTrue(true);
17+
public class CompositeListenerTest extends AbstractListenerTest {
18+
@BeforeEach
19+
void setUp() {
20+
this.listener = new CompositeListener();
5621
}
5722
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016 Goldman Sachs.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
5+
*
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.gs.dmn.runtime.listener;
14+
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
public class LoggingEventListenerTest extends AbstractListenerTest {
20+
protected static final Logger LOGGER = LoggerFactory.getLogger(LoggingEventListenerTest.class);
21+
22+
@BeforeEach
23+
void setUp() {
24+
this.listener = new LoggingEventListener(LOGGER);
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2016 Goldman Sachs.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
5+
*
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.gs.dmn.runtime.listener;
14+
15+
import org.junit.jupiter.api.BeforeEach;
16+
17+
public class NopListenerTest extends AbstractListenerTest {
18+
@BeforeEach
19+
void setUp() {
20+
this.listener = new NopEventListener();
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2016 Goldman Sachs.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
5+
*
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.gs.dmn.runtime.listener;
14+
15+
import org.junit.jupiter.api.BeforeEach;
16+
17+
class PostorderTraceEventListenerTest extends AbstractListenerTest {
18+
@BeforeEach
19+
void setUp() {
20+
this.listener = new PostorderTraceEventListener();
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2016 Goldman Sachs.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
5+
*
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.gs.dmn.runtime.listener;
14+
15+
import org.junit.jupiter.api.BeforeEach;
16+
17+
class TreeTraceEventListenerTest extends AbstractListenerTest {
18+
@BeforeEach
19+
void setUp() {
20+
this.listener = new TreeTraceEventListener();
21+
}
22+
}

0 commit comments

Comments
 (0)