Skip to content

Commit

Permalink
IJMP-1841 Fixid updating config file if connection was deleted.
Browse files Browse the repository at this point in the history
Signed-off-by: Katsiaryna Tsytsenia <KTsytsenia@ibagroup.eu>
  • Loading branch information
Katsiaryna Tsytsenia authored and Katsiaryna Tsytsenia committed Oct 8, 2024
1 parent a1a6822 commit 3a291fa
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ class ZOSMFConnectionConfigurable : BoundSearchableConfigurable("z/OSMF Connecti
/** Delete selected connections from Connections table */
private fun removeSelectedConnections() {
val indices = connectionsTable?.selectedRows
val connToRemove =
indices?.map { connectionsTableModel?.get(it) }?.toSet()
connToRemove?.forEach {
if (it?.zoweConfigPath != null)
if (zoweConfigStates.contains(it?.connectionName))
zoweConfigStates.remove(it?.connectionName)
}
indices?.forEachIndexed { i, idx ->
connectionsTableModel?.removeRow(idx - i)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,34 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.ui.showOkCancelDialog
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.mockk.*
import org.zowe.explorer.common.ui.DialogMode
import org.zowe.explorer.common.ui.ValidatingTableView
import org.zowe.explorer.config.ConfigStateV2
import org.zowe.explorer.config.connect.ConnectionConfig
import org.zowe.explorer.config.makeCrudableWithoutListeners
import org.zowe.explorer.testutils.WithApplicationShouldSpec
import org.zowe.kotlinsdk.annotations.ZVersion
import org.zowe.kotlinsdk.zowe.config.DefaultKeytarWrapper
import org.zowe.kotlinsdk.zowe.config.KeytarWrapper
import org.zowe.kotlinsdk.zowe.config.ZoweConfig
import java.lang.reflect.Modifier
import java.nio.file.Path
import java.util.stream.Stream
import javax.swing.Icon
import kotlin.reflect.KFunction
import kotlin.reflect.full.declaredMemberFunctions
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.jvm.isAccessible

class ZOSMFConnectionConfigurableTest : WithApplicationShouldSpec({

val zOSMFConnectionConfigurableMock = spyk<ZOSMFConnectionConfigurable>()
val zOSMFConnectionConfigurableMock = spyk(ZOSMFConnectionConfigurable(), recordPrivateCalls = true)
var isShowOkCancelDialogCalled = false
var isFindFileByNioPathCalled = false
var isInputStreamCalled = false
Expand Down Expand Up @@ -246,6 +256,115 @@ class ZOSMFConnectionConfigurableTest : WithApplicationShouldSpec({
}
}

fun Any.mockPrivateFields(name: String, mocks: Any?): Any? {
javaClass.declaredFields
.filter { it.modifiers.and(Modifier.PRIVATE) > 0 || it.modifiers.and(Modifier.PROTECTED) > 0 }
.firstOrNull { it.name == name }
?.also { it.isAccessible = true }
?.set(this, mocks)
return this
}

val connectionConfig = ConnectionConfig(
uuid = "0000",
name = "zowe-local-zosmf/testProj",
url = "https://url.com",
isAllowSelfSigned = true,
zVersion = ZVersion.ZOS_2_1,
zoweConfigPath = "zowe/config/path",
owner = "owner"
)
state.connectionUrl = "https://testhost.com"
state.username = "testuser"
state.password = "testpass"
state.owner = "owner"
state.zoweConfigPath = "zowe/config/path"
state.mode = DialogMode.UPDATE
var crud = spyk(makeCrudableWithoutListeners(false) { ConfigStateV2() })
every { crud.getAll(ConnectionConfig::class.java) } returns Stream.of(connectionConfig)
every { crud.find(ConnectionConfig::class.java, any()) } returns Stream.of(connectionConfig)
var connTModel = ConnectionsTableModel(crud)
zOSMFConnectionConfigurableMock.mockPrivateFields("connectionsTableModel", connTModel)
var valTView = spyk(ValidatingTableView<ConnectionDialogState>(connTModel, Disposer.newDisposable()))
every { valTView.selectedRow } returns 0
every { valTView.selectedRows } returns intArrayOf(0)
zOSMFConnectionConfigurableMock.mockPrivateFields("connectionsTable", valTView)
every { zOSMFConnectionConfigurableMock["showAndTestConnection"](any<ConnectionDialogState>()) } returns state

should("editConnection/removeSelectedConnections") {
zOSMFConnectionConfigurableMock::class.declaredMemberFunctions.find { it.name == "editConnection" }
?.let {
it.isAccessible = true
it.call(zOSMFConnectionConfigurableMock)
}
zOSMFConnectionConfigurableMock::class.declaredMemberProperties.find { it.name == "zoweConfigStates" }
?.let {
it.isAccessible = true
(it.getter.call(zOSMFConnectionConfigurableMock) as HashMap<*, *>).size shouldBe 1
}
crud = spyk(makeCrudableWithoutListeners(false) { ConfigStateV2() })
every { crud.getAll(ConnectionConfig::class.java) } returns Stream.of(connectionConfig)
every { crud.find(ConnectionConfig::class.java, any()) } returns Stream.of(connectionConfig)
connTModel = ConnectionsTableModel(crud)
zOSMFConnectionConfigurableMock.mockPrivateFields("connectionsTableModel", connTModel)
zOSMFConnectionConfigurableMock::class.declaredMemberFunctions.find { it.name == "removeSelectedConnections" }
?.let {
it.isAccessible = true
it.call(zOSMFConnectionConfigurableMock)
}
zOSMFConnectionConfigurableMock::class.declaredMemberProperties.find { it.name == "zoweConfigStates" }
?.let {
it.isAccessible = true
(it.getter.call(zOSMFConnectionConfigurableMock) as HashMap<*, *>).size shouldBe 0
}
}

should("removeSelectedConnections null selectedRows") {
every { valTView.selectedRows } returns null
zOSMFConnectionConfigurableMock::class.declaredMemberFunctions.find { it.name == "removeSelectedConnections" }
?.let {
it.isAccessible = true
it.call(zOSMFConnectionConfigurableMock)
}
zOSMFConnectionConfigurableMock::class.declaredMemberProperties.find { it.name == "zoweConfigStates" }
?.let {
it.isAccessible = true
(it.getter.call(zOSMFConnectionConfigurableMock) as HashMap<*, *>).size shouldBe 0
}
}

should("removeSelectedConnections null connectionsTable") {
every { valTView.selectedRows } returns intArrayOf(0)
zOSMFConnectionConfigurableMock.mockPrivateFields("connectionsTable", null)
zOSMFConnectionConfigurableMock::class.declaredMemberFunctions.find { it.name == "removeSelectedConnections" }
?.let {
it.isAccessible = true
it.call(zOSMFConnectionConfigurableMock)
}
zOSMFConnectionConfigurableMock::class.declaredMemberProperties.find { it.name == "zoweConfigStates" }
?.let {
it.isAccessible = true
(it.getter.call(zOSMFConnectionConfigurableMock) as HashMap<*, *>).size shouldBe 0
}
zOSMFConnectionConfigurableMock.mockPrivateFields("connectionsTable", valTView)
}

should("removeSelectedConnections null connectionsTableModel") {
every { valTView.selectedRows } returns intArrayOf(0)
zOSMFConnectionConfigurableMock.mockPrivateFields("connectionsTableModel", null)
zOSMFConnectionConfigurableMock::class.declaredMemberFunctions.find { it.name == "removeSelectedConnections" }
?.let {
it.isAccessible = true
it.call(zOSMFConnectionConfigurableMock)
}
zOSMFConnectionConfigurableMock::class.declaredMemberProperties.find { it.name == "zoweConfigStates" }
?.let {
it.isAccessible = true
(it.getter.call(zOSMFConnectionConfigurableMock) as HashMap<*, *>).size shouldBe 0
}
zOSMFConnectionConfigurableMock.mockPrivateFields("connectionsTableModel", connTModel)
}

}
}
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import org.zowe.explorer.telemetry.NotificationsService
import org.zowe.explorer.testutils.testServiceImpl.*
import io.kotest.core.spec.Spec
import io.kotest.core.spec.style.ShouldSpec
import io.mockk.clearMocks
import io.mockk.*
import org.zowe.explorer.zowe.ZoweStartupActivity
import org.zowe.kotlinsdk.zowe.config.DefaultKeytarWrapper

private var appFixture: CodeInsightTestFixture? = null

Expand Down Expand Up @@ -74,5 +76,7 @@ abstract class WithApplicationShouldSpec(body: ShouldSpec.() -> Unit = {}) : Sho

init {
body()
mockkConstructor(ZoweStartupActivity::class)
every { anyConstructed<ZoweStartupActivity>().runActivity(any()) } just Runs
}
}

0 comments on commit 3a291fa

Please sign in to comment.