|
19 | 19 | */
|
20 | 20 | package it.bancaditalia.oss.vtl.coverage.tests;
|
21 | 21 |
|
| 22 | +import static it.bancaditalia.oss.vtl.config.VTLGeneralProperties.ENVIRONMENT_IMPLEMENTATION; |
| 23 | +import static it.bancaditalia.oss.vtl.config.VTLGeneralProperties.METADATA_REPOSITORY; |
| 24 | +import static it.bancaditalia.oss.vtl.impl.environment.CSVPathEnvironment.VTL_CSV_ENVIRONMENT_SEARCH_PATH; |
| 25 | +import static it.bancaditalia.oss.vtl.impl.meta.json.JsonMetadataRepository.JSON_METADATA_URL; |
| 26 | +import static it.bancaditalia.oss.vtl.util.SerCollectors.toList; |
22 | 27 | import static java.nio.charset.StandardCharsets.UTF_8;
|
| 28 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | +import static org.hamcrest.Matchers.anyOf; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
23 | 31 |
|
24 | 32 | import java.io.BufferedReader;
|
25 | 33 | import java.io.IOException;
|
26 | 34 | import java.io.InputStreamReader;
|
27 | 35 | import java.net.URISyntaxException;
|
28 |
| -import java.net.URL; |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
| 38 | +import java.nio.file.Paths; |
29 | 39 | import java.util.ArrayList;
|
30 | 40 | import java.util.List;
|
31 |
| -import java.util.regex.Matcher; |
32 |
| -import java.util.regex.Pattern; |
33 | 41 | import java.util.stream.Stream;
|
34 | 42 |
|
| 43 | +import org.hamcrest.Matchers; |
35 | 44 | import org.junit.jupiter.params.ParameterizedTest;
|
36 | 45 | import org.junit.jupiter.params.provider.Arguments;
|
37 | 46 | import org.junit.jupiter.params.provider.MethodSource;
|
38 | 47 |
|
39 | 48 | import it.bancaditalia.oss.vtl.config.ConfigurationManagerFactory;
|
40 |
| -import it.bancaditalia.oss.vtl.impl.meta.InMemoryMetadataRepository; |
41 |
| -import it.bancaditalia.oss.vtl.impl.session.VTLSessionImpl; |
| 49 | +import it.bancaditalia.oss.vtl.impl.environment.CSVPathEnvironment; |
| 50 | +import it.bancaditalia.oss.vtl.impl.environment.WorkspaceImpl; |
| 51 | +import it.bancaditalia.oss.vtl.impl.meta.json.JsonMetadataRepository; |
42 | 52 | import it.bancaditalia.oss.vtl.model.data.DataPoint;
|
43 | 53 | import it.bancaditalia.oss.vtl.model.data.DataSet;
|
44 |
| -import it.bancaditalia.oss.vtl.model.data.VTLValue; |
| 54 | +import it.bancaditalia.oss.vtl.model.data.DataStructureComponent; |
| 55 | +import it.bancaditalia.oss.vtl.session.VTLSession; |
45 | 56 |
|
46 | 57 | public class IntegrationTestSuite
|
47 | 58 | {
|
48 | 59 | static {
|
49 | 60 | System.setProperty("vtl.sequential", "true");
|
50 | 61 | }
|
| 62 | + |
| 63 | + private static Path root; |
51 | 64 |
|
52 | 65 | public static Stream<Arguments> test() throws IOException, URISyntaxException
|
53 | 66 | {
|
54 |
| - URL root = IntegrationTestSuite.class.getResource("vtl"); |
55 |
| - Pattern pattern = Pattern.compile("'csv:([^']+)'"); |
| 67 | + root = Paths.get(IntegrationTestSuite.class.getResource("../tests").toURI()); |
| 68 | + |
| 69 | + METADATA_REPOSITORY.setValue(JsonMetadataRepository.class); |
| 70 | + ENVIRONMENT_IMPLEMENTATION.setValues(CSVPathEnvironment.class, WorkspaceImpl.class); |
| 71 | + |
56 | 72 | List<Arguments> tests = new ArrayList<>();
|
57 |
| - try (BufferedReader dirReader = new BufferedReader(new InputStreamReader(IntegrationTestSuite.class.getResourceAsStream("vtl"), UTF_8))) |
| 73 | + String testName; |
| 74 | + StringBuilder testCode = new StringBuilder(); |
| 75 | + |
| 76 | + try (BufferedReader dirReader = new BufferedReader(new InputStreamReader(IntegrationTestSuite.class.getResourceAsStream("../tests"), UTF_8))) |
58 | 77 | {
|
59 |
| - String testName; |
60 |
| - StringBuilder testCode = new StringBuilder(); |
61 |
| - StringBuilder parsedLine = new StringBuilder(); |
62 | 78 | while ((testName = dirReader.readLine()) != null)
|
63 |
| - { |
64 |
| - try (BufferedReader testReader = new BufferedReader(new InputStreamReader(new URL(root, "vtl/" + testName).openStream(), UTF_8))) |
| 79 | + if (!testName.endsWith(".class")) |
65 | 80 | {
|
66 |
| - String testLine; |
67 |
| - int headerLines = 20; |
68 |
| - while ((testLine = testReader.readLine()) != null) |
| 81 | + try (BufferedReader testReader = Files.newBufferedReader(root.resolve(testName).resolve(testName + ".vtl"))) |
69 | 82 | {
|
70 |
| - if (--headerLines > 0) |
71 |
| - continue; |
72 |
| - Matcher matcher = pattern.matcher(testLine); |
73 |
| - int start = 0; |
74 |
| - while (matcher.find()) |
| 83 | + String testLine; |
| 84 | + int headerLines = 20; |
| 85 | + while ((testLine = testReader.readLine()) != null) |
75 | 86 | {
|
76 |
| - parsedLine.append(testLine.substring(start, matcher.start(1))) |
77 |
| - .append(new URL(root, "data/" + matcher.group(1)).toString()); |
78 |
| - start = matcher.end(1); |
| 87 | + if (--headerLines > 0) |
| 88 | + continue; |
| 89 | + testCode.append(testLine).append(System.lineSeparator()); |
79 | 90 | }
|
80 |
| - testCode.append(parsedLine.append(testLine.substring(start)).toString()) |
81 |
| - .append(System.lineSeparator()); |
82 |
| - parsedLine.setLength(0); |
83 | 91 | }
|
| 92 | + tests.add(Arguments.of(testName, testCode.toString())); |
| 93 | + testCode.setLength(0); |
84 | 94 | }
|
85 |
| - tests.add(Arguments.of(testName, testCode.toString())); |
86 |
| - testCode.setLength(0); |
87 |
| - } |
88 | 95 | }
|
89 | 96 |
|
90 | 97 | return tests.stream();
|
91 | 98 | }
|
92 | 99 |
|
93 | 100 | @ParameterizedTest(name = "{0}")
|
94 | 101 | @MethodSource
|
95 |
| - public void test(String testName, String testCode) |
| 102 | + public synchronized void test(final String testName, final String testCode) throws IOException, URISyntaxException |
96 | 103 | {
|
97 |
| - try |
98 |
| - { |
99 |
| - ((InMemoryMetadataRepository) ConfigurationManagerFactory.getInstance().getMetadataRepository()).clearVariables(); |
100 |
| - |
101 |
| - System.out.println("------------------------------------------------------------------------------------------------"); |
102 |
| - System.out.println(" " + testName); |
103 |
| - System.out.println("------------------------------------------------------------------------------------------------"); |
104 |
| - System.out.println(); |
105 |
| - System.out.println(testCode); |
106 |
| - System.out.println("------------------------------------------------------------------------------------------------"); |
107 |
| - VTLSessionImpl session = new VTLSessionImpl(testCode); |
108 |
| - session.compile(); |
109 |
| - VTLValue result = session.resolve("test_result"); |
110 |
| - if (result instanceof DataSet) |
111 |
| - try (Stream<DataPoint> stream = ((DataSet) result).stream()) |
112 |
| - { |
113 |
| - stream.forEach(dp -> System.out.print(".")); |
114 |
| - System.out.println(); |
115 |
| - } |
116 |
| - } |
117 |
| - catch (RuntimeException e) |
| 104 | + System.out.println("------------------------------------------------------------------------------------------------"); |
| 105 | + System.out.println(" " + testName); |
| 106 | + System.out.println("------------------------------------------------------------------------------------------------"); |
| 107 | + System.out.println(); |
| 108 | + System.out.println(testCode); |
| 109 | + System.out.println("------------------------------------------------------------------------------------------------"); |
| 110 | + |
| 111 | + VTL_CSV_ENVIRONMENT_SEARCH_PATH.setValues(root.resolve(testName).toString()); |
| 112 | + JSON_METADATA_URL.setValue(IntegrationTestSuite.class.getResource(testName + "/" + testName + ".json").toString()); |
| 113 | + VTLSession session = ConfigurationManagerFactory.getInstance().createSession(testCode); |
| 114 | + |
| 115 | + session.compile(); |
| 116 | + |
| 117 | + DataSet expected = session.resolve("expected", DataSet.class); |
| 118 | + DataSet result = session.resolve("test_result", DataSet.class); |
| 119 | + |
| 120 | + for (DataStructureComponent<?, ?, ?> comp: expected.getMetadata()) |
| 121 | + assertTrue(result.getMetadata().contains(comp), "Expected component " + comp + " is missing in " + result.getMetadata()); |
| 122 | + for (DataStructureComponent<?, ?, ?> comp: result.getMetadata()) |
| 123 | + assertTrue(expected.getMetadata().contains(comp), "Unexpected component " + comp + " in result."); |
| 124 | + |
| 125 | + List<DataPoint> resDPs; |
| 126 | + List<DataPoint> expectedDPs; |
| 127 | + try (Stream<DataPoint> resStream = result.stream(); Stream<DataPoint> expStream = expected.stream()) |
118 | 128 | {
|
119 |
| - throw new RuntimeException("Error in testName\n" + testCode + "\n\n" + e.getClass().getName() + ": " + e.getMessage() + "\n", e) { |
120 |
| - private static final long serialVersionUID = 1L; |
121 |
| - |
122 |
| - { |
123 |
| - setStackTrace(e.getStackTrace()); |
124 |
| - } |
125 |
| - }; |
| 129 | + resDPs = resStream.collect(toList()); |
| 130 | + expectedDPs = expStream.collect(toList()); |
126 | 131 | }
|
| 132 | + |
| 133 | + System.out.println("Expected:"); |
| 134 | + expectedDPs.forEach(System.out::println); |
| 135 | + System.out.println("Actual:"); |
| 136 | + resDPs.forEach(System.out::println); |
| 137 | + |
| 138 | + for (DataPoint dp: resDPs) |
| 139 | + assertThat(dp, anyOf(expectedDPs.stream().map(Matchers::equalTo).collect(toList()))); |
| 140 | + for (DataPoint dp: expectedDPs) |
| 141 | + assertThat(dp, anyOf(resDPs.stream().map(Matchers::equalTo).collect(toList()))); |
127 | 142 | }
|
128 | 143 | }
|
0 commit comments