• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / ro-crate-java / #419

05 May 2025 08:11AM UTC coverage: 90.169% (+0.8%) from 89.357%
#419

Pull #255

github

web-flow
Merge pull request #256 from kit-data-manager/make-spec-examples-executable

Make specification examples (from readme) executable
Pull Request #255: Next Version (v2.1.0-rc2 | v2.1.0)

63 of 70 new or added lines in 16 files covered. (90.0%)

14 existing lines in 5 files now uncovered.

1917 of 2126 relevant lines covered (90.17%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.29
/src/main/java/edu/kit/datamanager/ro_crate/entities/validation/JsonSchemaValidation.java
1
package edu.kit.datamanager.ro_crate.entities.validation;
2

3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.JsonNode;
5
import com.fasterxml.jackson.databind.ObjectMapper;
6
import com.networknt.schema.JsonSchema;
7
import com.networknt.schema.JsonSchemaFactory;
8
import com.networknt.schema.SpecVersion;
9
import com.networknt.schema.ValidationMessage;
10

11
import edu.kit.datamanager.ro_crate.objectmapper.MyObjectMapper;
12

13
import java.net.URISyntaxException;
14
import java.net.URL;
15
import java.util.Objects;
16
import java.util.Set;
17

18
/**
19
 * Implementation of the entity validation strategy that uses json schema.
20
 */
21
public class JsonSchemaValidation implements EntityValidationStrategy {
22

23
  private static final URL entitySchemaDefault
1✔
24
      = Objects.requireNonNull(JsonSchemaValidation.class.getClassLoader()
1✔
25
      .getResource("json_schemas/entity_schema.json"));
1✔
26
  private static final URL fieldSchemaDefault
1✔
27
      = Objects.requireNonNull(JsonSchemaValidation.class.getClassLoader()
1✔
28
      .getResource("json_schemas/entity_field_structure_schema.json"));
1✔
29

30
  private JsonSchema entitySchema;
31
  private JsonSchema entityFieldSchema;
32

33
  /**
34
   *  Default constructor that uses the default schemas.
35
   */
36
  public JsonSchemaValidation() {
1✔
37
    JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
1✔
38
    try {
39
      this.entitySchema = factory.getSchema(entitySchemaDefault.toURI());
1✔
40
      this.entityFieldSchema = factory.getSchema(fieldSchemaDefault.toURI());
1✔
41
    } catch (URISyntaxException e) {
×
42
      e.printStackTrace();
×
43
    }
1✔
44
  }
1✔
45

46
  /**
47
   * Constructor that uses custom schemas present as Json files.
48
   *
49
   * @param entitySchema schema for the entities validation.
50
   * @param fieldSchema schema for the field validation.
51
   */
52
  public JsonSchemaValidation(JsonNode entitySchema, JsonNode fieldSchema) {
×
53
    JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
×
54
    this.entitySchema = factory.getSchema(entitySchema);
×
55
    this.entityFieldSchema = factory.getSchema(fieldSchema);
×
56
  }
×
57

58
  @Override
59
  public boolean validateEntity(JsonNode entity) {
60
    Set<ValidationMessage> errors = this.entitySchema.validate(entity);
1✔
61
    if (errors.size() != 0) {
1✔
62
      System.err.println("This entity does not comply to the basic RO-Crate entity structure.");
1✔
63
      return false;
1✔
64
    }
65
    return true;
1✔
66
  }
67

68
  @Override
69
  public boolean validateFieldOfEntity(JsonNode field) {
70
    Set<ValidationMessage> errors = this.entityFieldSchema.validate(field);
1✔
71
    if (!errors.isEmpty()) {
1✔
72
      ObjectMapper objectMapper = MyObjectMapper.getMapper();
1✔
73
      System.err.println("The property: ");
1✔
74
      try {
75
        System.err.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(field));
1✔
UNCOV
76
      } catch (JsonProcessingException e) {
×
77
        e.printStackTrace();
×
78
      }
1✔
79
      System.err.println("does not comply with the flattened structure"
1✔
80
          + " of the RO-Crate json document.");
81
      return false;
1✔
82
    }
83
    return true;
1✔
84
  }
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc