View Javadoc
1   package io.apicurio.registry.maven;
2   
3   import io.apicurio.registry.rest.client.RegistryClient;
4   import io.apicurio.registry.rest.client.models.CreateVersion;
5   import io.apicurio.registry.rest.client.models.VersionContent;
6   import io.apicurio.registry.utils.IoUtil;
7   import io.vertx.core.Vertx;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugins.annotations.Mojo;
10  import org.apache.maven.plugins.annotations.Parameter;
11  
12  import java.io.FileInputStream;
13  import java.io.InputStream;
14  import java.util.List;
15  
16  /**
17   * Test artifact against current artifact rules, if an update is possible / valid.
18   * 
19   * @deprecated In favor of using the "dryRun" option of the "register" mojo.
20   */
21  @Mojo(name = "test-update")
22  @Deprecated
23  public class TestUpdateRegistryMojo extends AbstractRegistryMojo {
24  
25      /**
26       * The list of artifacts to test.
27       */
28      @Parameter(required = true)
29      List<TestArtifact> artifacts;
30  
31      /**
32       * Validate the configuration.
33       */
34      protected void validate() throws MojoExecutionException {
35          if (artifacts == null || artifacts.isEmpty()) {
36              getLog().warn("No artifacts are configured for testing/validation.");
37          } else {
38              int idx = 0;
39              int errorCount = 0;
40              for (TestArtifact artifact : artifacts) {
41                  if (artifact.getGroupId() == null) {
42                      getLog().error(String.format(
43                              "GroupId is required when testing an artifact.  Missing from artifacts[%d].",
44                              idx));
45                      errorCount++;
46                  }
47                  if (artifact.getArtifactId() == null) {
48                      getLog().error(String.format(
49                              "ArtifactId is required when testing an artifact.  Missing from artifacts[%s].",
50                              idx));
51                      errorCount++;
52                  }
53                  if (artifact.getFile() == null) {
54                      getLog().error(String.format(
55                              "File is required when testing an artifact.  Missing from artifacts[%s].", idx));
56                      errorCount++;
57                  } else if (!artifact.getFile().isFile()) {
58                      getLog().error(String.format(
59                              "Artifact file to test is configured but file does not exist or is not a file: %s",
60                              artifact.getFile().getPath()));
61                      errorCount++;
62                  }
63  
64                  idx++;
65              }
66  
67              if (errorCount > 0) {
68                  throw new MojoExecutionException(
69                          "Invalid configuration of the Test Update Artifact(s) mojo. See the output log for details.");
70              }
71          }
72      }
73  
74      @Override
75      protected void executeInternal() throws MojoExecutionException {
76          validate();
77          Vertx vertx = createVertx();
78          RegistryClient registryClient = createClient(vertx);
79  
80          try {
81              int errorCount = 0;
82              if (artifacts != null) {
83                  for (TestArtifact artifact : artifacts) {
84                      String groupId = artifact.getGroupId();
85                      String artifactId = artifact.getArtifactId();
86                      String contentType = contentType(artifact);
87                      try (InputStream data = new FileInputStream(artifact.getFile())) {
88                          String content = IoUtil.toString(data);
89                          CreateVersion cv = new CreateVersion();
90                          cv.setContent(new VersionContent());
91                          cv.getContent().setContentType(contentType);
92                          cv.getContent().setContent(content);
93                          registryClient.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId)
94                                  .versions().post(cv, config -> {
95                                      config.queryParameters.dryRun = true;
96                                  });
97                          getLog().info(String.format(
98                                  "[%s] / [%s] :: Artifact successfully tested (updating is allowed for the given content).",
99                                  groupId, artifactId));
100                     } catch (Exception e) {
101                         errorCount++;
102                         getLog().error(String.format(
103                                 "[%s] / [%s] :: Artifact test FAILED (updating is not allowed for the given content).",
104                                 groupId, artifactId), e);
105                     }
106                 }
107             }
108 
109             if (errorCount > 0) {
110                 throw new MojoExecutionException("Errors while testing artifacts for update...");
111             }
112         } finally {
113             vertx.close();
114         }
115     }
116 
117     private String contentType(TestArtifact testArtifact) {
118         String contentType = testArtifact.getContentType();
119         if (contentType != null) {
120             return contentType;
121         }
122         return getContentTypeByExtension(testArtifact.getFile().getName());
123     }
124 
125     public void setArtifacts(List<TestArtifact> artifacts) {
126         this.artifacts = artifacts;
127     }
128 }