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

kit-data-manager / pit-service / #576

30 Sep 2025 10:09AM UTC coverage: 78.134% (+5.4%) from 72.712%
#576

Pull #264

github

web-flow
Merge 269776c6c into ef6582172
Pull Request #264: Development branch for v3.0.0

661 of 796 new or added lines in 25 files covered. (83.04%)

3 existing lines in 2 files now uncovered.

1072 of 1372 relevant lines covered (78.13%)

0.78 hits per line

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

88.0
/src/main/java/edu/kit/datamanager/pit/typeregistry/AttributeInfo.java
1
package edu.kit.datamanager.pit.typeregistry;
2

3
import com.fasterxml.jackson.core.JsonProcessingException;
4
import com.fasterxml.jackson.databind.JsonNode;
5
import com.fasterxml.jackson.databind.node.TextNode;
6
import com.networknt.schema.JsonSchema;
7
import com.networknt.schema.ValidationMessage;
8
import edu.kit.datamanager.pit.Application;
9
import edu.kit.datamanager.pit.typeregistry.schema.SchemaInfo;
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12

13
import java.util.Collection;
14
import java.util.Objects;
15
import java.util.Set;
16

17
/**
18
 * @param pid the pid of this attribute
19
 * @param name a human-readable name, defined in the DTR for this type.
20
 *            Note this is usually different from the name in a specific profile!
21
 * @param typeName name of the schema type of this attribute in the DTR,
22
 *                e.g. "Profile", "InfoType", "Special-Info-Type", ...
23
 * @param jsonSchema the json schema to validate a value of this attribute
24
 */
25
public record AttributeInfo(
1✔
26
        String pid,
27
        String name,
28
        String typeName,
29
        Collection<SchemaInfo> jsonSchema
30
) {
31
    private static final Logger log = LoggerFactory.getLogger(AttributeInfo.class);
1✔
32

33
    public boolean validate(String value) {
34
        return this.jsonSchema().stream()
1✔
35
                .filter(schemaInfo -> schemaInfo.error() == null)
1✔
36
                .filter(schemaInfo -> schemaInfo.schema() != null)
1✔
37
                .peek(schemaInfo -> log.warn("Found valid schema from {} to validate {} / {}.", schemaInfo.origin(), pid, value))
1✔
38
                .anyMatch(schemaInfo -> this.validate(schemaInfo.schema(), value));
1✔
39
    }
40

41
    private boolean validate(JsonSchema schema, String value) {
42
        try {
43
            JsonNode toValidate = valueToJsonNode(value);
1✔
44
            Set<ValidationMessage> errors = schema.validate(toValidate, executionContext -> {
1✔
45
                // By default, since Draft 2019-09, the format keyword only generates annotations and not assertions
46
                executionContext.getExecutionConfig().setFormatAssertionsEnabled(true);
1✔
47
            });
1✔
48
            if (!errors.isEmpty()) {
1✔
49
                log.warn("Validation errors for value '{}': {}", value, errors);
1✔
50
            }
51
            return errors.isEmpty();
1✔
NEW
52
        } catch (Exception e) {
×
NEW
53
            log.error("Exception during validation for value '{}': {}", value, e.getMessage(), e);
×
NEW
54
            return false;
×
55
        }
56
    }
57

58
    /**
59
     * Converts the given value to a JsonNode.
60
     *
61
     * @param value the value to convert
62
     * @return a JsonNode representation of the value
63
     */
64
    public static JsonNode valueToJsonNode(String value) {
65
        JsonNode toValidate;
66
        if (value.isBlank()) {
1✔
67
            return new TextNode(value);
1✔
68
        }
69
        try {
70
            toValidate = Application.jsonObjectMapper().readTree(value);
1✔
71
        } catch (JsonProcessingException e) {
1✔
72
            log.warn("Failed to parse value '{}' as JSON, treating it as a plain text node.", value);
1✔
73
            toValidate = new TextNode(value);
1✔
74
        }
1✔
75
        return toValidate;
1✔
76
    }
77
}
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