Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export notice schema as json file #925

Merged
merged 50 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
9f6b822
feat: --dump_notices_schema or -d
Jun 22, 2021
932a795
Merge remote-tracking branch 'origin/master' into feat/dump-json-schema
Jul 9, 2021
3c76b9a
rename CLI parameter
Jul 9, 2021
1036c0b
make NoticeExport annotation visible at runtime
Jul 9, 2021
03b9424
pick the right constructor for schema export in CsvParsingFailedNotice
Jul 9, 2021
e0834e7
Make parameters's name discoverable a runtime + clean up of gradle.bu…
Jul 9, 2021
e7ad1b3
implement logic for notice schema export
Jul 9, 2021
8870e71
unit tests
Jul 9, 2021
d21f9b5
create json file
Jul 9, 2021
30ed17e
export if -n is provided
Jul 9, 2021
390aa20
typo
Jul 9, 2021
0b44560
Merge remote-tracking branch 'origin/master' into feat/dump-json-schema
Jul 15, 2021
2557254
apply suggestions from code review
Jul 21, 2021
898f284
provide suggestions from code review
Jul 21, 2021
09e11bd
use SchemaExport annotation
Jul 21, 2021
1cf0106
add copyright header
Jul 21, 2021
3298fc3
revert changes to javadocs
Jul 23, 2021
4a128d8
move variables to annotation definition
Jul 23, 2021
d6f549b
enable schema generation without providing other parameters
Jul 23, 2021
ff781dd
simplify checks from extractCoreNoticesProperties
Jul 23, 2021
12acd6f
refactor: rename method for clarity
Jul 23, 2021
1eab42b
programmatically retrieve package name from classes
Jul 23, 2021
7347206
docs: update to match the latest dev
Jul 23, 2021
96d6278
Merge branch 'master' into feat/dump-json-schema
Jul 28, 2021
7d3da1c
introduce new argument: --abort_after_schema_extraction
Jul 28, 2021
b9000a3
rework package name retrieval process
Jul 28, 2021
5163bd9
remove usused import statements
Jul 28, 2021
6e97a97
rework default value logic for --abort_after_schema_extraction CLI pa…
Jul 28, 2021
8af89f2
rework notice construction
Jul 28, 2021
bc04ce4
docs: update to match latest dev
Jul 28, 2021
7941d71
docs: update to match latest dev
Jul 28, 2021
72730b7
apply suggestion from code review
lionel-nj Jul 28, 2021
a46270f
apply suggestions from code review
Jul 30, 2021
f7be4cd
apply suggestions from code review:
Aug 3, 2021
790597b
fix tests
Aug 3, 2021
7cae579
remove code for debugging in test
Aug 3, 2021
ef77a4f
define custom exception ConstructorParametersInconsistencyException
Aug 3, 2021
8b08e4d
remove package com.sun.jdi.InvalidTypeException form tests
Aug 3, 2021
5043dbc
apply google-java-style plugin
Aug 3, 2021
3331ead
sort keys from json schema alphabetically
Aug 3, 2021
3f591e2
remove CLI parameter abort_after_schema_extraction
Aug 4, 2021
5863170
Merge branch 'master' into feat/dump-json-schema
Aug 10, 2021
e882db5
remove support for annotations
Aug 10, 2021
25bf9a6
Merge branch 'master' into feat/dump-json-schema
Aug 12, 2021
58f8a93
plug contribution from #951 into Main.java
Aug 12, 2021
1c4db69
remove unused classes
Aug 12, 2021
0d699e7
style: apply google-java-format plugin
Aug 12, 2021
7d6b8be
touch
Aug 12, 2021
6c02c30
Merge branch 'master' into feat/dump-json-schema
Aug 16, 2021
d31b637
style: format code
Aug 16, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove support for annotations
  • Loading branch information
lionel-nj committed Aug 10, 2021
commit e882db5cb043b082e9531d2bcea3c8a38c955dbc
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,21 @@ public SeverityLevel getSeverityLevel() {
*
* @return notice code, e.g., "foreign_key_violation".
*/
public String getCode() {
public static String getCode(String classSimpleName) {
return StringUtils.removeEnd(
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, classSimpleName),
NOTICE_SUFFIX);
}

/**
* Returns a descriptive type-specific name for this notice based on the class simple name.
*
* @return notice code, e.g., "foreign_key_violation".
*/
public String getCode() {
return getCode(getClass().getSimpleName());
}

@Override
public boolean equals(Object other) {
if (this == other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,18 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.IOException;
import java.lang.annotation.AnnotationFormatError;
import java.lang.reflect.Constructor;
import java.lang.reflect.Parameter;
import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.mobilitydata.gtfsvalidator.annotation.SchemaExport;
import org.mobilitydata.gtfsvalidator.exception.ConstructorParametersInconsistencyException;
import org.mobilitydata.gtfsvalidator.type.GtfsColor;
import org.mobilitydata.gtfsvalidator.type.GtfsDate;
Expand Down Expand Up @@ -215,20 +210,18 @@ private static JsonObject extractMainNoticesProperties(String validatorPackageNa

/**
* Return a {@code JsonArray} that contains information about the type of each parameter of a
* {@code ValidationNotice} using the constructors of the class that are annotated by {@code
* SchemaExport}.
* {@code ValidationNotice} using the constructors of the class.
*
* @param validationNoticeSubClass the {@code ValidationNotice} sub class to extract information
* from
* @return a {@code JsonArray} that contains information about the type of each parameter of said
* {@code ValidationNotice} using the constructors of the class that are annotated by {@code
* SchemaExport}.
* {@code ValidationNotice} using the constructors of the class.
* @throws ConstructorParametersInconsistencyException if two notice constructors defines the same
* parameter with different types.
*/
private static JsonArray extractNoticeProperties(Class<?> validationNoticeSubClass)
throws ConstructorParametersInconsistencyException {
List<Constructor<?>> constructors = getAnnotatedConstructors(validationNoticeSubClass);
Constructor<?>[] constructors = validationNoticeSubClass.getDeclaredConstructors();
JsonArray parametersAsJsonArray = new JsonArray();
Map<String, Parameter> parameterMap = new TreeMap<>();
for (Constructor<?> constructor : constructors) {
Expand Down Expand Up @@ -277,32 +270,4 @@ private static String mapDataType(Parameter parameter) {
parameter.getType().getSimpleName().toUpperCase()))
.getName();
}

/**
* Returns the annotated constructors of a {@code ValidationNotice} subclass. Throws {@code
* AnnotationFormatError} if the {@code ValidationNotice} subclass does not define a constructor
* for schema export i.e. no constructor for the given class uses {@code SchemaExport}
* annotation.
*
* @param clazz the class to extract the annotated constructor from
* @return the annotated constructors of a {@code ValidationNotice} subclass. Throws {@code
* AnnotationFormatError} if the {@code ValidationNotice} subclass does not define a constructor
* for schema export i.e. no constructor for the given class uses {@code SchemaExport}
* annotation.
*/
private static List<Constructor<?>> getAnnotatedConstructors(Class<?> clazz) {
List<Constructor<?>> constructors = new ArrayList<>();
for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
if (constructor.isAnnotationPresent(SchemaExport.class)) {
constructors.add(constructor);
}
}
if (constructors.size() == 0) {
throw new AnnotationFormatError(
String.format(
"Validation notice %s does not define constructor for schema export",
clazz.getSimpleName()));
}
return constructors;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package org.mobilitydata.gtfsvalidator.notice;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.lang.annotation.AnnotationFormatError;
import org.junit.Test;
import org.mobilitydata.gtfsvalidator.exception.ConstructorParametersInconsistencyException;
import org.mobilitydata.gtfsvalidator.notice.sample.AnotherTestValidationNotice;
import org.mobilitydata.gtfsvalidator.notice.sample.mismatching_types.MismatchingTypesNotice;
import org.mobilitydata.gtfsvalidator.notice.sample.no_annotation.NoAnnotationNotice;
import org.mobilitydata.gtfsvalidator.validator.sample.SampleTestValidator;

public class NoticeSchemaGeneratorTest {
Expand Down Expand Up @@ -104,29 +100,4 @@ public void exportNoticesSchema()
+ "}"
);
}

@Test
public void noAnnotationOnNotice_throwsException() {
AnnotationFormatError exception = assertThrows(AnnotationFormatError.class,
() -> NoticeSchemaGenerator.export(
true,
ImmutableList.of(NoAnnotationNotice.class.getPackage().getName()),
ImmutableList.of(SampleTestValidator.class.getPackage().getName())));
assertThat(exception.getMessage()).matches(String
.format("Validation notice %s does not define constructor for schema export",
NoAnnotationNotice.class.getSimpleName()));
}

@Test
public void mismatchingTypesInNoticeConstructor_throwsException() {
ConstructorParametersInconsistencyException exception = assertThrows(ConstructorParametersInconsistencyException.class,
() -> NoticeSchemaGenerator.export(
true,
ImmutableList.of(MismatchingTypesNotice.class.getPackage().getName()),
ImmutableList.of(SampleTestValidator.class.getPackage().getName())));
assertThat(exception.getMessage()).matches(String
.format(
"Validation notice %s defines parameter %s with different types in its constructors.",
MismatchingTypesNotice.class.getSimpleName(), "parameterWithWrongType"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.mobilitydata.gtfsvalidator.notice.sample;

import com.google.common.collect.ImmutableMap;
import org.mobilitydata.gtfsvalidator.annotation.SchemaExport;
import org.mobilitydata.gtfsvalidator.notice.SeverityLevel;
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;
import org.mobilitydata.gtfsvalidator.type.GtfsColor;
Expand All @@ -29,35 +27,42 @@
*/
public class AnotherTestValidationNotice extends ValidationNotice {

@SchemaExport
private final String filename;
private final Long csvRowNumber;
private final String fieldName;
private final Double otherFieldValue;
private final Object fieldValue;
private final GtfsDate sampleDate;
private final GtfsTime sampleTime;
private final GtfsColor sampleColor;
private final Integer integerValue;

public AnotherTestValidationNotice(String filename, long csvRowNumber, String fieldName,
Object fieldValue, double otherFieldValue, GtfsDate sampleDate, GtfsTime sampleTime,
GtfsColor sampleColor) {
super(new ImmutableMap.Builder<String, Object>()
.put("filename", filename)
.put("csvRowNumber", csvRowNumber)
.put("fieldName", fieldName)
.put("otherFieldValue", otherFieldValue)
.put("fieldValue", fieldValue)
.put("sampleDate", sampleDate)
.put("sampleTime", sampleTime)
.put("sampleColor", sampleColor)
.build(),
SeverityLevel.WARNING);
super(SeverityLevel.WARNING);
this.filename = filename;
this.csvRowNumber = csvRowNumber;
this.fieldName = fieldName;
this.otherFieldValue = otherFieldValue;
this.fieldValue = fieldValue;
this.sampleDate = sampleDate;
this.sampleTime = sampleTime;
this.sampleColor = sampleColor;
this.integerValue = null;
}

@SchemaExport
public AnotherTestValidationNotice(String filename, long csvRowNumber, String fieldName,
Object fieldValue, GtfsTime sampleTime, GtfsColor sampleColor, int integerValue) {
super(new ImmutableMap.Builder<String, Object>()
.put("filename", filename)
.put("csvRowNumber", csvRowNumber)
.put("fieldName", fieldName)
.put("fieldValue", fieldValue)
.put("sampleTime", sampleTime)
.put("sampleColor", sampleColor)
.put("integerValue", integerValue)
.build(),
SeverityLevel.WARNING);
super(SeverityLevel.WARNING);
this.filename = filename;
this.csvRowNumber = csvRowNumber;
this.fieldValue = fieldValue;
this.fieldName = fieldName;
this.sampleTime = sampleTime;
this.sampleColor = sampleColor;
this.integerValue = integerValue;
this.sampleDate = null;
this.otherFieldValue = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,24 @@
*/
package org.mobilitydata.gtfsvalidator.notice.sample;

import com.google.common.collect.ImmutableMap;
import org.mobilitydata.gtfsvalidator.annotation.SchemaExport;
import org.mobilitydata.gtfsvalidator.notice.SeverityLevel;
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;

/**
* {@code ValidationNotice} defined for tests purpose.
*/
public class SomeTestValidationNotice extends ValidationNotice {
private final String filename;
private final long csvRowNumber;
private final String fieldName;
private final Object fieldValue;

@SchemaExport
public SomeTestValidationNotice(String filename, long csvRowNumber, String fieldName,
Object fieldValue, SeverityLevel severityLevel) {
super(new ImmutableMap.Builder<String, Object>()
.put("filename", filename)
.put("csvRowNumber", csvRowNumber)
.put("fieldName", fieldName)
.put("fieldValue", fieldValue)
.build(),
severityLevel);
super(severityLevel);
this.filename = filename;
this.csvRowNumber = csvRowNumber;
this.fieldName = fieldName;
this.fieldValue = fieldValue;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

package org.mobilitydata.gtfsvalidator.validator.sample;

import com.google.common.collect.ImmutableMap;
import javax.inject.Inject;
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator;
import org.mobilitydata.gtfsvalidator.annotation.SchemaExport;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.notice.SeverityLevel;
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice;
Expand All @@ -41,14 +39,13 @@ public void validate(NoticeContainer noticeContainer) {
}

static class NestedTestValidatorNotice extends ValidationNotice {
private final String tripId;
private final Long csvRowNumber;

@SchemaExport
NestedTestValidatorNotice(String tripId, long csvRowNumber) {
super(new ImmutableMap.Builder<String, Object>()
.put("tripId", tripId)
.put("csvRowNumber", csvRowNumber)
.build(),
SeverityLevel.WARNING);
super(SeverityLevel.WARNING);
this.tripId = tripId;
this.csvRowNumber = csvRowNumber;
}
}
}