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

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

30 Sep 2025 10:09AM UTC coverage: 78.134% (+5.4%) from 72.712%
#576

Pull #264

github

web-flow
Merge 269776c6c into ef6582172
Pull Request #264: Development branch for v3.0.0

661 of 796 new or added lines in 25 files covered. (83.04%)

3 existing lines in 2 files now uncovered.

1072 of 1372 relevant lines covered (78.13%)

0.78 hits per line

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

70.0
/src/main/java/edu/kit/datamanager/pit/typeregistry/schema/TypeApiSchemaGenerator.java
1
package edu.kit.datamanager.pit.typeregistry.schema;
2

3
import com.fasterxml.jackson.databind.JsonNode;
4
import com.networknt.schema.JsonSchema;
5
import com.networknt.schema.JsonSchemaFactory;
6
import com.networknt.schema.SpecVersion;
7
import edu.kit.datamanager.pit.Application;
8
import edu.kit.datamanager.pit.common.ExternalServiceException;
9
import edu.kit.datamanager.pit.common.InvalidConfigException;
10
import edu.kit.datamanager.pit.common.TypeNotFoundException;
11
import edu.kit.datamanager.pit.configuration.ApplicationProperties;
12
import jakarta.validation.constraints.NotNull;
13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
import org.springframework.http.HttpStatusCode;
16
import org.springframework.http.client.ClientHttpResponse;
17
import org.springframework.web.client.RestClient;
18

19
import java.io.IOException;
20
import java.io.InputStream;
21
import java.net.URISyntaxException;
22
import java.net.URL;
23

24
public class TypeApiSchemaGenerator implements SchemaGenerator {
25
    private static final Logger LOG = LoggerFactory.getLogger(TypeApiSchemaGenerator.class);
1✔
26

27
    protected final URL baseUrl;
28
    protected final RestClient http;
29
    JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
1✔
30

31
    public TypeApiSchemaGenerator(@NotNull ApplicationProperties props) {
1✔
32
        this.baseUrl = props.getTypeRegistryUri();
1✔
33
        String baseUri;
34
        try {
35
            baseUri = baseUrl.toURI().resolve("v1/types").toString();
1✔
NEW
36
        } catch (URISyntaxException e) {
×
NEW
37
            throw new InvalidConfigException("Type-Api base url not valid: " + baseUrl, e);
×
38
        }
1✔
39
        this.http = RestClient.builder()
1✔
40
                .baseUrl(baseUri)
1✔
41
                .requestInterceptor((request, body, execution) -> {
1✔
42
                    long start = System.currentTimeMillis();
1✔
43
                    ClientHttpResponse response = execution.execute(request,  body);
1✔
44
                    long timeSpan = System.currentTimeMillis() - start;
1✔
45
                    boolean isLongRequest = timeSpan > Application.LONG_HTTP_REQUEST_THRESHOLD;
1✔
46
                    if (isLongRequest) {
1✔
47
                        LOG.warn("Long http request to {} ({}ms)", request.getURI(), timeSpan);
1✔
48
                    }
49
                    return response;
1✔
50
                })
51
                .build();
1✔
52
    }
1✔
53

54
    @Override
55
    public SchemaInfo generateSchema(@NotNull String maybeTypePid) {
56
        return http.get()
1✔
57
                .uri(uriBuilder -> uriBuilder
1✔
58
                        .pathSegment("schema")
1✔
59
                        .path(maybeTypePid)
1✔
60
                        .build())
1✔
61
                .exchange((request, response) -> {
1✔
62
                    HttpStatusCode statusCode = response.getStatusCode();
1✔
63
                    if (statusCode.is2xxSuccessful()) {
1✔
64
                        JsonSchema schema = null;
1✔
65
                        try (InputStream inputStream = response.getBody()) {
1✔
66
                            JsonNode schemaDocument = Application.jsonObjectMapper()
1✔
67
                                    .readTree(inputStream);
1✔
68
                            schema = schemaFactory.getSchema(schemaDocument);
1✔
69
                            if (schema == null || schema.getSchemaNode().isMissingNode() || schema.getSchemaNode().isTextual()) {
1✔
NEW
70
                                throw new IOException("Could not create valid schema for %s from %s "
×
NEW
71
                                        .formatted(maybeTypePid, schemaDocument));
×
72
                            }
73
                            schema.initializeValidators();
1✔
NEW
74
                        } catch (IOException e) {
×
NEW
75
                            return new SchemaInfo(
×
NEW
76
                                    this.baseUrl.toString(),
×
77
                                    schema,
78
                                    new ExternalServiceException(
NEW
79
                                            baseUrl.toString(),
×
80
                                            "Response (" + maybeTypePid + ") is not a valid schema.")
81
                            );
82
                        }
1✔
83
                        return new SchemaInfo(this.baseUrl.toString(), schema, null);
1✔
NEW
84
                    } else if (statusCode.value() == 404) {
×
NEW
85
                        return new SchemaInfo(
×
NEW
86
                                this.baseUrl.toString(),
×
87
                                null,
88
                                new TypeNotFoundException(maybeTypePid));
89
                    } else {
NEW
90
                        return new SchemaInfo(
×
NEW
91
                                this.baseUrl.toString(),
×
92
                                null,
93
                                new ExternalServiceException(
NEW
94
                                        this.baseUrl.toString(),
×
NEW
95
                                        "Error generating schema: %s - %s".formatted(statusCode.value(), response.getStatusText())));
×
96
                    }
97
                });
98
    }
99
}
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

© 2026 Coveralls, Inc