From 86fc0474cd01c3e637c8c8d4345bd098ae38606d Mon Sep 17 00:00:00 2001 From: Anatoli Kalbasin Date: Thu, 19 Sep 2024 15:27:37 +0200 Subject: [PATCH 1/8] add declarative method response handling Signed-off-by: Anatoli Kalbasin --- .../AbstractZosmfActionWithResult.kt | 109 ++++++++++++++++++ .../PerformTsoCommandWithResultDeclarative.kt | 50 ++++++++ .../SubmitJobStepWithResultDeclarative.kt | 50 ++++++++ .../logic/PerformTsoCommandOperation.kt | 9 +- .../zowe/zdevops/logic/SubmitJobOperation.kt | 4 +- 5 files changed, 217 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt create mode 100644 src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandWithResultDeclarative.kt create mode 100644 src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobStepWithResultDeclarative.kt diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt b/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt new file mode 100644 index 0000000..14de123 --- /dev/null +++ b/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt @@ -0,0 +1,109 @@ +/* + * 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.declarative + +import hudson.EnvVars +import hudson.FilePath +import hudson.model.Run +import hudson.model.TaskListener +import org.jenkinsci.plugins.workflow.steps.* +import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.zdevops.utils.getZoweZosConnection +import java.nio.charset.StandardCharsets + +/** + * Abstract class that represents an action that performs some work with a String result in a Jenkins pipeline. + * This class is designed to be extended by other classes that need to perform specific actions + * and return a result. In order to return the result, the method must be wrapped in `script` tag. + */ +abstract class AbstractZosmfActionWithResult : Step() { + + /** + * Executes the action using the provided context and returns the result as a String. + * + * @param workspace the workspace where the action is executed. + * @param listener the listener used to log messages during execution. + * @param envVars the environment variables that may influence the execution. + * @param zoweZOSConnection the connection to z/OSMF used for the action execution. + * @return the result of the action as a String. + */ + abstract fun run(workspace: FilePath, listener: TaskListener, envVars: EnvVars, zoweZOSConnection: ZOSConnection): String + + /** + * Starts the execution of this step. + * + * @param context the context in which the step is executed. + * @return a StepExecution instance that will manage the execution of this step. + */ + override fun start(context: StepContext): StepExecution { + return Execution(this, context) + } + + companion object { + /** + * Default descriptor for steps that extend AbstractZosmfActionWithResult. + * Provides metadata about the step for Jenkins, which in our case is a name for the method in declarative pipeline. + */ + open class DefaultStepDescriptor(private val functionName: String): StepDescriptor() { + + /** + * Provides the context to the Step. + */ + override fun getRequiredContext(): Set> { + return setOf>( + Run::class.java, + FilePath::class.java, + TaskListener::class.java, + EnvVars::class.java + ) + } + + /** + * The name for a declarative method returning result. + */ + override fun getFunctionName(): String = functionName + + /** + * We do not expect a block argument as an input for a declarative method returning result. + */ + override fun takesImplicitBlockArgument(): Boolean = false + } + + /** + * Synchronous step execution class for actions that extend AbstractZosmfActionWithResult. + * Manages the execution of the step and retrieves the necessary context information. + */ + class Execution(@Transient private val step: AbstractZosmfActionWithResult, context: StepContext) + : SynchronousNonBlockingStepExecution(context) { + /** + * Prepares everything for the actual step run. + */ + override fun run(): String { + val workspace: FilePath = getClassFromContext(context, FilePath::class.java) + val listener = getClassFromContext(context, TaskListener::class.java) + val env = getClassFromContext(context, EnvVars::class.java) + + val connectionName: String = workspace.read().readBytes().toString(StandardCharsets.UTF_8) + val zoweConnection = getZoweZosConnection(connectionName, listener) + + return step.run(workspace, listener, env, zoweConnection) + } + + /** + * Gets necessary context classes for the execution and ensures they are not empty. + */ + private fun getClassFromContext(context: StepContext, clazz: Class): T { + return context.get(clazz) ?: throw RuntimeException("Couldn't get ${clazz.simpleName}") + } + + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandWithResultDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandWithResultDeclarative.kt new file mode 100644 index 0000000..4084524 --- /dev/null +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandWithResultDeclarative.kt @@ -0,0 +1,50 @@ +/* + * 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.declarative.jobs + +import hudson.AbortException +import hudson.EnvVars +import hudson.Extension +import hudson.FilePath +import hudson.model.TaskListener +import org.kohsuke.stapler.DataBoundConstructor +import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.zdevops.declarative.AbstractZosmfActionWithResult +import org.zowe.zdevops.logic.performTsoCommand + +/** + * Class that represents an action to perform a TSO command with a result in a declarative pipeline. + * This class extends {@code AbstractZosmfActionWithResult} and is designed to execute a TSO command + * via Zowe z/OSMF and return the command's output. + * + * @param acct the TSO account number. + * @param command the TSO command to be executed. + */ +class PerformTsoCommandWithResultDeclarative +@DataBoundConstructor +constructor( + val acct: String, + val command: String, +) : AbstractZosmfActionWithResult() { + + override fun run( + workspace: FilePath, + listener: TaskListener, + envVars: EnvVars, + zoweZOSConnection: ZOSConnection + ): String { + return performTsoCommand(zoweZOSConnection, listener, acct, command) + ?: throw AbortException("TSO command execution returned an empty result") + } + + @Extension + class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "performTsoCommandWithResult") +} diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobStepWithResultDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobStepWithResultDeclarative.kt new file mode 100644 index 0000000..c8ef6f8 --- /dev/null +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobStepWithResultDeclarative.kt @@ -0,0 +1,50 @@ +/* + * 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.declarative.jobs + +import hudson.EnvVars +import hudson.Extension +import hudson.FilePath +import hudson.model.TaskListener +import org.kohsuke.stapler.DataBoundConstructor +import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.zdevops.declarative.AbstractZosmfActionWithResult +import org.zowe.zdevops.logic.submitJobSync + +/** + * Class that represents an action to submit a z/OS job and retrieve the return code in a declarative pipeline. + * This class extends {@code AbstractZosmfActionWithResult} and is designed to submit a job via Zowe z/OSMF + * and return the job's return code. + * + * @param fileToSubmit the path to the file containing the JCL to be submitted. + */ +class SubmitJobStepWithResultDeclarative +@DataBoundConstructor +constructor(val fileToSubmit: String) + : AbstractZosmfActionWithResult() { + + override fun run( + workspace: FilePath, + listener: TaskListener, + envVars: EnvVars, + zoweZOSConnection: ZOSConnection + ): String { + val workspacePath = FilePath(null, workspace.remote.replace(workspace.name,"")) + val linkBuilder: (String?, String, String) -> String = { buildUrl, jobName, jobId -> + "$buildUrl/execution/node/3/ws/${jobName}.${jobId}/*view*/" + } + return submitJobSync(fileToSubmit, zoweZOSConnection, + listener, workspacePath, envVars["BUILD_URL"], linkBuilder) + } + + @Extension + class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "submitJobSyncWithResult") +} diff --git a/src/main/kotlin/org/zowe/zdevops/logic/PerformTsoCommandOperation.kt b/src/main/kotlin/org/zowe/zdevops/logic/PerformTsoCommandOperation.kt index c6aeb07..31cf407 100644 --- a/src/main/kotlin/org/zowe/zdevops/logic/PerformTsoCommandOperation.kt +++ b/src/main/kotlin/org/zowe/zdevops/logic/PerformTsoCommandOperation.kt @@ -13,6 +13,7 @@ package org.zowe.zdevops.logic import hudson.AbortException import hudson.model.TaskListener import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.kotlinsdk.zowe.client.sdk.zostso.IssueResponse import org.zowe.kotlinsdk.zowe.client.sdk.zostso.IssueTso import org.zowe.kotlinsdk.zowe.client.sdk.zostso.input.StartTsoParams import org.zowe.zdevops.Messages @@ -26,7 +27,7 @@ import org.zowe.zdevops.Messages * @param listener The Jenkins build listener for logging and monitoring the execution. * @param acct The z/OS account number. * @param command The TSO command to be executed. - * + * @return the command output. * @throws AbortException if the TSO command execution fails, with the error message indicating * the reason for the failure. */ @@ -35,14 +36,16 @@ fun performTsoCommand( listener: TaskListener, acct: String, command: String, - ) { + ): String? { listener.logger.println(Messages.zdevops_issue_TSO_command(command)) + val tsoCommandResponse: IssueResponse try { - val tsoCommandResponse = IssueTso(zosConnection).issueTsoCommand(acct, command, StartTsoParams(), failOnPrompt = true) + tsoCommandResponse = IssueTso(zosConnection).issueTsoCommand(acct, command, StartTsoParams(), failOnPrompt = true) listener.logger.println(tsoCommandResponse.commandResponses) } catch (ex: Exception) { listener.logger.println(Messages.zdevops_TSO_command_fail()) throw ex } listener.logger.println(Messages.zdevops_TSO_command_success()) + return tsoCommandResponse.commandResponses } \ No newline at end of file diff --git a/src/main/kotlin/org/zowe/zdevops/logic/SubmitJobOperation.kt b/src/main/kotlin/org/zowe/zdevops/logic/SubmitJobOperation.kt index 420bec8..8fd19bd 100644 --- a/src/main/kotlin/org/zowe/zdevops/logic/SubmitJobOperation.kt +++ b/src/main/kotlin/org/zowe/zdevops/logic/SubmitJobOperation.kt @@ -71,7 +71,7 @@ fun submitJobSync( workspacePath: FilePath, buildUrl: String?, linkBuilder: (String?, String, String) -> String -): String? { +): String { val submitJobRsp = submitJob(fileToSubmit, zosConnection, listener) listener.logger.println(Messages.zdevops_declarative_ZOSJobs_submitted_waiting()) @@ -103,5 +103,5 @@ fun submitJobSync( listener.logger.println(Messages.zdevops_no_spool_files(submitJobRsp.jobid)) } - return finalResult?.returnedCode + return finalResult.returnedCode ?: throw AbortException("Couldn't get the job $jobId") } \ No newline at end of file From 157a6f722627a539280ebdb686c08521af501c66 Mon Sep 17 00:00:00 2001 From: Anatoli Kalbasin Date: Fri, 20 Sep 2024 16:27:23 +0200 Subject: [PATCH 2/8] replace original methods Signed-off-by: Anatoli Kalbasin --- .../jobs/PerformTsoCommandDeclarative.kt | 64 ++++++------------- .../PerformTsoCommandWithResultDeclarative.kt | 50 --------------- .../SubmitJobStepWithResultDeclarative.kt | 50 --------------- .../jobs/SubmitJobSyncStepDeclarative.kt | 49 +++++++------- 4 files changed, 47 insertions(+), 166 deletions(-) delete mode 100644 src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandWithResultDeclarative.kt delete mode 100644 src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobStepWithResultDeclarative.kt diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt index 060d546..ac3cfbd 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt @@ -5,70 +5,46 @@ * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2023 + * Copyright IBA Group 2024 */ package org.zowe.zdevops.declarative.jobs -import hudson.* -import hudson.model.Run +import hudson.AbortException +import hudson.EnvVars +import hudson.Extension +import hudson.FilePath import hudson.model.TaskListener -import org.jenkinsci.Symbol import org.kohsuke.stapler.DataBoundConstructor import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection -import org.zowe.zdevops.declarative.AbstractZosmfAction +import org.zowe.zdevops.declarative.AbstractZosmfActionWithResult import org.zowe.zdevops.logic.performTsoCommand /** - * A Jenkins Pipeline step for performing a TSO (Time Sharing Option) command on a z/OS system - * using the Declarative Pipeline syntax. + * Class that represents an action to perform a TSO command with a result in a declarative pipeline. + * This class extends {@code AbstractZosmfActionWithResult} and is designed to execute a TSO command + * via Zowe z/OSMF and return the command's output. * - * This step allows you to execute TSO commands on a z/OS system and provides integration with - * Jenkins Pipelines for mainframe automation. + * @param acct the TSO account number. + * @param command the TSO command to be executed. */ class PerformTsoCommandDeclarative -/** - * Data-bound constructor for the {@code PerformTsoCommandDeclarative} step. - * - * @param acct The z/OS account under which to run the TSO command. - * @param command The TSO command to be executed. - */ @DataBoundConstructor constructor( val acct: String, val command: String, -) : AbstractZosmfAction() { - - override val exceptionMessage: String = zMessages.zdevops_TSO_command_fail() +) : AbstractZosmfActionWithResult() { - /** - * Performs the TSO command execution step within a Jenkins Pipeline. - * - * @param run The current Jenkins build run. - * @param workspace The workspace where the build is executed. - * @param env The environment variables for the build. - * @param launcher The build launcher. - * @param listener The build listener. - * @param zosConnection The z/OS connection to execute the TSO command. - */ - override fun perform( - run: Run<*, *>, + override fun run( workspace: FilePath, - env: EnvVars, - launcher: Launcher, listener: TaskListener, - zosConnection: ZOSConnection - ) { - performTsoCommand(zosConnection, listener, acct, command) + envVars: EnvVars, + zoweZOSConnection: ZOSConnection + ): String { + return performTsoCommand(zoweZOSConnection, listener, acct, command) + ?: throw AbortException("TSO command execution returned an empty result") } - /** - * Descriptor for the {@code PerformTsoCommandDeclarative} step. - * - * This descriptor provides information about the step and makes it available for use - * within Jenkins Pipelines. - */ - @Symbol("performTsoCommand") @Extension - class DescriptorImpl : Companion.DefaultBuildDescriptor("Perform TSO command Declarative") -} \ No newline at end of file + class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "performTsoCommandWithResult") +} diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandWithResultDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandWithResultDeclarative.kt deleted file mode 100644 index 4084524..0000000 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandWithResultDeclarative.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.declarative.jobs - -import hudson.AbortException -import hudson.EnvVars -import hudson.Extension -import hudson.FilePath -import hudson.model.TaskListener -import org.kohsuke.stapler.DataBoundConstructor -import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection -import org.zowe.zdevops.declarative.AbstractZosmfActionWithResult -import org.zowe.zdevops.logic.performTsoCommand - -/** - * Class that represents an action to perform a TSO command with a result in a declarative pipeline. - * This class extends {@code AbstractZosmfActionWithResult} and is designed to execute a TSO command - * via Zowe z/OSMF and return the command's output. - * - * @param acct the TSO account number. - * @param command the TSO command to be executed. - */ -class PerformTsoCommandWithResultDeclarative -@DataBoundConstructor -constructor( - val acct: String, - val command: String, -) : AbstractZosmfActionWithResult() { - - override fun run( - workspace: FilePath, - listener: TaskListener, - envVars: EnvVars, - zoweZOSConnection: ZOSConnection - ): String { - return performTsoCommand(zoweZOSConnection, listener, acct, command) - ?: throw AbortException("TSO command execution returned an empty result") - } - - @Extension - class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "performTsoCommandWithResult") -} diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobStepWithResultDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobStepWithResultDeclarative.kt deleted file mode 100644 index c8ef6f8..0000000 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobStepWithResultDeclarative.kt +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.declarative.jobs - -import hudson.EnvVars -import hudson.Extension -import hudson.FilePath -import hudson.model.TaskListener -import org.kohsuke.stapler.DataBoundConstructor -import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection -import org.zowe.zdevops.declarative.AbstractZosmfActionWithResult -import org.zowe.zdevops.logic.submitJobSync - -/** - * Class that represents an action to submit a z/OS job and retrieve the return code in a declarative pipeline. - * This class extends {@code AbstractZosmfActionWithResult} and is designed to submit a job via Zowe z/OSMF - * and return the job's return code. - * - * @param fileToSubmit the path to the file containing the JCL to be submitted. - */ -class SubmitJobStepWithResultDeclarative -@DataBoundConstructor -constructor(val fileToSubmit: String) - : AbstractZosmfActionWithResult() { - - override fun run( - workspace: FilePath, - listener: TaskListener, - envVars: EnvVars, - zoweZOSConnection: ZOSConnection - ): String { - val workspacePath = FilePath(null, workspace.remote.replace(workspace.name,"")) - val linkBuilder: (String?, String, String) -> String = { buildUrl, jobName, jobId -> - "$buildUrl/execution/node/3/ws/${jobName}.${jobId}/*view*/" - } - return submitJobSync(fileToSubmit, zoweZOSConnection, - listener, workspacePath, envVars["BUILD_URL"], linkBuilder) - } - - @Extension - class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "submitJobSyncWithResult") -} diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt index 9fc9df4..3420092 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt @@ -5,41 +5,46 @@ * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2022 + * Copyright IBA Group 2024 */ package org.zowe.zdevops.declarative.jobs -import hudson.* -import hudson.model.Run +import hudson.EnvVars +import hudson.Extension +import hudson.FilePath import hudson.model.TaskListener -import org.jenkinsci.Symbol import org.kohsuke.stapler.DataBoundConstructor import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection -import org.zowe.zdevops.declarative.AbstractZosmfAction +import org.zowe.zdevops.declarative.AbstractZosmfActionWithResult import org.zowe.zdevops.logic.submitJobSync -class SubmitJobSyncStepDeclarative @DataBoundConstructor constructor(private val fileToSubmit: String): - AbstractZosmfAction() { - - override val exceptionMessage: String = zMessages.zdevops_declarative_ZOSJobs_submitted_fail(fileToSubmit) +/** + * Class that represents an action to submit a z/OS job and retrieve the return code in a declarative pipeline. + * This class extends {@code AbstractZosmfActionWithResult} and is designed to submit a job via Zowe z/OSMF + * and return the job's return code. + * + * @param fileToSubmit the path to the file containing the JCL to be submitted. + */ +class SubmitJobSyncStepDeclarative +@DataBoundConstructor +constructor(val fileToSubmit: String) + : AbstractZosmfActionWithResult() { - override fun perform( - run: Run<*, *>, + override fun run( workspace: FilePath, - env: EnvVars, - launcher: Launcher, listener: TaskListener, - zosConnection: ZOSConnection - ) { - val workspacePath = FilePath(null, workspace.remote.replace(workspace.name,"")) - val linkBuilder: (String?, String, String) -> String = { buildUrl, jobName, jobId -> - "$buildUrl/execution/node/3/ws/${jobName}.${jobId}/*view*/" - } - submitJobSync(fileToSubmit, zosConnection, listener, workspacePath, env["BUILD_URL"], linkBuilder) + envVars: EnvVars, + zoweZOSConnection: ZOSConnection + ): String { + val workspacePath = FilePath(null, workspace.remote.replace(workspace.name,"")) + val linkBuilder: (String?, String, String) -> String = { buildUrl, jobName, jobId -> + "$buildUrl/execution/node/3/ws/${jobName}.${jobId}/*view*/" + } + return submitJobSync(fileToSubmit, zoweZOSConnection, + listener, workspacePath, envVars["BUILD_URL"], linkBuilder) } - @Symbol("submitJobSync") @Extension - class DescriptorImpl : Companion.DefaultBuildDescriptor("Submit Job Synchronously Declarative") + class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "submitJobSyncWithResult") } From 74ddbf15cb41493400591f2f0b2793b282347730 Mon Sep 17 00:00:00 2001 From: Anatoli Kalbasin Date: Fri, 20 Sep 2024 16:28:32 +0200 Subject: [PATCH 3/8] add tests Signed-off-by: Anatoli Kalbasin --- .../jobs/PerformTsoCommandDeclarativeSpec.kt | 119 ++++++++++++++++++ .../jobs/SubmitJobSyncStepDeclarativeSpec.kt | 34 +---- 2 files changed, 125 insertions(+), 28 deletions(-) create mode 100644 src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt 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 } From 265604ff3d172244a11cff277f6171e5280956cb Mon Sep 17 00:00:00 2001 From: Anatoli Kalbasin Date: Tue, 24 Sep 2024 16:41:59 +0200 Subject: [PATCH 4/8] update license Signed-off-by: Anatoli Kalbasin --- .../declarative/jobs/PerformTsoCommandDeclarative.kt | 6 +++++- .../declarative/jobs/SubmitJobSyncStepDeclarative.kt | 6 +++++- .../declarative/jobs/PerformTsoCommandDeclarativeSpec.kt | 6 +++++- .../declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt | 6 +++++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt index ac3cfbd..e8fe02c 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.jobs diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt index 3420092..3086399 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.jobs diff --git a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt index 438dced..df0bbf1 100644 --- a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.classic.steps 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 a21ced5..a110efe 100644 --- a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 2022 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.jobs From 23c06c1e744ec083ee332609f10b3d5833ebe1b2 Mon Sep 17 00:00:00 2001 From: Anatoli Kalbasin Date: Tue, 24 Sep 2024 16:41:59 +0200 Subject: [PATCH 5/8] update license Signed-off-by: Anatoli Kalbasin --- .../zdevops/declarative/AbstractZosmfActionWithResult.kt | 6 +++++- .../declarative/jobs/PerformTsoCommandDeclarative.kt | 6 +++++- .../declarative/jobs/SubmitJobSyncStepDeclarative.kt | 6 +++++- .../declarative/jobs/PerformTsoCommandDeclarativeSpec.kt | 6 +++++- .../declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt | 6 +++++- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt b/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt index 14de123..4dc44c7 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt index ac3cfbd..e8fe02c 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.jobs diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt index 3420092..3086399 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarative.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.jobs diff --git a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt index 438dced..df0bbf1 100644 --- a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarativeSpec.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.classic.steps 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 a21ced5..a110efe 100644 --- a/src/test/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/declarative/jobs/SubmitJobSyncStepDeclarativeSpec.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2024 IBA Group. + * * 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 2022 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.jobs From a3f931d84dab13082a99a8721a9d635c05686c3e Mon Sep 17 00:00:00 2001 From: Uladzislau Date: Wed, 25 Sep 2024 18:57:01 +0200 Subject: [PATCH 6/8] IJMP-1940 Testcases added Signed-off-by: Uladzislau --- .../declarative/ZosmfStepDeclarative.kt | 3 +- .../AbstractZosmfActionWithResultSpec.kt | 79 +++++++++++++++++++ .../declarative/ZosmfStepDeclarativeSpec.kt | 53 +++++++++++++ .../kotlin/org/zowe/zdevops/testutils/misc.kt | 51 ++++++++++++ 4 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResultSpec.kt create mode 100644 src/test/kotlin/org/zowe/zdevops/declarative/ZosmfStepDeclarativeSpec.kt create mode 100644 src/test/kotlin/org/zowe/zdevops/testutils/misc.kt diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/ZosmfStepDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/ZosmfStepDeclarative.kt index 75349ed..3a99a10 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/ZosmfStepDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/ZosmfStepDeclarative.kt @@ -23,11 +23,10 @@ import org.kohsuke.stapler.DataBoundConstructor import org.zowe.zdevops.utils.getZoweZosConnection import org.zowe.zdevops.utils.validateConnection - class ZosmfStepDeclarative @DataBoundConstructor constructor(private val connectionName: String) : Step() { override fun start(context: StepContext): StepExecution { val listener: TaskListener? = context.get(TaskListener::class.java) - val zosConnection = getZoweZosConnection(connectionName, listener) + val zosConnection = getZoweZosConnection(connectionName, listener) validateConnection(zosConnection) diff --git a/src/test/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResultSpec.kt b/src/test/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResultSpec.kt new file mode 100644 index 0000000..ef51a2a --- /dev/null +++ b/src/test/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResultSpec.kt @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 IBA Group. + * + * 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 + * + * Contributors: + * IBA Group + * Zowe Community + */ + +package org.zowe.zdevops.declarative + +import hudson.EnvVars +import hudson.FilePath +import hudson.model.TaskListener +import io.kotest.assertions.assertSoftly +import io.kotest.core.spec.style.ShouldSpec +import io.kotest.matchers.shouldBe +import io.mockk.* +import org.jenkinsci.plugins.workflow.steps.StepContext +import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution +import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.zdevops.testutils.getPrivateFieldValue +import org.zowe.zdevops.utils.getZoweZosConnection +import java.io.ByteArrayInputStream +import java.nio.charset.StandardCharsets +import java.util.concurrent.Future + +class AbstractZosmfActionWithResultSpec : ShouldSpec({ + var runCalledCount = 0 + + val zosmfActionWithResult = object : AbstractZosmfActionWithResult() { + override fun run( + workspace: FilePath, + listener: TaskListener, + envVars: EnvVars, + zoweZOSConnection: ZOSConnection + ): String { + runCalledCount++ + return "test" + } + } + + afterEach { + runCalledCount = 0 + } + + context("declarative module: AbstractZosmfActionWithResult") { + should("check execution runs the step provided when the task is started and finished successfully") { + val workspace = mockk() + every { workspace.read() } returns ByteArrayInputStream("test".toByteArray(StandardCharsets.UTF_8)) + + val stepContext = mockk() + every { stepContext.get(FilePath::class.java) } returns workspace + every { stepContext.get(TaskListener::class.java) } returns mockk() + every { stepContext.get(EnvVars::class.java) } returns mockk() + every { stepContext.onSuccess(any()) } just Runs + + mockkStatic(::getZoweZosConnection) + every { getZoweZosConnection("test", any()) } returns mockk() + + val execution = zosmfActionWithResult.start(stepContext) as AbstractZosmfActionWithResult.Companion.Execution + execution.start() + val executionTask: Future<*> = getPrivateFieldValue( + execution, + SynchronousNonBlockingStepExecution::class.java, + "task" + ) as Future<*> + executionTask.get() + + verify(exactly = 1) { stepContext.onSuccess(any()) } + assertSoftly { runCalledCount shouldBe 1 } + } + } +}) \ No newline at end of file diff --git a/src/test/kotlin/org/zowe/zdevops/declarative/ZosmfStepDeclarativeSpec.kt b/src/test/kotlin/org/zowe/zdevops/declarative/ZosmfStepDeclarativeSpec.kt new file mode 100644 index 0000000..26a0b5b --- /dev/null +++ b/src/test/kotlin/org/zowe/zdevops/declarative/ZosmfStepDeclarativeSpec.kt @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2024 IBA Group. + * + * 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 + * + * Contributors: + * IBA Group + * Zowe Community + */ + +package org.zowe.zdevops.declarative + +import hudson.model.TaskListener +import io.kotest.core.spec.style.ShouldSpec +import io.mockk.* +import org.jenkinsci.plugins.workflow.steps.StepContext +import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.zdevops.config.ZOSConnectionList +import org.zowe.zdevops.model.ResolvedZOSConnection +import org.zowe.zdevops.utils.getZoweZosConnection +import org.zowe.zdevops.utils.validateConnection + +class ZosmfStepDeclarativeSpec : ShouldSpec({ + context("declarative module: ZosmfStepDeclarative") { + should("check the zosmf declarative step is run, connection is formed and validated") { + val zosmfStepDeclarative = ZosmfStepDeclarative("test") + + val taskListener = mockk() + + val stepContext = mockk() + every { stepContext.get(TaskListener::class.java) } returns taskListener + + val resolvedZosConnection = mockk() + every { resolvedZosConnection.url } returns "https://test.com:1234" + every { resolvedZosConnection.username } returns "test_user" + every { resolvedZosConnection.password } returns "test_pass" + + mockkObject(ZOSConnectionList) + every { ZOSConnectionList.resolve("test") } returns resolvedZosConnection + + mockkStatic(::validateConnection) + every { validateConnection(any()) } returns Unit + + zosmfStepDeclarative.start(stepContext) + verify(exactly = 1) { validateConnection(any()) } + verify(exactly = 1) { getZoweZosConnection("test", taskListener) } + } + } +}) diff --git a/src/test/kotlin/org/zowe/zdevops/testutils/misc.kt b/src/test/kotlin/org/zowe/zdevops/testutils/misc.kt new file mode 100644 index 0000000..1ec1013 --- /dev/null +++ b/src/test/kotlin/org/zowe/zdevops/testutils/misc.kt @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2024 IBA Group. + * + * 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 + * + * Contributors: + * IBA Group + * Zowe Community + */ + +package org.zowe.zdevops.testutils + +import java.lang.reflect.Modifier + +/** + * Mock private/protected field of the class for the object + * @param sourceObj the source object to mock the field for + * @param classWithTheField the class where the field is declared + * @param fieldName the field name to mock + * @param mockValue the mock value to set for the field + */ +fun setPrivateFieldValue(sourceObj: Any, classWithTheField: Class<*>, fieldName: String, mockValue: Any) { + return classWithTheField + .declaredFields + .filter { it.modifiers.and(Modifier.PRIVATE) > 0 || it.modifiers.and(Modifier.PROTECTED) > 0 } + .find { it.name == fieldName } + ?.also { it.isAccessible = true } + ?.set(sourceObj, mockValue) + ?: throw NoSuchFieldException("Field with name '$fieldName' is not found amongst private or protected fields") +} + +/** + * Get private/protected field of the class stored in the object + * @param sourceObj the source object to get the field from + * @param classWithTheField the class where the field is declared + * @param fieldName the field name to get value of + */ +fun getPrivateFieldValue(sourceObj: Any, classWithTheField: Class<*>, fieldName: String): Any { + val theField = classWithTheField + .declaredFields + .filter { it.modifiers.and(Modifier.PRIVATE) > 0 || it.modifiers.and(Modifier.PROTECTED) > 0 } + .find { it.name == fieldName } + ?.also { it.isAccessible = true } + ?: throw NoSuchFieldException("Field with name '$fieldName' is not found amongst private or protected fields") + return theField.get(sourceObj) + ?: throw Exception("Field with name '$fieldName' is not accessible") +} From b60192d58037b154a861abeec0e4e6f23a40d194 Mon Sep 17 00:00:00 2001 From: Anatoli Kalbasin Date: Thu, 26 Sep 2024 10:03:07 +0200 Subject: [PATCH 7/8] reindent the file Signed-off-by: Anatoli Kalbasin --- .../jobs/PerformTsoCommandDeclarative.kt | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt index e8fe02c..f81f8c3 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformTsoCommandDeclarative.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 IBA Group. + * Copyright (c) 2023-2024 IBA Group. * * 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 @@ -35,20 +35,20 @@ import org.zowe.zdevops.logic.performTsoCommand class PerformTsoCommandDeclarative @DataBoundConstructor constructor( - val acct: String, - val command: String, + val acct: String, + val command: String, ) : AbstractZosmfActionWithResult() { - override fun run( - workspace: FilePath, - listener: TaskListener, - envVars: EnvVars, - zoweZOSConnection: ZOSConnection - ): String { - return performTsoCommand(zoweZOSConnection, listener, acct, command) - ?: throw AbortException("TSO command execution returned an empty result") - } + override fun run( + workspace: FilePath, + listener: TaskListener, + envVars: EnvVars, + zoweZOSConnection: ZOSConnection + ): String { + return performTsoCommand(zoweZOSConnection, listener, acct, command) + ?: throw AbortException("TSO command execution returned an empty result") + } - @Extension - class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "performTsoCommandWithResult") + @Extension + class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "performTsoCommandWithResult") } From f8b7c69af9467f01e36d5ed43aed5e3c55e28161 Mon Sep 17 00:00:00 2001 From: Anatoli Kalbasin Date: Thu, 26 Sep 2024 10:04:34 +0200 Subject: [PATCH 8/8] update license Signed-off-by: Anatoli Kalbasin --- .../zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt b/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt index 4dc44c7..88c920d 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/AbstractZosmfActionWithResult.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 IBA Group. + * Copyright (c) 2024 IBA Group. * * 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