Skip to content

Commit

Permalink
IJMP-1522 Fixes for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Katsiaryna Tsytsenia <[email protected]>
  • Loading branch information
Katsiaryna Tsytsenia authored and Katsiaryna Tsytsenia committed May 10, 2024
1 parent d57d1db commit 1c388cc
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 79 deletions.
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ tasks {

test {
useJUnitPlatform()

jvmArgs("--add-opens", "java.desktop/java.awt=ALL-UNNAMED")
jvmArgs("--add-opens", "java.desktop/sun.awt=ALL-UNNAMED")
jvmArgs("--add-opens", "java.desktop/java.awt.event=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
jvmArgs("--add-exports", "java.base/jdk.internal.vm=ALL-UNNAMED")
jvmArgs("--add-opens", "java.base/java.nio.file=ALL-UNNAMED")

testLogging {
events("passed", "skipped", "failed")
}
Expand Down
218 changes: 139 additions & 79 deletions src/test/kotlin/org/zowe/explorer/config/ZoweConfigTestSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,41 @@

package org.zowe.explorer.config

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import io.kotest.matchers.shouldBe
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.spyk
import io.mockk.unmockkAll
import io.mockk.*
import org.zowe.explorer.config.connect.ConnectionConfig
import org.zowe.explorer.config.connect.ui.zosmf.ConnectionDialogState
import org.zowe.explorer.config.connect.whoAmI
import org.zowe.explorer.config.ws.FilesWorkingSetConfig
import org.zowe.explorer.config.ws.JesWorkingSetConfig
import org.zowe.explorer.dataops.DataOpsManager
import org.zowe.explorer.dataops.Operation
import org.zowe.explorer.dataops.operations.InfoOperation
import org.zowe.explorer.dataops.operations.ZOSInfoOperation
import org.zowe.explorer.explorer.Explorer
import org.zowe.explorer.explorer.WorkingSet
import org.zowe.explorer.testutils.WithApplicationShouldSpec
import org.zowe.explorer.testutils.testServiceImpl.TestDataOpsManagerImpl
import org.zowe.explorer.utils.crudable.Crudable
import org.zowe.explorer.utils.crudable.getAll
import org.zowe.explorer.utils.service
import org.zowe.explorer.zowe.ZOWE_CONFIG_NAME
import org.zowe.explorer.zowe.service.ZoweConfigServiceImpl
import org.zowe.kotlinsdk.*
import org.zowe.kotlinsdk.annotations.ZVersion
import org.zowe.kotlinsdk.zowe.config.KeytarWrapper
import org.zowe.kotlinsdk.zowe.config.ZoweConfig
import org.zowe.kotlinsdk.zowe.config.parseConfigJson
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.OpenOption
import java.nio.file.Path
import java.nio.file.*
import java.util.*
import java.util.stream.Stream
import kotlin.reflect.KFunction

Expand All @@ -55,27 +67,47 @@ class ZoweConfigTestSpec : WithApplicationShouldSpec({
var isFilesWriteTriggered = false
var isRunWriteActionCalled = false
var isSaveNewSecurePropertiesCalled = false
var isFindFileByNioPathCalled = false
var isInputStreamCalled = false
var isReturnedZoweConfig = false
var isAddOrUpdateConnectionCalled = false
var isZOSInfoCalled = false
var isScanForZoweConfigCalled = false
var isConnectionDeleted = false

val mockedProject = mockk<Project>(relaxed = true)
every { mockedProject.basePath } returns "test"
every { mockedProject.name } returns "testProj"

val connectionId = "000000000000"
val connection = ConnectionConfig(
"ID$connectionId",
connectionId,
"URL$connectionId",
true,
ZVersion.ZOS_2_4,
zoweConfigPath = "/zowe/config/path"
)
val crudableMockk = mockk<Crudable>()
every { crudableMockk.getAll<ConnectionConfig>() } returns Stream.of()
every { crudableMockk.getAll<FilesWorkingSetConfig>() } returns Stream.of()
every { crudableMockk.getAll<JesWorkingSetConfig>() } returns Stream.of()
mockkObject(ConfigService)
every { ConfigService.instance.crudable } returns crudableMockk

val filesWrite: (Path, ByteArray, Array<out OpenOption>) -> Path = Files::write
mockkStatic(filesWrite as KFunction<*>)
every {
filesWrite(any<Path>(), any<ByteArray>(), anyVararg())
} answers {
isFilesWriteTriggered = true
Path.of("")
every { crudableMockk.find<ConnectionConfig>(any(), any()) } answers {
listOf(connection).stream()
}
every { crudableMockk.delete(any()) } answers {
isConnectionDeleted = true
Optional.of(ConnectionConfig())
}
every { crudableMockk.addOrUpdate(any<ConnectionConfig>()) } answers {
isAddOrUpdateConnectionCalled = true
Optional.of(ConnectionConfig())
}

val mockedZoweConfig = spyk(ZoweConfigServiceImpl(mockedProject), recordPrivateCalls = true)
every { mockedZoweConfig["createZoweSchemaJsonIfNotExists"]() } returns Unit
val mockedZoweConfigService = spyk(ZoweConfigServiceImpl(mockedProject), recordPrivateCalls = true)
every { mockedZoweConfigService["createZoweSchemaJsonIfNotExists"]() } returns Unit

val mockedZoweConfigInputStream = mockk<InputStream>()
every { mockedZoweConfigInputStream.readAllBytes() } returns "<HOST>:<PORT>;SSL".toByteArray()
Expand Down Expand Up @@ -114,73 +146,101 @@ class ZoweConfigTestSpec : WithApplicationShouldSpec({
isFilesWriteTriggered = false
isRunWriteActionCalled = false
isSaveNewSecurePropertiesCalled = false

isFindFileByNioPathCalled = false
isInputStreamCalled = false
isReturnedZoweConfig = false
isAddOrUpdateConnectionCalled = false
isZOSInfoCalled = false
isScanForZoweConfigCalled = false

isConnectionDeleted = false
}

should("add zowe team config file") {
mockedZoweConfig.addZoweConfigFile(connectionDialogState)
val vfMock = mockk<VirtualFile>()
val isMock = mockk<InputStream>()
val vfmMock: VirtualFileManager = mockk<VirtualFileManager>()
mockkStatic(VirtualFileManager::class)
every { VirtualFileManager.getInstance() } returns vfmMock
every { vfmMock.findFileByNioPath(any<Path>()) } answers {
isFindFileByNioPathCalled = true
vfMock
}
every { vfMock.path } returns "/zowe/file/path"
every { vfMock.inputStream } answers {
isInputStreamCalled = true
isMock
}
every { isMock.close() } just Runs
val zoweConfigMock = mockk<ZoweConfig>()
every { zoweConfigMock.extractSecureProperties(any<Array<String>>(), any<KeytarWrapper>()) } answers {
isScanForZoweConfigCalled = true
Unit
}
every { zoweConfigMock.user } returns "ZoweUserName"
every { zoweConfigMock.password } returns "ZoweUserPass"
every { zoweConfigMock.basePath } returns "/base/config/path"
every { zoweConfigMock.port } returns 555
every { zoweConfigMock.host } returns "111.111.111.111"
every { zoweConfigMock.protocol } returns "https"
every { zoweConfigMock.rejectUnauthorized } returns false

val parseConfigJsonFun: (InputStream) -> ZoweConfig = ::parseConfigJson
mockkStatic(parseConfigJsonFun as KFunction<*>)
every { parseConfigJson(any<InputStream>()) } answers {
isReturnedZoweConfig = true
zoweConfigMock
}

val explorerMock = mockk<Explorer<ConnectionConfig, WorkingSet<ConnectionConfig, *>>>()
every { explorerMock.componentManager } returns ApplicationManager.getApplication()
val dataOpsManagerService = ApplicationManager.getApplication().service<DataOpsManager>() as TestDataOpsManagerImpl
dataOpsManagerService.testInstance = object : TestDataOpsManagerImpl(explorerMock.componentManager) {
override fun <R : Any> performOperation(operation: Operation<R>, progressIndicator: ProgressIndicator): R {
if (operation is InfoOperation) {
@Suppress("UNCHECKED_CAST")
return SystemsResponse(numRows = 1) as R
}
if (operation is ZOSInfoOperation) {
isZOSInfoCalled = true
@Suppress("UNCHECKED_CAST")
return InfoResponse(zosVersion = "04.27.00") as R
}
@Suppress("UNCHECKED_CAST")
return InfoResponse() as R
}
}

mockkStatic(::whoAmI as KFunction<*>)
every { whoAmI(any<ConnectionConfig>()) } returns "USERID"

should("add zowe team config file") {
mockkStatic(Files::class) {
every { Files.write(any<Path>(), any<ByteArray>()) } answers {
isFilesWriteTriggered = true
Path.of("")
}
mockedZoweConfigService.addZoweConfigFile(connectionDialogState)
}
isFilesWriteTriggered shouldBe true
isRunWriteActionCalled shouldBe true
isSaveNewSecurePropertiesCalled shouldBe true
}

// should("get zowe team config state") {
//
// val run1 = zoweConfigService.getZoweConfigState(false)
// run1 shouldBeEqual ZoweConfigState.NOT_EXISTS
//
// val run2 = zoweConfigService.getZoweConfigState()
// run2 shouldBeEqual ZoweConfigState.NEED_TO_ADD
//
// crudableInst.addOrUpdate(zoweConnConf)
//
// val run3 = zoweConfigService.getZoweConfigState()
// run3 shouldBeEqual ZoweConfigState.NEED_TO_UPDATE
//
//// confMap[winTmpZoweConfFile]?.set("profiles.base.properties.password", "testPassword")
//
// val run4 = zoweConfigService.getZoweConfigState()
// run4 shouldBeEqual ZoweConfigState.SYNCHRONIZED
//
// }
// val zoweConfig = zoweConfigService.zoweConfig
// val host = zoweConfig?.host
// val port = zoweConfig?.port
//
// should("add or update zowe team config connection") {
//
// zoweConnConf.url = "222.222.222.222:666"
// crudableInst.addOrUpdate(zoweConnConf)
// crudableInst.getAll<ConnectionConfig>().toList()
// .filter { it.name == zoweConnConf.name }[0].url shouldBeEqual zoweConnConf.url
//
// zoweConfigService.addOrUpdateZoweConfig(scanProject = true, checkConnection = false)
//
// crudableInst.getAll<ConnectionConfig>().toList()
// .filter { it.name == zoweConnConf.name }[0].url shouldBeEqual "https://$host:$port"
// }
//
// should("delete zowe team config connection") {
// var isDeleteMessageInDialogCalled = false
// val showDialogSpecificMock: (
// Project?, String, String, Array<String>, Int, Icon?
// ) -> Int = Messages::showDialog
// mockkStatic(showDialogSpecificMock as KFunction<*>)
// every {
// showDialogSpecificMock(any(), any<String>(), any<String>(), any<Array<String>>(), any<Int>(), any())
// } answers {
// isDeleteMessageInDialogCalled = true
// 1
// }
//
//// crudableInst.getAll<ConnectionConfig>().toList()
//// .filter { it.name == zoweConnConf.name }[0].url shouldBeEqual "https://$host:$port"
//
// zoweConfigService.deleteZoweConfig()
//
//// crudableInst.getAll<ConnectionConfig>().toList().filter { it.name == zoweConnConf.name }.size shouldBeEqual 0
//
// }
should("add or update zowe team config connection") {
mockedZoweConfigService.addOrUpdateZoweConfig(scanProject = true, checkConnection = true)
isFindFileByNioPathCalled shouldBe true
isInputStreamCalled shouldBe true
isReturnedZoweConfig shouldBe true
isScanForZoweConfigCalled shouldBe true
isAddOrUpdateConnectionCalled shouldBe true
isZOSInfoCalled shouldBe true
}

should("delete zowe team config connection") {
mockedZoweConfigService.deleteZoweConfig()
isConnectionDeleted shouldBe true
}

}

Expand Down

0 comments on commit 1c388cc

Please sign in to comment.