diff --git a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt new file mode 100644 index 0000000..438dced --- /dev/null +++ b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt @@ -0,0 +1,119 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright IBA Group 2024 + */ + +package org.zowe.zdevops.classic.steps + +import hudson.EnvVars +import hudson.FilePath +import io.kotest.assertions.assertSoftly +import io.kotest.core.spec.style.ShouldSpec +import io.kotest.engine.spec.tempdir +import io.kotest.matchers.shouldBe +import io.kotest.matchers.string.shouldContain +import io.mockk.every +import io.mockk.mockk +import io.mockk.spyk +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.zdevops.MOCK_SERVER_HOST +import org.zowe.zdevops.MockResponseDispatcher +import org.zowe.zdevops.MockServerFactory +import org.zowe.zdevops.declarative.jobs.PerformTsoCommandDeclarative +import java.io.File +import java.io.PrintStream +import java.nio.file.Paths + +class PerformTsoCommandDeclarativeSpec : ShouldSpec({ + lateinit var mockServer: MockWebServer + lateinit var responseDispatcher: MockResponseDispatcher + val mockServerFactory = MockServerFactory() + + beforeSpec { + mockServer = mockServerFactory.startMockServer(MOCK_SERVER_HOST) + responseDispatcher = mockServerFactory.responseDispatcher + } + afterSpec { + mockServerFactory.stopMockServer() + } + context("declarative/jobs module: PerformTsoCommandStep") { + val zosConnection = ZOSConnection(mockServer.hostName, mockServer.port.toString(), "test", "test", "https") + val trashDir = tempdir() + val trashDirWithInternal = Paths.get(trashDir.absolutePath, "test_name").toString() + val workspace = FilePath(File(trashDirWithInternal)) + val env = EnvVars() + + afterEach { + responseDispatcher.removeAllEndpoints() + } + should("perform PerformTsoCommandDeclarative operation and return its result") { + var isPreExecuteStage = false + var isCommandExecuted = false + val expectedTsoTimeCommandOutput = "IKJ56650I TIME-02:20:29 PM. CPU-00:00:00 SERVICE-448 SESSION-00:00:39 OCTOBER 4,2023" + val taskListener = object : TestBuildListener() { + override fun getLogger(): PrintStream { + val logger = mockk() + every { + logger.println(any()) + } answers { + if (firstArg().contains("Issuing command")) { + isPreExecuteStage = true + } else if (firstArg().contains("The command has been successfully executed")) { + isCommandExecuted = true + } + } + return logger + } + } + + + responseDispatcher.injectEndpoint( + this.testCase.name.testName, + { it?.requestLine?.contains("POST /zosmf/tsoApp/tso") ?: false }, + { MockResponse() + .setResponseCode(200) + .setBody(responseDispatcher.readMockJson("startTsoResponse") ?: "") } + ) + responseDispatcher.injectEndpoint( + this.testCase.name.testName, + { it?.requestLine?.contains("GET /zosmf/tsoApp/tso/") ?: false }, + { MockResponse() + .setResponseCode(200) + .setBody(responseDispatcher.readMockJson("getTsoResponse") ?: "") } + ) + responseDispatcher.injectEndpoint( + this.testCase.name.testName, + { it?.requestLine?.contains("PUT /zosmf/tsoApp/tso/") ?: false }, + { MockResponse() + .setResponseCode(200) + .setBody(responseDispatcher.readMockJson("sendTsoResponse") ?: "") } + ) + responseDispatcher.injectEndpoint( + this.testCase.name.testName, + { it?.requestLine?.contains("DELETE /zosmf/tsoApp/tso/") ?: false }, + { MockResponse() + .setResponseCode(200) + .setBody(responseDispatcher.readMockJson("endTsoResponse") ?: "") } + ) + + val performTsoCommandInst = spyk( + PerformTsoCommandDeclarative( + "test", + "TIME", + ) + ) + val tsoCommandResult = performTsoCommandInst.run(workspace, taskListener, env, zosConnection) + + assertSoftly { tsoCommandResult shouldContain expectedTsoTimeCommandOutput } + assertSoftly { isPreExecuteStage shouldBe true } + assertSoftly { isCommandExecuted shouldBe true } + } + } +}) \ No newline at end of file diff --git a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt index c7c5f3f..a21ced5 100644 --- a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt @@ -12,7 +12,6 @@ package org.zowe.zdevops.declarative.jobs import hudson.EnvVars import hudson.FilePath -import hudson.model.Item import io.kotest.assertions.assertSoftly import io.kotest.assertions.fail import io.kotest.core.spec.style.ShouldSpec @@ -43,17 +42,9 @@ class SubmitJobSyncStepDeclarativeSpec : ShouldSpec({ afterSpec { mockServerFactory.stopMockServer() } - context("declarative/jobs module: SubmitJobStep") { - val virtualChannel = TestVirtualChannel() + context("declarative/jobs module: SubmitJobStepSync") { val zosConnection = ZOSConnection(mockServer.hostName, mockServer.port.toString(), "test", "test", "https") val trashDir = tempdir() - val itemGroup = object : TestItemGroup() { - override fun getRootDirFor(child: Item?): File { - return trashDir - } - } - val job = TestJob(itemGroup, "test") - val run = TestRun(job) val trashDirWithInternal = Paths.get(trashDir.absolutePath, "test_name").toString() val workspace = FilePath(File(trashDirWithInternal)) val env = EnvVars() @@ -68,6 +59,7 @@ class SubmitJobSyncStepDeclarativeSpec : ShouldSpec({ var isJobFinished = false var isDownloadingExecutionLog = false var isNoSpoolLogs = false + val jobFinishedWellRC = "CC 0000" val taskListener = object : TestBuildListener() { override fun getLogger(): PrintStream { @@ -94,7 +86,6 @@ class SubmitJobSyncStepDeclarativeSpec : ShouldSpec({ return logger } } - val launcher = TestLauncher(taskListener, virtualChannel) responseDispatcher.injectEndpoint( "${this.testCase.name.testName}_submitJob", @@ -116,15 +107,10 @@ class SubmitJobSyncStepDeclarativeSpec : ShouldSpec({ val submitJobSyncStepDeclInst = spyk( SubmitJobSyncStepDeclarative("test") ) - submitJobSyncStepDeclInst.perform( - run, - workspace, - env, - launcher, - taskListener, - zosConnection - ) + val jobRC = submitJobSyncStepDeclInst.run(workspace, taskListener, env, zosConnection) + + assertSoftly { jobRC shouldBe jobFinishedWellRC } assertSoftly { isJobSubmitting shouldBe true } assertSoftly { isJobSubmitted shouldBe true } assertSoftly { isWaitingJobFinish shouldBe true } @@ -165,7 +151,6 @@ class SubmitJobSyncStepDeclarativeSpec : ShouldSpec({ return logger } } - val launcher = TestLauncher(taskListener, virtualChannel) responseDispatcher.injectEndpoint( "${this.testCase.name.testName}_submitJob", @@ -193,14 +178,7 @@ class SubmitJobSyncStepDeclarativeSpec : ShouldSpec({ val submitJobSyncStepDeclInst = spyk( SubmitJobSyncStepDeclarative("test") ) - submitJobSyncStepDeclInst.perform( - run, - workspace, - env, - launcher, - taskListener, - zosConnection - ) + submitJobSyncStepDeclInst.run(workspace, taskListener, env, zosConnection) assertSoftly { isJobSubmitting shouldBe true } assertSoftly { isJobSubmitted shouldBe true }