From df4ee7187839b2ef978486950e75c0832948b0f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zaj=C4=85czkowski?= Date: Thu, 3 Jan 2019 00:17:18 +0100 Subject: [PATCH] [#77] Ability to provide stagingRepositoryId E.g. by some external plugin which created it explicitly. --- .../e2e/BasicPublishSmokeE2ESpec.groovy | 10 ++-- .../functional/e2e/BasicSmokeE2ESpec.groovy | 2 + .../functional/e2e/ExploratoryE2ESpec.groovy | 2 +- .../gradle/nexus/BaseStagingTask.groovy | 60 ++++++++++++++++++- .../gradle/nexus/CloseRepositoryTask.groovy | 34 ++--------- .../gradle/nexus/GetStagingProfileTask.groovy | 6 +- .../gradle/nexus/NexusStagingExtension.groovy | 12 ++++ .../gradle/nexus/NexusStagingPlugin.groovy | 9 +-- .../gradle/nexus/ReleaseRepositoryTask.groovy | 25 ++------ .../nexus/logic/RepositoryFetcher.groovy | 12 +--- .../nexus/logic/OperationRetrierSpec.groovy | 24 ++++---- .../nexus/logic/RepositoryFetcherSpec.groovy | 24 ++++---- 12 files changed, 129 insertions(+), 91 deletions(-) diff --git a/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/BasicPublishSmokeE2ESpec.groovy b/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/BasicPublishSmokeE2ESpec.groovy index 3039d81..08ecc49 100644 --- a/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/BasicPublishSmokeE2ESpec.groovy +++ b/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/BasicPublishSmokeE2ESpec.groovy @@ -4,7 +4,7 @@ import io.codearte.gradle.nexus.functional.BaseNexusStagingFunctionalSpec import nebula.test.functional.ExecutionResult import spock.lang.Stepwise -//TODO: Temporary. Remove duplication with tests for 'maven' +//TODO: Remove duplication with tests for 'maven' - @Stepwise works with abstract tests in super class? @Stepwise class BasicPublishSmokeE2ESpec extends BaseNexusStagingFunctionalSpec implements E2ESpecHelperTrait { @@ -31,8 +31,6 @@ class BasicPublishSmokeE2ESpec extends BaseNexusStagingFunctionalSpec implements result.wasExecuted("publishToNexus") and: result.standardOutput.contains('to repository remote at https://oss.sonatype.org/service/local/staging/deployByRepositoryId/iogitlabnexus-at-') - and: - println result.standardOutput } def "should close and release repository"() { @@ -46,6 +44,10 @@ class BasicPublishSmokeE2ESpec extends BaseNexusStagingFunctionalSpec implements result.wasExecuted("closeAndReleaseRepository") and: result.standardOutput.contains('has been effectively released') - println result.standardOutput + + and: "reuse provided staging profile in both close and release" + result.standardOutput.contains("Reusing staging repository id: iogitlabnexus-at") +// //Uncomment once bumped nexus-publish-plugin dependency to version implementing https://github.com/marcphilipp/nexus-publish-plugin/issues/11 +// !result.standardOutput.contains("DEPRECATION WARNING. The staging repository ID is not provided.") } } diff --git a/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/BasicSmokeE2ESpec.groovy b/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/BasicSmokeE2ESpec.groovy index 4ac3da4..126f708 100644 --- a/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/BasicSmokeE2ESpec.groovy +++ b/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/BasicSmokeE2ESpec.groovy @@ -41,5 +41,7 @@ class BasicSmokeE2ESpec extends BaseNexusStagingFunctionalSpec implements E2ESpe result.wasExecuted("closeAndReleaseRepository") and: result.standardOutput.contains('has been effectively released') + and: + result.standardOutput.contains("Reusing staging repository id: iogitlabnexus-at") //at least in release } } diff --git a/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/ExploratoryE2ESpec.groovy b/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/ExploratoryE2ESpec.groovy index 7d3e679..1b5f562 100644 --- a/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/ExploratoryE2ESpec.groovy +++ b/src/funcTest/groovy/io/codearte/gradle/nexus/functional/e2e/ExploratoryE2ESpec.groovy @@ -76,7 +76,7 @@ class ExploratoryE2ESpec extends BaseNexusStagingFunctionalSpec implements E2ESp given: RepositoryFetcher fetcher = new RepositoryFetcher(client, E2E_SERVER_BASE_PATH) when: - String stagingRepositoryId = fetcher.getOpenRepositoryIdForStagingProfileId(E2E_STAGING_PROFILE_ID) + String stagingRepositoryId = fetcher.getRepositoryIdWithGivenStateForStagingProfileId(E2E_STAGING_PROFILE_ID, RepositoryState.OPEN) then: println stagingRepositoryId stagingRepositoryId == resolvedStagingRepositoryId diff --git a/src/main/groovy/io/codearte/gradle/nexus/BaseStagingTask.groovy b/src/main/groovy/io/codearte/gradle/nexus/BaseStagingTask.groovy index 16cda08..84f910a 100644 --- a/src/main/groovy/io/codearte/gradle/nexus/BaseStagingTask.groovy +++ b/src/main/groovy/io/codearte/gradle/nexus/BaseStagingTask.groovy @@ -8,13 +8,21 @@ import io.codearte.gradle.nexus.logic.OperationRetrier import io.codearte.gradle.nexus.logic.RepositoryCloser import io.codearte.gradle.nexus.logic.RepositoryFetcher import io.codearte.gradle.nexus.logic.RepositoryReleaser +import io.codearte.gradle.nexus.logic.RepositoryState import io.codearte.gradle.nexus.logic.RepositoryStateFetcher import io.codearte.gradle.nexus.logic.StagingProfileFetcher import org.gradle.api.DefaultTask +import org.gradle.api.Incubating +import org.gradle.api.Project +import org.gradle.api.model.ObjectFactory +import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.Optional +import javax.inject.Inject + @CompileStatic +@SuppressWarnings("UnstableApiUsage") abstract class BaseStagingTask extends DefaultTask { @Input @@ -44,12 +52,27 @@ abstract class BaseStagingTask extends DefaultTask { @Input String repositoryDescription + @Input + @Optional + @Incubating + final Property stagingRepositoryId + + final private NexusStagingExtension extension + + @Inject + BaseStagingTask(Project project, NexusStagingExtension extension) { + this.extension = extension + ObjectFactory objectFactory = project.getObjects(); + stagingRepositoryId = objectFactory.property(String) + stagingRepositoryId.set(extension.getStagingRepositoryId()) + } + @PackageScope SimplifiedHttpJsonRestClient createClient() { return new SimplifiedHttpJsonRestClient(new RESTClient(), getUsername(), getPassword()) } - protected StagingProfileFetcher createFetcherWithGivenClient(SimplifiedHttpJsonRestClient client) { + protected StagingProfileFetcher createProfileFetcherWithGivenClient(SimplifiedHttpJsonRestClient client) { return new StagingProfileFetcher(client, getServerUrl()) } @@ -73,15 +96,48 @@ abstract class BaseStagingTask extends DefaultTask { return new OperationRetrier(getNumberOfRetries(), getDelayBetweenRetriesInMillis()) } - protected String fetchAndCacheStagingProfileId(StagingProfileFetcher stagingProfileFetcher) { + protected String getConfiguredStagingProfileIdOrFindAndCacheOne(StagingProfileFetcher stagingProfileFetcher) { String configuredStagingProfileId = getStagingProfileId() if (configuredStagingProfileId != null) { logger.info("Using configured staging profile id: $configuredStagingProfileId") return configuredStagingProfileId } else { String receivedStagingProfileId = stagingProfileFetcher.getStagingProfileIdForPackageGroup(getPackageGroup()) + //TODO: Get from and set in plugin extension instead of in task directly setStagingProfileId(receivedStagingProfileId) return receivedStagingProfileId } } + + protected void savePassedRepositoryIdForReusingInInOtherTasks(String repositoryId) { + extension.stagingRepositoryId.set(repositoryId) + } + + protected String getConfiguredRepositoryIdForStagingProfileOrFindAndCacheOneInGivenState(String stagingProfileId, RepositoryState repositoryState) { + return tryToGetConfiguredRepositoryId().orElseGet { + String repositoryId = findOneRepositoryIdInGivenStateForStagingProfileIdWithRetrying(repositoryState, stagingProfileId, + createRepositoryFetcherWithGivenClient(createClient())) + savePassedRepositoryIdForReusingInInOtherTasks(repositoryId) + return repositoryId + } + } + + private java.util.Optional tryToGetConfiguredRepositoryId() { + //Provider doesn't not provide orElseGet() + if (getStagingRepositoryId().isPresent()) { + String reusedStagingRepositoryId = getStagingRepositoryId().get() + logger.info("Reusing staging repository id: $reusedStagingRepositoryId") + return java.util.Optional.of(reusedStagingRepositoryId) + } else { + return java.util.Optional.empty() + } + } + + private String findOneRepositoryIdInGivenStateForStagingProfileIdWithRetrying(RepositoryState repositoryState, String stagingProfileId, + RepositoryFetcher repositoryFetcher) { + logger.warn("DEPRECATION WARNING. The staging repository ID is not provided. The fallback mode may impact release reliability and is deprecated. " + + "Please consult the project FAQ how it can be fixed.") + OperationRetrier retrier = createOperationRetrier() + return retrier.doWithRetry { repositoryFetcher.getRepositoryIdWithGivenStateForStagingProfileId(stagingProfileId, repositoryState) } + } } diff --git a/src/main/groovy/io/codearte/gradle/nexus/CloseRepositoryTask.groovy b/src/main/groovy/io/codearte/gradle/nexus/CloseRepositoryTask.groovy index f2a6ccc..8126906 100644 --- a/src/main/groovy/io/codearte/gradle/nexus/CloseRepositoryTask.groovy +++ b/src/main/groovy/io/codearte/gradle/nexus/CloseRepositoryTask.groovy @@ -1,49 +1,27 @@ package io.codearte.gradle.nexus import groovy.transform.CompileStatic +import groovy.transform.InheritConstructors import io.codearte.gradle.nexus.logic.OperationRetrier import io.codearte.gradle.nexus.logic.RepositoryCloser -import io.codearte.gradle.nexus.logic.RepositoryFetcher import io.codearte.gradle.nexus.logic.RepositoryState import io.codearte.gradle.nexus.logic.RepositoryStateFetcher import io.codearte.gradle.nexus.logic.RetryingRepositoryTransitioner -import io.codearte.gradle.nexus.logic.StagingProfileFetcher -import org.gradle.api.tasks.Input -import org.gradle.api.tasks.Optional import org.gradle.api.tasks.TaskAction @CompileStatic +@InheritConstructors(constructorAnnotations = true) class CloseRepositoryTask extends BaseStagingTask { - @Input - @Optional - String stagingRepositoryId - + @SuppressWarnings("unused") @TaskAction - void doAction() { - StagingProfileFetcher stagingProfileFetcher = createFetcherWithGivenClient(createClient()) //Here or in fetchAndCacheStagingProfileId()? - RepositoryFetcher repositoryFetcher = createRepositoryFetcherWithGivenClient(createClient()) - - String stagingProfileId = fetchAndCacheStagingProfileId(stagingProfileFetcher) - - String repositoryId = findOneOpenRepositoryIdForStagingProfileIdWithRetrying(stagingProfileId, repositoryFetcher) - - memorizeRepositoryIdForReusingInInOtherTasks(repositoryId) + void closeRepository() { + String stagingProfileId = getConfiguredStagingProfileIdOrFindAndCacheOne(createProfileFetcherWithGivenClient(createClient())) + String repositoryId = getConfiguredRepositoryIdForStagingProfileOrFindAndCacheOneInGivenState(stagingProfileId, RepositoryState.OPEN) closeRepositoryByIdAndProfileIdWithRetrying(repositoryId, stagingProfileId) } - private String findOneOpenRepositoryIdForStagingProfileIdWithRetrying(String stagingProfileId, RepositoryFetcher repositoryFetcher) { - //TODO: Repository provided by Gradle upload mechanism should be used, but unfortunately it seems to be unsupported by Gradle. - // Therefore, check for just one repository in "open" state - OperationRetrier retrier = createOperationRetrier() - return retrier.doWithRetry { repositoryFetcher.getOpenRepositoryIdForStagingProfileId(stagingProfileId) } - } - - private void memorizeRepositoryIdForReusingInInOtherTasks(String repositoryId) { - stagingRepositoryId = repositoryId - } - private void closeRepositoryByIdAndProfileIdWithRetrying(String repositoryId, String stagingProfileId) { RepositoryCloser repositoryCloser = createRepositoryCloserWithGivenClient(createClient()) RepositoryStateFetcher repositoryStateFetcher = createRepositoryStateFetcherWithGivenClient(createClient()) diff --git a/src/main/groovy/io/codearte/gradle/nexus/GetStagingProfileTask.groovy b/src/main/groovy/io/codearte/gradle/nexus/GetStagingProfileTask.groovy index 324e95b..a39049b 100644 --- a/src/main/groovy/io/codearte/gradle/nexus/GetStagingProfileTask.groovy +++ b/src/main/groovy/io/codearte/gradle/nexus/GetStagingProfileTask.groovy @@ -1,13 +1,17 @@ package io.codearte.gradle.nexus +import groovy.transform.CompileStatic +import groovy.transform.InheritConstructors import io.codearte.gradle.nexus.logic.StagingProfileFetcher import org.gradle.api.tasks.TaskAction +@CompileStatic +@InheritConstructors(constructorAnnotations = true) class GetStagingProfileTask extends BaseStagingTask { @TaskAction void doAction() { - StagingProfileFetcher stagingProfileFetcher = createFetcherWithGivenClient(createClient()) + StagingProfileFetcher stagingProfileFetcher = createProfileFetcherWithGivenClient(createClient()) String receivedStagingProfileId = stagingProfileFetcher.getStagingProfileIdForPackageGroup(getPackageGroup()) logger.lifecycle("Received staging profile id: $receivedStagingProfileId") setStagingProfileId(receivedStagingProfileId) diff --git a/src/main/groovy/io/codearte/gradle/nexus/NexusStagingExtension.groovy b/src/main/groovy/io/codearte/gradle/nexus/NexusStagingExtension.groovy index c907b72..bfc5998 100644 --- a/src/main/groovy/io/codearte/gradle/nexus/NexusStagingExtension.groovy +++ b/src/main/groovy/io/codearte/gradle/nexus/NexusStagingExtension.groovy @@ -4,7 +4,11 @@ import groovy.transform.CompileStatic import groovy.transform.ToString import groovy.util.logging.Slf4j import org.gradle.api.Incubating +import org.gradle.api.Project +import org.gradle.api.model.ObjectFactory +import org.gradle.api.provider.Property +@SuppressWarnings("UnstableApiUsage") @CompileStatic @Slf4j @ToString(includeFields = true, includeNames = true, includePackage = false) @@ -18,4 +22,12 @@ class NexusStagingExtension { @Incubating Integer numberOfRetries @Incubating Integer delayBetweenRetriesInMillis @Incubating String repositoryDescription //since 0.10.0 + + @Incubating + final Property stagingRepositoryId //since 0.20.0 + + NexusStagingExtension(Project project) { + ObjectFactory objectFactory = project.getObjects() + stagingRepositoryId = objectFactory.property(String) + } } diff --git a/src/main/groovy/io/codearte/gradle/nexus/NexusStagingPlugin.groovy b/src/main/groovy/io/codearte/gradle/nexus/NexusStagingPlugin.groovy index 991c871..9180f2a 100644 --- a/src/main/groovy/io/codearte/gradle/nexus/NexusStagingPlugin.groovy +++ b/src/main/groovy/io/codearte/gradle/nexus/NexusStagingPlugin.groovy @@ -14,6 +14,7 @@ import org.gradle.api.tasks.Upload import java.lang.invoke.MethodHandles +@SuppressWarnings("UnstableApiUsage") class NexusStagingPlugin implements Plugin { private final static Logger log = Logging.getLogger(MethodHandles.lookup().lookupClass()) @@ -58,7 +59,7 @@ class NexusStagingPlugin implements Plugin { } private NexusStagingExtension createAndConfigureExtension(Project project) { - NexusStagingExtension extension = project.extensions.create("nexusStaging", NexusStagingExtension) + NexusStagingExtension extension = project.extensions.create("nexusStaging", NexusStagingExtension, project) extension.with { serverUrl = "https://oss.sonatype.org/service/local/" numberOfRetries = OperationRetrier.DEFAULT_NUMBER_OF_RETRIES @@ -69,20 +70,20 @@ class NexusStagingPlugin implements Plugin { } private void createAndConfigureGetStagingProfileTask(Project project) { - GetStagingProfileTask task = project.tasks.create(GET_STAGING_PROFILE_TASK_NAME, GetStagingProfileTask) + GetStagingProfileTask task = project.tasks.create(GET_STAGING_PROFILE_TASK_NAME, GetStagingProfileTask, project, extension) setTaskDescriptionAndGroup(task, "Gets a staging profile id in Nexus - a diagnostic task") setTaskDefaultsAndDescription(task) } private CloseRepositoryTask createAndConfigureCloseRepositoryTask(Project project) { - CloseRepositoryTask task = project.tasks.create(CLOSE_REPOSITORY_TASK_NAME, CloseRepositoryTask) + CloseRepositoryTask task = project.tasks.create(CLOSE_REPOSITORY_TASK_NAME, CloseRepositoryTask, project, extension) setTaskDescriptionAndGroup(task, "Closes an open artifacts repository in Nexus") setTaskDefaultsAndDescription(task) return task } private ReleaseRepositoryTask createAndConfigureReleaseRepositoryTask(Project project) { - ReleaseRepositoryTask task = project.tasks.create(RELEASE_REPOSITORY_TASK_NAME, ReleaseRepositoryTask) + ReleaseRepositoryTask task = project.tasks.create(RELEASE_REPOSITORY_TASK_NAME, ReleaseRepositoryTask, project, extension) setTaskDescriptionAndGroup(task, "Releases a closed artifacts repository in Nexus") setTaskDefaultsAndDescription(task) return task diff --git a/src/main/groovy/io/codearte/gradle/nexus/ReleaseRepositoryTask.groovy b/src/main/groovy/io/codearte/gradle/nexus/ReleaseRepositoryTask.groovy index 840f75d..ddc14cc 100644 --- a/src/main/groovy/io/codearte/gradle/nexus/ReleaseRepositoryTask.groovy +++ b/src/main/groovy/io/codearte/gradle/nexus/ReleaseRepositoryTask.groovy @@ -1,27 +1,25 @@ package io.codearte.gradle.nexus import groovy.transform.CompileStatic +import groovy.transform.InheritConstructors import io.codearte.gradle.nexus.logic.OperationRetrier -import io.codearte.gradle.nexus.logic.RepositoryFetcher import io.codearte.gradle.nexus.logic.RepositoryReleaser import io.codearte.gradle.nexus.logic.RepositoryState import io.codearte.gradle.nexus.logic.RepositoryStateFetcher import io.codearte.gradle.nexus.logic.RetryingRepositoryTransitioner -import io.codearte.gradle.nexus.logic.StagingProfileFetcher import org.gradle.api.tasks.TaskAction @CompileStatic +@InheritConstructors(constructorAnnotations = true) class ReleaseRepositoryTask extends BaseStagingTask { @TaskAction - void doAction() { - StagingProfileFetcher stagingProfileFetcher = createFetcherWithGivenClient(createClient()) - RepositoryFetcher repositoryFetcher = createRepositoryFetcherWithGivenClient(createClient()) - + void releaseRepository() { + //TODO: Remove once stagingProfileId migrated to plugin extension tryToTakeStagingProfileIdFromCloseRepositoryTask() - String stagingProfileId = fetchAndCacheStagingProfileId(stagingProfileFetcher) - String repositoryId = getRepositoryIdFromCloseTaskOrFromServer(stagingProfileId, repositoryFetcher) + String stagingProfileId = getConfiguredStagingProfileIdOrFindAndCacheOne(createProfileFetcherWithGivenClient(createClient())) + String repositoryId = getConfiguredRepositoryIdForStagingProfileOrFindAndCacheOneInGivenState(stagingProfileId, RepositoryState.CLOSED) releaseRepositoryByIdAndProfileIdWithRetrying(repositoryId, stagingProfileId) } @@ -42,17 +40,6 @@ class ReleaseRepositoryTask extends BaseStagingTask { return project.tasks.withType(CloseRepositoryTask)[0] } - private String getRepositoryIdFromCloseTaskOrFromServer(String stagingProfileId, RepositoryFetcher repositoryFetcher) { - String repositoryIdFromCloseTask = getCloseRepositoryTask().stagingRepositoryId - if (repositoryIdFromCloseTask != null) { - logger.debug("Reusing staging repository id from closeRepository task: $repositoryIdFromCloseTask") - return repositoryIdFromCloseTask - } - - OperationRetrier retrier = createOperationRetrier() - return retrier.doWithRetry { repositoryFetcher.getClosedRepositoryIdForStagingProfileId(stagingProfileId) } - } - private void releaseRepositoryByIdAndProfileIdWithRetrying(String repositoryId, String stagingProfileId) { RepositoryReleaser repositoryReleaser = createRepositoryReleaserWithGivenClient(createClient()) RepositoryStateFetcher repositoryStateFetcher = createRepositoryStateFetcherWithGivenClient(createClient()) diff --git a/src/main/groovy/io/codearte/gradle/nexus/logic/RepositoryFetcher.groovy b/src/main/groovy/io/codearte/gradle/nexus/logic/RepositoryFetcher.groovy index 90f014e..7db8ddc 100644 --- a/src/main/groovy/io/codearte/gradle/nexus/logic/RepositoryFetcher.groovy +++ b/src/main/groovy/io/codearte/gradle/nexus/logic/RepositoryFetcher.groovy @@ -10,18 +10,10 @@ import io.codearte.gradle.nexus.infra.WrongNumberOfRepositories @Slf4j class RepositoryFetcher extends BaseOperationExecutor { - String getOpenRepositoryIdForStagingProfileId(String stagingProfileId) { - return getRepositoryIdWithGivenStateForStagingProfileId(RepositoryState.OPEN.toString(), stagingProfileId) - } - - String getClosedRepositoryIdForStagingProfileId(String stagingProfileId) { - return getRepositoryIdWithGivenStateForStagingProfileId(RepositoryState.CLOSED.toString(), stagingProfileId) - } - - private String getRepositoryIdWithGivenStateForStagingProfileId(String state, String stagingProfileId) { + String getRepositoryIdWithGivenStateForStagingProfileId(String stagingProfileId, RepositoryState state) { log.info("Getting '$state' repository for staging profile '$stagingProfileId'") Map allStagingRepositoriesResponseAsMap = client.get(nexusUrl + "/staging/profile_repositories/$stagingProfileId") //TODO: Constant - return parseResponseAndGetRepositoryIdInGivenState(allStagingRepositoriesResponseAsMap, state) + return parseResponseAndGetRepositoryIdInGivenState(allStagingRepositoriesResponseAsMap, state.toString()) } private String parseResponseAndGetRepositoryIdInGivenState(Map allStagingRepositoriesResponseAsMap, String repositoryState) { diff --git a/src/test/groovy/io/codearte/gradle/nexus/logic/OperationRetrierSpec.groovy b/src/test/groovy/io/codearte/gradle/nexus/logic/OperationRetrierSpec.groovy index a3fdbdc..0fe7a70 100644 --- a/src/test/groovy/io/codearte/gradle/nexus/logic/OperationRetrierSpec.groovy +++ b/src/test/groovy/io/codearte/gradle/nexus/logic/OperationRetrierSpec.groovy @@ -6,6 +6,8 @@ import spock.lang.Specification class OperationRetrierSpec extends Specification { + private static final String TEST_PROFILE_ID = "profileId" + private OperationRetrier retrier void setup() { @@ -17,9 +19,11 @@ class OperationRetrierSpec extends Specification { RepositoryFetcher fetcherMock = Mock() int counter = 0 when: - String returnedValue = retrier.doWithRetry { fetcherMock.getClosedRepositoryIdForStagingProfileId("profileId") } + String returnedValue = retrier.doWithRetry { + fetcherMock.getRepositoryIdWithGivenStateForStagingProfileId(TEST_PROFILE_ID, RepositoryState.CLOSED) + } then: - 2 * fetcherMock.getClosedRepositoryIdForStagingProfileId(_) >> { + 2 * fetcherMock.getRepositoryIdWithGivenStateForStagingProfileId(TEST_PROFILE_ID, RepositoryState.CLOSED) >> { if (counter++ == 0) { throw exceptionToThrow } else { @@ -37,10 +41,10 @@ class OperationRetrierSpec extends Specification { given: RepositoryFetcher fetcherMock = Mock() when: - retrier.doWithRetry { fetcherMock.getClosedRepositoryIdForStagingProfileId("profileId") } + retrier.doWithRetry { fetcherMock.getRepositoryIdWithGivenStateForStagingProfileId(TEST_PROFILE_ID, RepositoryState.CLOSED) } then: - 3 * fetcherMock.getClosedRepositoryIdForStagingProfileId(_) >> { - throw new WrongNumberOfRepositories(0, "open") + 3 * fetcherMock.getRepositoryIdWithGivenStateForStagingProfileId(TEST_PROFILE_ID, RepositoryState.CLOSED) >> { + throw new WrongNumberOfRepositories(0, "closed") } and: thrown(WrongNumberOfRepositories) @@ -50,9 +54,9 @@ class OperationRetrierSpec extends Specification { given: RepositoryFetcher fetcherMock = Mock() when: - retrier.doWithRetry { fetcherMock.getClosedRepositoryIdForStagingProfileId("profileId") } + retrier.doWithRetry { fetcherMock.getRepositoryIdWithGivenStateForStagingProfileId(TEST_PROFILE_ID, RepositoryState.CLOSED) } then: - 1 * fetcherMock.getClosedRepositoryIdForStagingProfileId(_) >> { + 1 * fetcherMock.getRepositoryIdWithGivenStateForStagingProfileId(TEST_PROFILE_ID, RepositoryState.CLOSED) >> { throw new NullPointerException() } and: @@ -66,13 +70,13 @@ class OperationRetrierSpec extends Specification { def fetcherMock = Mock(RepositoryFetcher) int counter = 0 when: - String returnedValue = spiedRetrier.doWithRetry { fetcherMock.getClosedRepositoryIdForStagingProfileId("profileId") } + String returnedValue = spiedRetrier.doWithRetry { fetcherMock.getRepositoryIdWithGivenStateForStagingProfileId(TEST_PROFILE_ID, RepositoryState.CLOSED) } then: 1 * spiedRetrier.waitBeforeNextAttempt() >> { /* do nothing */ } and: - 2 * fetcherMock.getClosedRepositoryIdForStagingProfileId(_) >> { + 2 * fetcherMock.getRepositoryIdWithGivenStateForStagingProfileId(TEST_PROFILE_ID, RepositoryState.CLOSED) >> { if (counter++ == 0) { - throw new WrongNumberOfRepositories(0, "open") + throw new WrongNumberOfRepositories(0, "closed") } else { return "repoId" } diff --git a/src/test/groovy/io/codearte/gradle/nexus/logic/RepositoryFetcherSpec.groovy b/src/test/groovy/io/codearte/gradle/nexus/logic/RepositoryFetcherSpec.groovy index 609825b..72d58b7 100644 --- a/src/test/groovy/io/codearte/gradle/nexus/logic/RepositoryFetcherSpec.groovy +++ b/src/test/groovy/io/codearte/gradle/nexus/logic/RepositoryFetcherSpec.groovy @@ -20,7 +20,7 @@ class RepositoryFetcherSpec extends BaseOperationExecutorSpec implements Fetcher given: client.get(GET_REPOSITORY_ID_FULL_URL) >> { createResponseMapWithGivenRepos([anOpenRepo()]) } when: - String repositoryId = fetcher.getOpenRepositoryIdForStagingProfileId(TEST_STAGING_PROFILE_ID) + String repositoryId = fetcher.getRepositoryIdWithGivenStateForStagingProfileId(TEST_STAGING_PROFILE_ID, RepositoryState.OPEN) then: repositoryId == TEST_REPOSITORY_ID } @@ -29,7 +29,7 @@ class RepositoryFetcherSpec extends BaseOperationExecutorSpec implements Fetcher given: client.get(GET_REPOSITORY_ID_FULL_URL) >> { createResponseMapWithGivenRepos([aClosedRepo()]) } when: - String repositoryId = fetcher.getClosedRepositoryIdForStagingProfileId(TEST_STAGING_PROFILE_ID) + String repositoryId = fetcher.getRepositoryIdWithGivenStateForStagingProfileId(TEST_STAGING_PROFILE_ID, RepositoryState.CLOSED) then: repositoryId == TEST_REPOSITORY_ID } @@ -38,14 +38,14 @@ class RepositoryFetcherSpec extends BaseOperationExecutorSpec implements Fetcher given: client.get(GET_REPOSITORY_ID_FULL_URL) >> { createResponseMapWithGivenRepos([]) } when: - fetcher."get${state.capitalize()}RepositoryIdForStagingProfileId"(TEST_STAGING_PROFILE_ID) + fetcher.getRepositoryIdWithGivenStateForStagingProfileId(TEST_STAGING_PROFILE_ID, state) then: WrongNumberOfRepositories e = thrown() e.message == "Wrong number of received repositories in state '$state'. Expected 1, received 0".toString() e.numberOfRepositories == 0 - e.state == state + e.state == state.toString() where: - state << ["open", "closed"] + state << [RepositoryState.OPEN, RepositoryState.CLOSED] } def "should fail with meaningful exception on too many repositories in given state #state"() { @@ -55,7 +55,7 @@ class RepositoryFetcherSpec extends BaseOperationExecutorSpec implements Fetcher aRepoInStateAndId(TEST_REPOSITORY_ID + "2", state)]) } when: - fetcher."get${state.name().toLowerCase().capitalize()}RepositoryIdForStagingProfileId"(TEST_STAGING_PROFILE_ID) + fetcher.getRepositoryIdWithGivenStateForStagingProfileId(TEST_STAGING_PROFILE_ID, state) then: WrongNumberOfRepositories e = thrown() e.message == "Wrong number of received repositories in state '$state'. Expected 1, received 2".toString() @@ -69,14 +69,14 @@ class RepositoryFetcherSpec extends BaseOperationExecutorSpec implements Fetcher given: client.get(GET_REPOSITORY_ID_FULL_URL) >> { createResponseMapWithGivenRepos([aRepoInState(receivedState)]) } when: - fetcher."get${expectedState.capitalize()}RepositoryIdForStagingProfileId"(TEST_STAGING_PROFILE_ID) + fetcher.getRepositoryIdWithGivenStateForStagingProfileId(TEST_STAGING_PROFILE_ID, expectedState) then: WrongNumberOfRepositories e = thrown() e.message == "Wrong number of received repositories in state '$expectedState'. Expected 1, received 0".toString() where: - expectedState | receivedState - "open" | "closed" - "closed" | "open" + expectedState | receivedState + RepositoryState.OPEN | RepositoryState.CLOSED + RepositoryState.CLOSED | RepositoryState.OPEN } private Map anOpenRepo() { @@ -87,7 +87,7 @@ class RepositoryFetcherSpec extends BaseOperationExecutorSpec implements Fetcher return aRepoInStateAndId(TEST_REPOSITORY_ID, RepositoryState.CLOSED) } - private Map aRepoInState(String state) { - return aRepoInStateAndId(TEST_REPOSITORY_ID, RepositoryState.parseString(state)) + private Map aRepoInState(RepositoryState state) { + return aRepoInStateAndId(TEST_REPOSITORY_ID, state) } }