• 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

55.36
/src/main/java/edu/kit/datamanager/pit/pitservice/impl/EmbeddedStrictValidatorStrategy.java
1
package edu.kit.datamanager.pit.pitservice.impl;
2

3
import edu.kit.datamanager.pit.common.ExternalServiceException;
4
import edu.kit.datamanager.pit.common.RecordValidationException;
5
import edu.kit.datamanager.pit.configuration.ApplicationProperties;
6
import edu.kit.datamanager.pit.domain.PIDRecord;
7
import edu.kit.datamanager.pit.domain.TypeDefinition;
8
import edu.kit.datamanager.pit.pitservice.IValidationStrategy;
9
import edu.kit.datamanager.pit.util.TypeValidationUtils;
10

11
import java.util.concurrent.ExecutionException;
12

13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
import org.springframework.beans.factory.annotation.Autowired;
16

17
import com.google.common.cache.LoadingCache;
18

19
public class EmbeddedStrictValidatorStrategy implements IValidationStrategy {
1✔
20

21
    private static final Logger LOG = LoggerFactory.getLogger(EmbeddedStrictValidatorStrategy.class);
1✔
22

23
    @Autowired
24
    public LoadingCache<String, TypeDefinition> typeLoader;
25

26
    @Autowired
27
    ApplicationProperties applicationProps;
28

29
    @Override
30
    public void validate(PIDRecord pidRecord) throws RecordValidationException, ExternalServiceException {
31
        String profileKey = applicationProps.getProfileKey();
1✔
32
        if (!pidRecord.hasProperty(profileKey)) {
1✔
33
            throw new RecordValidationException(
1✔
34
                    pidRecord.getPid(),
1✔
35
                    "Profile attribute not found. Expected key: " + profileKey);
36
        }
37

38
        String[] profilePIDs = pidRecord.getPropertyValues(profileKey);
1✔
39
        boolean hasProfile = profilePIDs.length > 0;
1✔
40
        if (!hasProfile) {
1✔
41
            throw new RecordValidationException(
×
42
                    pidRecord.getPid(),
×
43
                    "Profile attribute " + profileKey + " has no values.");
44
        }
45

46
        for (String profilePID : profilePIDs) {
1✔
47
            TypeDefinition profileDefinition;
48
            try {
49
                profileDefinition = this.typeLoader.get(profilePID);
1✔
50
            } catch (ExecutionException e) {
×
51
                LOG.error("Could not resolve identifier {}.", profilePID);
×
52
                throw new ExternalServiceException(
×
53
                        applicationProps.getTypeRegistryUri().toString());
×
54
            }
1✔
55
            if (profileDefinition == null) {
1✔
56
                LOG.error("No type definition found for identifier {}.", profilePID);
×
57
                throw new RecordValidationException(
×
58
                        pidRecord.getPid(),
×
59
                        String.format("No type found for identifier %s.", profilePID));
×
60
            }
61

62
            LOG.debug("validating profile {}", profilePID);
1✔
63
            this.strictProfileValidation(pidRecord, profileDefinition);
1✔
64
            LOG.debug("successfully validated {}", profilePID);
1✔
65
        }
66
    }
1✔
67

68
    /**
69
     * Exceptions indicate failure. No Exceptions mean success.
70
     * 
71
     * @param pidRecord the PID record to validate.
72
     * @param profile   the profile to validate against.
73
     * @throws RecordValidationException with error message on validation errors.
74
     */
75
    private void strictProfileValidation(PIDRecord pidRecord, TypeDefinition profile) throws RecordValidationException {
76
        // if (profile.hasSchema()) {
77
        // TODO issue https://github.com/kit-data-manager/pit-service/issues/104
78
        // validate using schema and you are done (strict validation)
79
        // String jsonRecord = ""; // TODO format depends on schema source
80
        // return profile.validate(jsonRecord);
81
        // }
82

83
        LOG.trace("Validating PID record against type definition.");
1✔
84

85
        TypeValidationUtils.checkMandatoryAttributes(pidRecord, profile);
1✔
86

87
        for (String attributeKey : pidRecord.getPropertyIdentifiers()) {
1✔
88
            LOG.trace("Checking PID record key {}.", attributeKey);
1✔
89

90
            TypeDefinition type = profile.getSubTypes().get(attributeKey);
1✔
91
            if (type == null) {
1✔
92
                LOG.error("No sub-type found for key {}.", attributeKey);
×
93
                // TODO try to resolve it (for later when we support "allow additional
94
                // attributes")
95
                // if profile.allowsAdditionalAttributes() {...} else
96
                throw new RecordValidationException(
×
97
                        pidRecord.getPid(),
×
98
                        String.format("Attribute %s is not allowed in profile %s",
×
99
                                attributeKey,
100
                                profile.getIdentifier()));
×
101
            }
102

103
            validateValuesForKey(pidRecord, attributeKey, type);
1✔
104
        }
1✔
105
    }
1✔
106

107
    private void validateValuesForKey(PIDRecord pidRecord, String attributeKey, TypeDefinition type)
108
            throws RecordValidationException {
109
        String[] values = pidRecord.getPropertyValues(attributeKey);
1✔
110
        for (String value : values) {
1✔
111
            if (value == null) {
1✔
112
                LOG.error("'null' record value found for key {}.", attributeKey);
×
113
                throw new RecordValidationException(
×
114
                        pidRecord.getPid(),
×
115
                        String.format("Validation of value %s against type %s failed.",
×
116
                                value,
117
                                type.getIdentifier()));
×
118
            }
119

120
            if (!type.validate(value)) {
1✔
121
                LOG.error("Validation of value {} against type {} failed.", value, type.getIdentifier());
×
122
                throw new RecordValidationException(
×
123
                        pidRecord.getPid(),
×
124
                        String.format("Validation of value %s against type %s failed.",
×
125
                                value,
126
                                type.getIdentifier()));
×
127
            }
128
        }
129
    }
1✔
130
}
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