|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.gradle.pluginzip; |
| 10 | + |
| 11 | +import org.gradle.testkit.runner.BuildResult; |
| 12 | +import org.gradle.testkit.runner.GradleRunner; |
| 13 | +import org.gradle.testfixtures.ProjectBuilder; |
| 14 | +import org.gradle.api.Project; |
| 15 | +import org.opensearch.gradle.test.GradleUnitTestCase; |
| 16 | +import org.junit.Test; |
| 17 | +import java.io.IOException; |
| 18 | +import org.gradle.api.publish.maven.tasks.PublishToMavenRepository; |
| 19 | +import java.io.File; |
| 20 | +import org.gradle.testkit.runner.BuildResult; |
| 21 | +import java.io.FileWriter; |
| 22 | +import java.io.Writer; |
| 23 | +import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | +import java.nio.file.Files; |
| 26 | +import org.apache.maven.model.Model; |
| 27 | +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
| 28 | +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 29 | +import java.io.FileReader; |
| 30 | +import org.gradle.api.tasks.bundling.Zip; |
| 31 | + |
| 32 | +public class PublishTests extends GradleUnitTestCase { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testZipPublish() throws IOException, XmlPullParserException { |
| 36 | + Project project = ProjectBuilder.builder().build(); |
| 37 | + String zipPublishTask = "publishPluginZipPublicationToZipStagingRepository"; |
| 38 | + // Apply the opensearch.pluginzip plugin |
| 39 | + project.getPluginManager().apply("opensearch.pluginzip"); |
| 40 | + // Check if the plugin has been applied to the project |
| 41 | + assertTrue(project.getPluginManager().hasPlugin("opensearch.pluginzip")); |
| 42 | + // Check if the project has the task from class PublishToMavenRepository after plugin apply |
| 43 | + assertNotNull(project.getTasks().withType(PublishToMavenRepository.class)); |
| 44 | + // Create a mock bundlePlugin task |
| 45 | + Zip task = project.getTasks().create("bundlePlugin", Zip.class); |
| 46 | + Publish.configMaven(project); |
| 47 | + // Check if the main task publishPluginZipPublicationToZipStagingRepository exists after plugin apply |
| 48 | + assertTrue(project.getTasks().getNames().contains(zipPublishTask)); |
| 49 | + assertNotNull("Task to generate: ", project.getTasks().getByName(zipPublishTask)); |
| 50 | + // Run Gradle functional tests, but calling a build.gradle file, that resembles the plugin publish behavior |
| 51 | + File projectDir = new File("build/functionalTest"); |
| 52 | + // Create a sample plugin zip file |
| 53 | + File sampleZip = new File("build/functionalTest/sample-plugin.zip"); |
| 54 | + Files.createDirectories(projectDir.toPath()); |
| 55 | + Files.createFile(sampleZip.toPath()); |
| 56 | + writeString(new File(projectDir, "settings.gradle"), ""); |
| 57 | + // Generate the build.gradle file |
| 58 | + String buildFileContent = "apply plugin: 'maven-publish' \n" |
| 59 | + + "publishing {\n" |
| 60 | + + " repositories {\n" |
| 61 | + + " maven {\n" |
| 62 | + + " url = 'local-staging-repo/'\n" |
| 63 | + + " name = 'zipStaging'\n" |
| 64 | + + " }\n" |
| 65 | + + " }\n" |
| 66 | + + " publications {\n" |
| 67 | + + " pluginZip(MavenPublication) {\n" |
| 68 | + + " groupId = 'org.opensearch.plugin' \n" |
| 69 | + + " artifactId = 'sample-plugin' \n" |
| 70 | + + " version = '2.0.0.0' \n" |
| 71 | + + " artifact('sample-plugin.zip') \n" |
| 72 | + + " }\n" |
| 73 | + + " }\n" |
| 74 | + + "}"; |
| 75 | + writeString(new File(projectDir, "build.gradle"), buildFileContent); |
| 76 | + // Execute the task publishPluginZipPublicationToZipStagingRepository |
| 77 | + GradleRunner runner = GradleRunner.create(); |
| 78 | + runner.forwardOutput(); |
| 79 | + runner.withPluginClasspath(); |
| 80 | + runner.withArguments(zipPublishTask); |
| 81 | + runner.withProjectDir(projectDir); |
| 82 | + BuildResult result = runner.build(); |
| 83 | + // Check if task publishMavenzipPublicationToZipstagingRepository has ran well |
| 84 | + assertEquals(SUCCESS, result.task(":" + zipPublishTask).getOutcome()); |
| 85 | + // check if the zip has been published to local staging repo |
| 86 | + assertTrue( |
| 87 | + new File("build/functionalTest/local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.zip") |
| 88 | + .exists() |
| 89 | + ); |
| 90 | + // Parse the maven file and validate the groupID to org.opensearch.plugin |
| 91 | + MavenXpp3Reader reader = new MavenXpp3Reader(); |
| 92 | + Model model = reader.read( |
| 93 | + new FileReader("build/functionalTest/local-staging-repo/org/opensearch/plugin/sample-plugin/2.0.0.0/sample-plugin-2.0.0.0.pom") |
| 94 | + ); |
| 95 | + assertEquals(model.getGroupId(), "org.opensearch.plugin"); |
| 96 | + } |
| 97 | + |
| 98 | + private void writeString(File file, String string) throws IOException { |
| 99 | + try (Writer writer = new FileWriter(file)) { |
| 100 | + writer.write(string); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments