Skip to content

Commit

Permalink
#14
Browse files Browse the repository at this point in the history
  • Loading branch information
mpostelnicu committed May 17, 2018
1 parent a20dbe6 commit d1415cf
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 33 deletions.
15 changes: 8 additions & 7 deletions jocds-cli/src/main/java/org/devgateway/jocds/cli/CliRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
package org.devgateway.jocds.cli;

import com.github.fge.jackson.JsonLoader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;
import org.devgateway.jocds.OcdsValidatorConstants;
import org.devgateway.jocds.OcdsValidatorNodeRequest;
import org.devgateway.jocds.OcdsValidatorRequest;
Expand All @@ -24,6 +20,11 @@
import org.springframework.validation.DirectFieldBindingResult;
import org.springframework.validation.Validator;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Properties;

@Component
public class CliRunner implements CommandLineRunner {

Expand Down Expand Up @@ -120,18 +121,18 @@ public void run(String... strings) throws Exception {
.equals(OcdsValidatorConstants.Operations.VALIDATE)) {
OcdsValidatorNodeRequest request = new OcdsValidatorNodeRequest(createCommonValidatorRequest(), null);
validateAndPrintUsage(request, "request");
System.out.println(ocdsValidatorService.processingReportToJsonNode(
System.out.println(ocdsValidatorService.processingReportToJsonText(
ocdsValidatorService.validate(request), request));
} else if (properties.containsKey(CliConstants.PARAM_FILE)) {
OcdsValidatorNodeRequest fileValidationRequest = createFileValidationRequest();
validateAndPrintUsage(fileValidationRequest, "fileValidationRequest");
System.out.println(ocdsValidatorService.processingReportToJsonNode(
System.out.println(ocdsValidatorService.processingReportToJsonText(
ocdsValidatorService.validate(fileValidationRequest), fileValidationRequest));
} else {
if (properties.containsKey(CliConstants.PARAM_URL)) {
OcdsValidatorUrlRequest urlValidationRequest = createUrlValidationRequest();
validateAndPrintUsage(urlValidationRequest, "urlValidationRequest");
System.out.println(ocdsValidatorService.processingReportToJsonNode(
System.out.println(ocdsValidatorService.processingReportToJsonText(
ocdsValidatorService.validate(urlValidationRequest), urlValidationRequest));
} else {
printUsage();
Expand Down
8 changes: 4 additions & 4 deletions jocds-cli/src/test/java/test/TestOcdsValidatorRelease.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public class TestOcdsValidatorRelease {
@Test
public void testReleaseValidation() {

OcdsValidatorStringRequest request = new OcdsValidatorStringRequest(OcdsValidatorConstants.Versions.OCDS_1_1_1,
OcdsValidatorStringRequest request = new OcdsValidatorStringRequest(OcdsValidatorConstants.Versions.OCDS_1_1_3,
OcdsValidatorConstants.EXTENSIONS, OcdsValidatorConstants.Schemas.RELEASE);
request.setJson(getJsonFromResource("/full-release.json"));

ProcessingReport processingReport = ocdsValidatorService.validate(request);
if (!processingReport.isSuccess()) {
System.out.println(processingReport);
}

System.out.println(ocdsValidatorService.processingReportToJsonText(
ocdsValidatorService.validate(request), request));

Assert.assertTrue(processingReport.isSuccess());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public static final class CustomSchemaKeywords {
* See http://standard.open-contracting.org/latest/en/schema/deprecation/
*/
public static final String DEPRECATED = "deprecated";
public static final String WHOLE_LIST_MERGE = "wholeListMerge";
public static final String OMIT_WHEN_MERGED = "omitWhenMerged";
public static final String MERGE_STRATEGY = "mergeStrategy";
public static final String VERSION_ID = "versionId";
public static final String OPEN_CODE_LIST = "openCodelist";
public static final String CODE_LIST = "codelist";
}

public static final class Schemas {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.devgateway.jocds;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.TextNode;
Expand All @@ -31,13 +32,13 @@
import com.github.fge.msgsimple.source.MessageSource;
import com.vdurmont.semver4j.Requirement;
import com.vdurmont.semver4j.Semver;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.PostConstruct;
import org.devgateway.jocds.jsonschema.checker.CodelistSyntaxChecker;
import org.devgateway.jocds.jsonschema.checker.DeprecatedSyntaxChecker;
import org.devgateway.jocds.jsonschema.checker.MergeStrategySyntaxChecker;
import org.devgateway.jocds.jsonschema.checker.OmitWhenMergedSyntaxChecker;
import org.devgateway.jocds.jsonschema.checker.OpenCodelistSyntaxChecker;
import org.devgateway.jocds.jsonschema.checker.VersionIdSyntaxChecker;
import org.devgateway.jocds.jsonschema.checker.WholeListMergeSyntaxChecker;
import org.devgateway.jocds.jsonschema.validator.DeprecatedValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -55,6 +56,7 @@
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;


/**
* Created by mpostelnicu on 7/5/17.
*/
Expand Down Expand Up @@ -91,10 +93,26 @@ private JsonNode getUnmodifiedSchemaNode(OcdsValidatorRequest request) {
}
}


public String processingReportToJsonText(ProcessingReport report, OcdsValidatorRequest request) {
try {
return jacksonObjectMapper.writeValueAsString(processingReportToJsonNode(report, request));
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}

public JsonNode processingReportToJsonNode(ProcessingReport report, OcdsValidatorRequest request) {
if (report.isSuccess() && request.getOperation().equals(OcdsValidatorConstants.Operations.VALIDATE)) {
return TextNode.valueOf("OK");
JsonNode r = ((ListProcessingReport) report).asJson();
if (r.toString().length() == 2) {
return TextNode.valueOf("OK");
} else {
return r;
}
}

if (report instanceof ListProcessingReport) {
return ((ListProcessingReport) report).asJson();
}
Expand Down Expand Up @@ -276,7 +294,7 @@ private JsonSchema getSchema(OcdsValidatorRequest request) {
try {
JsonSchema schema = JsonSchemaFactory.newBuilder()
.setValidationConfiguration(validationConfiguration)
.setReportProvider(new ListReportProvider(LogLevel.ERROR, LogLevel.NONE)).freeze()
.setReportProvider(new ListReportProvider(LogLevel.WARNING, LogLevel.NONE)).freeze()
.getJsonSchema(schemaNode);
logger.debug("Saving to cache schema with extensions " + request.getKey());
keySchema.put(request.getKey(), schema);
Expand Down Expand Up @@ -339,11 +357,50 @@ private void init() {
private Keyword createDeprecatedKeyword() {
return Keyword.newBuilder(OcdsValidatorConstants.CustomSchemaKeywords.DEPRECATED)
.withSyntaxChecker(DeprecatedSyntaxChecker.getInstance())
.withSimpleDigester(NodeType.OBJECT)
.withIdentityDigester(NodeType.ARRAY, NodeType.OBJECT)
.withValidatorClass(DeprecatedValidator.class)
.freeze();
}

private Keyword createMergeStrategyKeyword() {
return Keyword.newBuilder(OcdsValidatorConstants.CustomSchemaKeywords.MERGE_STRATEGY)
.withSyntaxChecker(MergeStrategySyntaxChecker.getInstance())
.freeze();
}

private Keyword createWholeListMergeKeyword() {
return Keyword.newBuilder(OcdsValidatorConstants.CustomSchemaKeywords.WHOLE_LIST_MERGE)
.withSyntaxChecker(WholeListMergeSyntaxChecker.getInstance())
.freeze();
}

private Keyword createOmitWhenMergedKeyword() {
return Keyword.newBuilder(OcdsValidatorConstants.CustomSchemaKeywords.OMIT_WHEN_MERGED)
.withSyntaxChecker(OmitWhenMergedSyntaxChecker.getInstance())
.freeze();
}


private Keyword createVersionIdKeyword() {
return Keyword.newBuilder(OcdsValidatorConstants.CustomSchemaKeywords.VERSION_ID)
.withSyntaxChecker(VersionIdSyntaxChecker.getInstance())
.freeze();
}


private Keyword openCodelistKeyword() {
return Keyword.newBuilder(OcdsValidatorConstants.CustomSchemaKeywords.OPEN_CODE_LIST)
.withSyntaxChecker(OpenCodelistSyntaxChecker.getInstance())
.freeze();
}

private Keyword codelistKeyword() {
return Keyword.newBuilder(OcdsValidatorConstants.CustomSchemaKeywords.CODE_LIST)
.withSyntaxChecker(CodelistSyntaxChecker.getInstance())
.freeze();
}


/**
* This method initializes the custom json schema factory
*/
Expand All @@ -352,10 +409,22 @@ private void initJsonSchemaFactory() {
* Build a library, based on the v4 library, with this new keyword
*/
final Library library = DraftV4Library.get().thaw()
.addKeyword(createDeprecatedKeyword()).freeze();
.addKeyword(createDeprecatedKeyword())
.addKeyword(createWholeListMergeKeyword())
.addKeyword(createVersionIdKeyword())
.addKeyword(openCodelistKeyword())
.addKeyword(codelistKeyword())
.addKeyword(createOmitWhenMergedKeyword())
.addKeyword(createMergeStrategyKeyword())
.freeze();

validationConfiguration = ValidationConfiguration.newBuilder()
.setDefaultLibrary("https://www.open-contracting.org/data-standard/", library)
.setDefaultLibrary(
"https://raw.githubusercontent"
+ ".com/open-contracting/standard/1.1-dev/standard/schema/metaschema/json-schema"
+ "-draft-4.json",
library
)
.setValidationMessages(createJsonSchemaCustomMessages()).freeze();

}
Expand All @@ -364,8 +433,12 @@ private void initJsonSchemaFactory() {
* These are custom messages used by the validator to report warnings and errors
*/
private MessageBundle createJsonSchemaCustomMessages() {
final String key = "missingDivisors";
final String value = "integer value is not a multiple of all divisors";
final String key = "warn.jocds.deprecatedValidator";
final String value = "This element has been marked deprecated and will soon be removed from the OCDS standard !"
+ " This will be either due to limited use, or because they have been replaced by alternative fields "
+ "or codelists. Before a field or codelist value is removed, "
+ "it will be first marked as deprecated in a major or minor release (e.g. in 1.1), and removal will "
+ "only take place, subject to the governance process, in the next major version (e.g. 2.0).";
final MessageSource source = MapMessageSource.newBuilder()
.put(key, value).build();
final MessageBundle bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
package org.devgateway.jocds;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

/**
* Created by mpostelnicu on 7/7/17.
*/
Expand All @@ -26,8 +28,8 @@ public Jackson2ObjectMapperBuilder objectMapperBuilder() {
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
builder.serializationInclusion(Include.NON_EMPTY).dateFormat(dateFormatGmt);
builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT);
builder.defaultViewInclusion(true);

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2018. Development Gateway and contributors. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for details.
*/

package org.devgateway.jocds.jsonschema.checker;

import com.github.fge.jackson.NodeType;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.keyword.syntax.checkers.AbstractSyntaxChecker;
import com.github.fge.jsonschema.core.keyword.syntax.checkers.SyntaxChecker;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.tree.SchemaTree;
import com.github.fge.msgsimple.bundle.MessageBundle;
import org.devgateway.jocds.OcdsValidatorConstants;

import java.util.Collection;

public final class CodelistSyntaxChecker extends AbstractSyntaxChecker {

private static final SyntaxChecker INSTANCE = new CodelistSyntaxChecker();

public static SyntaxChecker getInstance() {
return INSTANCE;
}

private CodelistSyntaxChecker() {
super(OcdsValidatorConstants.CustomSchemaKeywords.CODE_LIST, NodeType.STRING);
}

@Override
protected void checkValue(final Collection<JsonPointer> pointers,
final MessageBundle bundle, final ProcessingReport report,
final SchemaTree tree)
throws ProcessingException {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.tree.SchemaTree;
import com.github.fge.msgsimple.bundle.MessageBundle;
import java.util.Collection;
import org.devgateway.jocds.OcdsValidatorConstants;

import java.util.Collection;

/**
* This adds functionality for "deprecated" keyword.
* http://standard.open-contracting.org/latest/en/schema/deprecation/
* It will print warning messages when deprecated elements are found
*/
public final class DeprecatedSyntaxChecker extends AbstractSyntaxChecker {

private static final SyntaxChecker INSTANCE = new DeprecatedSyntaxChecker();
Expand All @@ -33,6 +39,6 @@ protected void checkValue(final Collection<JsonPointer> pointers,
final MessageBundle bundle, final ProcessingReport report,
final SchemaTree tree)
throws ProcessingException {
System.out.println(tree);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2018. Development Gateway and contributors. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for details.
*/

package org.devgateway.jocds.jsonschema.checker;

import com.github.fge.jackson.NodeType;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.keyword.syntax.checkers.AbstractSyntaxChecker;
import com.github.fge.jsonschema.core.keyword.syntax.checkers.SyntaxChecker;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.tree.SchemaTree;
import com.github.fge.msgsimple.bundle.MessageBundle;
import org.devgateway.jocds.OcdsValidatorConstants;

import java.util.Collection;

public final class MergeStrategySyntaxChecker extends AbstractSyntaxChecker {

private static final SyntaxChecker INSTANCE = new MergeStrategySyntaxChecker();

public static SyntaxChecker getInstance() {
return INSTANCE;
}

private MergeStrategySyntaxChecker() {
super(OcdsValidatorConstants.CustomSchemaKeywords.MERGE_STRATEGY, NodeType.STRING);
}

@Override
protected void checkValue(final Collection<JsonPointer> pointers,
final MessageBundle bundle, final ProcessingReport report,
final SchemaTree tree)
throws ProcessingException {

}
}
Loading

0 comments on commit d1415cf

Please sign in to comment.