From 4d091b868bcbe9cda0716329e48cec843f772e2b Mon Sep 17 00:00:00 2001 From: Katsiaryna Tsytsenia Date: Wed, 13 Nov 2024 13:48:03 +0200 Subject: [PATCH] IJMP-1923 Fixed adding invalid connections from Zowe config file Signed-off-by: Katsiaryna Tsytsenia --- .../zowe/service/ZoweConfigServiceImpl.kt | 153 +++++++++--------- .../explorer/config/ZoweConfigTestSpec.kt | 24 +-- 2 files changed, 88 insertions(+), 89 deletions(-) diff --git a/src/main/kotlin/org/zowe/explorer/zowe/service/ZoweConfigServiceImpl.kt b/src/main/kotlin/org/zowe/explorer/zowe/service/ZoweConfigServiceImpl.kt index 04f0d277..64a359f5 100644 --- a/src/main/kotlin/org/zowe/explorer/zowe/service/ZoweConfigServiceImpl.kt +++ b/src/main/kotlin/org/zowe/explorer/zowe/service/ZoweConfigServiceImpl.kt @@ -189,8 +189,8 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService /** * Added notification about connection failure with action of force connection adding. - * @param title - notification title. - * @param content - notification content. + * @param title notification title. + * @param content notification content. * @return Nothing. */ private fun notifyUiOnConnectionFailure(title: String, content: String, type: ZoweConfigType) { @@ -258,27 +258,23 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService this.globalZoweConfig zoweConfig ?: throw Exception("Cannot get $type Zowe config") - var allPreparedConn = mutableListOf() - if (checkConnection) { - val failedURLs = mutableListOf() - allPreparedConn = testAndPrepareAllZosmfConnections(zoweConfig, type, failedURLs) - if (failedURLs.isNotEmpty()) { - val andMore = if (failedURLs.size > 3) - "..." - else - "" - notifyUiOnConnectionFailure( - "Connection failed to:", - "${failedURLs.joinToString(separator = ",

")} ${andMore}", - type - ) - return - } + val (allPreparedConn, failedConnections) = testAndPrepareAllZosmfConnections(zoweConfig, type) + if (checkConnection and failedConnections.isNotEmpty()) { + val andMore = if (failedConnections.size > 3) "..." else "" + notifyUiOnConnectionFailure( + "Connection failed to:", + "${failedConnections.map{it.url}.joinToString(separator = ",

")} $andMore", + type + ) } - for (zosmfConnection in allPreparedConn) { + val conToAdd = if (checkConnection) + allPreparedConn.subtract(failedConnections.toSet()) + else + allPreparedConn + conToAdd.forEach { zosmfConnection -> val connectionOpt = configCrudable.addOrUpdate(zosmfConnection) if (!connectionOpt.isEmpty) { - var topic = if (type == ZoweConfigType.LOCAL) + val topic = if (type == ZoweConfigType.LOCAL) LOCAL_ZOWE_CONFIG_CHANGED else GLOBAL_ZOWE_CONFIG_CHANGED @@ -300,9 +296,9 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService private fun prepareConnection(zosmfConnection: ZOSConnection, type: ZoweConfigType): ConnectionConfig { val username = zosmfConnection.user val password = zosmfConnection.password - val zoweConnection = findExistingConnection(type, zosmfConnection.profileName)?.let { - zosmfConnection.toConnectionConfig(it.uuid, it.zVersion, type = type) - } ?: zosmfConnection.toConnectionConfig(UUID.randomUUID().toString(), type = type) + val zoweConnection = findExistingConnection(type, zosmfConnection.profileName) + ?.let { zosmfConnection.toConnectionConfig(it.uuid, it.zVersion, type = type) } + ?: zosmfConnection.toConnectionConfig(UUID.randomUUID().toString(), type = type) CredentialService.getService().setCredentials(zoweConnection.uuid, username, password) return zoweConnection } @@ -311,24 +307,25 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService * Convert all zosmf connections from zowe config file to ConnectionConfig and tests them * @param zoweConfig * @param type of zowe config - * @return list of URLs which did not pass the test + * @return pair of lists, one is the connections list, the second is the list of URLs which did not pass the test */ private fun testAndPrepareAllZosmfConnections( zoweConfig: ZoweConfig, - type: ZoweConfigType, - failedURLs: MutableList - ): MutableList { - var allPreparedConn = mutableListOf() - for (zosmfConnection in zoweConfig.getListOfZosmfConections()) { - val zoweConnection = prepareConnection(zosmfConnection, type) - try { - testAndPrepareConnection(zoweConnection) - } catch (t: Throwable) { - failedURLs.add(zoweConnection.url) + type: ZoweConfigType + ): Pair, List> { + return zoweConfig.getListOfZosmfConections() + .fold( + mutableListOf() to mutableListOf() + ) { (allConnectionConfigs, failedURLs), zosmfConnection -> + val zoweConnection = prepareConnection(zosmfConnection, type) + try { + testAndPrepareConnection(zoweConnection) + } catch (t: Throwable) { + failedURLs.add(zoweConnection) + } + allConnectionConfigs.add(zoweConnection) + allConnectionConfigs to failedURLs } - allPreparedConn.add(zoweConnection) - } - return allPreparedConn } /** @@ -415,18 +412,20 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService val allConnections = configCrudable.getAll().toList() val allConnectionsNames: MutableList = allConnections.map { it.name }.toMutableList() - allConnections.filter { it.zoweConfigPath == getZoweConfigLocation(myProject, type) }.forEach { - var index = 1 - var newName = it.name - while (allConnectionsNames.contains(newName)) { - newName = it.name.plus(index.toString()) - index++ + allConnections + .filter { it.zoweConfigPath == getZoweConfigLocation(myProject, type) } + .forEach { + var index = 1 + var newName = it.name + while (allConnectionsNames.contains(newName)) { + newName = it.name.plus(index.toString()) + index++ + } + allConnectionsNames.add(newName) + it.name = newName + it.zoweConfigPath = null + configCrudable.update(it) } - allConnectionsNames.add(newName) - it.name = newName - it.zoweConfigPath = null - configCrudable.update(it) - } } private fun createZoweSchemaJsonIfNotExists() { @@ -479,39 +478,37 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService globalZoweConfig ?: return ZoweConfigState.NOT_EXISTS findAllZosmfExistingConnection(type) ?: return ZoweConfigState.NEED_TO_ADD - var ret = ZoweConfigState.SYNCHRONIZED - - for (zosConnection in zoweConfig.getListOfZosmfConections()) { - val existingConnection = - findExistingConnection(type, zosConnection.profileName) - if (existingConnection == null) - ret = setZoweConfigState(ret, ZoweConfigState.NEED_TO_ADD) - else { - val newConnectionList = zoweConfig.getListOfZosmfConections() - .filter { it.profileName == getProfileNameFromConnName(existingConnection.name) } - val newConnection = if (newConnectionList.isNotEmpty()) { - newConnectionList[0].toConnectionConfig( - existingConnection.uuid.toString(), existingConnection.zVersion, existingConnection.owner, type = type - ) - } else { - ret = setZoweConfigState(ret, ZoweConfigState.NEED_TO_ADD) - continue - } - val zoweUsername = zosConnection.user - val zowePassword = zosConnection.password - - ret = if ( - existingConnection == newConnection - && CredentialService.getUsername(newConnection) == zoweUsername - && CredentialService.getPassword(newConnection) == zowePassword - ) { - setZoweConfigState(ret, ZoweConfigState.SYNCHRONIZED) + + return zoweConfig + .getListOfZosmfConections() + .fold(ZoweConfigState.SYNCHRONIZED) { prevZoweConfigState, zosConnection -> + val existingConnection = findExistingConnection(type, zosConnection.profileName) + val currZoweConfigState = if (existingConnection == null) { + ZoweConfigState.NEED_TO_ADD } else { - setZoweConfigState(ret, ZoweConfigState.NEED_TO_UPDATE) + val newConnectionList = zoweConfig.getListOfZosmfConections() + .filter { it.profileName == getProfileNameFromConnName(existingConnection.name) } + if (newConnectionList.isNotEmpty()) { + val newConnection = newConnectionList[0].toConnectionConfig( + existingConnection.uuid, existingConnection.zVersion, existingConnection.owner, type = type + ) + val zoweUsername = zosConnection.user + val zowePassword = zosConnection.password + if ( + existingConnection == newConnection + && CredentialService.getUsername(newConnection) == zoweUsername + && CredentialService.getPassword(newConnection) == zowePassword + ) { + ZoweConfigState.SYNCHRONIZED + } else { + ZoweConfigState.NEED_TO_UPDATE + } + } else { + ZoweConfigState.NEED_TO_ADD + } } + setZoweConfigState(prevZoweConfigState, currZoweConfigState) } - } - return ret } /** diff --git a/src/test/kotlin/org/zowe/explorer/config/ZoweConfigTestSpec.kt b/src/test/kotlin/org/zowe/explorer/config/ZoweConfigTestSpec.kt index 3e2192d8..cf3b0003 100644 --- a/src/test/kotlin/org/zowe/explorer/config/ZoweConfigTestSpec.kt +++ b/src/test/kotlin/org/zowe/explorer/config/ZoweConfigTestSpec.kt @@ -346,7 +346,6 @@ class ZoweConfigTestSpec : WithApplicationShouldSpec({ isInputStreamCalled shouldBe true isReturnedZoweConfig shouldBe true isScanForZoweConfigCalled shouldBe true - isZOSInfoCalled shouldBe false } should("delete zowe team config connection") { @@ -377,20 +376,23 @@ class ZoweConfigTestSpec : WithApplicationShouldSpec({ j = true listOf(jWSConf).stream() } - var isShowOkCancelDialogCalled = false - val showOkCancelDialogMock: (String, String, String, String, Icon?) -> Int = ::showOkCancelDialog - mockkStatic(showOkCancelDialogMock as KFunction<*>) - every { - showOkCancelDialogMock(any(), any(), any(), any(), any()) - } answers { - isShowOkCancelDialogCalled = true - Messages.OK + val notificationsService = NotificationsService.getService() as TestNotificationsServiceImpl + notificationsService.testInstance = object : TestNotificationsServiceImpl() { + override fun notifyError( + t: Throwable, + project: Project?, + custTitle: String?, + custDetailsShort: String?, + custDetailsLong: String? + ) { + notified = true + } } mockedZoweConfigService.deleteZoweConfig(type = ZoweConfigType.LOCAL) f shouldBe true j shouldBe true - isShowOkCancelDialogCalled shouldBe true + notified shouldBe true isConnectionDeleted shouldBe false } @@ -741,4 +743,4 @@ class ZoweConfigTestSpec : WithApplicationShouldSpec({ } -}) \ No newline at end of file +})