refactor: add null check for BroadcastDomainType retrievals (#11572)

Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
This commit is contained in:
Abhishek Kumar 2025-12-01 08:19:09 +01:00 committed by GitHub
parent 44119cf34f
commit 243f566a60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 6 deletions

View File

@ -78,7 +78,7 @@ public class Networks {
} }
@Override @Override
public String getValueFrom(URI uri) { public String getValueFrom(URI uri) {
return uri.getAuthority(); return uri == null ? null : uri.getAuthority();
} }
}, },
Vswitch("vs", String.class), LinkLocal(null, null), Vnet("vnet", Long.class), Storage("storage", Integer.class), Lswitch("lswitch", String.class) { Vswitch("vs", String.class), LinkLocal(null, null), Vnet("vnet", Long.class), Storage("storage", Integer.class), Lswitch("lswitch", String.class) {
@ -96,7 +96,7 @@ public class Networks {
*/ */
@Override @Override
public String getValueFrom(URI uri) { public String getValueFrom(URI uri) {
return uri.getSchemeSpecificPart(); return uri == null ? null : uri.getSchemeSpecificPart();
} }
}, },
Mido("mido", String.class), Pvlan("pvlan", String.class), Mido("mido", String.class), Pvlan("pvlan", String.class),
@ -176,7 +176,7 @@ public class Networks {
* @return the scheme as BroadcastDomainType * @return the scheme as BroadcastDomainType
*/ */
public static BroadcastDomainType getSchemeValue(URI uri) { public static BroadcastDomainType getSchemeValue(URI uri) {
return toEnumValue(uri.getScheme()); return toEnumValue(uri == null ? null : uri.getScheme());
} }
/** /**
@ -190,7 +190,7 @@ public class Networks {
if (com.cloud.dc.Vlan.UNTAGGED.equalsIgnoreCase(str)) { if (com.cloud.dc.Vlan.UNTAGGED.equalsIgnoreCase(str)) {
return Native; return Native;
} }
return getSchemeValue(new URI(str)); return getSchemeValue(str == null ? null : new URI(str));
} }
/** /**
@ -219,7 +219,7 @@ public class Networks {
* @return the host part as String * @return the host part as String
*/ */
public String getValueFrom(URI uri) { public String getValueFrom(URI uri) {
return uri.getHost(); return uri == null ? null : uri.getHost();
} }
/** /**
@ -242,7 +242,7 @@ public class Networks {
* @throws URISyntaxException the string is not even an uri * @throws URISyntaxException the string is not even an uri
*/ */
public static String getValue(String uriString) throws URISyntaxException { public static String getValue(String uriString) throws URISyntaxException {
return getValue(new URI(uriString)); return getValue(uriString == null ? null : new URI(uriString));
} }
/** /**

View File

@ -37,6 +37,24 @@ public class NetworksTest {
public void setUp() { public void setUp() {
} }
@Test
public void nullBroadcastDomainTypeTest() throws URISyntaxException {
BroadcastDomainType type = BroadcastDomainType.getTypeOf(null);
Assert.assertEquals("a null uri should mean a broadcasttype of undecided", BroadcastDomainType.UnDecided, type);
}
@Test
public void nullBroadcastDomainTypeValueTest() {
URI uri = null;
Assert.assertNull(BroadcastDomainType.getValue(uri));
}
@Test
public void nullBroadcastDomainTypeStringValueTest() throws URISyntaxException {
String uriString = null;
Assert.assertNull(BroadcastDomainType.getValue(uriString));
}
@Test @Test
public void emptyBroadcastDomainTypeTest() throws URISyntaxException { public void emptyBroadcastDomainTypeTest() throws URISyntaxException {
BroadcastDomainType type = BroadcastDomainType.getTypeOf(""); BroadcastDomainType type = BroadcastDomainType.getTypeOf("");