-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce extensible validation mechanism
This also introduces a built-in validator which makes sure the spec content comply to the schema and more.
- Loading branch information
Showing
14 changed files
with
1,487 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/org/neo4j/importer/v1/validation/InvalidSpecificationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.importer.v1.validation; | ||
|
||
import java.util.Set; | ||
|
||
public class InvalidSpecificationException extends SpecificationException { | ||
public InvalidSpecificationException(SpecificationValidationResult result) { | ||
super(String.format("Import specification is invalid, see report below:\n%s", validationReport(result))); | ||
} | ||
|
||
// TODO: wrap long error messages | ||
private static String validationReport(SpecificationValidationResult result) { | ||
Set<SpecificationError> errors = result.getErrors(); | ||
Set<SpecificationWarning> warnings = result.getWarnings(); | ||
return String.format( | ||
"===============================================================================\n" + "Summary\n" | ||
+ "===============================================================================\n" | ||
+ "\t- %d error(s)\n" | ||
+ "\t- %d warning(s)\n\n" | ||
+ "%s" | ||
+ "%s" | ||
+ "===============================================================================", | ||
errors.size(), warnings.size(), errorReport(errors), warningReport(warnings)); | ||
} | ||
|
||
private static String errorReport(Set<SpecificationError> errors) { | ||
if (errors.isEmpty()) { | ||
return ""; | ||
} | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("=== Errors ===\n"); | ||
errors.forEach((error) -> { | ||
builder.append(String.format("\t- [%s] %s\n", error.getCode(), error.getMessage())); | ||
}); | ||
return builder.toString(); | ||
} | ||
|
||
private static String warningReport(Set<SpecificationWarning> warnings) { | ||
if (warnings.isEmpty()) { | ||
return ""; | ||
} | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append("\n=== Warnings ===\n"); | ||
warnings.forEach((warning) -> { | ||
builder.append(String.format("\t- [%s] %s\n", warning.getCode(), warning.getMessage())); | ||
}); | ||
return builder.toString(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/org/neo4j/importer/v1/validation/SpecificationError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.importer.v1.validation; | ||
|
||
import java.util.Objects; | ||
|
||
public final class SpecificationError { | ||
|
||
private final String elementPath; | ||
private final String code; | ||
private final String message; | ||
|
||
public SpecificationError(String elementPath, String code, String message) { | ||
this.elementPath = elementPath; | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public String getElementPath() { | ||
return elementPath; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (object == null || getClass() != object.getClass()) return false; | ||
SpecificationError that = (SpecificationError) object; | ||
return Objects.equals(elementPath, that.elementPath) | ||
&& Objects.equals(code, that.code) | ||
&& Objects.equals(message, that.message); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(elementPath, code, message); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SpecificationError{" + "elementPath='" | ||
+ elementPath + '\'' + ", code='" | ||
+ code + '\'' + ", message='" | ||
+ message + '\'' + '}'; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/neo4j/importer/v1/validation/SpecificationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.importer.v1.validation; | ||
|
||
public abstract class SpecificationException extends Exception { | ||
|
||
public SpecificationException(String message) { | ||
super(message); | ||
} | ||
|
||
public SpecificationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/org/neo4j/importer/v1/validation/SpecificationValidationResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.importer.v1.validation; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class SpecificationValidationResult { | ||
|
||
private final Set<SpecificationError> errors; | ||
private final Set<SpecificationWarning> warnings; | ||
|
||
// visible for testing | ||
SpecificationValidationResult(Set<SpecificationError> errors, Set<SpecificationWarning> warnings) { | ||
this.errors = errors; | ||
this.warnings = warnings; | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public boolean passes() { | ||
return errors.isEmpty(); | ||
} | ||
|
||
public Set<SpecificationError> getErrors() { | ||
return errors; | ||
} | ||
|
||
public Set<SpecificationWarning> getWarnings() { | ||
return warnings; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) return true; | ||
if (object == null || getClass() != object.getClass()) return false; | ||
SpecificationValidationResult that = (SpecificationValidationResult) object; | ||
return Objects.equals(errors, that.errors) && Objects.equals(warnings, that.warnings); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(errors, warnings); | ||
} | ||
|
||
public static final class Builder { | ||
|
||
private final Set<SpecificationError> errors = new LinkedHashSet<>(); | ||
private final Set<SpecificationWarning> warnings = new LinkedHashSet<>(); | ||
|
||
public Builder addError(String elementPath, String code, String message) { | ||
return addError(new SpecificationError(elementPath, code, message)); | ||
} | ||
|
||
public Builder addWarning(String elementPath, String code, String message) { | ||
return addWarning(new SpecificationWarning(elementPath, code, message)); | ||
} | ||
|
||
public Builder merge(SpecificationValidationResult other) { | ||
errors.addAll(other.errors); | ||
warnings.addAll(other.warnings); | ||
return this; | ||
} | ||
|
||
public SpecificationValidationResult build() { | ||
return new SpecificationValidationResult(errors, warnings); | ||
} | ||
|
||
private Builder addError(SpecificationError error) { | ||
errors.add(error); | ||
return this; | ||
} | ||
|
||
private Builder addWarning(SpecificationWarning warning) { | ||
warnings.add(warning); | ||
return this; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/neo4j/importer/v1/validation/SpecificationValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.neo4j.importer.v1.validation; | ||
|
||
import org.neo4j.importer.v1.ImportSpecification; | ||
|
||
public interface SpecificationValidator { | ||
|
||
SpecificationValidationResult validate(ImportSpecification specification); | ||
} |
Oops, something went wrong.