Skip to content

Commit 243f566

Browse files
authored
refactor: add null check for BroadcastDomainType retrievals (#11572)
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 44119cf commit 243f566

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

api/src/main/java/com/cloud/network/Networks.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public <T> URI toUri(T value) {
7878
}
7979
@Override
8080
public String getValueFrom(URI uri) {
81-
return uri.getAuthority();
81+
return uri == null ? null : uri.getAuthority();
8282
}
8383
},
8484
Vswitch("vs", String.class), LinkLocal(null, null), Vnet("vnet", Long.class), Storage("storage", Integer.class), Lswitch("lswitch", String.class) {
@@ -96,7 +96,7 @@ public <T> URI toUri(T value) {
9696
*/
9797
@Override
9898
public String getValueFrom(URI uri) {
99-
return uri.getSchemeSpecificPart();
99+
return uri == null ? null : uri.getSchemeSpecificPart();
100100
}
101101
},
102102
Mido("mido", String.class), Pvlan("pvlan", String.class),
@@ -176,7 +176,7 @@ public <T> URI toUri(T value) {
176176
* @return the scheme as BroadcastDomainType
177177
*/
178178
public static BroadcastDomainType getSchemeValue(URI uri) {
179-
return toEnumValue(uri.getScheme());
179+
return toEnumValue(uri == null ? null : uri.getScheme());
180180
}
181181

182182
/**
@@ -190,7 +190,7 @@ public static BroadcastDomainType getTypeOf(String str) throws URISyntaxExceptio
190190
if (com.cloud.dc.Vlan.UNTAGGED.equalsIgnoreCase(str)) {
191191
return Native;
192192
}
193-
return getSchemeValue(new URI(str));
193+
return getSchemeValue(str == null ? null : new URI(str));
194194
}
195195

196196
/**
@@ -219,7 +219,7 @@ public static BroadcastDomainType toEnumValue(String scheme) {
219219
* @return the host part as String
220220
*/
221221
public String getValueFrom(URI uri) {
222-
return uri.getHost();
222+
return uri == null ? null : uri.getHost();
223223
}
224224

225225
/**
@@ -242,7 +242,7 @@ public static String getValue(URI uri) {
242242
* @throws URISyntaxException the string is not even an uri
243243
*/
244244
public static String getValue(String uriString) throws URISyntaxException {
245-
return getValue(new URI(uriString));
245+
return getValue(uriString == null ? null : new URI(uriString));
246246
}
247247

248248
/**

api/src/test/java/com/cloud/network/NetworksTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ public class NetworksTest {
3737
public void setUp() {
3838
}
3939

40+
@Test
41+
public void nullBroadcastDomainTypeTest() throws URISyntaxException {
42+
BroadcastDomainType type = BroadcastDomainType.getTypeOf(null);
43+
Assert.assertEquals("a null uri should mean a broadcasttype of undecided", BroadcastDomainType.UnDecided, type);
44+
}
45+
46+
@Test
47+
public void nullBroadcastDomainTypeValueTest() {
48+
URI uri = null;
49+
Assert.assertNull(BroadcastDomainType.getValue(uri));
50+
}
51+
52+
@Test
53+
public void nullBroadcastDomainTypeStringValueTest() throws URISyntaxException {
54+
String uriString = null;
55+
Assert.assertNull(BroadcastDomainType.getValue(uriString));
56+
}
57+
4058
@Test
4159
public void emptyBroadcastDomainTypeTest() throws URISyntaxException {
4260
BroadcastDomainType type = BroadcastDomainType.getTypeOf("");

0 commit comments

Comments
 (0)