|
| 1 | +/* |
| 2 | + * Copyright 2013-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.contract.maven.verifier; |
| 18 | + |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.nio.file.Path; |
| 21 | + |
| 22 | +import org.apache.maven.api.plugin.testing.MojoExtension; |
| 23 | +import org.apache.maven.plugin.AbstractMojo; |
| 24 | +import org.apache.maven.repository.internal.MavenRepositorySystemUtils; |
| 25 | +import org.eclipse.aether.DefaultRepositorySystemSession; |
| 26 | +import org.eclipse.aether.RepositorySystem; |
| 27 | +import org.eclipse.aether.repository.LocalRepository; |
| 28 | +import org.eclipse.aether.supplier.RepositorySystemSupplier; |
| 29 | +import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy; |
| 30 | +import org.junit.jupiter.api.AfterEach; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | + |
| 33 | +import static org.assertj.core.api.BDDAssertions.then; |
| 34 | + |
| 35 | +/** |
| 36 | + * Small helper for the project-per-folder integration tests. |
| 37 | + */ |
| 38 | +abstract class AbstractProjectIntegrationTests extends AbstractMojoIntegrationTests { |
| 39 | + |
| 40 | + private Path targetDir; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUpTargetDir() throws Exception { |
| 44 | + Path projectDir = Path.of(MojoExtension.getBasedir()); |
| 45 | + this.targetDir = projectDir.resolve("target"); |
| 46 | + deleteDirectoryIfExists(this.targetDir); |
| 47 | + Files.createDirectories(this.targetDir); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void cleanTargetDir() throws Exception { |
| 52 | + deleteDirectoryIfExists(this.targetDir); |
| 53 | + } |
| 54 | + |
| 55 | + void runConvertMojo(ConvertMojo mojo) throws Exception { |
| 56 | + configureRepositorySystem(mojo); |
| 57 | + setupBuildPaths(mojo, this.targetDir); |
| 58 | + Path stubsDir = this.targetDir.resolve("stubs"); |
| 59 | + MojoExtension.setVariableValueToObject(mojo, "stubsDirectory", stubsDir.toFile()); |
| 60 | + MojoExtension.setVariableValueToObject(mojo, "incrementalContractStubs", false); |
| 61 | + mojo.execute(); |
| 62 | + then(Files.exists(stubsDir)).as("generated stubs directory should exist at %s", stubsDir).isTrue(); |
| 63 | + } |
| 64 | + |
| 65 | + void runGenerateTestsMojo(GenerateTestsMojo mojo) throws Exception { |
| 66 | + configureRepositorySystem(mojo); |
| 67 | + setupBuildPaths(mojo, this.targetDir); |
| 68 | + Path generatedSources = this.targetDir.resolve("generated-test-sources/contracts"); |
| 69 | + Path generatedResources = this.targetDir.resolve("generated-test-resources/contracts"); |
| 70 | + MojoExtension.setVariableValueToObject(mojo, "generatedTestSourcesDir", generatedSources.toFile()); |
| 71 | + MojoExtension.setVariableValueToObject(mojo, "generatedTestResourcesDir", generatedResources.toFile()); |
| 72 | + MojoExtension.setVariableValueToObject(mojo, "incrementalContractTests", false); |
| 73 | + mojo.execute(); |
| 74 | + then(Files.exists(generatedSources)).as("generated test sources should exist at %s", generatedSources).isTrue(); |
| 75 | + then(Files.exists(generatedResources)).as("generated test resources should exist at %s", generatedResources) |
| 76 | + .isTrue(); |
| 77 | + } |
| 78 | + |
| 79 | + void configureRemoteRepo(AbstractMojo mojo, WireMockRepositoryServer server) throws Exception { |
| 80 | + MojoExtension.setVariableValueToObject(mojo, "contractsRepositoryUrl", server.baseUrl()); |
| 81 | + } |
| 82 | + |
| 83 | + Path repositoryRoot() { |
| 84 | + return Path.of("src/test/resources/m2repo/repository").toAbsolutePath(); |
| 85 | + } |
| 86 | + |
| 87 | + private void configureRepositorySystem(AbstractMojo mojo) throws Exception { |
| 88 | + RepositorySystem repoSystem = new RepositorySystemSupplier().get(); |
| 89 | + DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession(); |
| 90 | + LocalRepository localRepo = new LocalRepository(this.targetDir.resolve("local-repo").toFile()); |
| 91 | + repoSession.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(repoSession, localRepo)); |
| 92 | + repoSession.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(true, true)); |
| 93 | + MojoExtension.setVariableValueToObject(mojo, "repositorySystem", repoSystem); |
| 94 | + MojoExtension.setVariableValueToObject(mojo, "repoSession", repoSession); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments