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

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

23 Jan 2025 02:48PM UTC coverage: 75.997% (+3.6%) from 72.4%
#451

Pull #218

github

web-flow
Merge a92bd4d02 into 459f0c036
Pull Request #218: Type-Api support and validation speedup

287 of 352 new or added lines in 18 files covered. (81.53%)

3 existing lines in 2 files now uncovered.

915 of 1204 relevant lines covered (76.0%)

0.76 hits per line

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

82.93
/src/main/java/edu/kit/datamanager/pit/typeregistry/schema/DtrTestSchemaGenerator.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.http.client.JdkClientHttpRequestFactory;
19
import org.springframework.web.client.RestClient;
20

21
import java.io.InputStream;
22
import java.net.URI;
23
import java.net.URISyntaxException;
24
import java.net.http.HttpClient;
25

26
public class DtrTestSchemaGenerator implements SchemaGenerator {
27
    private static final Logger LOG = LoggerFactory.getLogger(DtrTestSchemaGenerator.class);
1✔
28

29
    protected static final String ORIGIN = "dtr-test";
30
    protected final URI baseUrl;
31
    protected final RestClient http;
32

33
    public DtrTestSchemaGenerator(@NotNull ApplicationProperties props) {
1✔
34
        try {
35
            this.baseUrl = props.getHandleBaseUri().toURI();
1✔
NEW
36
        } catch (URISyntaxException e) {
×
NEW
37
            throw new InvalidConfigException("BaseUrl not configured properly.");
×
38
        }
1✔
39
        HttpClient httpClient = java.net.http.HttpClient.newBuilder()
1✔
40
                .followRedirects(java.net.http.HttpClient.Redirect.NORMAL)
1✔
41
                .build();
1✔
42
        this.http = RestClient.builder()
1✔
43
                .baseUrl(this.baseUrl.toString())
1✔
44
                .requestFactory(new JdkClientHttpRequestFactory(httpClient))
1✔
45
                .requestInterceptor((request, body, execution) -> {
1✔
46
                    long start = System.currentTimeMillis();
1✔
47
                    ClientHttpResponse response = execution.execute(request,  body);
1✔
48
                    long timeSpan = System.currentTimeMillis() - start;
1✔
49
                    boolean isLongRequest = timeSpan > Application.LONG_HTTP_REQUEST_THRESHOLD;
1✔
50
                    if (isLongRequest) {
1✔
51
                        LOG.warn("Long http request to {} ({}ms)", request.getURI(), timeSpan);
1✔
52
                    }
53
                    return response;
1✔
54
                })
55
                .build();
1✔
56
    }
1✔
57

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