Skip to content

Commit 655c8a4

Browse files
committed
Complete review of unit & integration tests
1 parent 3dca74c commit 655c8a4

File tree

994 files changed

+20312
-5297
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

994 files changed

+20312
-5297
lines changed

vtl-api/src/main/java/it/bancaditalia/oss/vtl/config/VTLProperty.java

+10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ public default List<String> getValues()
8686
return getValue() == null ? null : isMultiple() ? Arrays.asList(getValue().split(",")) : singletonList(getValue());
8787
}
8888

89+
/**
90+
* Change the value for this property
91+
*
92+
* @param newValue The new value for this property
93+
*/
94+
public default void setValue(Class<?> newValue)
95+
{
96+
setValue(newValue.getName());
97+
}
98+
8999
/**
90100
* Change the values for this property. If the property is not multiple, behaviour is undefined
91101
*

vtl-api/src/main/java/it/bancaditalia/oss/vtl/exceptions/VTLMissingComponentsException.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public VTLMissingComponentsException(Set<? extends DataStructureComponent<?, ?,
5454

5555
public VTLMissingComponentsException(DataStructureComponent<?, ?, ?> missing, Collection<? extends DataStructureComponent<?, ?, ?>> operand)
5656
{
57-
super("Component " + missing.getVariable().getName() + " not found in " + operand);
57+
super("Component " + missing + " not found in " + operand);
5858
}
5959

6060
public VTLMissingComponentsException(Set<? extends DataStructureComponent<?, ?, ?>> missing, Map<? extends DataStructureComponent<?, ?, ?>, ? extends ScalarValue<?, ?, ?, ?>> datapoint)

vtl-api/src/main/java/it/bancaditalia/oss/vtl/exceptions/VTLUnboundAliasException.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ public class VTLUnboundAliasException extends VTLException {
2929

3030
private static final long serialVersionUID = 1L;
3131

32-
private final String name;
32+
private final String alias;
3333

3434
public VTLUnboundAliasException(String alias)
3535
{
3636
super("Alias '" + alias + "' is not bound to this transformation scheme.");
3737

38-
this.name = alias;
38+
this.alias = alias;
3939
}
4040

41-
public String getName()
41+
public String getAlias()
4242
{
43-
return name;
43+
return alias;
4444
}
4545
}

vtl-api/src/main/java/it/bancaditalia/oss/vtl/model/data/DataPoint.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ public default boolean matches(Map<? extends DataStructureComponent<? extends Id
199199
* @param names The names of the component
200200
* @return map with the values for the chosen components
201201
*/
202-
public default Map<String, ScalarValue<?,?,?,?>> getValuesByNames(Collection<String> names)
202+
public default Map<DataStructureComponent<?, ?, ?>, ScalarValue<?,?,?,?>> getValuesByNames(Collection<String> names)
203203
{
204204
return Utils.getStream(keySet())
205205
.map(c -> new SimpleEntry<>(c.getVariable().getName(), c))
206206
.filter(entryByKey(names::contains))
207-
.collect(Collectors.toMap(Entry::getKey, e -> get(e.getValue())));
207+
.collect(Collectors.toMap(Entry::getValue, e -> get(e.getValue())));
208208

209209
}
210210
/**

vtl-api/src/main/java/it/bancaditalia/oss/vtl/model/data/DataSet.java

-8
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,6 @@ public default DataSet union(SerFunction<DataPoint, Lineage> lineageOp, List<Dat
384384
*/
385385
public DataSet union(SerFunction<DataPoint, Lineage> lineageOp, List<DataSet> others, boolean check);
386386

387-
/**
388-
* Creates a new dataset as containing all the datapoints of this dataset that don't have the same identifiers as the ones in the other dataset.
389-
*
390-
* @param other the other dataset
391-
* @return the set difference between the two datasets.
392-
*/
393-
public DataSet setDiff(DataSet other);
394-
395387
/**
396388
* <b>NOTE</b>: The default implementation traverses this DataSet entirely.
397389
*

vtl-api/src/main/java/it/bancaditalia/oss/vtl/model/data/DataStructureComponent.java

+8
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,12 @@ public default DataStructureComponent<R, S, D> getRenamed(MetadataRepository rep
108108
{
109109
return ((Variable<S, D>) repo.createTempVariable(newName, getVariable().getDomain())).as(getRole());
110110
}
111+
112+
public default int defaultHashCode()
113+
{
114+
int prime = 31;
115+
int result = prime + getVariable().hashCode();
116+
result = prime * result + getRole().hashCode();
117+
return result;
118+
}
111119
}

vtl-api/src/main/java/it/bancaditalia/oss/vtl/model/data/Variable.java

+13
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,17 @@ public static String normalizeAlias(String alias)
9999
{
100100
return getDomain().cast(value);
101101
}
102+
103+
/**
104+
* A default implementation to initialize the hashCode
105+
*
106+
* @return A default variable hashCode.
107+
*/
108+
public default int defaultHashCode()
109+
{
110+
int prime = 31;
111+
int result = prime + getDomain().hashCode();
112+
result = prime * result + getName().hashCode();
113+
return result;
114+
}
102115
}

vtl-api/src/main/java/it/bancaditalia/oss/vtl/model/transform/analytic/LimitCriterion.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ public static enum LimitDirection
4040
/**
4141
* @return The distance of the bound from current data point
4242
*/
43-
public long getCount();
43+
public int getCount();
4444
}

vtl-api/src/main/java/it/bancaditalia/oss/vtl/session/MetadataRepository.java

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public interface MetadataRepository
5050
*/
5151
public ValueDomainSubset<?, ?> getDomain(String name);
5252

53+
/**
54+
* Returns the source definition for the dataset with the specified name if it's defined.
55+
*
56+
* @param name the name of the dataset
57+
* @return the source definition or null if it's not defined.
58+
*/
59+
public String getDatasetSource(String name);
60+
5361
/**
5462
* Returns a dataset structure with the specified name if it exists.
5563
*

vtl-api/src/main/java/it/bancaditalia/oss/vtl/util/SerCollectors.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.Objects;
4141
import java.util.Optional;
4242
import java.util.OptionalDouble;
43+
import java.util.OptionalLong;
4344
import java.util.Set;
4445
import java.util.concurrent.ConcurrentHashMap;
4546
import java.util.concurrent.ConcurrentMap;
@@ -124,21 +125,21 @@ public static <T, U> SerCollector<T, U[], U> reducing(U identity, SerFunction<?
124125
return collectingAndThen(summarizingDouble(mapper), SerDoubleSumAvgCount::getSum);
125126
}
126127

127-
public static <T> SerCollector<T, ?, Long> summingLong(SerToLongFunction<? super T> mapper)
128+
public static <T> SerCollector<T, ?, OptionalLong> summingLong(SerToLongFunction<? super T> mapper)
128129
{
129130
return new SerCollector<>(
130-
() -> new long[1],
131-
(a, t) -> { a[0] += mapper.applyAsLong(t); },
132-
(a, b) -> { a[0] += b[0]; return a; },
133-
a -> a[0], EnumSet.of(CONCURRENT, UNORDERED));
131+
() -> new long[2],
132+
(a, t) -> { a[0] += mapper.applyAsLong(t); a[1]++; },
133+
(a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; },
134+
a -> a[1] <= 0 ? OptionalLong.empty() : OptionalLong.of(a[0]), EnumSet.of(CONCURRENT, UNORDERED));
134135
}
135136

136137
public static <T> SerCollector<T, ?, OptionalDouble> averagingDouble(SerToDoubleFunction<? super T> mapper)
137138
{
138139
return collectingAndThen(summarizingDouble(mapper), SerDoubleSumAvgCount::getAverage);
139140
}
140141

141-
public static <T> SerCollector<T, SerDoubleSumAvgCount, SerDoubleSumAvgCount> summarizingDouble(SerToDoubleFunction<? super T> mapper)
142+
public static <T> SerCollector<T, ?, SerDoubleSumAvgCount> summarizingDouble(SerToDoubleFunction<? super T> mapper)
142143
{
143144
return SerCollector.of(
144145
() -> new SerDoubleSumAvgCount(),

vtl-api/src/main/java/it/bancaditalia/oss/vtl/util/SerDoubleSumAvgCount.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public final long getCount()
9191

9292
public final OptionalDouble getSum()
9393
{
94-
if (getCount() < 0)
94+
if (count <= 0)
9595
return OptionalDouble.empty();
9696

9797
return OptionalDouble.of(internalSum());

vtl-bundles/vtl-coverage/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@
136136
<artifactId>junit-jupiter-params</artifactId>
137137
<scope>test</scope>
138138
</dependency>
139+
<dependency>
140+
<groupId>org.hamcrest</groupId>
141+
<artifactId>hamcrest</artifactId>
142+
<version>2.2</version>
143+
<scope>test</scope>
144+
</dependency>
139145
<dependency>
140146
<groupId>org.apache.logging.log4j</groupId>
141147
<artifactId>log4j-slf4j2-impl</artifactId>

vtl-bundles/vtl-coverage/src/test/java/it/bancaditalia/oss/vtl/coverage/tests/IntegrationTestSuite.java

+75-60
Original file line numberDiff line numberDiff line change
@@ -19,110 +19,125 @@
1919
*/
2020
package it.bancaditalia.oss.vtl.coverage.tests;
2121

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;
2227
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;
2331

2432
import java.io.BufferedReader;
2533
import java.io.IOException;
2634
import java.io.InputStreamReader;
2735
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;
2939
import java.util.ArrayList;
3040
import java.util.List;
31-
import java.util.regex.Matcher;
32-
import java.util.regex.Pattern;
3341
import java.util.stream.Stream;
3442

43+
import org.hamcrest.Matchers;
3544
import org.junit.jupiter.params.ParameterizedTest;
3645
import org.junit.jupiter.params.provider.Arguments;
3746
import org.junit.jupiter.params.provider.MethodSource;
3847

3948
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;
4252
import it.bancaditalia.oss.vtl.model.data.DataPoint;
4353
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;
4556

4657
public class IntegrationTestSuite
4758
{
4859
static {
4960
System.setProperty("vtl.sequential", "true");
5061
}
62+
63+
private static Path root;
5164

5265
public static Stream<Arguments> test() throws IOException, URISyntaxException
5366
{
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+
5672
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)))
5877
{
59-
String testName;
60-
StringBuilder testCode = new StringBuilder();
61-
StringBuilder parsedLine = new StringBuilder();
6278
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"))
6580
{
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")))
6982
{
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)
7586
{
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());
7990
}
80-
testCode.append(parsedLine.append(testLine.substring(start)).toString())
81-
.append(System.lineSeparator());
82-
parsedLine.setLength(0);
8391
}
92+
tests.add(Arguments.of(testName, testCode.toString()));
93+
testCode.setLength(0);
8494
}
85-
tests.add(Arguments.of(testName, testCode.toString()));
86-
testCode.setLength(0);
87-
}
8895
}
8996

9097
return tests.stream();
9198
}
9299

93100
@ParameterizedTest(name = "{0}")
94101
@MethodSource
95-
public void test(String testName, String testCode)
102+
public synchronized void test(final String testName, final String testCode) throws IOException, URISyntaxException
96103
{
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())
118128
{
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());
126131
}
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())));
127142
}
128143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
id1,m1
2+
a,3.346
3+
b,3.671
4+
c,6.1893434
5+
d,

0 commit comments

Comments
 (0)