-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathSchemasConfluentIT.java
348 lines (288 loc) · 15.7 KB
/
SchemasConfluentIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package io.apicurio.tests.smokeTests.confluent;
import io.apicurio.registry.rest.client.models.CreateRule;
import io.apicurio.registry.rest.client.models.Rule;
import io.apicurio.registry.rest.client.models.RuleType;
import io.apicurio.registry.utils.tests.TestUtils;
import io.apicurio.tests.ConfluentBaseIT;
import io.apicurio.tests.utils.ArtifactUtils;
import io.apicurio.tests.utils.Constants;
import io.confluent.kafka.schemaregistry.ParsedSchema;
import io.confluent.kafka.schemaregistry.avro.AvroSchema;
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException;
import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.response.Response;
import jakarta.ws.rs.WebApplicationException;
import org.apache.avro.SchemaParseException;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static io.apicurio.tests.utils.Constants.SMOKE;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@Tag(SMOKE)
@QuarkusIntegrationTest
public class SchemasConfluentIT extends ConfluentBaseIT {
private static final Logger LOGGER = LoggerFactory.getLogger(SchemasConfluentIT.class);
@Test
@Tag(ACCEPTANCE)
void createAndUpdateSchema() throws Exception {
String artifactId = TestUtils.generateArtifactId();
ParsedSchema schema = new AvroSchema(
"{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"foo1\",\"type\":\"string\"}]}");
createArtifactViaConfluentClient(schema, artifactId);
ParsedSchema updatedSchema = new AvroSchema(
"{\"type\":\"record\",\"name\":\"myrecord2\",\"fields\":[{\"name\":\"foo2\",\"type\":\"long\"}]}");
createArtifactViaConfluentClient(updatedSchema, artifactId);
assertThrows(SchemaParseException.class,
() -> new AvroSchema("<type>record</type>\n<name>test</name>"));
assertThat(confluentService.getAllVersions(artifactId), hasItems(1, 2));
confluentService.deleteSubject(artifactId);
confluentService.deleteSubject(artifactId, true);
waitForSubjectDeleted(artifactId);
}
@Test
void createAndDeleteMultipleSchemas() throws IOException, RestClientException, TimeoutException {
String prefix = TestUtils.generateArtifactId();
for (int i = 0; i < 50; i++) {
String name = "myrecord" + i;
String subjectName = prefix + i;
ParsedSchema schema = new AvroSchema("{\"type\":\"record\",\"name\":\"" + name
+ "\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}");
createArtifactViaConfluentClient(schema, subjectName);
}
assertThat(50, is(confluentService.getAllSubjects().size()));
LOGGER.info("All subjects {} schemas", confluentService.getAllSubjects().size());
for (int i = 0; i < 50; i++) {
confluentService.deleteSubject(prefix + i);
}
TestUtils.waitFor("all schemas deletion", Constants.POLL_INTERVAL, Constants.TIMEOUT_GLOBAL, () -> {
try {
return confluentService.getAllSubjects().size() == 0;
} catch (IOException | RestClientException e) {
return false;
}
});
}
@Test
void deleteSchemasSpecificVersion() throws Exception {
String artifactId = TestUtils.generateArtifactId();
ParsedSchema schema = new AvroSchema(
"{\"type\":\"record\",\"name\":\"mynewrecord\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}");
createArtifactViaConfluentClient(schema, artifactId);
schema = new AvroSchema(
"{\"type\":\"record\",\"name\":\"myrecordx\",\"fields\":[{\"name\":\"foo1\",\"type\":\"string\"}]}");
createArtifactViaConfluentClient(schema, artifactId);
List<Integer> schemeVersions = confluentService.getAllVersions(artifactId);
LOGGER.info("Available version of schema with name:{} are {}", artifactId, schemeVersions);
assertThat(schemeVersions, hasItems(1, 2));
schemeVersions = confluentService.getAllVersions(artifactId);
LOGGER.info("Available version of schema with name:{} are {}", artifactId, schemeVersions);
assertThat(schemeVersions, hasItems(1));
schema = new AvroSchema("{\"type\":\"record\",\"name\":\"myrecordx\",\"fields\":[{\"name\":\"foo" + 4
+ "\",\"type\":\"string\"}]}");
createArtifactViaConfluentClient(schema, artifactId);
confluentService.deleteSchemaVersion(artifactId, "2");
confluentService.deleteSchemaVersion(artifactId, "2", true);
TestUtils.waitFor("all specific schema version deletion", Constants.POLL_INTERVAL,
Constants.TIMEOUT_GLOBAL, () -> {
try {
return confluentService.getAllVersions(artifactId).size() == 2;
} catch (IOException | RestClientException e) {
return false;
}
});
schemeVersions = confluentService.getAllVersions(artifactId);
LOGGER.info("Available version of schema with name:{} are {}", artifactId, schemeVersions);
assertThat(schemeVersions, hasItems(1, 3));
confluentService.deleteSubject(artifactId);
confluentService.deleteSubject(artifactId, true);
waitForSubjectDeleted(artifactId);
}
@Test
void createSchemaSpecifyVersion() throws Exception {
String artifactId = TestUtils.generateArtifactId();
ParsedSchema schema = new AvroSchema(
"{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}");
createArtifactViaConfluentClient(schema, artifactId);
ParsedSchema updatedArtifact = new AvroSchema(
"{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"bar\",\"type\":\"string\"}]}");
createArtifactViaConfluentClient(updatedArtifact, artifactId);
List<Integer> schemaVersions = confluentService.getAllVersions(artifactId);
LOGGER.info("Available versions of schema with NAME {} are: {}", artifactId,
schemaVersions.toString());
assertThat(schemaVersions, hasItems(1, 2));
confluentService.deleteSubject(artifactId);
confluentService.deleteSubject(artifactId, true);
waitForSubjectDeleted(artifactId);
}
@Test
void deleteNonexistingSchema() {
assertThrows(RestClientException.class, () -> confluentService.deleteSubject("non-existing"));
}
@Test
void createInvalidSchemaDefinition() throws Exception {
String subjectName = TestUtils.generateArtifactId();
ParsedSchema schema = new AvroSchema(
"{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}");
createArtifactViaConfluentClient(schema, subjectName);
TestUtils.waitFor("artifactCreated", Constants.POLL_INTERVAL, Constants.TIMEOUT_GLOBAL, () -> {
return registryClient.groups().byGroupId("default").artifacts().byArtifactId(subjectName)
.get() != null;
});
String invalidSchema = "{\"schema\":\"{\\\"type\\\": \\\"bloop\\\"}\"}";
CreateRule createRule = new CreateRule();
createRule.setRuleType(RuleType.COMPATIBILITY);
createRule.setConfig("BACKWARD");
registryClient.groups().byGroupId("default").artifacts().byArtifactId(subjectName).rules()
.post(createRule);
TestUtils.waitFor("artifact rule created", Constants.POLL_INTERVAL, Constants.TIMEOUT_GLOBAL, () -> {
try {
Rule r = registryClient.groups().byGroupId("default").artifacts().byArtifactId(subjectName)
.rules().byRuleType(RuleType.COMPATIBILITY.name()).get();
return r != null && r.getConfig() != null && r.getConfig().equalsIgnoreCase("BACKWARD");
} catch (WebApplicationException e) {
return false;
}
});
ConfluentSubjectsUtils.createSchema(invalidSchema, subjectName, 422);
}
@Test
void createConfluentQueryApicurio() throws IOException, RestClientException, TimeoutException {
String name = "schemaname";
String subjectName = TestUtils.generateArtifactId();
String rawSchema = "{\"type\":\"record\",\"name\":\"" + name
+ "\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}";
ParsedSchema schema = new AvroSchema(rawSchema);
createArtifactViaConfluentClient(schema, subjectName);
assertThat(1, is(confluentService.getAllSubjects().size()));
Response ar = ArtifactUtils.getArtifact("default", subjectName, "branch=latest", 200);
assertEquals(rawSchema, ar.asString());
LOGGER.info(ar.asString());
}
@Test
void testCreateDeleteSchemaRuleIsDeleted() throws Exception {
String name = "schemaname";
String subjectName = TestUtils.generateArtifactId();
ParsedSchema schema = new AvroSchema("{\"type\":\"record\",\"name\":\"" + name
+ "\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}");
long contentId = createArtifactViaConfluentClient(schema, subjectName);
assertThat(1, is(confluentService.getAllSubjects().size()));
TestUtils.waitFor("waiting for content to be created", Constants.POLL_INTERVAL,
Constants.TIMEOUT_GLOBAL, () -> {
try {
return registryClient.ids().contentIds().byContentId(contentId).get()
.readAllBytes().length > 0;
} catch (IOException cnfe) {
return false;
}
});
TestUtils.waitFor("artifact created", Constants.POLL_INTERVAL, Constants.TIMEOUT_GLOBAL, () -> {
try {
return registryClient.groups().byGroupId("default").artifacts().byArtifactId(subjectName)
.versions().byVersionExpression("branch=latest").content().get()
.readAllBytes().length > 0;
} catch (WebApplicationException e) {
return false;
} catch (IOException e) {
throw new RuntimeException(e);
}
});
CreateRule createRule = new CreateRule();
createRule.setRuleType(RuleType.VALIDITY);
createRule.setConfig("FULL");
registryClient.groups().byGroupId("default").artifacts().byArtifactId(subjectName).rules()
.post(createRule);
TestUtils.waitFor("artifact rule created", Constants.POLL_INTERVAL, Constants.TIMEOUT_GLOBAL, () -> {
try {
Rule r = registryClient.groups().byGroupId("default").artifacts().byArtifactId(subjectName)
.rules().byRuleType(RuleType.VALIDITY.name()).get();
return r != null && r.getConfig() != null && r.getConfig().equalsIgnoreCase("FULL");
} catch (WebApplicationException e) {
return false;
}
});
List<RuleType> rules = registryClient.groups().byGroupId("default").artifacts()
.byArtifactId(subjectName).rules().get();
assertThat(1, is(rules.size()));
confluentService.deleteSubject(subjectName);
TestUtils.waitFor("schema deletion", Constants.POLL_INTERVAL, Constants.TIMEOUT_GLOBAL, () -> {
try {
return confluentService.getAllSubjects().size() == 0;
} catch (IOException | RestClientException e) {
return false;
}
});
confluentService.deleteSubject(subjectName, true);
retryOp((rc) -> {
TestUtils.assertClientError("ArtifactNotFoundException", 404,
() -> rc.groups().byGroupId("default").artifacts().byArtifactId(subjectName).get(),
errorCodeExtractor);
TestUtils.assertClientError("ArtifactNotFoundException", 404, () -> rc.groups()
.byGroupId("default").artifacts().byArtifactId(subjectName).rules().get(),
errorCodeExtractor);
TestUtils.assertClientError(
"ArtifactNotFoundException", 404, () -> rc.groups().byGroupId("default").artifacts()
.byArtifactId(subjectName).rules().byRuleType(rules.get(0).name()).get(),
errorCodeExtractor);
});
// if rule was actually deleted creating same artifact again shouldn't fail
createArtifactViaConfluentClient(schema, subjectName);
assertThat(1, is(confluentService.getAllSubjects().size()));
}
@Test
void testConcurrentSchemaRegistration() throws Exception {
String subject = TestUtils.generateArtifactId();
// Use a valid Avro record name instead of the subject UUID
String recordName = "TestRecord_" + subject.replace("-", "_"); // Ensure valid chars if needed, or just use a fixed name
String schemaDefinition = "{\"type\":\"record\",\"name\":\"" + recordName + "\",\"fields\":[{\"name\":\"foo\",\"type\":\"string\"}]}";
ParsedSchema schema = new AvroSchema(schemaDefinition);
int numThreads = 50;
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
try {
List<CompletableFuture<Integer>> futures = IntStream.range(0, numThreads)
.mapToObj(i -> CompletableFuture.supplyAsync(() -> {
try {
confluentService.reset(); // clear cache
return confluentService.register(subject, schema);
} catch (IOException | RestClientException e) {
throw new CompletionException(e);
}
}, executorService))
.collect(Collectors.toList());
// Wait for all futures to complete and collect results
List<Integer> results = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(v -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()))
.get(); // Wait for completion and get the list of IDs
// Assert all returned IDs are the same and positive
assertEquals(numThreads, results.size());
int firstId = results.get(0);
assertThat("All registered schema IDs should be the same", firstId > 0);
results.forEach(id -> assertEquals(firstId, id.intValue(), "Concurrent registration resulted in different IDs"));
// Verify only one version was actually created
List<Integer> versions = confluentService.getAllVersions(subject);
assertEquals(1, versions.size(), "Only one version should be created despite concurrent requests");
assertEquals(1, versions.get(0).intValue()); // The version number should be 1
} finally {
executorService.shutdown();
// Clean up
confluentService.deleteSubject(subject, false);
confluentService.deleteSubject(subject, true);
waitForSubjectDeleted(subject);
}
}
}