Skip to content

Commit 8b87f28

Browse files
committed
tests : add tests
1 parent ce9f8ee commit 8b87f28

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<packaging>jar</packaging>
1616

1717
<name>BPM</name>
18-
<description>Commmon library for metadata files parsing</description>
18+
<description>Common library for metadata files parsing</description>
1919
<url>https://github.com/InseeFr/BPM</url>
2020
<organization>
2121
<name>INSEE</name>

src/main/java/fr/insee/bpm/metadata/reader/lunatic/processor/RoundaboutProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void process(JsonNode primaryComponent, Group group, List<String> variabl
3030
}
3131
}
3232

33-
private static String extractVariableName(String input) {
33+
static String extractVariableName(String input) {
3434
// Regex to capture what's between parenthesis
3535
Pattern pattern = Pattern.compile("\\((.*?)\\)");
3636
Matcher matcher = pattern.matcher(input);

src/test/java/fr/insee/bpm/metadata/model/MetadataModelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static MetadataModel createCompleteFakeVariablesMap() {
7575
}
7676

7777
@BeforeEach
78-
public void createTestVariablesMap() {
78+
void createTestVariablesMap() {
7979
metadataModel = new MetadataModel();
8080

8181
Group rootGroup = metadataModel.getRootGroup();
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package fr.insee.bpm.metadata.reader.lunatic.processor;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.node.ArrayNode;
5+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
6+
import com.fasterxml.jackson.databind.node.ObjectNode;
7+
import fr.insee.bpm.metadata.model.Group;
8+
import fr.insee.bpm.metadata.model.MetadataModel;
9+
import fr.insee.bpm.metadata.model.Variable;
10+
import fr.insee.bpm.metadata.model.VariableType;
11+
import fr.insee.bpm.metadata.model.VariablesMap;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
import org.mockito.InjectMocks;
16+
import org.mockito.Mock;
17+
import org.mockito.junit.jupiter.MockitoExtension;
18+
19+
import java.util.ArrayList;
20+
21+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
24+
@ExtendWith(MockitoExtension.class)
25+
class RoundaboutProcessorTest {
26+
MetadataModel metadataModel;
27+
Variable loopVariable;
28+
Group groupRoundabout;
29+
30+
@Mock
31+
Group fallbackGroup;
32+
33+
@Mock
34+
ComponentProcessor mockProcessor;
35+
36+
@InjectMocks
37+
RoundaboutProcessor roundaboutProcessor;
38+
39+
VariablesMap variablesMap = new VariablesMap();
40+
41+
42+
@BeforeEach
43+
void setup() {
44+
roundaboutProcessor = new RoundaboutProcessor();
45+
groupRoundabout = new Group();
46+
loopVariable = new Variable("LOOP_VARIABLE", groupRoundabout, VariableType.STRING);
47+
variablesMap.putVariable(loopVariable);
48+
metadataModel = new MetadataModel();
49+
metadataModel.setVariables(variablesMap);
50+
}
51+
52+
53+
@Test
54+
void shouldProcessAllComponentsInRoundaboutGroup() {
55+
// Given
56+
String formula = "LIST(LOOP_VARIABLE)";
57+
58+
// Add vtl formula
59+
JsonNode primaryComponent = JsonNodeFactory.instance.objectNode()
60+
.set("iterations", JsonNodeFactory.instance.objectNode()
61+
.put("value", formula));
62+
63+
// Add children component
64+
ArrayNode components = JsonNodeFactory.instance.arrayNode();
65+
components.add(objectNodeWithType("Input"));
66+
components.add(objectNodeWithType("Checkbox"));
67+
68+
((ObjectNode) primaryComponent).set("components", components);
69+
70+
71+
// When
72+
roundaboutProcessor.process(primaryComponent, fallbackGroup, new ArrayList<>(), metadataModel, false);
73+
74+
// Then
75+
assertDoesNotThrow(() -> {});
76+
}
77+
78+
@Test
79+
void shouldReturnNullWhenFormulaHasNoParentheses() {
80+
String badFormula = "INVALID_FORMULA";
81+
String variable = RoundaboutProcessor.extractVariableName(badFormula);
82+
assertNull(variable);
83+
}
84+
85+
@Test
86+
void shouldHandleEmptyComponentsArray() {
87+
ObjectNode component = JsonNodeFactory.instance.objectNode();
88+
component.set("iterations", JsonNodeFactory.instance.objectNode()
89+
.put("value", "LIST(LOOP_VARIABLE)"));
90+
component.set("components", JsonNodeFactory.instance.arrayNode());
91+
92+
roundaboutProcessor.process(component, fallbackGroup, new ArrayList<>(), metadataModel, false);
93+
assertDoesNotThrow(() -> {});
94+
}
95+
96+
private ObjectNode objectNodeWithType(String type) {
97+
ObjectNode name = JsonNodeFactory.instance.objectNode();
98+
name.put("name", "name"+type);
99+
ObjectNode obj = JsonNodeFactory.instance.objectNode();
100+
obj.put("componentType", type);
101+
obj.put("response", name);
102+
return obj;
103+
}
104+
}

0 commit comments

Comments
 (0)