|
| 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