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

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

28 Jan 2025 02:40PM UTC coverage: 72.318% (-0.08%) from 72.4%
#471

push

github

web-flow
Merge pull request #269 from kit-data-manager/enable-actuators

Permit configuration of actuators

883 of 1221 relevant lines covered (72.32%)

0.72 hits per line

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

69.77
/src/main/java/edu/kit/datamanager/pit/domain/TypeDefinition.java
1
/* SPDX-License-Identifier: Apache-2.0 */
2

3
package edu.kit.datamanager.pit.domain;
4

5
import com.fasterxml.jackson.annotation.JsonIgnore;
6
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7
import com.fasterxml.jackson.annotation.JsonProperty;
8
import java.util.HashMap;
9
import java.util.HashSet;
10
import java.util.Map;
11
import java.util.Map.Entry;
12
import java.util.Set;
13
import lombok.Data;
14
import org.everit.json.schema.Schema;
15
import org.everit.json.schema.ValidationException;
16
import org.everit.json.schema.loader.SchemaLoader;
17
import org.json.JSONObject;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20

21
/**
22
 * Representation of a type or profile definition in a data type registry.
23
 * 
24
 * @author Thomas Jejkal
25
 */
26
@Data
1✔
27
@JsonIgnoreProperties(ignoreUnknown = true)
28
public class TypeDefinition {
29

30
    private static final Logger LOG = LoggerFactory.getLogger(TypeDefinition.class);
1✔
31

32
    private String name;
×
33
    private String identifier;
1✔
34
    private String description;
×
35
    private boolean optional = false;
1✔
36
    private boolean repeatable = false;
1✔
37
    private String expression;
×
38
    private String value;
×
39
    private Schema jsonSchema;
×
40

41
    private ProvenanceInformation provenance;
×
42
    @JsonProperty("properties")
1✔
43
    private Map<String, TypeDefinition> subTypes = new HashMap<>();
1✔
44

45
    @JsonIgnore
46
    private TypeDefinition resolvedTypeDefinition;
×
47

48
    @JsonIgnore
49
    public Set<String> getAllProperties() {
50
        Set<String> props = new HashSet<>();
1✔
51
        Set<Entry<String, TypeDefinition>> entries = subTypes.entrySet();
1✔
52
        entries.forEach(entry -> props.add(entry.getKey()));
1✔
53

54
        return props;
1✔
55
    }
56

57
    public void setSchema(String schema) {
58
        if (schema == null) {
1✔
59
            return;
×
60
        }
61

62
        JSONObject jsonSchema = new JSONObject(schema);
1✔
63
        this.jsonSchema = SchemaLoader.load(jsonSchema);
1✔
64
    }
1✔
65

66
    /**
67
     * Takes a value and validates it using this types JSON schema.
68
     * 
69
     * @param document the value, usually taken from a PID record to be validated.
70
     * @return true if the given value is valid accodting to this type.
71
     */
72
    public boolean validate(String document) {
73
        LOG.trace("Performing validate({}).", document);
1✔
74
        if (jsonSchema != null) {
1✔
75
            LOG.trace("Using schema-based validation.");
1✔
76
            Object toValidate = document;
1✔
77
            if (document.startsWith("{")) {
1✔
78
                LOG.trace("Creating JSON object from provided value.");
1✔
79
                toValidate = new JSONObject(document);
1✔
80
            }
81
            try {
82
                LOG.trace("Validating provided value using type schema.");
1✔
83
                jsonSchema.validate(toValidate);
1✔
84
                LOG.trace("Validation successful.");
1✔
85
            } catch (ValidationException ex) {
×
86
                LOG.error("Validation failed.", ex);
×
87
                return false;
×
88
            }
1✔
89
        } else {
1✔
90
            LOG.trace("No schema available. Skipping validation.");
×
91
        }
92

93
        return true;
1✔
94
    }
95

96
    public boolean isOptional(String property) {
97
        return subTypes.get(property).isOptional();
×
98
    }
99

100
    public void addSubType(TypeDefinition subType) {
101
        subTypes.put(subType.getIdentifier(), subType);
1✔
102
    }
1✔
103
}
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

© 2025 Coveralls, Inc