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

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

04 Mar 2025 12:05PM UTC coverage: 77.85% (+5.5%) from 72.4%
#480

Pull #264

github

web-flow
Merge 2a565921e into 534c1d4a6
Pull Request #264: Development branch for v3.0.0

371 of 466 new or added lines in 22 files covered. (79.61%)

2 existing lines in 1 file now uncovered.

956 of 1228 relevant lines covered (77.85%)

0.78 hits per line

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

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

3
import edu.kit.datamanager.pit.Application;
4
import edu.kit.datamanager.pit.common.ExternalServiceException;
5
import edu.kit.datamanager.pit.common.InvalidConfigException;
6
import edu.kit.datamanager.pit.common.TypeNotFoundException;
7
import edu.kit.datamanager.pit.configuration.ApplicationProperties;
8
import jakarta.validation.constraints.NotNull;
9
import org.everit.json.schema.Schema;
10
import org.everit.json.schema.loader.SchemaLoader;
11
import org.json.JSONException;
12
import org.json.JSONObject;
13
import org.json.JSONTokener;
14
import org.slf4j.Logger;
15
import org.slf4j.LoggerFactory;
16
import org.springframework.http.HttpStatusCode;
17
import org.springframework.http.client.ClientHttpResponse;
18
import org.springframework.web.client.RestClient;
19

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

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

53
    @Override
54
    public SchemaInfo generateSchema(@NotNull String maybeTypePid) {
55
        return http.get()
1✔
56
                .uri(uriBuilder -> uriBuilder
1✔
57
                        .pathSegment("schema")
1✔
58
                        .path(maybeTypePid)
1✔
59
                        .build())
1✔
60
                .exchange((request, response) -> {
1✔
61
                    HttpStatusCode statusCode = response.getStatusCode();
1✔
62
                    if (statusCode.is2xxSuccessful()) {
1✔
63
                        Schema schema = null;
1✔
64
                        try (InputStream inputStream = response.getBody()) {
1✔
65
                            JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
1✔
66
                            schema = SchemaLoader.load(rawSchema);
1✔
NEW
67
                        } catch (JSONException e) {
×
NEW
68
                            return new SchemaInfo(
×
NEW
69
                                    this.baseUrl.toString(),
×
70
                                    schema,
NEW
71
                                    new ExternalServiceException(baseUrl.toString(), "Response (" + maybeTypePid + ") is not a valid schema.")
×
72
                            );
73
                        }
1✔
74
                        return new SchemaInfo(this.baseUrl.toString(), schema, null);
1✔
NEW
75
                    } else if (statusCode.value() == 404) {
×
NEW
76
                        return new SchemaInfo(
×
NEW
77
                                this.baseUrl.toString(),
×
78
                                null,
79
                                new TypeNotFoundException(maybeTypePid));
80
                    } else {
NEW
81
                        return new SchemaInfo(
×
NEW
82
                                this.baseUrl.toString(),
×
83
                                null,
84
                                new ExternalServiceException(
NEW
85
                                        this.baseUrl.toString(),
×
NEW
86
                                        "Error generating schema: %s - %s".formatted(statusCode.value(), response.getStatusText())));
×
87
                    }
88
                });
89
    }
90
}
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