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

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

13 Nov 2023 05:22PM UTC coverage: 72.606% (+11.8%) from 60.79%
#324

push

github

web-flow
Merge pull request #174 from kit-data-manager/dev-v2

Development branch for v2.0.0

136 of 205 new or added lines in 14 files covered. (66.34%)

4 existing lines in 3 files now uncovered.

872 of 1201 relevant lines covered (72.61%)

0.73 hits per line

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

60.0
/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
/**
20
 * Validates a PID record using embedded profile(s).
21
 * 
22
 * - checks if all mandatory attributes are present
23
 * - validates all available attributes
24
 * - fails if an attribute is not defined within the profile
25
 */
26
public class EmbeddedStrictValidatorStrategy implements IValidationStrategy {
1✔
27

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

30
    @Autowired
31
    public LoadingCache<String, TypeDefinition> typeLoader;
32

33
    @Autowired
34
    ApplicationProperties applicationProps;
35

36
    @Override
37
    public void validate(PIDRecord pidRecord) throws RecordValidationException, ExternalServiceException {
38
        String profileKey = applicationProps.getProfileKey();
1✔
39
        if (!pidRecord.hasProperty(profileKey)) {
1✔
40
            throw new RecordValidationException(
1✔
41
                    pidRecord,
42
                    "Profile attribute not found. Expected key: " + profileKey);
43
        }
44

45
        String[] profilePIDs = pidRecord.getPropertyValues(profileKey);
1✔
46
        boolean hasProfile = profilePIDs.length > 0;
1✔
47
        if (!hasProfile) {
1✔
NEW
48
            throw new RecordValidationException(
×
49
                    pidRecord,
50
                    "Profile attribute " + profileKey + " has no values.");
51
        }
52

53
        for (String profilePID : profilePIDs) {
1✔
54
            TypeDefinition profileDefinition;
55
            try {
56
                profileDefinition = this.typeLoader.get(profilePID);
1✔
NEW
57
            } catch (ExecutionException e) {
×
NEW
58
                LOG.error("Could not resolve identifier {}.", profilePID);
×
NEW
59
                throw new ExternalServiceException(
×
NEW
60
                        applicationProps.getTypeRegistryUri().toString());
×
61
            }
1✔
62
            if (profileDefinition == null) {
1✔
NEW
63
                LOG.error("No type definition found for identifier {}.", profilePID);
×
NEW
64
                throw new RecordValidationException(
×
65
                        pidRecord,
NEW
66
                        String.format("No type found for identifier %s.", profilePID));
×
67
            }
68

69
            LOG.debug("validating profile {}", profilePID);
1✔
70
            this.strictProfileValidation(pidRecord, profileDefinition);
1✔
71
            LOG.debug("successfully validated {}", profilePID);
1✔
72
        }
73
    }
1✔
74

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

90
        LOG.trace("Validating PID record against type definition.");
1✔
91

92
        TypeValidationUtils.checkMandatoryAttributes(pidRecord, profile);
1✔
93

94
        for (String attributeKey : pidRecord.getPropertyIdentifiers()) {
1✔
95
            LOG.trace("Checking PID record key {}.", attributeKey);
1✔
96

97
            TypeDefinition type = profile.getSubTypes().get(attributeKey);
1✔
98
            if (type == null) {
1✔
NEW
99
                LOG.error("No sub-type found for key {}.", attributeKey);
×
100
                // TODO try to resolve it (for later when we support "allow additional
101
                // attributes")
102
                // if profile.allowsAdditionalAttributes() {...} else
NEW
103
                throw new RecordValidationException(
×
104
                        pidRecord,
NEW
105
                        String.format("Attribute %s is not allowed in profile %s",
×
106
                                attributeKey,
NEW
107
                                profile.getIdentifier()));
×
108
            }
109

110
            validateValuesForKey(pidRecord, attributeKey, type);
1✔
111
        }
1✔
112
    }
1✔
113

114
    /**
115
     * Validates all values of an attribute against a given type definition.
116
     * 
117
     * @param pidRecord the record containing the attribute and value.
118
     * @param attributeKey the attribute to check the values for.
119
     * @param type the type definition to check against.
120
     * @throws RecordValidationException on error.
121
     */
122
    private void validateValuesForKey(PIDRecord pidRecord, String attributeKey, TypeDefinition type)
123
            throws RecordValidationException {
124
        String[] values = pidRecord.getPropertyValues(attributeKey);
1✔
125
        for (String value : values) {
1✔
126
            if (value == null) {
1✔
NEW
127
                LOG.error("'null' record value found for key {}.", attributeKey);
×
NEW
128
                throw new RecordValidationException(
×
129
                        pidRecord,
NEW
130
                        String.format("Validation of value %s against type %s failed.",
×
131
                                value,
NEW
132
                                type.getIdentifier()));
×
133
            }
134

135
            if (!type.validate(value)) {
1✔
NEW
136
                LOG.error("Validation of value {} against type {} failed.", value, type.getIdentifier());
×
NEW
137
                throw new RecordValidationException(
×
138
                        pidRecord,
NEW
139
                        String.format("Validation of value %s against type %s failed.",
×
140
                                value,
NEW
141
                                type.getIdentifier()));
×
142
            }
143
        }
144
    }
1✔
145
}
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