Skip to content

Commit

Permalink
GH-310 Fixed NullPointerException when zowe.config is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Katsiaryna Tsytsenia <[email protected]>
  • Loading branch information
ktsytsen committed Feb 18, 2025
1 parent 2866c9d commit b243be0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mockk = "1.13.12"
kotest = "5.9.1"
jgrapht = "1.5.2"
keytar = "1.0.0"
zowe-kotlin-sdk = "0.5.0"
zowe-kotlin-sdk = "0.5.2-rc.1"
ibm-mq-allclient = "9.4.0.0"

# plugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ class ZOSMFConnectionConfigurable : BoundSearchableConfigurable("z/OSMF Connecti

val zoweConfig = try {
parseConfigJson(configFile.inputStream)
} catch (e: JsonSyntaxException) {
NotificationsService.errorNotification(
e,
project = DataManager.getInstance().getDataContext(panel).getData(PlatformDataKeys.PROJECT),
custTitle = "Error with Zowe config file"
)
} catch (e: Exception) {
if (e is Exception)
NotificationsService.errorNotification(
e,
project = DataManager.getInstance().getDataContext(panel).getData(PlatformDataKeys.PROJECT),
custTitle = "Error with Zowe config file"
)
return
}
zoweConfig.extractSecureProperties(configFile.path.split("/").toTypedArray())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.zowe.explorer.zowe.actions

import com.intellij.icons.AllIcons
import com.google.gson.JsonSyntaxException
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
Expand Down Expand Up @@ -94,7 +93,7 @@ class UpdateZoweConfigAction : DumbAwareAction() {
zoweConfigService.globalZoweConfig = parseConfigJson(editor.document.text)
zoweConfigService.globalZoweConfig?.extractSecureProperties(vFile.path.split("/").toTypedArray())
}
} catch (ex: JsonSyntaxException) {
} catch (ex: Exception) {
e.presentation.isEnabledAndVisible = false
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBA Group
* Zowe Community
* IBA Group
* Katsiaryna Tsytsenia
*/

package org.zowe.explorer.zowe.service
Expand All @@ -19,7 +20,6 @@ import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.components.service
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
Expand All @@ -44,6 +44,7 @@ import org.zowe.explorer.utils.crudable.getAll
import org.zowe.explorer.zowe.ZOWE_CONFIG_NAME
import org.zowe.explorer.zowe.service.ZoweConfigService.Companion.lock
import org.zowe.kotlinsdk.annotations.ZVersion
import org.zowe.kotlinsdk.exceptions.EmptyZoweConfigFileException
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection
import org.zowe.kotlinsdk.zowe.config.ZoweConfig
import org.zowe.kotlinsdk.zowe.config.parseConfigJson
Expand Down Expand Up @@ -180,7 +181,9 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
}
}
} catch (e: Exception) {
NotificationsService.errorNotification(e, project = myProject, custTitle="Error with Zowe config file")
//parseConfigJson returns EmptyZoweConfigFileException in case of empty file
if (e !is EmptyZoweConfigFileException)
NotificationsService.errorNotification(e, project = myProject, custTitle = "Error with Zowe config file")
return null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

package org.zowe.explorer.config.connect.ui.zosmf

import com.intellij.ide.DataManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.Messages
Expand Down Expand Up @@ -204,16 +201,28 @@ class ZOSMFConnectionConfigurableTest : WithApplicationShouldSpec({
zOSMFConnectionConfigurableMock::class.declaredMemberFunctions.find { it.name == "updateZoweConfigIfNeeded" }
?.let {
it.isAccessible = true
try {
it.call(zOSMFConnectionConfigurableMock, state)
} catch (t: Throwable) {
println("ghjkk")
t.cause.toString().shouldContain("Zowe config file not found")
}
it.call(zOSMFConnectionConfigurableMock, state)
}
notified shouldBe true
}

every { vfMock.inputStream } answers {
isInputStreamCalled = true
"".toByteArray().inputStream()
}

should("updateZoweConfigIfNeeded empty zowe config file") {
zOSMFConnectionConfigurableMock::class.declaredMemberFunctions.find { it.name == "updateZoweConfigIfNeeded" }
?.let {
it.isAccessible = true
it.call(zOSMFConnectionConfigurableMock, state)
}
notified shouldBe true
isShowOkCancelDialogCalled shouldBe true
isFindFileByNioPathCalled shouldBe true
isInputStreamCalled shouldBe true
}

every { vfMock.inputStream } answers {
isInputStreamCalled = true
val fileCont = "{\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,18 @@ class ZoweConfigServiceTestSpec : WithApplicationShouldSpec({

assertSoftly { zoweConfigState shouldBe ZoweConfigState.NOT_EXISTS }
}

should("return NOT_EXISTS config state for the empty local Zowe config file") {
val parseConfigJsonRef: (InputStream) -> ZoweConfig = ::parseConfigJson
mockkStatic(parseConfigJsonRef as KFunction<*>)
every { parseConfigJsonRef(any<InputStream>()) } answers { throw NullPointerException() }

val zoweConfigService = spyk(ZoweConfigServiceImpl(projectMock), recordPrivateCalls = true)

val zoweConfigState = zoweConfigService.getZoweConfigState(true, ZoweConfigType.LOCAL)

assertSoftly { zoweConfigState shouldBe ZoweConfigState.NOT_EXISTS }
}
}
}
})

0 comments on commit b243be0

Please sign in to comment.