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

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

08 Nov 2024 06:34PM UTC coverage: 71.805% (-0.5%) from 72.332%
#421

Pull #218

github

web-flow
Merge 0d8ce406f into cfdf84673
Pull Request #218: Validation speedup experiments

101 of 136 new or added lines in 4 files covered. (74.26%)

3 existing lines in 2 files now uncovered.

871 of 1213 relevant lines covered (71.81%)

0.72 hits per line

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

31.17
/src/main/java/edu/kit/datamanager/pit/pitservice/impl/TypingService.java
1
package edu.kit.datamanager.pit.pitservice.impl;
2

3
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
4
import edu.kit.datamanager.pit.common.InvalidConfigException;
5
import edu.kit.datamanager.pit.common.PidAlreadyExistsException;
6
import edu.kit.datamanager.pit.common.PidNotFoundException;
7
import edu.kit.datamanager.pit.common.RecordValidationException;
8

9
import java.io.IOException;
10
import java.util.Collection;
11
import java.util.Optional;
12

13
import edu.kit.datamanager.pit.pidsystem.IIdentifierSystem;
14
import edu.kit.datamanager.pit.typeregistry.ITypeRegistry;
15
import edu.kit.datamanager.pit.pitservice.ITypingService;
16
import edu.kit.datamanager.pit.pitservice.IValidationStrategy;
17
import edu.kit.datamanager.pit.common.ExternalServiceException;
18
import edu.kit.datamanager.pit.domain.Operations;
19
import edu.kit.datamanager.pit.domain.PIDRecord;
20
import edu.kit.datamanager.pit.domain.TypeDefinition;
21

22
import java.util.concurrent.CompletableFuture;
23
import java.util.concurrent.ExecutionException;
24

25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27
import org.springframework.beans.factory.annotation.Autowired;
28

29
/**
30
 * Core implementation class that offers the combined higher-level services
31
 * through a type registry and an identifier system.
32
 *
33
 */
34
public class TypingService implements ITypingService {
35

36
    private static final Logger LOG = LoggerFactory.getLogger(TypingService.class);
1✔
37
    private static final String LOG_MSG_TYPING_SERVICE_MISCONFIGURED = "Typing service misconfigured.";
38
    private static final String LOG_MSG_QUERY_TYPE = "Querying for type with identifier {}.";
39

40

41
    protected final AsyncLoadingCache<String, TypeDefinition> typeCache;
42
    protected final IIdentifierSystem identifierSystem;
43
    protected final ITypeRegistry typeRegistry;
44

45
    /**
46
     * A validation strategy. Will never be null.
47
     * 
48
     * ApplicationProperties::defaultValidationStrategy there is always either a
49
     * default strategy or a noop strategy assigned. Therefore, autowiring will
50
     * always work. Assigning null is done to avoid warnings on constructor.
51
     */
52
    @Autowired
1✔
53
    protected IValidationStrategy defaultStrategy = null;
54

55
    public TypingService(IIdentifierSystem identifierSystem, ITypeRegistry typeRegistry,
56
            AsyncLoadingCache<String, TypeDefinition> typeCache) {
57
        super();
1✔
58
        this.identifierSystem = identifierSystem;
1✔
59
        this.typeRegistry = typeRegistry;
1✔
60
        this.typeCache = typeCache;
1✔
61
    }
1✔
62

63
    @Override
64
    public Optional<String> getPrefix() {
65
        return this.identifierSystem.getPrefix();
1✔
66
    }
67

68
    @Override
69
    public void setValidationStrategy(IValidationStrategy strategy) {
70
        this.defaultStrategy = strategy;
1✔
71
    }
1✔
72

73
    @Override
74
    public void validate(PIDRecord pidRecord)
75
            throws RecordValidationException, ExternalServiceException {
76
        this.defaultStrategy.validate(pidRecord);
1✔
77
    }
1✔
78

79
    @Override
80
    public boolean isIdentifierRegistered(String pid) throws ExternalServiceException {
81
        LOG.trace("Performing isIdentifierRegistered({}).", pid);
1✔
82
        return identifierSystem.isIdentifierRegistered(pid);
1✔
83
    }
84

85
    @Override
86
    public String queryProperty(String pid, TypeDefinition typeDefinition) throws PidNotFoundException, ExternalServiceException {
87
        LOG.trace("Performing queryProperty({}, TypeDefinition#{}).", pid, typeDefinition.getIdentifier());
×
88
        return identifierSystem.queryProperty(pid, typeDefinition);
×
89
    }
90

91
    @Override
92
    public String registerPidUnchecked(final PIDRecord pidRecord) throws PidAlreadyExistsException, ExternalServiceException {
93
        LOG.trace("Performing registerPID({}).", pidRecord);
1✔
94
        return identifierSystem.registerPidUnchecked(pidRecord);
1✔
95
    }
96

97
    @Override
98
    public PIDRecord queryByType(String pid, TypeDefinition typeDefinition) throws PidNotFoundException, ExternalServiceException {
99
        LOG.trace("Performing queryByType({}, TypeDefinition#{}).", pid, typeDefinition.getIdentifier());
×
100
        return identifierSystem.queryByType(pid, typeDefinition);
×
101
    }
102

103
    @Override
104
    public boolean deletePID(String pid) throws ExternalServiceException {
105
        LOG.trace("Performing deletePID({}).", pid);
×
106
        return identifierSystem.deletePID(pid);
×
107
    }
108

109
    @Override
110
    public CompletableFuture<TypeDefinition> describeType(String typeIdentifier) throws IOException {
111
        LOG.trace("Performing describeType({}).", typeIdentifier);
×
112
        try {
113
            LOG.trace(LOG_MSG_QUERY_TYPE, typeIdentifier);
×
114
            return typeCache.get(typeIdentifier);
×
NEW
115
        } catch (RuntimeException ex) {
×
116
            LOG.error("Failed to query for type with identifier " + typeIdentifier + ".", ex);
×
117
            throw new InvalidConfigException(LOG_MSG_TYPING_SERVICE_MISCONFIGURED);
×
118
        }
119
    }
120

121
    @Override
122
    public PIDRecord queryAllProperties(String pid) throws PidNotFoundException, ExternalServiceException {
123
        LOG.trace("Performing queryAllProperties({}).", pid);
1✔
124
        PIDRecord pidRecord = identifierSystem.queryAllProperties(pid);
1✔
125
        if (pidRecord == null) {
1✔
126
            throw new PidNotFoundException(pid);
1✔
127
        }
128
        // ensure the PID is always contained
129
        pidRecord.setPid(pid);
1✔
130
        return pidRecord;
1✔
131
    }
132

133
    @Override
134
    public PIDRecord queryAllProperties(String pid, boolean includePropertyNames)
135
            throws IOException {
136
        LOG.trace("Performing queryAllProperties({}, {}).", pid, includePropertyNames);
×
137
        PIDRecord pidInfo = identifierSystem.queryAllProperties(pid);
×
138
        LOG.trace("PID record found. {}", (includePropertyNames) ? "Adding property names." : "Returning result.");
×
139

140
        if (includePropertyNames) {
×
141
            enrichPIDInformationRecord(pidInfo);
×
142
        }
143
        return pidInfo;
×
144
    }
145

146
    @Override
147
    public PIDRecord queryProperty(String pid, String propertyIdentifier) throws IOException {
148
        LOG.trace("Performing queryProperty({}, {}).", pid, propertyIdentifier);
×
149
        PIDRecord pidInfo = new PIDRecord();
×
150
        // query type registry
151
        TypeDefinition typeDef;
152
        try {
153
            LOG.trace(LOG_MSG_QUERY_TYPE, propertyIdentifier);
×
NEW
154
            typeDef = typeCache.get(propertyIdentifier).get();
×
NEW
155
        } catch (ExecutionException | InterruptedException ex) {
×
UNCOV
156
            LOG.error(LOG_MSG_QUERY_TYPE, propertyIdentifier);
×
UNCOV
157
            throw new InvalidConfigException(LOG_MSG_TYPING_SERVICE_MISCONFIGURED);
×
158
        }
×
159

160
        if (typeDef != null) {
×
161
            pidInfo.addEntry(propertyIdentifier, typeDef.getName(), identifierSystem.queryProperty(pid, typeDef));
×
162
            return pidInfo;
×
163
        }
164
        return null;
×
165
    }
166

167
    private void enrichPIDInformationRecord(PIDRecord pidInfo) {
168
        // enrich record by querying type registry for all property definitions
169
        // to get the property names
170
        for (String typeIdentifier : pidInfo.getPropertyIdentifiers()) {
×
171
            TypeDefinition typeDef;
172
            try {
NEW
173
                typeDef = typeCache.get(typeIdentifier).get();
×
NEW
174
            } catch (ExecutionException | InterruptedException ex) {
×
175
                throw new InvalidConfigException(LOG_MSG_TYPING_SERVICE_MISCONFIGURED);
×
176
            }
×
177

178
            if (typeDef != null) {
×
179
                pidInfo.setPropertyName(typeIdentifier, typeDef.getName());
×
180
            } else {
181
                pidInfo.setPropertyName(typeIdentifier, typeIdentifier);
×
182
            }
183
        }
×
184
    }
×
185

186
    @Override
187
    public PIDRecord queryByType(String pid, String typeIdentifier, boolean includePropertyNames)
188
            throws IOException {
189
        TypeDefinition typeDef;
190
        try {
NEW
191
            typeDef = typeCache.get(typeIdentifier).get();
×
NEW
192
        } catch (ExecutionException | InterruptedException ex) {
×
193
            throw new InvalidConfigException(LOG_MSG_TYPING_SERVICE_MISCONFIGURED);
×
194
        }
×
195

196
        if (typeDef == null) {
×
197
            return null;
×
198
        }
199
        // now query PID record and fill in information based on property keys in type definition
200
        PIDRecord result = identifierSystem.queryByType(pid, typeDef);
×
201
        if (includePropertyNames) {
×
202
            enrichPIDInformationRecord(result);
×
203
        }
204
        return result;
×
205
    }
206

207
    public ITypeRegistry getTypeRegistry() {
208
        return typeRegistry;
×
209
    }
210

211
    public IIdentifierSystem getIdentifierSystem() {
212
        return identifierSystem;
×
213
    }
214

215
    @Override
216
    public boolean updatePID(PIDRecord pidRecord) throws PidNotFoundException, ExternalServiceException, RecordValidationException {
217
        return this.identifierSystem.updatePID(pidRecord);
1✔
218
    }
219

220
    @Override
221
    public Collection<String> resolveAllPidsOfPrefix() throws ExternalServiceException, InvalidConfigException {
222
        return this.identifierSystem.resolveAllPidsOfPrefix();
×
223
    }
224

225
    public Operations getOperations()  {
226
        return new Operations(this);
1✔
227
    }
228

229
}
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