Skip to content

Commit e593ad6

Browse files
committed
Add a tests for IfExists logic
1 parent e185e13 commit e593ad6

File tree

1 file changed

+345
-0
lines changed

1 file changed

+345
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
package io.apicurio.registry.noprofile.rest.v3;
2+
3+
import io.apicurio.registry.AbstractResourceTestBase;
4+
import io.apicurio.registry.rest.client.models.CreateArtifact;
5+
import io.apicurio.registry.rest.client.models.CreateArtifactResponse;
6+
import io.apicurio.registry.rest.client.models.CreateGroup;
7+
import io.apicurio.registry.rest.client.models.IfArtifactExists;
8+
import io.apicurio.registry.rest.client.models.VersionSearchResults;
9+
import io.apicurio.registry.types.ArtifactType;
10+
import io.apicurio.registry.types.ContentTypes;
11+
import io.apicurio.registry.utils.tests.MutabilityEnabledProfile;
12+
import io.apicurio.registry.utils.tests.TestUtils;
13+
import io.quarkus.test.junit.QuarkusTest;
14+
import io.quarkus.test.junit.TestProfile;
15+
import org.junit.jupiter.api.Assertions;
16+
import org.junit.jupiter.api.Test;
17+
18+
@QuarkusTest
19+
@TestProfile(MutabilityEnabledProfile.class)
20+
public class IfExistsTest extends AbstractResourceTestBase {
21+
22+
private static final String SCHEMA_SIMPLE = """
23+
{
24+
"type": "record",
25+
"namespace": "com.example",
26+
"name": "FullName",
27+
"fields": [
28+
{ "name": "first", "type": "string" },
29+
{ "name": "last", "type": "string" }
30+
]
31+
}
32+
""";
33+
private static final String SCHEMA_SIMPLE_MIN = """
34+
{"type":"record","namespace":"com.example","name":"FullName","fields":[{"name":"first","type":"string"},{"name":"last","type":"string"}]}
35+
""";
36+
private static final String SCHEMA_SIMPLE_V2 = """
37+
{
38+
"type": "record",
39+
"namespace": "com.example",
40+
"name": "FullName",
41+
"fields": [
42+
{ "name": "first", "type": "string" },
43+
{ "name": "middle", "type": "string" },
44+
{ "name": "last", "type": "string" }
45+
]
46+
}
47+
""";
48+
49+
@Test
50+
public void testIfExistsFail() throws Exception {
51+
String groupId = "testIfExistsFail";
52+
String artifactId = "valid-artifact";
53+
54+
// Create a group
55+
CreateGroup createGroup = new CreateGroup();
56+
createGroup.setGroupId(groupId);
57+
clientV3.groups().post(createGroup);
58+
59+
CreateArtifact createArtifact = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
60+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
61+
CreateArtifactResponse car = clientV3.groups().byGroupId(groupId).artifacts().post(createArtifact);
62+
Assertions.assertNotNull(car);
63+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
64+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
65+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
66+
Assertions.assertEquals("1", car.getVersion().getVersion());
67+
68+
// Create the same thing again, with ifExists=FAIL
69+
Assertions.assertThrows(Exception.class, () -> {
70+
CreateArtifact ca = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
71+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
72+
clientV3.groups().byGroupId(groupId).artifacts().post(ca,
73+
config -> config.queryParameters.ifExists = IfArtifactExists.FAIL);
74+
});
75+
76+
// Should only have one version
77+
VersionSearchResults vsr = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().get();
78+
Assertions.assertNotNull(vsr);
79+
Assertions.assertEquals(1, vsr.getVersions().size());
80+
Assertions.assertEquals(1, vsr.getCount().intValue());
81+
}
82+
83+
@Test
84+
public void testIfExistsCreateVersion_Identical() throws Exception {
85+
String groupId = "testIfExistsCreateVersion_Identical";
86+
String artifactId = "valid-artifact";
87+
88+
// Create a group
89+
CreateGroup createGroup = new CreateGroup();
90+
createGroup.setGroupId(groupId);
91+
clientV3.groups().post(createGroup);
92+
93+
CreateArtifact createArtifact = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
94+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
95+
CreateArtifactResponse car = clientV3.groups().byGroupId(groupId).artifacts().post(createArtifact);
96+
Assertions.assertNotNull(car);
97+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
98+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
99+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
100+
Assertions.assertEquals("1", car.getVersion().getVersion());
101+
102+
// Create the same thing again, with ifExists=CREATE_VERSION
103+
CreateArtifact ca = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
104+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
105+
car = clientV3.groups().byGroupId(groupId).artifacts().post(ca,
106+
config -> config.queryParameters.ifExists = IfArtifactExists.CREATE_VERSION);
107+
Assertions.assertNotNull(car);
108+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
109+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
110+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
111+
Assertions.assertEquals("2", car.getVersion().getVersion());
112+
113+
// Should have two versions now
114+
VersionSearchResults vsr = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().get();
115+
Assertions.assertNotNull(vsr);
116+
Assertions.assertEquals(2, vsr.getVersions().size());
117+
Assertions.assertEquals(2, vsr.getCount().intValue());
118+
}
119+
120+
@Test
121+
public void testIfExistsCreateVersion_Equivalent() throws Exception {
122+
String groupId = "testIfExistsCreateVersion_Equivalent";
123+
String artifactId = "valid-artifact";
124+
125+
// Create a group
126+
CreateGroup createGroup = new CreateGroup();
127+
createGroup.setGroupId(groupId);
128+
clientV3.groups().post(createGroup);
129+
130+
CreateArtifact createArtifact = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
131+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
132+
CreateArtifactResponse car = clientV3.groups().byGroupId(groupId).artifacts().post(createArtifact);
133+
Assertions.assertNotNull(car);
134+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
135+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
136+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
137+
Assertions.assertEquals("1", car.getVersion().getVersion());
138+
139+
// Create the same thing again, with ifExists=CREATE_VERSION
140+
CreateArtifact ca = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
141+
SCHEMA_SIMPLE_MIN, ContentTypes.APPLICATION_JSON);
142+
car = clientV3.groups().byGroupId(groupId).artifacts().post(ca,
143+
config -> config.queryParameters.ifExists = IfArtifactExists.CREATE_VERSION);
144+
Assertions.assertNotNull(car);
145+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
146+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
147+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
148+
Assertions.assertEquals("2", car.getVersion().getVersion());
149+
150+
// Should have two versions now
151+
VersionSearchResults vsr = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().get();
152+
Assertions.assertNotNull(vsr);
153+
Assertions.assertEquals(2, vsr.getVersions().size());
154+
Assertions.assertEquals(2, vsr.getCount().intValue());
155+
}
156+
157+
@Test
158+
public void testIfExistsCreateVersion_New() throws Exception {
159+
String groupId = "testIfExistsCreateVersion_New";
160+
String artifactId = "valid-artifact";
161+
162+
// Create a group
163+
CreateGroup createGroup = new CreateGroup();
164+
createGroup.setGroupId(groupId);
165+
clientV3.groups().post(createGroup);
166+
167+
CreateArtifact createArtifact = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
168+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
169+
CreateArtifactResponse car = clientV3.groups().byGroupId(groupId).artifacts().post(createArtifact);
170+
Assertions.assertNotNull(car);
171+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
172+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
173+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
174+
Assertions.assertEquals("1", car.getVersion().getVersion());
175+
176+
// Create the same artifact again (but with a new version), with ifExists=CREATE_VERSION
177+
CreateArtifact ca = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
178+
SCHEMA_SIMPLE_V2, ContentTypes.APPLICATION_JSON);
179+
car = clientV3.groups().byGroupId(groupId).artifacts().post(ca,
180+
config -> config.queryParameters.ifExists = IfArtifactExists.CREATE_VERSION);
181+
Assertions.assertNotNull(car);
182+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
183+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
184+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
185+
Assertions.assertEquals("2", car.getVersion().getVersion());
186+
187+
// Should have two versions now
188+
VersionSearchResults vsr = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().get();
189+
Assertions.assertNotNull(vsr);
190+
Assertions.assertEquals(2, vsr.getVersions().size());
191+
Assertions.assertEquals(2, vsr.getCount().intValue());
192+
}
193+
194+
@Test
195+
public void testIfExistsFindOrCreateVersion_Identical() throws Exception {
196+
String groupId = "testIfExistsFindOrCreateVersion_Identical";
197+
String artifactId = "valid-artifact";
198+
199+
// Create a group
200+
CreateGroup createGroup = new CreateGroup();
201+
createGroup.setGroupId(groupId);
202+
clientV3.groups().post(createGroup);
203+
204+
CreateArtifact createArtifact = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
205+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
206+
CreateArtifactResponse car = clientV3.groups().byGroupId(groupId).artifacts().post(createArtifact);
207+
Assertions.assertNotNull(car);
208+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
209+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
210+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
211+
Assertions.assertEquals("1", car.getVersion().getVersion());
212+
213+
// Create the same exact thing again, with ifExists=FIND_OR_CREATE_VERSION
214+
CreateArtifact ca = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
215+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
216+
car = clientV3.groups().byGroupId(groupId).artifacts().post(ca,
217+
config -> config.queryParameters.ifExists = IfArtifactExists.FIND_OR_CREATE_VERSION);
218+
Assertions.assertNotNull(car);
219+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
220+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
221+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
222+
Assertions.assertEquals("1", car.getVersion().getVersion());
223+
224+
// Should have only one version
225+
VersionSearchResults vsr = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().get();
226+
Assertions.assertNotNull(vsr);
227+
Assertions.assertEquals(1, vsr.getVersions().size());
228+
Assertions.assertEquals(1, vsr.getCount().intValue());
229+
}
230+
231+
@Test
232+
public void testIfExistsFindOrCreateVersion_New() throws Exception {
233+
String groupId = "testIfExistsFindOrCreateVersion_New";
234+
String artifactId = "valid-artifact";
235+
236+
// Create a group
237+
CreateGroup createGroup = new CreateGroup();
238+
createGroup.setGroupId(groupId);
239+
clientV3.groups().post(createGroup);
240+
241+
CreateArtifact createArtifact = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
242+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
243+
CreateArtifactResponse car = clientV3.groups().byGroupId(groupId).artifacts().post(createArtifact);
244+
Assertions.assertNotNull(car);
245+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
246+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
247+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
248+
Assertions.assertEquals("1", car.getVersion().getVersion());
249+
250+
// Create the same exact thing again, with ifExists=FIND_OR_CREATE_VERSION
251+
CreateArtifact ca = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
252+
SCHEMA_SIMPLE_V2, ContentTypes.APPLICATION_JSON);
253+
car = clientV3.groups().byGroupId(groupId).artifacts().post(ca,
254+
config -> config.queryParameters.ifExists = IfArtifactExists.FIND_OR_CREATE_VERSION);
255+
Assertions.assertNotNull(car);
256+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
257+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
258+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
259+
Assertions.assertEquals("2", car.getVersion().getVersion());
260+
261+
// Should have only one version
262+
VersionSearchResults vsr = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().get();
263+
Assertions.assertNotNull(vsr);
264+
Assertions.assertEquals(2, vsr.getVersions().size());
265+
Assertions.assertEquals(2, vsr.getCount().intValue());
266+
}
267+
268+
@Test
269+
public void testIfExistsFindOrCreateVersion_Equivalent() throws Exception {
270+
String groupId = "testIfExistsFindOrCreateVersion_Equivalent";
271+
String artifactId = "valid-artifact";
272+
273+
// Create a group
274+
CreateGroup createGroup = new CreateGroup();
275+
createGroup.setGroupId(groupId);
276+
clientV3.groups().post(createGroup);
277+
278+
CreateArtifact createArtifact = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
279+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
280+
CreateArtifactResponse car = clientV3.groups().byGroupId(groupId).artifacts().post(createArtifact);
281+
Assertions.assertNotNull(car);
282+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
283+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
284+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
285+
Assertions.assertEquals("1", car.getVersion().getVersion());
286+
287+
// Create the same exact thing again, with ifExists=FIND_OR_CREATE_VERSION
288+
CreateArtifact ca = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
289+
SCHEMA_SIMPLE_MIN, ContentTypes.APPLICATION_JSON);
290+
car = clientV3.groups().byGroupId(groupId).artifacts().post(ca,
291+
config -> config.queryParameters.ifExists = IfArtifactExists.FIND_OR_CREATE_VERSION);
292+
Assertions.assertNotNull(car);
293+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
294+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
295+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
296+
Assertions.assertEquals("2", car.getVersion().getVersion());
297+
298+
// Should have only one version
299+
VersionSearchResults vsr = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().get();
300+
Assertions.assertNotNull(vsr);
301+
Assertions.assertEquals(2, vsr.getVersions().size());
302+
Assertions.assertEquals(2, vsr.getCount().intValue());
303+
}
304+
305+
@Test
306+
public void testIfExistsFindOrCreateVersion_Equivalent_Canonical() throws Exception {
307+
String groupId = "testIfExistsFindOrCreateVersion_Equivalent_Canonical";
308+
String artifactId = "valid-artifact";
309+
310+
// Create a group
311+
CreateGroup createGroup = new CreateGroup();
312+
createGroup.setGroupId(groupId);
313+
clientV3.groups().post(createGroup);
314+
315+
CreateArtifact createArtifact = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
316+
SCHEMA_SIMPLE, ContentTypes.APPLICATION_JSON);
317+
CreateArtifactResponse car = clientV3.groups().byGroupId(groupId).artifacts().post(createArtifact);
318+
Assertions.assertNotNull(car);
319+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
320+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
321+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
322+
Assertions.assertEquals("1", car.getVersion().getVersion());
323+
324+
// Create the same exact thing again, with ifExists=FIND_OR_CREATE_VERSION
325+
CreateArtifact ca = TestUtils.clientCreateArtifact(artifactId, ArtifactType.AVRO,
326+
SCHEMA_SIMPLE_MIN, ContentTypes.APPLICATION_JSON);
327+
car = clientV3.groups().byGroupId(groupId).artifacts().post(ca,
328+
config -> {
329+
config.queryParameters.ifExists = IfArtifactExists.FIND_OR_CREATE_VERSION;
330+
config.queryParameters.canonical = true;
331+
});
332+
Assertions.assertNotNull(car);
333+
Assertions.assertEquals(groupId, car.getArtifact().getGroupId());
334+
Assertions.assertEquals(artifactId, car.getArtifact().getArtifactId());
335+
Assertions.assertEquals(ArtifactType.AVRO, car.getArtifact().getArtifactType());
336+
Assertions.assertEquals("1", car.getVersion().getVersion());
337+
338+
// Should have only one version
339+
VersionSearchResults vsr = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().get();
340+
Assertions.assertNotNull(vsr);
341+
Assertions.assertEquals(1, vsr.getVersions().size());
342+
Assertions.assertEquals(1, vsr.getCount().intValue());
343+
}
344+
345+
}

0 commit comments

Comments
 (0)