1
1
package io .apicurio .registry .storage .impl .kafkasql ;
2
2
3
3
import io .apicurio .registry .AbstractResourceTestBase ;
4
- import io .apicurio .registry .rest .client .models .ArtifactContent ;
4
+ import io .apicurio .registry .rest .client .models .CreateArtifact ;
5
5
import io .apicurio .registry .rest .client .models .Rule ;
6
6
import io .apicurio .registry .rest .client .models .RuleType ;
7
- import io .apicurio .registry .utils . kafka . KafkaUtil ;
8
- import io .apicurio .registry .utils . tests . KafkasqlSnapshotTestProfile ;
9
- import io .quarkus . test . common . QuarkusTestResource ;
10
- import io .quarkus . test . common . QuarkusTestResourceLifecycleManager ;
7
+ import io .apicurio .registry .types . ArtifactType ;
8
+ import io .apicurio .registry .types . ContentTypes ;
9
+ import io .apicurio . registry . utils . tests . KafkasqlTestProfile ;
10
+ import io .apicurio . registry . utils . tests . TestUtils ;
11
11
import io .quarkus .test .junit .QuarkusTest ;
12
12
import io .quarkus .test .junit .TestProfile ;
13
13
import jakarta .inject .Inject ;
14
- import lombok .SneakyThrows ;
15
- import org .apache .kafka .clients .CommonClientConfigs ;
16
- import org .apache .kafka .clients .producer .KafkaProducer ;
17
- import org .apache .kafka .clients .producer .ProducerRecord ;
18
- import org .apache .kafka .common .header .internals .RecordHeader ;
19
- import org .apache .kafka .common .serialization .StringSerializer ;
20
14
import org .junit .jupiter .api .Assertions ;
21
15
import org .junit .jupiter .api .BeforeAll ;
22
16
import org .junit .jupiter .api .Test ;
23
17
24
18
import java .io .IOException ;
25
- import java .net .URISyntaxException ;
26
- import java .net .URL ;
27
- import java .nio .charset .StandardCharsets ;
28
19
import java .nio .file .Files ;
29
20
import java .nio .file .Path ;
30
- import java .nio .file .Paths ;
31
- import java .util .Collections ;
32
- import java .util .List ;
33
- import java .util .Map ;
34
- import java .util .Properties ;
35
- import java .util .Set ;
36
21
import java .util .UUID ;
37
- import java .util .concurrent .ExecutionException ;
38
22
39
23
@ QuarkusTest
40
- @ TestProfile (KafkasqlSnapshotTestProfile .class )
41
- @ QuarkusTestResource (value = KafkaSqlSnapshotTest .KafkaSqlSnapshotTestInitializer .class , restrictToAnnotatedClass = true )
24
+ @ TestProfile (KafkasqlTestProfile .class )
42
25
public class KafkaSqlSnapshotTest extends AbstractResourceTestBase {
43
26
44
27
private static final String NEW_ARTIFACTS_SNAPSHOT_TEST_GROUP_ID = "SNAPSHOT_TEST_GROUP_ID" ;
@@ -54,15 +37,15 @@ public void init() {
54
37
for (int idx = 0 ; idx < 1000 ; idx ++) {
55
38
System .out .println ("Iteration: " + idx );
56
39
String artifactId = UUID .randomUUID ().toString ();
57
- ArtifactContent content = new ArtifactContent ();
58
- content .setContent (simpleAvro );
59
- clientV3 .groups ().byGroupId (NEW_ARTIFACTS_SNAPSHOT_TEST_GROUP_ID ).artifacts ().post (content , config -> {
60
- config .headers .add ("X-Registry-ArtifactId" , artifactId );
61
- });
40
+ CreateArtifact createArtifact = TestUtils .clientCreateArtifact (artifactId , ArtifactType .AVRO , simpleAvro ,
41
+ ContentTypes .APPLICATION_JSON );
42
+ clientV3 .groups ().byGroupId (NEW_ARTIFACTS_SNAPSHOT_TEST_GROUP_ID ).artifacts ()
43
+ .post (createArtifact , config -> config .headers .add ("X-Registry-ArtifactId" , artifactId ));
62
44
Rule rule = new Rule ();
63
45
rule .setType (RuleType .VALIDITY );
64
46
rule .setConfig ("SYNTAX_ONLY" );
65
- clientV3 .groups ().byGroupId (NEW_ARTIFACTS_SNAPSHOT_TEST_GROUP_ID ).artifacts ().byArtifactId (artifactId ).rules ().post (rule ); }
47
+ clientV3 .groups ().byGroupId (NEW_ARTIFACTS_SNAPSHOT_TEST_GROUP_ID ).artifacts ().byArtifactId (artifactId ).rules ().post (rule );
48
+ }
66
49
}
67
50
68
51
@ Test
@@ -72,95 +55,4 @@ public void testSnapshotCreation() throws IOException {
72
55
Assertions .assertTrue (Files .exists (path ));
73
56
Files .delete (path );
74
57
}
75
-
76
-
77
- @ Test
78
- public void testRecoverFromSnapshot () throws InterruptedException {
79
- //We expect 4001 artifacts coming from the snapshot
80
- Assertions .assertEquals (4001 , clientV3 .groups ().byGroupId ("default" ).artifacts ().get ().getCount ());
81
- //We expect another 1000 artifacts coming added on top of the snapshot
82
- Assertions .assertEquals (1000 , clientV3 .groups ().byGroupId (NEW_ARTIFACTS_SNAPSHOT_TEST_GROUP_ID ).artifacts ().get ().getCount ());
83
- }
84
-
85
- protected static class KafkaSqlSnapshotTestInitializer implements QuarkusTestResourceLifecycleManager {
86
-
87
- public KafkaSqlSnapshotTestInitializer () {
88
- }
89
-
90
- @ Override
91
- public int order () {
92
- return 10001 ;
93
- }
94
-
95
- @ Override
96
- @ SneakyThrows
97
- public Map <String , String > start () {
98
- Properties props = connectionProperties ();
99
-
100
- KafkaUtil .createTopics (props , Set .of ("kafkasql-snapshots" , "kafkasql-journal" ), Collections .emptyMap ());
101
-
102
- prepareSnapshotMarker (props );
103
- prepareSnapshotMessages (props );
104
-
105
- return Collections .emptyMap ();
106
- }
107
-
108
- private void prepareSnapshotMarker (Properties props ) throws ExecutionException , InterruptedException {
109
- StringSerializer keySerializer = new StringSerializer ();
110
- StringSerializer valueSerializer = new StringSerializer ();
111
-
112
- //Create the data producer to send a snapshot marker
113
- KafkaProducer <String , String > dataProducer = new KafkaProducer <>(props , keySerializer , valueSerializer );
114
- RecordHeader messageTypeHeader = new RecordHeader ("mt" , "CreateSnapshot1Message" .getBytes (StandardCharsets .UTF_8 ));
115
- ProducerRecord <String , String > snapshotMarkerRecord = new ProducerRecord <>("kafkasql-journal" , 0 ,
116
- "{\" uuid\" :\" 1345b402-c707-457e-af76-10c1045e68e8\" ,\" messageType\" :\" CreateSnapshot1Message\" ,\" partitionKey\" :\" __GLOBAL_PARTITION__\" }" , "{\n "
117
- + " \" snapshotLocation\" : \" /io/apicurio/registry/storage/impl/kafkasql/1302b402-c707-457e-af76-10c1045e68e8.sql\" ,\n "
118
- + " \" snapshotId\" : \" 1302b402-c707-457e-af76-10c1045e68e8\" ,\n "
119
- + " \" key\" : {\n "
120
- + " \" uuid\" : \" 1302b402-c707-457e-af76-10c1045e68e8\" ,\n "
121
- + " \" messageType\" : \" CreateSnapshot1Message\" ,\n "
122
- + " \" partitionKey\" : \" __GLOBAL_PARTITION__\" \n "
123
- + " }\n "
124
- + " }" , List .of (messageTypeHeader ));
125
-
126
- //Send snapshot marker
127
- dataProducer .send (snapshotMarkerRecord ).get ();
128
- }
129
-
130
- private void prepareSnapshotMessages (Properties props ) throws URISyntaxException , ExecutionException , InterruptedException {
131
- StringSerializer keySerializer = new StringSerializer ();
132
- StringSerializer valueSerializer = new StringSerializer ();
133
-
134
- URL resource = getClass ().getResource ("/io/apicurio/registry/storage/impl/kafkasql/1302b402-c707-457e-af76-10c1045e68e8.sql" );
135
- String snapshotLocation = Paths .get (resource .toURI ()).toFile ().getAbsolutePath ();
136
-
137
- //Send three messages to the snapshots topic, two invalid, and one valid. Only the latest valid one must be processed.
138
- ProducerRecord <String , String > olderInvalidSnapshot = new ProducerRecord <>("kafkasql-snapshots" , 0 , "1312b402-c707-457e-af76-10c1045e68e8" ,
139
- "snapshotLocation" ,
140
- Collections .emptyList ());
141
- ProducerRecord <String , String > record = new ProducerRecord <>("kafkasql-snapshots" , 0 , "1302b402-c707-457e-af76-10c1045e68e8" , snapshotLocation ,
142
- Collections .emptyList ());
143
- ProducerRecord <String , String > newerInvalidSnaphot = new ProducerRecord <>("kafkasql-snapshots" , 0 , "1322b402-c707-457e-af76-10c1045e68e8" , "" ,
144
- Collections .emptyList ());
145
-
146
- // Create the Kafka Producer
147
- KafkaProducer <String , String > snapshotsProducer = new KafkaProducer <>(props , keySerializer , valueSerializer );
148
-
149
- snapshotsProducer .send (olderInvalidSnapshot ).get ();
150
- snapshotsProducer .send (record ).get ();
151
- snapshotsProducer .send (newerInvalidSnaphot ).get ();
152
- }
153
-
154
- public Properties connectionProperties () {
155
- Properties properties = new Properties ();
156
- properties .put (CommonClientConfigs .BOOTSTRAP_SERVERS_CONFIG , System .getProperty ("bootstrap.servers.external" ));
157
- properties .put (CommonClientConfigs .CONNECTIONS_MAX_IDLE_MS_CONFIG , 10000 );
158
- properties .put (CommonClientConfigs .REQUEST_TIMEOUT_MS_CONFIG , 5000 );
159
- return properties ;
160
- }
161
-
162
- @ Override
163
- public void stop () {
164
- }
165
- }
166
58
}
0 commit comments