Skip to content

Commit b2343df

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

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ repositoryUrl=https://github.com/zowe/zowe-client-kotlin-sdk
3131

3232
# Artifacts version (is changed automatically after the version is released)
3333
# !!!DO NOT CHANGE YOURSELF!!!
34-
version=0.5.2
34+
version=0.5.2-rc.1
3535

3636
# Artifactory repositories for builds
3737
artifactoryMavenRepo=https://zowe.jfrog.io/zowe/libs-release
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This program and the accompanying materials are made available under the terms of the
3+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
4+
* https://www.eclipse.org/legal/epl-v20.html
5+
*
6+
* SPDX-License-Identifier: EPL-2.0
7+
*
8+
* Copyright Contributors to the Zowe Project.
9+
*
10+
* Contributors:
11+
* Zowe Community
12+
* Katsiaryna Tsytsenia
13+
*/
14+
15+
package org.zowe.kotlinsdk.exceptions
16+
17+
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)