Skip to content

Commit acd0c87

Browse files
authored
Merge pull request #42 from entur/same-as
Implement sameAs method to check if the outcome of two validation events are the same
2 parents 851ba53 + 727a346 commit acd0c87

File tree

6 files changed

+175
-4
lines changed

6 files changed

+175
-4
lines changed

src/main/java/org/entur/gbfs/validation/model/FileValidationError.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
package org.entur.gbfs.validation.model;
2020

21-
public class FileValidationError {
21+
import java.util.Objects;
22+
23+
public class FileValidationError implements ValidationResultComponentIdentity<FileValidationError> {
2224
private String schemaPath;
2325
private String violationPath;
2426
private String message;
@@ -55,4 +57,13 @@ public String toString() {
5557
", message='" + message + '\'' +
5658
'}';
5759
}
60+
61+
@Override
62+
public boolean sameAs(FileValidationError other) {
63+
if (other == null) return false;
64+
if (!Objects.equals(schemaPath, other.schemaPath)) return false;
65+
if (!Objects.equals(violationPath, other.violationPath))
66+
return false;
67+
return Objects.equals(message, other.message);
68+
}
5869
}

src/main/java/org/entur/gbfs/validation/model/FileValidationResult.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
package org.entur.gbfs.validation.model;
2020

2121
import java.util.List;
22+
import java.util.Objects;
23+
import java.util.stream.IntStream;
2224

23-
public class FileValidationResult {
25+
public class FileValidationResult implements ValidationResultComponentIdentity<FileValidationResult> {
2426
private String file;
2527
private boolean required;
2628
private boolean exists;
@@ -107,4 +109,19 @@ public String toString() {
107109
", errors=" + errors +
108110
'}';
109111
}
112+
113+
@Override
114+
public boolean sameAs(FileValidationResult other) {
115+
if (other == null) return false;
116+
if (required != other.required) return false;
117+
if (exists != other.exists) return false;
118+
if (errorsCount != other.errorsCount) return false;
119+
if (!Objects.equals(file, other.file)) return false;
120+
if (!Objects.equals(schema, other.schema)) return false;
121+
if (!Objects.equals(fileContents, other.fileContents)) return false;
122+
if (!Objects.equals(version, other.version)) return false;
123+
return IntStream
124+
.range(0, errors.size())
125+
.allMatch(i -> errors.get(i).sameAs(other.errors.get(i)));
126+
}
110127
}

src/main/java/org/entur/gbfs/validation/model/ValidationResult.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.HashMap;
2222
import java.util.Map;
2323

24-
public class ValidationResult {
24+
public class ValidationResult implements ValidationResultComponentIdentity<ValidationResult> {
2525
private ValidationSummary summary = new ValidationSummary();
2626
private Map<String, FileValidationResult> files = new HashMap<>();
2727

@@ -48,4 +48,11 @@ public String toString() {
4848
", files=" + files +
4949
'}';
5050
}
51+
52+
@Override
53+
public boolean sameAs(ValidationResult other) {
54+
if (other == null) return false;
55+
if (!summary.sameAs(other.getSummary())) return false;
56+
return files.entrySet().stream().allMatch(entry -> other.files.get(entry.getKey()).sameAs(entry.getValue()));
57+
}
5158
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
*
3+
* *
4+
* *
5+
* * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by
6+
* * * the European Commission - subsequent versions of the EUPL (the "Licence");
7+
* * * You may not use this work except in compliance with the Licence.
8+
* * * You may obtain a copy of the Licence at:
9+
* * *
10+
* * * https://joinup.ec.europa.eu/software/page/eupl
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the Licence is distributed on an "AS IS" basis,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the Licence for the specific language governing permissions and
16+
* * * limitations under the Licence.
17+
* *
18+
*
19+
*/
20+
21+
package org.entur.gbfs.validation.model;
22+
23+
public interface ValidationResultComponentIdentity<T> {
24+
25+
/**
26+
* Check if the (part of) a validation result is the same as another one. Should return true for two validation
27+
* events at different times with the same outcome
28+
*/
29+
boolean sameAs(T other);
30+
}

src/main/java/org/entur/gbfs/validation/model/ValidationSummary.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
package org.entur.gbfs.validation.model;
2020

21-
public class ValidationSummary {
21+
import java.util.Objects;
22+
23+
public class ValidationSummary implements ValidationResultComponentIdentity<ValidationSummary> {
2224
private String version;
2325
private long timestamp;
2426
private int errorsCount;
@@ -55,4 +57,11 @@ public String toString() {
5557
", errorsCount=" + errorsCount +
5658
'}';
5759
}
60+
61+
@Override
62+
public boolean sameAs(ValidationSummary other) {
63+
if (other == null) return false;
64+
if (errorsCount != other.errorsCount) return false;
65+
return Objects.equals(version, other.version);
66+
}
5867
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
*
3+
* *
4+
* *
5+
* * * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by
6+
* * * the European Commission - subsequent versions of the EUPL (the "Licence");
7+
* * * You may not use this work except in compliance with the Licence.
8+
* * * You may obtain a copy of the Licence at:
9+
* * *
10+
* * * https://joinup.ec.europa.eu/software/page/eupl
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the Licence is distributed on an "AS IS" basis,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the Licence for the specific language governing permissions and
16+
* * * limitations under the Licence.
17+
* *
18+
*
19+
*/
20+
21+
package org.entur.gbfs.validation.model;
22+
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
class ValidationResultTest {
30+
31+
@Test
32+
void testSameAsSucceeds() {
33+
Assertions.assertTrue(generateValidationResult(
34+
"message-1",
35+
"message-2"
36+
).sameAs(generateValidationResult(
37+
"message-1",
38+
"message-2"
39+
)));
40+
}
41+
42+
@Test
43+
void testSameAsFails() {
44+
Assertions.assertFalse(generateValidationResult(
45+
"message-1",
46+
"message-2"
47+
).sameAs(generateValidationResult(
48+
"message-1",
49+
"message-3"
50+
)));
51+
}
52+
53+
private ValidationResult generateValidationResult(String message1, String message2) {
54+
var validationResult = new ValidationResult();
55+
56+
validationResult.setFiles(
57+
Map.of(
58+
"gbfs", generateFileValidationResult(message1, message2)
59+
)
60+
);
61+
62+
return validationResult;
63+
}
64+
65+
private ValidationSummary generateValidationSummary() {
66+
var validationSummary = new ValidationSummary();
67+
validationSummary.setVersion("2.2");
68+
validationSummary.setTimestamp(System.currentTimeMillis());
69+
validationSummary.setErrorsCount(2);
70+
return validationSummary;
71+
}
72+
73+
private FileValidationResult generateFileValidationResult(String message1, String message2) {
74+
var fileValidationResult = new FileValidationResult();
75+
76+
fileValidationResult.setFile("gbfs");
77+
fileValidationResult.setExists(true);
78+
fileValidationResult.setRequired(true);
79+
fileValidationResult.setErrorsCount(2);
80+
fileValidationResult.setErrors(List.of(
81+
generateFileValidationError(message1),
82+
generateFileValidationError(message2)
83+
));
84+
85+
return fileValidationResult;
86+
}
87+
88+
private FileValidationError generateFileValidationError(String message) {
89+
var fileValidationError = new FileValidationError();
90+
91+
fileValidationError.setMessage(message);
92+
fileValidationError.setSchemaPath("schema/path");
93+
fileValidationError.setViolationPath("violation/path");
94+
95+
return fileValidationError;
96+
}
97+
}

0 commit comments

Comments
 (0)