1 package io.apicurio.registry.maven;
2
3 import io.apicurio.registry.rest.v3.beans.IfArtifactExists;
4 import org.apache.maven.plugin.MojoExecutionException;
5 import org.junit.jupiter.api.AfterEach;
6 import org.junit.jupiter.api.Test;
7 import org.junit.jupiter.api.io.TempDir;
8
9 import java.beans.IntrospectionException;
10 import java.beans.Introspector;
11 import java.beans.PropertyDescriptor;
12 import java.io.IOException;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.stream.Collectors;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22
23 public class RegisterRegistryMojoCliPropertiesTest {
24
25 @TempDir
26 Path tempDir;
27
28 @AfterEach
29 void clearArtifactProperties() {
30 System.getProperties().stringPropertyNames().stream()
31 .filter(name -> name.startsWith("artifacts."))
32 .collect(Collectors.toList())
33 .forEach(System::clearProperty);
34 }
35
36 @Test
37 void testLoadsSingleArtifactFromCliProperties() throws IOException, MojoExecutionException {
38 Path artifactFile = Files.createFile(tempDir.resolve("schema.avsc"));
39
40 System.setProperty("artifacts.groupId", "my.group.id");
41 System.setProperty("artifacts.artifactId", "my-artifact-id");
42 System.setProperty("artifacts.artifactType", "ASYNCAPI");
43 System.setProperty("artifacts.file", artifactFile.toString());
44 System.setProperty("artifacts.ifExists", "FIND_OR_CREATE_VERSION");
45 System.setProperty("artifacts.canonicalize", "true");
46 System.setProperty("artifacts.autoRefs", "true");
47 System.setProperty("artifacts.versionStrategy", "API_INFO_VERSION");
48
49 RegisterRegistryMojo mojo = new RegisterRegistryMojo();
50 mojo.validate();
51
52 assertNotNull(mojo.artifacts);
53 assertEquals(1, mojo.artifacts.size());
54
55 RegisterArtifact artifact = mojo.artifacts.get(0);
56 assertEquals("my.group.id", artifact.getGroupId());
57 assertEquals("my-artifact-id", artifact.getArtifactId());
58 assertEquals("ASYNCAPI", artifact.getArtifactType());
59 assertEquals(artifactFile.toFile(), artifact.getFile());
60 assertEquals(IfArtifactExists.FIND_OR_CREATE_VERSION, artifact.getIfExists());
61 assertEquals(Boolean.TRUE, artifact.getCanonicalize());
62 assertEquals(Boolean.TRUE, artifact.getAutoRefs());
63 assertEquals(RegisterArtifact.VersionStrategy.API_INFO_VERSION, artifact.getVersionStrategy());
64 }
65
66 @Test
67 void testLoadsNestedArtifactListsFromCliProperties() throws IOException, MojoExecutionException {
68 Path artifactFile = Files.createFile(tempDir.resolve("api.yaml"));
69 Path protoPathOne = Files.createDirectory(tempDir.resolve("proto"));
70 Path protoPathTwo = Files.createDirectory(tempDir.resolve("shared-proto"));
71
72 System.setProperty("artifacts.groupId", "my.group.id");
73 System.setProperty("artifacts.artifactId", "my-artifact-id");
74 System.setProperty("artifacts.artifactType", "ASYNCAPI");
75 System.setProperty("artifacts.file", artifactFile.toString());
76
77 System.setProperty("artifacts.references.0.name", "ref-name");
78 System.setProperty("artifacts.references.0.groupId", "ref.group");
79 System.setProperty("artifacts.references.0.artifactId", "ref-artifact");
80 System.setProperty("artifacts.references.0.version", "1.0.0");
81
82 System.setProperty("artifacts.existingReferences.0.resourceName", "./schemas/shared.avsc");
83 System.setProperty("artifacts.existingReferences.0.groupId", "existing.group");
84 System.setProperty("artifacts.existingReferences.0.artifactId", "existing-artifact");
85 System.setProperty("artifacts.existingReferences.0.version", "2.0.0");
86
87 System.setProperty("artifacts.protoPaths.0", protoPathOne.toString());
88 System.setProperty("artifacts.protoPaths.1", protoPathTwo.toString());
89
90 RegisterRegistryMojo mojo = new RegisterRegistryMojo();
91 mojo.validate();
92
93 RegisterArtifact artifact = mojo.artifacts.get(0);
94
95 assertNotNull(artifact.getReferences());
96 assertEquals(1, artifact.getReferences().size());
97 RegisterArtifactReference reference = artifact.getReferences().get(0);
98 assertEquals("ref-name", reference.getName());
99 assertEquals("ref.group", reference.getGroupId());
100 assertEquals("ref-artifact", reference.getArtifactId());
101 assertEquals("1.0.0", reference.getVersion());
102
103 assertNotNull(artifact.getExistingReferences());
104 assertEquals(1, artifact.getExistingReferences().size());
105 ExistingReference existingReference = artifact.getExistingReferences().get(0);
106 assertEquals("./schemas/shared.avsc", existingReference.getResourceName());
107 assertEquals("existing.group", existingReference.getGroupId());
108 assertEquals("existing-artifact", existingReference.getArtifactId());
109 assertEquals("2.0.0", existingReference.getVersion());
110
111 assertEquals(List.of(protoPathOne.toFile(), protoPathTwo.toFile()), artifact.getProtoPaths());
112 }
113
114 @Test
115 void testCliSupportedFieldsStayAlignedWithRegisterArtifactProperties() throws IntrospectionException {
116
117
118 Set<String> expectedCliFields = Set.of(
119 "groupId",
120 "artifactId",
121 "version",
122 "artifactType",
123 "file",
124 "ifExists",
125 "canonicalize",
126 "minify",
127 "autoRefs",
128 "versionStrategy",
129 "avroAutoRefsNamingStrategy",
130 "isDraft",
131 "contentType",
132 "references",
133 "existingReferences",
134 "protoPaths");
135
136 Set<String> actualWritableProperties = Arrays.stream(
137 Introspector.getBeanInfo(RegisterArtifact.class, Object.class).getPropertyDescriptors())
138 .filter(pd -> pd.getWriteMethod() != null)
139 .map(PropertyDescriptor::getName)
140 .collect(Collectors.toSet());
141
142 assertEquals(expectedCliFields, actualWritableProperties);
143 }
144 }