Skip to content

Commit 17e765e

Browse files
authored
test: reduce PowerMock usage for code coverage (#7452)
JaCoCo used for code coverage calculation in the project doesn't support PowerMockito classes. This PR attemps to reduce usage of PowerMockito. Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent f636580 commit 17e765e

File tree

3 files changed

+88
-39
lines changed

3 files changed

+88
-39
lines changed

api/src/test/java/org/apache/cloudstack/api/command/user/network/CreateNetworkCmdTest.java

+43-11
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@
1717

1818
package org.apache.cloudstack.api.command.user.network;
1919

20-
import com.cloud.exception.InsufficientCapacityException;
21-
import com.cloud.exception.ResourceAllocationException;
22-
import com.cloud.network.Network;
23-
import com.cloud.network.NetworkService;
24-
import com.cloud.offering.NetworkOffering;
25-
import com.cloud.utils.db.EntityManager;
26-
import junit.framework.TestCase;
2720
import org.apache.cloudstack.api.ResponseGenerator;
2821
import org.apache.cloudstack.api.ResponseObject;
2922
import org.apache.cloudstack.api.response.NetworkResponse;
3023
import org.junit.Assert;
24+
import org.junit.Test;
3125
import org.junit.runner.RunWith;
3226
import org.mockito.InjectMocks;
3327
import org.mockito.Mock;
3428
import org.mockito.Mockito;
35-
import org.powermock.modules.junit4.PowerMockRunner;
29+
import org.mockito.junit.MockitoJUnitRunner;
3630
import org.springframework.test.util.ReflectionTestUtils;
3731

38-
@RunWith(PowerMockRunner.class)
39-
public class CreateNetworkCmdTest extends TestCase {
32+
import com.cloud.exception.InsufficientCapacityException;
33+
import com.cloud.exception.ResourceAllocationException;
34+
import com.cloud.network.Network;
35+
import com.cloud.network.NetworkService;
36+
import com.cloud.offering.NetworkOffering;
37+
import com.cloud.utils.db.EntityManager;
38+
39+
@RunWith(MockitoJUnitRunner.class)
40+
public class CreateNetworkCmdTest {
4041

4142
@Mock
4243
public EntityManager _entityMgr;
@@ -46,139 +47,161 @@ public class CreateNetworkCmdTest extends TestCase {
4647

4748
@InjectMocks
4849
CreateNetworkCmd cmd = new CreateNetworkCmd();
50+
51+
@Test
4952
public void testGetNetworkOfferingId() {
5053
Long networkOfferingId = 1L;
5154
ReflectionTestUtils.setField(cmd, "networkOfferingId", networkOfferingId);
5255
Assert.assertEquals(cmd.getNetworkOfferingId(), networkOfferingId);
5356
}
5457

58+
@Test
5559
public void testGetGateway() {
5660
String gateway = "10.10.10.1";
5761
ReflectionTestUtils.setField(cmd, "gateway", gateway);
5862
Assert.assertEquals(cmd.getGateway(), gateway);
5963
}
6064

65+
@Test
6166
public void testGetIsolatedPvlan() {
6267
String isolatedPvlan = "1234";
6368
ReflectionTestUtils.setField(cmd, "isolatedPvlan", isolatedPvlan);
6469
Assert.assertEquals(cmd.getIsolatedPvlan(), isolatedPvlan);
6570
}
6671

72+
@Test
6773
public void testGetAccountName() {
6874
String accountName = "admin";
6975
ReflectionTestUtils.setField(cmd, "accountName", accountName);
7076
Assert.assertEquals(cmd.getAccountName(), accountName);
7177
}
7278

79+
@Test
7380
public void testGetDomainId() {
7481
Long domainId = 1L;
7582
ReflectionTestUtils.setField(cmd, "domainId", domainId);
7683
Assert.assertEquals(cmd.getDomainId(), domainId);
7784
}
7885

86+
@Test
7987
public void testGetNetmask() {
8088
String netmask = "255.255.255.0";
8189
ReflectionTestUtils.setField(cmd, "netmask", netmask);
8290
Assert.assertEquals(cmd.getNetmask(), netmask);
8391
}
8492

93+
@Test
8594
public void testGetStartIp() {
8695
String startIp = "10.10.10.2";
8796
ReflectionTestUtils.setField(cmd, "startIp", startIp);
8897
Assert.assertEquals(cmd.getStartIp(), startIp);
8998
}
9099

100+
@Test
91101
public void testGetEndIp() {
92102
String endIp = "10.10.10.10";
93103
ReflectionTestUtils.setField(cmd, "endIp", endIp);
94104
Assert.assertEquals(cmd.getEndIp(), endIp);
95105
}
96106

107+
@Test
97108
public void testGetNetworkName() {
98109
String netName = "net-isolated";
99110
ReflectionTestUtils.setField(cmd, "name", netName);
100111
Assert.assertEquals(cmd.getNetworkName(), netName);
101112
}
102113

114+
@Test
103115
public void testGetDisplayTextWhenNotEmpty() {
104116
String description = "Isolated Network";
105117
ReflectionTestUtils.setField(cmd, "displayText", description);
106118
Assert.assertEquals(cmd.getDisplayText(), description);
107119
}
108120

121+
@Test
109122
public void testGetDisplayTextWhenEmpty() {
110123
String description = null;
111124
String netName = "net-isolated";
112125
ReflectionTestUtils.setField(cmd, "name", netName);
113126
Assert.assertEquals(cmd.getDisplayText(), netName);
114127
}
115128

129+
@Test
116130
public void testGetNetworkDomain() {
117131
String netDomain = "cs1cloud.internal";
118132
ReflectionTestUtils.setField(cmd, "networkDomain", netDomain);
119133
Assert.assertEquals(cmd.getNetworkDomain(), netDomain);
120134
}
121135

136+
@Test
122137
public void testGetProjectId() {
123138
Long projectId = 1L;
124139
ReflectionTestUtils.setField(cmd, "projectId", projectId);
125140
Assert.assertEquals(cmd.getProjectId(), projectId);
126141
}
127142

143+
@Test
128144
public void testGetAclType() {
129145
String aclType = "account";
130146
ReflectionTestUtils.setField(cmd, "aclType", aclType);
131147
Assert.assertEquals(cmd.getAclType(), aclType);
132148
}
133149

150+
@Test
134151
public void testGetSubdomainAccess() {
135152
Boolean subDomAccess = false;
136153
ReflectionTestUtils.setField(cmd, "subdomainAccess", subDomAccess);
137154
Assert.assertEquals(cmd.getSubdomainAccess(), subDomAccess);
138155
}
139156

157+
@Test
140158
public void testGetVpcId() {
141159
Long vpcId = 1L;
142160
ReflectionTestUtils.setField(cmd, "vpcId", vpcId);
143161
Assert.assertEquals(cmd.getVpcId(), vpcId);
144162
}
145163

164+
@Test
146165
public void testGetDisplayNetwork() {
147166
Boolean displayNet = true;
148167
ReflectionTestUtils.setField(cmd, "displayNetwork", displayNet);
149168
Assert.assertEquals(cmd.getDisplayNetwork(), displayNet);
150169
}
151170

171+
@Test
152172
public void testGetExternalId() {
153173
String externalId = "1";
154174
ReflectionTestUtils.setField(cmd, "externalId", externalId);
155175
Assert.assertEquals(cmd.getExternalId(), externalId);
156176
}
157177

178+
@Test
158179
public void testGetAssociatedNetworkId() {
159180
Long associatedNetId = 1L;
160181
ReflectionTestUtils.setField(cmd, "associatedNetworkId", associatedNetId);
161182
Assert.assertEquals(cmd.getAssociatedNetworkId(), associatedNetId);
162183
}
163184

185+
@Test
164186
public void testIsDisplayNullDefaultsToTrue() {
165187
Boolean displayNetwork = null;
166188
ReflectionTestUtils.setField(cmd, "displayNetwork", displayNetwork);
167189
Assert.assertTrue(cmd.isDisplay());
168190
}
169191

192+
@Test
170193
public void testGetPhysicalNetworkIdForInvalidNetOfferingId() {
171194
Long physicalNetworkId = 1L;
172195

173196
ReflectionTestUtils.setField(cmd, "physicalNetworkId", physicalNetworkId);
174-
Mockito.when(_entityMgr.findById(NetworkOffering.class, 1L)).thenReturn(null);
175197
try {
176198
cmd.getPhysicalNetworkId();
177199
} catch (Exception e) {
178200
Assert.assertTrue(e.getMessage().startsWith("Unable to find network offering by ID"));
179201
}
180202
}
181203

204+
@Test
182205
public void testGetPhysicalNetworkIdForInvalidAssociatedNetId() {
183206
Long physicalNetworkId = 1L;
184207
Long networkOfferingId = 1L;
@@ -196,6 +219,7 @@ public void testGetPhysicalNetworkIdForInvalidAssociatedNetId() {
196219
}
197220
}
198221

222+
@Test
199223
public void testGetPhysicalNetworkIdForAssociatedNetIdForNonSharedNet() {
200224
Long physicalNetworkId = 1L;
201225
Long networkOfferingId = 1L;
@@ -215,6 +239,7 @@ public void testGetPhysicalNetworkIdForAssociatedNetIdForNonSharedNet() {
215239
}
216240
}
217241

242+
@Test
218243
public void testGetPhysicalNetworkIdForNonSharedNet() {
219244
Long physicalNetworkId = 1L;
220245
Long networkOfferingId = 1L;
@@ -230,6 +255,7 @@ public void testGetPhysicalNetworkIdForNonSharedNet() {
230255
}
231256
}
232257

258+
@Test
233259
public void testGetPhysicalNetworkIdForSharedNet() {
234260
Long physicalNetworkId = 1L;
235261
Long networkOfferingId = 1L;
@@ -245,6 +271,7 @@ public void testGetPhysicalNetworkIdForSharedNet() {
245271
}
246272
}
247273

274+
@Test
248275
public void testGetZoneId() {
249276
Long physicalNetworkId = 1L;
250277
Long networkOfferingId = 1L;
@@ -258,30 +285,35 @@ public void testGetZoneId() {
258285
Assert.assertEquals(cmd.getZoneId(), zoneId);
259286
}
260287

288+
@Test
261289
public void testGetPublicMtuWhenNotSet() {
262290
Integer publicMtu = null;
263291
ReflectionTestUtils.setField(cmd, "publicMtu", publicMtu);
264292
Assert.assertEquals(NetworkService.DEFAULT_MTU, cmd.getPublicMtu());
265293
}
266294

295+
@Test
267296
public void testGetPublicMtuWhenSet() {
268297
Integer publicMtu = 1450;
269298
ReflectionTestUtils.setField(cmd, "publicMtu", publicMtu);
270299
Assert.assertEquals(cmd.getPublicMtu(), publicMtu);
271300
}
272301

302+
@Test
273303
public void testGetPrivateMtuWhenNotSet() {
274304
Integer privateMtu = null;
275305
ReflectionTestUtils.setField(cmd, "privateMtu", privateMtu);
276306
Assert.assertEquals(NetworkService.DEFAULT_MTU, cmd.getPrivateMtu());
277307
}
278308

309+
@Test
279310
public void testGetPrivateMtuWhenSet() {
280311
Integer privateMtu = 1250;
281312
ReflectionTestUtils.setField(cmd, "privateMtu", privateMtu);
282313
Assert.assertEquals(cmd.getPrivateMtu(), privateMtu);
283314
}
284315

316+
@Test
285317
public void testExecute() throws InsufficientCapacityException, ResourceAllocationException {
286318
ReflectionTestUtils.setField(cmd, "displayText", "testNetwork");
287319
ReflectionTestUtils.setField(cmd, "name", "testNetwork");

0 commit comments

Comments
 (0)