-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathRegistryMojoWithAutoReferencesTest.java
214 lines (176 loc) · 8.98 KB
/
RegistryMojoWithAutoReferencesTest.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
package io.apicurio.registry.noprofile.maven;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.apicurio.registry.maven.DownloadRegistryMojo;
import io.apicurio.registry.maven.RegisterArtifact;
import io.apicurio.registry.maven.RegisterRegistryMojo;
import io.apicurio.registry.rest.client.models.ArtifactReference;
import io.apicurio.registry.rest.client.models.VersionMetaData;
import io.apicurio.registry.rest.v3.beans.IfExists;
import io.apicurio.registry.types.ArtifactType;
import io.apicurio.registry.utils.IoUtil;
import io.apicurio.registry.utils.tests.TestUtils;
import io.quarkus.test.junit.QuarkusTest;
@QuarkusTest
public class RegistryMojoWithAutoReferencesTest extends RegistryMojoTestBase {
private static final String PROTO_SCHEMA_EXTENSION = ".proto";
private static final String AVSC_SCHEMA_EXTENSION = ".avsc";
private static final String JSON_SCHEMA_EXTENSION = ".json";
RegisterRegistryMojo registerMojo;
DownloadRegistryMojo downloadMojo;
@BeforeEach
public void createMojos() {
this.registerMojo = new RegisterRegistryMojo();
this.registerMojo.setRegistryUrl(TestUtils.getRegistryV3ApiUrl(testPort));
this.downloadMojo = new DownloadRegistryMojo();
this.downloadMojo.setRegistryUrl(TestUtils.getRegistryV3ApiUrl(testPort));
}
@Test
public void autoRegisterAvroWithReferences() throws Exception {
String groupId = "autoRegisterAvroWithReferences";
String artifactId = "tradeRaw";
File tradeRawFile = new File(getClass().getResource("TradeRawArray.avsc").getFile());
Set<String> avroFiles = Arrays.stream(Objects.requireNonNull(tradeRawFile.getParentFile().listFiles((dir, name) -> name.endsWith(AVSC_SCHEMA_EXTENSION))))
.map(file -> {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
}
return IoUtil.toString(fis);
})
.collect(Collectors.toSet());
RegisterArtifact tradeRawArtifact = new RegisterArtifact();
tradeRawArtifact.setGroupId(groupId);
tradeRawArtifact.setArtifactId(artifactId);
tradeRawArtifact.setType(ArtifactType.AVRO);
tradeRawArtifact.setFile(tradeRawFile);
tradeRawArtifact.setAnalyzeDirectory(true);
tradeRawArtifact.setIfExists(IfExists.FAIL);
registerMojo.setArtifacts(Collections.singletonList(tradeRawArtifact));
registerMojo.execute();
//Assertions
validateStructure(groupId, artifactId, 1, 3, avroFiles);
}
@Test
public void autoRegisterProtoWithReferences() throws Exception {
//Preparation
String groupId = "autoRegisterProtoWithReferences";
String artifactId = "tableNotification";
File tableNotificationFile = new File(getClass().getResource("table_notification.proto").getFile());
Set<String> protoFiles = Arrays.stream(Objects.requireNonNull(tableNotificationFile.getParentFile().listFiles((dir, name) -> name.endsWith(PROTO_SCHEMA_EXTENSION))))
.map(file -> {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
}
return IoUtil.toString(fis).trim();
})
.collect(Collectors.toSet());
RegisterArtifact tableNotification = new RegisterArtifact();
tableNotification.setGroupId(groupId);
tableNotification.setArtifactId(artifactId);
tableNotification.setType(ArtifactType.PROTOBUF);
tableNotification.setFile(tableNotificationFile);
tableNotification.setAnalyzeDirectory(true);
tableNotification.setIfExists(IfExists.FAIL);
registerMojo.setArtifacts(Collections.singletonList(tableNotification));
//Execution
registerMojo.execute();
//Assertions
validateStructure(groupId, artifactId, 2, 4, protoFiles);
}
@Test
public void autoRegisterJsonSchemaWithReferences() throws Exception {
//Preparation
String groupId = "autoRegisterJsonSchemaWithReferences";
String artifactId = "citizen";
File citizenFile = new File(getClass().getResource("citizen.json").getFile());
Set<String> protoFiles = Arrays.stream(Objects.requireNonNull(citizenFile.getParentFile().listFiles((dir, name) -> name.endsWith(JSON_SCHEMA_EXTENSION))))
.map(file -> {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
}
return IoUtil.toString(fis).trim();
})
.collect(Collectors.toSet());
RegisterArtifact citizen = new RegisterArtifact();
citizen.setGroupId(groupId);
citizen.setArtifactId(artifactId);
citizen.setType(ArtifactType.JSON);
citizen.setFile(citizenFile);
citizen.setAnalyzeDirectory(true);
citizen.setIfExists(IfExists.FAIL);
registerMojo.setArtifacts(Collections.singletonList(citizen));
//Execution
registerMojo.execute();
//Assertions
validateStructure(groupId, artifactId, 3, 4, protoFiles);
}
private void validateStructure(String groupId, String artifactId, int expectedMainReferences, int expectedTotalArtifacts, Set<String> originalContents) throws Exception {
final VersionMetaData artifactWithReferences = clientV3.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().byVersionExpression("branch=latest").get();
final String mainContent =
new String(
clientV3
.groups()
.byGroupId(groupId)
.artifacts()
.byArtifactId(artifactId)
.versions()
.byVersionExpression(artifactWithReferences.getVersion())
.content()
.get()
.readAllBytes(), StandardCharsets.UTF_8);
Assertions.assertTrue(originalContents.contains(mainContent)); //The main content has been registered as-is.
final List<ArtifactReference> mainArtifactReferences = clientV3.ids().globalIds().byGlobalId(artifactWithReferences.getGlobalId()).references().get();
//The main artifact has the expected number of references
Assertions.assertEquals(expectedMainReferences, mainArtifactReferences.size());
//Validate all the contents are registered as they are in the file system.
validateReferences(mainArtifactReferences, originalContents);
//The total number of artifacts for the directory structure is the expected.
Assertions.assertEquals(expectedTotalArtifacts, clientV3.groups().byGroupId(groupId).artifacts().get().getCount().intValue());
}
private void validateReferences(List<ArtifactReference> artifactReferences, Set<String> loadedContents) throws Exception {
for (ArtifactReference artifactReference : artifactReferences) {
String referenceContent = new String(
clientV3
.groups()
.byGroupId(artifactReference.getGroupId())
.artifacts()
.byArtifactId(artifactReference.getArtifactId())
.versions()
.byVersionExpression(artifactReference.getVersion())
.content()
.get()
.readAllBytes(), StandardCharsets.UTF_8);
VersionMetaData referenceMetadata = clientV3
.groups()
.byGroupId(artifactReference.getGroupId())
.artifacts()
.byArtifactId(artifactReference.getArtifactId())
.versions()
.byVersionExpression("branch=latest")
.get()
;
Assertions.assertTrue(loadedContents.contains(referenceContent.trim()));
List<ArtifactReference> nestedReferences = clientV3.ids().globalIds().byGlobalId(referenceMetadata.getGlobalId()).references().get();
if (!nestedReferences.isEmpty()) {
validateReferences(nestedReferences, loadedContents);
}
}
}
}