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

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

pending completion
#157

Pull #125

github-actions

web-flow
Merge 4f575b7a7 into 7470716c4
Pull Request #125: 114 Unclear/non-standard response for post api/v1/pit/pid

124 of 124 new or added lines in 11 files covered. (100.0%)

758 of 1377 relevant lines covered (55.05%)

0.55 hits per line

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

22.22
/src/main/java/edu/kit/datamanager/pit/util/TypeValidationUtils.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package edu.kit.datamanager.pit.util;
7

8
import edu.kit.datamanager.pit.common.RecordValidationException;
9
import edu.kit.datamanager.pit.domain.PIDRecord;
10
import edu.kit.datamanager.pit.domain.TypeDefinition;
11

12
import java.util.Collection;
13

14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16

17
/**
18
 * Utility class with static functions to validate PID records.
19
 * 
20
 * @author Thomas Jejkal
21
 */
22
public class TypeValidationUtils {
23

24
    private static final Logger LOG = LoggerFactory.getLogger(TypeValidationUtils.class);
1✔
25

26
    private TypeValidationUtils() {}
27

28
    /**
29
     * Check if all mandatory attributes are present. If not, throw an exception.
30
     * 
31
     * @param pidRecord the record to check for
32
     * @param profile   the profile to check against
33
     * @throws RecordValidationException if at least one attribute is missing. It
34
     *                                   shows all missing attributes in its error
35
     *                                   message.
36
     */
37
    public static void checkMandatoryAttributes(PIDRecord pidRecord, TypeDefinition profile)
38
            throws RecordValidationException {
39
        Collection<String> missing = pidRecord.getMissingMandatoryTypesOf(profile);
1✔
40
        if (!missing.isEmpty()) {
1✔
41
            throw new RecordValidationException(
1✔
42
                    pidRecord.getPid(),
1✔
43
                    "Missing mandatory types: " + missing);
44
        }
45
    }
1✔
46

47
    /**
48
     * Validates a PID record against a given profile.
49
     * 
50
     * - All mandatory properties of the profile must be available in the PID
51
     * record.
52
     * - All properties of the record must be successfully validated according to
53
     * the profile subtypes (properties).
54
     * 
55
     * @param pidRecord the record to validate.
56
     * @param profile   the profile to validate against, defining the rules for the
57
     *                  record.
58
     * @return true if all validations were successful, false otherwise.
59
     */
60
    public static boolean isValid(PIDRecord pidRecord, TypeDefinition profile) {
61
        LOG.trace("Validating PID record against type definition.");
×
62
        if (!pidRecord.getMissingMandatoryTypesOf(profile).isEmpty()) {
×
63
            LOG.warn("PID record does not contain all required elements of type definition.");
×
64
            // invalid according to type
65
            return false;
×
66
        }
67
        for (String recordKey : pidRecord.getPropertyIdentifiers()) {
×
68
            LOG.trace("Checking PID record key {}.", recordKey);
×
69
            TypeDefinition type = profile.getSubTypes().get(recordKey);
×
70
            if (type == null) {
×
71
                LOG.error("No sub-type found for key {}.", recordKey);
×
72
                return false;
×
73
            }
74

75
            String[] values = pidRecord.getPropertyValues(recordKey);
×
76
            for (String value : values) {
×
77
                if (value == null) {
×
78
                    LOG.error("'null' record value found for key {}.", recordKey);
×
79
                    return false;
×
80
                }
81

82
                if (!type.validate(value)) {
×
83
                    LOG.error("Validation of value {} against type {} failed.", value, type.getIdentifier());
×
84
                    return false;
×
85
                }
86
            }
87
        }
×
88
        LOG.trace("PID record is matching the provided type definition.");
×
89
        return true;
×
90
    }
91
}
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