Skip to content

Commit 044cfae

Browse files
committed
GH-310 Fixed NullPointerException when zowe.config is empty
Signed-off-by: Katsiaryna Tsytsenia <[email protected]>
1 parent e9eca9a commit 044cfae

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2025 IBA Group.
3+
*
4+
* This program and the accompanying materials are made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBA Group
12+
* Zowe Community
13+
* Katsiaryna Tsytsenia
14+
*/
15+
16+
package org.zowe.kotlinsdk.exceptions
17+
18+
class EmptyZoweConfigFileException: Exception("Zowe configuration file is empty")

src/main/kotlin/org/zowe/kotlinsdk/zowe/config/utils.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.zowe.kotlinsdk.zowe.config
1717
import com.google.gson.Gson
1818
import com.starxg.keytar.Keytar
1919
import org.yaml.snakeyaml.Yaml
20+
import org.zowe.kotlinsdk.exceptions.EmptyZoweConfigFileException
2021
import java.io.ByteArrayInputStream
2122
import java.io.InputStream
2223
import java.nio.charset.Charset
@@ -87,9 +88,10 @@ private fun formProfiles (profiles: Map<String, ZoweConfigProfile>?) {
8788
* Parses JSON string to [ZoweConfig] object model.
8889
* @param configString JSON string with zowe config.
8990
* @return [ZoweConfig] object model.
91+
* @throws [com.google.gson.JsonSyntaxException] and [org.zowe.kotlinsdk.exceptions.EmptyZoweConfigFileException]
9092
*/
9193
fun parseConfigJson(configString: String): ZoweConfig {
92-
val zoweConfig = Gson().fromJson(configString, ZoweConfig::class.java)
94+
val zoweConfig = Gson().fromJson(configString, ZoweConfig::class.java) ?: throw EmptyZoweConfigFileException()
9395
zoweConfig.zosmfProfile = zoweConfig.profile(zoweConfig.defaults["zosmf"])
9496
formProfiles(zoweConfig.profiles)
9597
return zoweConfig
@@ -99,6 +101,7 @@ fun parseConfigJson(configString: String): ZoweConfig {
99101
* Reads input stream and parse it to ZoweConfig object model.
100102
* @param inputStream - stream with json string of zowe config.
101103
* @return ZoweConfig object model.
104+
* @throws [com.google.gson.JsonSyntaxException] and [org.zowe.kotlinsdk.exceptions.EmptyZoweConfigFileException]
102105
*/
103106
fun parseConfigJson (inputStream: InputStream): ZoweConfig = parseConfigJson(String(inputStream.readBytes()))
104107

src/test/kotlin/org/zowe/kotlinsdk/zowe/ZoweConfigParsingTest.kt

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package org.zowe.kotlinsdk.zowe
1717
import okhttp3.OkHttpClient
1818
import okhttp3.mockwebserver.MockWebServer
1919
import org.junit.jupiter.api.*
20+
import org.zowe.kotlinsdk.exceptions.EmptyZoweConfigFileException
2021
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection
2122
import org.zowe.kotlinsdk.zowe.config.*
2223
import java.net.InetSocketAddress
@@ -68,6 +69,11 @@ class ZoweConfigParsingTest: ZoweConfigTestBase() {
6869
checkZoweConfig(zoweConfig)
6970
}
7071

72+
@Test
73+
fun testParsingEmptyJsonString() {
74+
Assertions.assertThrows(EmptyZoweConfigFileException::class.java){ parseConfigJson("") }
75+
}
76+
7177
@Test
7278
fun testZOSConnection() {
7379
val zoweConfig = parseConfigJson(streamConfigJson)

0 commit comments

Comments
 (0)