Skip to content

Commit 6cdaaad

Browse files
committed
add unit tests
1 parent 416b2cb commit 6cdaaad

File tree

11 files changed

+1098
-1
lines changed

11 files changed

+1098
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.cloudstack.extension;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.assertTrue;
24+
25+
import java.util.Map;
26+
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
public class CustomActionResultResponseTest {
31+
32+
private CustomActionResultResponse response;
33+
34+
@Before
35+
public void setUp() {
36+
response = new CustomActionResultResponse();
37+
}
38+
39+
@Test
40+
public void getResultReturnsNullByDefault() {
41+
assertNull(response.getResult());
42+
}
43+
44+
@Test
45+
public void getResultReturnsSetValue() {
46+
Map<String, String> result = Map.of("message", "OK", "details", "All good");
47+
response.setResult(result);
48+
assertEquals(result, response.getResult());
49+
assertEquals("OK", response.getResult().get("message"));
50+
}
51+
52+
@Test
53+
public void isSuccessReturnsFalseWhenSuccessIsNull() {
54+
// success is null by default
55+
assertFalse(response.isSuccess());
56+
}
57+
58+
@Test
59+
public void isSuccessReturnsFalseWhenSuccessIsFalse() {
60+
response.setSuccess(false);
61+
assertFalse(response.isSuccess());
62+
}
63+
64+
@Test
65+
public void isSuccessReturnsTrueWhenSuccessIsTrue() {
66+
response.setSuccess(true);
67+
assertTrue(response.isSuccess());
68+
}
69+
70+
@Test
71+
public void getSuccessReturnsNullByDefault() {
72+
assertNull(response.getSuccess());
73+
}
74+
75+
@Test
76+
public void getSuccessReturnsTrueAfterSetSuccessTrue() {
77+
response.setSuccess(true);
78+
assertTrue(response.getSuccess());
79+
}
80+
81+
@Test
82+
public void getSuccessReturnsFalseAfterSetSuccessFalse() {
83+
response.setSuccess(false);
84+
assertFalse(response.getSuccess());
85+
}
86+
87+
@Test
88+
public void setAndGetResultWithNullResult() {
89+
response.setResult(null);
90+
assertNull(response.getResult());
91+
}
92+
}
93+

engine/orchestration/src/test/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestratorTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.URI;
2828
import java.util.ArrayList;
2929
import java.util.Arrays;
30+
import java.util.Collections;
3031
import java.util.HashMap;
3132
import java.util.List;
3233
import java.util.Map;
@@ -35,12 +36,15 @@
3536
import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
3637
import com.cloud.network.IpAddressManager;
3738
import com.cloud.utils.Pair;
39+
import org.apache.cloudstack.extension.Extension;
3840
import org.apache.cloudstack.extension.ExtensionHelper;
41+
import org.apache.cloudstack.framework.extensions.network.NetworkExtensionElement;
3942
import org.junit.Assert;
4043
import org.junit.Before;
4144
import org.junit.Test;
4245
import org.junit.runner.RunWith;
4346
import org.junit.runners.JUnit4;
47+
import org.springframework.test.util.ReflectionTestUtils;
4448
import org.mockito.ArgumentMatchers;
4549
import org.mockito.MockedStatic;
4650
import org.mockito.Mockito;
@@ -69,6 +73,7 @@
6973
import com.cloud.network.dao.PhysicalNetworkVO;
7074
import com.cloud.network.dao.RouterNetworkDao;
7175
import com.cloud.network.element.DhcpServiceProvider;
76+
import com.cloud.network.element.NetworkElement;
7277
import com.cloud.network.guru.GuestNetworkGuru;
7378
import com.cloud.network.guru.NetworkGuru;
7479
import com.cloud.network.vpc.VpcManager;
@@ -106,6 +111,7 @@ public class NetworkOrchestratorTest extends TestCase {
106111
private String guruName = "GuestNetworkGuru";
107112
private String dhcpProvider = "VirtualRouter";
108113
private NetworkGuru guru = mock(NetworkGuru.class);
114+
private NetworkExtensionElement networkExtensionElement;
109115

110116
NetworkOfferingVO networkOffering = mock(NetworkOfferingVO.class);
111117

@@ -137,6 +143,8 @@ public void setUp() {
137143
testOrchestrator._ipAddrMgr = mock(IpAddressManager.class);
138144
testOrchestrator._entityMgr = mock(EntityManager.class);
139145
testOrchestrator.extensionHelper = mock(ExtensionHelper.class);
146+
networkExtensionElement = mock(NetworkExtensionElement.class);
147+
ReflectionTestUtils.setField(testOrchestrator, "networkExtensionElement", networkExtensionElement);
140148
DhcpServiceProvider provider = mock(DhcpServiceProvider.class);
141149

142150
Map<Network.Capability, String> capabilities = new HashMap<Network.Capability, String>();
@@ -1012,4 +1020,63 @@ public void testImportNicWithIP4Address() throws Exception {
10121020
assertEquals("testtag", nicProfile.getName());
10131021
}
10141022
}
1023+
1024+
// -----------------------------------------------------------------------
1025+
// Tests for getNetworkElementsIncludingExtensions
1026+
// -----------------------------------------------------------------------
1027+
1028+
@Test
1029+
public void getNetworkElementsIncludingExtensionsReturnsBaseListWhenNoExtensions() {
1030+
when(testOrchestrator.extensionHelper.listExtensionsByType(Extension.Type.NetworkOrchestrator))
1031+
.thenReturn(Collections.emptyList());
1032+
1033+
DhcpServiceProvider dhcpProvider = mock(DhcpServiceProvider.class);
1034+
List<NetworkElement> elements = new ArrayList<>(List.of(dhcpProvider));
1035+
testOrchestrator.networkElements = elements;
1036+
1037+
@SuppressWarnings("unchecked")
1038+
List<NetworkElement> result =
1039+
(List<NetworkElement>) ReflectionTestUtils
1040+
.invokeMethod(testOrchestrator, "getNetworkElementsIncludingExtensions");
1041+
assertNotNull(result);
1042+
assertEquals(elements.size(), result.size());
1043+
}
1044+
1045+
@Test
1046+
public void getNetworkElementsIncludingExtensionsAddsExtensionElements() {
1047+
Extension ext = mock(Extension.class);
1048+
when(ext.getName()).thenReturn("my-net-ext");
1049+
when(testOrchestrator.extensionHelper.listExtensionsByType(Extension.Type.NetworkOrchestrator))
1050+
.thenReturn(List.of(ext));
1051+
1052+
NetworkExtensionElement extElement = mock(NetworkExtensionElement.class);
1053+
when(networkExtensionElement.withProviderName("my-net-ext")).thenReturn(extElement);
1054+
1055+
DhcpServiceProvider dhcpProvider = mock(DhcpServiceProvider.class);
1056+
testOrchestrator.networkElements = new ArrayList<>(List.of(dhcpProvider));
1057+
1058+
@SuppressWarnings("unchecked")
1059+
List<NetworkElement> result =
1060+
(List<NetworkElement>) ReflectionTestUtils
1061+
.invokeMethod(testOrchestrator, "getNetworkElementsIncludingExtensions");
1062+
assertNotNull(result);
1063+
assertEquals(2, result.size());
1064+
assertTrue(result.contains(extElement));
1065+
}
1066+
1067+
@Test
1068+
public void getNetworkElementsIncludingExtensionsReturnsBaseListWhenExtensionHelperReturnsNull() {
1069+
when(testOrchestrator.extensionHelper.listExtensionsByType(Extension.Type.NetworkOrchestrator))
1070+
.thenReturn(null);
1071+
1072+
DhcpServiceProvider dhcpProvider = mock(DhcpServiceProvider.class);
1073+
testOrchestrator.networkElements = new ArrayList<>(List.of(dhcpProvider));
1074+
1075+
@SuppressWarnings("unchecked")
1076+
List<NetworkElement> result =
1077+
(List<NetworkElement>) ReflectionTestUtils
1078+
.invokeMethod(testOrchestrator, "getNetworkElementsIncludingExtensions");
1079+
assertNotNull(result);
1080+
assertEquals(1, result.size());
1081+
}
10151082
}

framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/network/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,4 +1090,3 @@ exit 0
10901090
For a full production implementation see https://github.com/apache/cloudstack-extensions/tree/network-namespace/Network-Namespace:
10911091
- `network-namespace.sh` — management-server entry-point (SSH proxy).
10921092
- `enetwork-namespace-wrapper.sh` — KVM-host wrapper that implements all commands using Linux network namespaces.
1093-

framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/api/ListExtensionsCmdTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.cloudstack.framework.extensions.api;
1919

2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertTrue;
2223

2324
import java.util.Arrays;
@@ -89,4 +90,44 @@ public void testGetDetailsWithInvalidValueThrowsException() {
8990
setPrivateField("details", detailsList);
9091
cmd.getDetails();
9192
}
93+
94+
// -----------------------------------------------------------------------
95+
// Tests for new getters: type, resourceId, resourceType
96+
// -----------------------------------------------------------------------
97+
98+
@Test
99+
public void testGetTypeReturnsValueWhenSet() {
100+
setPrivateField("type", "NetworkOrchestrator");
101+
assertEquals("NetworkOrchestrator", cmd.getType());
102+
}
103+
104+
@Test
105+
public void testGetTypeReturnsNullWhenUnset() {
106+
setPrivateField("type", null);
107+
assertNull(cmd.getType());
108+
}
109+
110+
@Test
111+
public void testGetResourceIdReturnsValueWhenSet() {
112+
setPrivateField("resourceId", "pnet-uuid-123");
113+
assertEquals("pnet-uuid-123", cmd.getResourceId());
114+
}
115+
116+
@Test
117+
public void testGetResourceIdReturnsNullWhenUnset() {
118+
setPrivateField("resourceId", null);
119+
assertNull(cmd.getResourceId());
120+
}
121+
122+
@Test
123+
public void testGetResourceTypeReturnsValueWhenSet() {
124+
setPrivateField("resourceType", "PhysicalNetwork");
125+
assertEquals("PhysicalNetwork", cmd.getResourceType());
126+
}
127+
128+
@Test
129+
public void testGetResourceTypeReturnsNullWhenUnset() {
130+
setPrivateField("resourceType", null);
131+
assertNull(cmd.getResourceType());
132+
}
92133
}

framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/api/UpdateRegisteredExtensionCmdTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.cloudstack.framework.extensions.api;
1919

2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
2122
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.assertNull;
2324
import static org.junit.Assert.assertTrue;
@@ -31,6 +32,7 @@
3132
import java.util.EnumSet;
3233
import java.util.Map;
3334

35+
import org.apache.cloudstack.api.ApiCommandResourceType;
3436
import org.apache.cloudstack.api.ApiConstants;
3537
import org.apache.cloudstack.api.ApiErrorCode;
3638
import org.apache.cloudstack.api.ServerApiException;
@@ -42,6 +44,8 @@
4244
import org.mockito.Mockito;
4345
import org.springframework.test.util.ReflectionTestUtils;
4446

47+
import com.cloud.user.Account;
48+
4549
public class UpdateRegisteredExtensionCmdTest {
4650

4751
private UpdateRegisteredExtensionCmd cmd;
@@ -60,6 +64,48 @@ public void extensionIdReturnsNullWhenUnset() {
6064
assertNull(cmd.getExtensionId());
6165
}
6266

67+
@Test
68+
public void extensionIdReturnsValueWhenSet() {
69+
Long extensionId = 42L;
70+
ReflectionTestUtils.setField(cmd, "extensionId", extensionId);
71+
assertEquals(extensionId, cmd.getExtensionId());
72+
}
73+
74+
@Test
75+
public void cleanupDetailsReturnsNullWhenUnset() {
76+
ReflectionTestUtils.setField(cmd, "cleanupDetails", null);
77+
assertNull(cmd.isCleanupDetails());
78+
}
79+
80+
@Test
81+
public void cleanupDetailsReturnsTrueWhenSet() {
82+
ReflectionTestUtils.setField(cmd, "cleanupDetails", true);
83+
assertTrue(cmd.isCleanupDetails());
84+
}
85+
86+
@Test
87+
public void cleanupDetailsReturnsFalseWhenSetToFalse() {
88+
ReflectionTestUtils.setField(cmd, "cleanupDetails", false);
89+
assertFalse(cmd.isCleanupDetails());
90+
}
91+
92+
@Test
93+
public void getEntityOwnerIdReturnsSystemAccountId() {
94+
assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId());
95+
}
96+
97+
@Test
98+
public void getApiResourceTypeReturnsExtension() {
99+
assertEquals(ApiCommandResourceType.Extension, cmd.getApiResourceType());
100+
}
101+
102+
@Test
103+
public void getApiResourceIdReturnsExtensionId() {
104+
Long extensionId = 99L;
105+
ReflectionTestUtils.setField(cmd, "extensionId", extensionId);
106+
assertEquals(extensionId, cmd.getApiResourceId());
107+
}
108+
63109
@Test
64110
public void resourceIdReturnsValueWhenSet() {
65111
String resourceId = "resource-123";

framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/dao/ExtensionResourceMapDaoImplTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,55 @@ public void listResourceIdsByExtensionIdAndTypeReturnsCorrectIds() {
8383
when(dao.listResourceIdsByExtensionIdAndType(1L, ExtensionResourceMap.ResourceType.Cluster)).thenReturn(expectedIds);
8484
assertEquals(expectedIds, dao.listResourceIdsByExtensionIdAndType(1L, ExtensionResourceMap.ResourceType.Cluster));
8585
}
86+
87+
// -----------------------------------------------------------------------
88+
// Tests for new methods: listByResourceIdAndType, listResourceIdsByType
89+
// -----------------------------------------------------------------------
90+
91+
@Test
92+
public void listByResourceIdAndTypeReturnsEmptyListWhenNoMatch() {
93+
when(dao.listByResourceIdAndType(999L, ExtensionResourceMap.ResourceType.PhysicalNetwork)).thenReturn(List.of());
94+
assertTrue(dao.listByResourceIdAndType(999L, ExtensionResourceMap.ResourceType.PhysicalNetwork).isEmpty());
95+
}
96+
97+
@Test
98+
public void listByResourceIdAndTypeReturnsMatchingEntries() {
99+
ExtensionResourceMapVO map1 = new ExtensionResourceMapVO();
100+
map1.setResourceId(42L);
101+
map1.setResourceType(ExtensionResourceMap.ResourceType.PhysicalNetwork);
102+
ExtensionResourceMapVO map2 = new ExtensionResourceMapVO();
103+
map2.setResourceId(42L);
104+
map2.setResourceType(ExtensionResourceMap.ResourceType.PhysicalNetwork);
105+
List<ExtensionResourceMapVO> expected = List.of(map1, map2);
106+
when(dao.listByResourceIdAndType(42L, ExtensionResourceMap.ResourceType.PhysicalNetwork)).thenReturn(expected);
107+
List<ExtensionResourceMapVO> result = dao.listByResourceIdAndType(42L, ExtensionResourceMap.ResourceType.PhysicalNetwork);
108+
assertEquals(2, result.size());
109+
assertEquals(expected, result);
110+
}
111+
112+
@Test
113+
public void listByResourceIdAndTypeDifferentiatesResourceTypes() {
114+
ExtensionResourceMapVO clusterMap = new ExtensionResourceMapVO();
115+
clusterMap.setResourceType(ExtensionResourceMap.ResourceType.Cluster);
116+
when(dao.listByResourceIdAndType(10L, ExtensionResourceMap.ResourceType.Cluster)).thenReturn(List.of(clusterMap));
117+
when(dao.listByResourceIdAndType(10L, ExtensionResourceMap.ResourceType.PhysicalNetwork)).thenReturn(List.of());
118+
119+
assertEquals(1, dao.listByResourceIdAndType(10L, ExtensionResourceMap.ResourceType.Cluster).size());
120+
assertTrue(dao.listByResourceIdAndType(10L, ExtensionResourceMap.ResourceType.PhysicalNetwork).isEmpty());
121+
}
122+
123+
@Test
124+
public void listResourceIdsByTypeReturnsEmptyListWhenNoMatch() {
125+
when(dao.listResourceIdsByType(ExtensionResourceMap.ResourceType.PhysicalNetwork)).thenReturn(List.of());
126+
assertTrue(dao.listResourceIdsByType(ExtensionResourceMap.ResourceType.PhysicalNetwork).isEmpty());
127+
}
128+
129+
@Test
130+
public void listResourceIdsByTypeReturnsMatchingIds() {
131+
List<Long> expectedIds = List.of(5L, 10L, 15L);
132+
when(dao.listResourceIdsByType(ExtensionResourceMap.ResourceType.PhysicalNetwork)).thenReturn(expectedIds);
133+
List<Long> result = dao.listResourceIdsByType(ExtensionResourceMap.ResourceType.PhysicalNetwork);
134+
assertEquals(3, result.size());
135+
assertEquals(expectedIds, result);
136+
}
86137
}

0 commit comments

Comments
 (0)