Skip to content

Commit a795f88

Browse files
nvazquezPearl1594
authored andcommitted
Increase code coverage (#54)
* Increase code coverage * More unit tests * Remove credentials and mock api client * NetrisResource tests * Fix unit test
1 parent 03f954c commit a795f88

File tree

9 files changed

+831
-27
lines changed

9 files changed

+831
-27
lines changed

plugins/network-elements/netris/src/main/java/org/apache/cloudstack/resource/NetrisResource.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,15 @@ private Answer executeRequest(CreateOrUpdateNetrisLoadBalancerRuleCommand cmd) {
362362
boolean result = netrisApiClient.createOrUpdateLbRule(cmd);
363363
if (!result) {
364364
return new NetrisAnswer(cmd, false, String.format("Failed to create Netris LB rule for %s: %s, " +
365-
"for private port: %s and public port: %s", getNetworkType(cmd.isVpc()), cmd.getName(), cmd.getPrivatePort(), cmd.getPublicPort(), cmd.getPublicPort()));
365+
"for private port: %s and public port: %s", getNetworkType(cmd.isVpc()), cmd.getName(), cmd.getPrivatePort(), cmd.getPublicPort()));
366366
}
367367
return new NetrisAnswer(cmd, true, "OK");
368368
}
369369

370370
private Answer executeRequest(DeleteNetrisLoadBalancerRuleCommand cmd) {
371371
boolean result = netrisApiClient.deleteLbRule(cmd);
372372
if (!result) {
373-
return new NetrisAnswer(cmd, false, String.format("Failed to delete Netris LB rule for %s: %s, " +
374-
"for private port: %s and public port: %s", getNetworkType(cmd.isVpc()), cmd.getName()));
373+
return new NetrisAnswer(cmd, false, String.format("Failed to delete Netris LB rule for %s: %s", getNetworkType(cmd.isVpc()), cmd.getName()));
375374
}
376375
return new NetrisAnswer(cmd, true, "OK");
377376
}

plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClientImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ public class NetrisApiClientImpl implements NetrisApiClient {
139139
private static final String ANY_IP = "0.0.0.0/0";
140140
private static final String[] PROTOCOL_LIST = new String[]{"TCP", "UDP", "ICMP", "ALL"};
141141

142-
private static ApiClient apiClient;
142+
protected ApiClient apiClient;
143143

144-
private final int siteId;
144+
protected final int siteId;
145145
private final String siteName;
146-
private final int tenantId;
146+
protected final int tenantId;
147147
private final String tenantName;
148148

149149
public NetrisApiClientImpl(String endpointBaseUrl, String username, String password, String siteName, String adminTenantName) {

plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected static List<AutoScaleCounter> getNetrisAutoScaleCounters() {
159159
return counterList;
160160
}
161161

162-
private static Map<Network.Service, Map<Network.Capability, String>> initCapabilities() {
162+
protected static Map<Network.Service, Map<Network.Capability, String>> initCapabilities() {
163163
Map<Network.Service, Map<Network.Capability, String>> capabilities = new HashMap<>();
164164

165165
Map<Network.Capability, String> dhcpCapabilities = Map.of(Network.Capability.DhcpAccrossMultipleSubnets, "true");

plugins/network-elements/netris/src/test/java/org/apache/cloudstack/resource/NetrisResourceObjectUtilsTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.resource;
1818

19+
import org.apache.cloudstack.agent.api.CreateNetrisVnetCommand;
1920
import org.apache.cloudstack.agent.api.CreateNetrisVpcCommand;
2021
import org.apache.cloudstack.agent.api.CreateOrUpdateNetrisNatCommand;
2122
import org.apache.cloudstack.agent.api.DeleteNetrisVpcCommand;
@@ -28,7 +29,7 @@ public class NetrisResourceObjectUtilsTest {
2829
private static final long accountId = 2;
2930
private static final long domainId = 2;
3031

31-
private static final long vpcId = 10;
32+
private static final long vpcId = 8;
3233
private static final String vpcName = "testVpc";
3334
private static final String vpcCidr = "10.10.0.0/16";
3435

@@ -40,6 +41,14 @@ public void testCreateVpcName() {
4041
Assert.assertEquals(expectedNetrisVpcName, netrisVpcName);
4142
}
4243

44+
@Test
45+
public void testCreateVpcNameWithSuffix() {
46+
CreateNetrisVpcCommand cmd = new CreateNetrisVpcCommand(zoneId, accountId, domainId, vpcName, vpcCidr, vpcId, true);
47+
String netrisVpcName = NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.VPC, String.valueOf(vpcId));
48+
String expectedNetrisVpcName = String.format("D%s-A%s-Z%s-V%s", domainId, accountId, zoneId, vpcId);
49+
Assert.assertEquals(expectedNetrisVpcName, netrisVpcName);
50+
}
51+
4352
@Test
4453
public void testCreateVpcIpamAllocationName() {
4554
CreateNetrisVpcCommand cmd = new CreateNetrisVpcCommand(zoneId, accountId, domainId, vpcName, vpcCidr, vpcId, true);
@@ -66,4 +75,24 @@ public void testSuffixesForDNAT() {
6675
String expectedNetrisRuleName = String.format("D%s-A%s-Z%s-V%s-DNAT-R%s", domainId, accountId, zoneId, vpcId, ruleId);
6776
Assert.assertEquals(expectedNetrisRuleName, ruleName);
6877
}
78+
79+
@Test
80+
public void testSubnetName() {
81+
String vNetName = "<NETRIS_VNET_NAME>";
82+
Long vpcTierNetworkId = 240L;
83+
String vpcTierNetworkCidr = "10.10.30.0/24";
84+
String vpcTierNetworkGateway = "10.10.30.1";
85+
CreateNetrisVnetCommand cmd = new CreateNetrisVnetCommand(zoneId, accountId, domainId, vpcName, vpcId, vNetName, vpcTierNetworkId, vpcTierNetworkCidr, vpcTierNetworkGateway, true);
86+
String subnetName = NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.IPAM_SUBNET, String.valueOf(vpcId), vpcTierNetworkCidr);
87+
String expectedName = String.format("D%s-A%s-Z%s-V%s-%s", domainId, accountId, zoneId, vpcId, vpcTierNetworkCidr);
88+
Assert.assertEquals(expectedName, subnetName);
89+
}
90+
91+
@Test
92+
public void testSourceNatName() {
93+
CreateOrUpdateNetrisNatCommand cmd = new CreateOrUpdateNetrisNatCommand(zoneId, accountId, domainId, vpcName, vpcId, null, null, true, vpcCidr);
94+
String snatRuleName = NetrisResourceObjectUtils.retrieveNetrisResourceObjectName(cmd, NetrisResourceObjectUtils.NetrisObjectType.SNAT, String.valueOf(vpcId));
95+
String expectedName = String.format("D%s-A%s-Z%s-V%s-SNAT", domainId, accountId, zoneId, vpcId);
96+
Assert.assertEquals(expectedName, snatRuleName);
97+
}
6998
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
package org.apache.cloudstack.resource;
18+
19+
import com.cloud.agent.api.Answer;
20+
import com.cloud.agent.api.Command;
21+
import org.apache.cloudstack.agent.api.AddOrUpdateNetrisStaticRouteCommand;
22+
import org.apache.cloudstack.agent.api.CreateNetrisACLCommand;
23+
import org.apache.cloudstack.agent.api.CreateNetrisVnetCommand;
24+
import org.apache.cloudstack.agent.api.CreateNetrisVpcCommand;
25+
import org.apache.cloudstack.agent.api.CreateOrUpdateNetrisLoadBalancerRuleCommand;
26+
import org.apache.cloudstack.agent.api.DeleteNetrisACLCommand;
27+
import org.apache.cloudstack.agent.api.DeleteNetrisLoadBalancerRuleCommand;
28+
import org.apache.cloudstack.agent.api.DeleteNetrisNatRuleCommand;
29+
import org.apache.cloudstack.agent.api.DeleteNetrisStaticRouteCommand;
30+
import org.apache.cloudstack.agent.api.DeleteNetrisVnetCommand;
31+
import org.apache.cloudstack.agent.api.DeleteNetrisVpcCommand;
32+
import org.apache.cloudstack.agent.api.ReleaseNatIpCommand;
33+
import org.apache.cloudstack.agent.api.SetupNetrisPublicRangeCommand;
34+
import org.apache.cloudstack.service.NetrisApiClient;
35+
import org.junit.After;
36+
import org.junit.Assert;
37+
import org.junit.Before;
38+
import org.junit.Test;
39+
import org.mockito.InjectMocks;
40+
import org.mockito.Mock;
41+
import org.mockito.Mockito;
42+
import org.mockito.MockitoAnnotations;
43+
import org.mockito.Spy;
44+
45+
import java.util.Arrays;
46+
import java.util.List;
47+
48+
public class NetrisResourceTest {
49+
50+
@Mock
51+
private NetrisApiClient netrisApiClient;
52+
53+
@Spy
54+
@InjectMocks
55+
private NetrisResource netrisResource = new NetrisResource();
56+
57+
private AutoCloseable closeable;
58+
59+
@Mock
60+
private CreateNetrisVpcCommand createNetrisVpcCommand;
61+
@Mock
62+
private CreateNetrisVnetCommand createNetrisVnetCommand;
63+
@Mock
64+
private DeleteNetrisVnetCommand deleteNetrisVnetCommand;
65+
@Mock
66+
private DeleteNetrisVpcCommand deleteNetrisVpcCommand;
67+
@Mock
68+
private SetupNetrisPublicRangeCommand setupNetrisPublicRangeCommand;
69+
@Mock
70+
private DeleteNetrisNatRuleCommand deleteNetrisNatRuleCommand;
71+
@Mock
72+
private CreateNetrisACLCommand createNetrisACLCommand;
73+
@Mock
74+
private DeleteNetrisACLCommand deleteNetrisACLCommand;
75+
@Mock
76+
private AddOrUpdateNetrisStaticRouteCommand addOrUpdateNetrisStaticRouteCommand;
77+
@Mock
78+
private DeleteNetrisStaticRouteCommand deleteNetrisStaticRouteCommand;
79+
@Mock
80+
private ReleaseNatIpCommand releaseNatIpCommand;
81+
@Mock
82+
private CreateOrUpdateNetrisLoadBalancerRuleCommand createOrUpdateNetrisLoadBalancerRuleCommand;
83+
@Mock
84+
private DeleteNetrisLoadBalancerRuleCommand deleteNetrisLoadBalancerRuleCommand;
85+
86+
@Before
87+
public void setup() {
88+
closeable = MockitoAnnotations.openMocks(this);
89+
}
90+
91+
@After
92+
public void tearDown() throws Exception {
93+
closeable.close();
94+
}
95+
96+
@Test
97+
public void testExecuteRequest() {
98+
List<Command> commands = Arrays.asList(createNetrisVpcCommand, createNetrisVnetCommand, deleteNetrisVnetCommand,
99+
deleteNetrisVpcCommand, setupNetrisPublicRangeCommand, deleteNetrisNatRuleCommand, createNetrisACLCommand,
100+
deleteNetrisACLCommand, addOrUpdateNetrisStaticRouteCommand, deleteNetrisStaticRouteCommand,
101+
releaseNatIpCommand, createOrUpdateNetrisLoadBalancerRuleCommand, deleteNetrisLoadBalancerRuleCommand);
102+
103+
for (boolean res : new boolean[]{true, false}) {
104+
setMocksToValue(res);
105+
for (Command command : commands) {
106+
Answer answer = netrisResource.executeRequest(command);
107+
Assert.assertEquals(res, answer.getResult());
108+
}
109+
}
110+
111+
Mockito.verify(netrisApiClient, Mockito.times(2)).createVpc(createNetrisVpcCommand);
112+
Mockito.verify(netrisApiClient, Mockito.times(2)).createVnet(createNetrisVnetCommand);
113+
Mockito.verify(netrisApiClient, Mockito.times(2)).deleteVnet(deleteNetrisVnetCommand);
114+
Mockito.verify(netrisApiClient, Mockito.times(2)).deleteVpc(deleteNetrisVpcCommand);
115+
Mockito.verify(netrisApiClient, Mockito.times(2)).setupZoneLevelPublicRange(setupNetrisPublicRangeCommand);
116+
Mockito.verify(netrisApiClient, Mockito.times(2)).deleteNatRule(deleteNetrisNatRuleCommand);
117+
Mockito.verify(netrisApiClient, Mockito.times(2)).addAclRule(createNetrisACLCommand);
118+
Mockito.verify(netrisApiClient, Mockito.times(2)).deleteAclRule(deleteNetrisACLCommand);
119+
Mockito.verify(netrisApiClient, Mockito.times(2)).addOrUpdateStaticRoute(addOrUpdateNetrisStaticRouteCommand);
120+
Mockito.verify(netrisApiClient, Mockito.times(2)).deleteStaticRoute(deleteNetrisStaticRouteCommand);
121+
Mockito.verify(netrisApiClient, Mockito.times(2)).releaseNatIp(releaseNatIpCommand);
122+
Mockito.verify(netrisApiClient, Mockito.times(2)).createOrUpdateLbRule(createOrUpdateNetrisLoadBalancerRuleCommand);
123+
Mockito.verify(netrisApiClient, Mockito.times(2)).deleteLbRule(deleteNetrisLoadBalancerRuleCommand);
124+
}
125+
126+
private void setMocksToValue(boolean value) {
127+
Mockito.when(netrisApiClient.createVpc(createNetrisVpcCommand)).thenReturn(value);
128+
Mockito.when(netrisApiClient.createVnet(createNetrisVnetCommand)).thenReturn(value);
129+
Mockito.when(netrisApiClient.deleteVnet(deleteNetrisVnetCommand)).thenReturn(value);
130+
Mockito.when(netrisApiClient.deleteVpc(deleteNetrisVpcCommand)).thenReturn(value);
131+
Mockito.when(netrisApiClient.setupZoneLevelPublicRange(setupNetrisPublicRangeCommand)).thenReturn(value);
132+
Mockito.when(netrisApiClient.deleteNatRule(deleteNetrisNatRuleCommand)).thenReturn(value);
133+
Mockito.when(netrisApiClient.addAclRule(createNetrisACLCommand)).thenReturn(value);
134+
Mockito.when(netrisApiClient.deleteAclRule(deleteNetrisACLCommand)).thenReturn(value);
135+
Mockito.when(netrisApiClient.addOrUpdateStaticRoute(addOrUpdateNetrisStaticRouteCommand)).thenReturn(value);
136+
Mockito.when(netrisApiClient.deleteStaticRoute(deleteNetrisStaticRouteCommand)).thenReturn(value);
137+
Mockito.when(netrisApiClient.releaseNatIp(releaseNatIpCommand)).thenReturn(value);
138+
Mockito.when(netrisApiClient.createOrUpdateLbRule(createOrUpdateNetrisLoadBalancerRuleCommand)).thenReturn(value);
139+
Mockito.when(netrisApiClient.deleteLbRule(deleteNetrisLoadBalancerRuleCommand)).thenReturn(value);
140+
}
141+
}

plugins/network-elements/netris/src/test/java/org/apache/cloudstack/service/NetrisApiClientImplTest.java

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,88 @@
1616
// under the License.
1717
package org.apache.cloudstack.service;
1818

19-
import io.netris.ApiException;
19+
import io.netris.ApiClient;
20+
import io.netris.ApiResponse;
21+
import io.netris.api.v1.AuthenticationApi;
22+
import io.netris.api.v1.SitesApi;
23+
import io.netris.api.v1.TenantsApi;
2024
import io.netris.model.GetSiteBody;
21-
import io.netris.model.VPCListing;
25+
import io.netris.model.SitesResponseOK;
26+
import io.netris.model.response.AuthResponse;
2227
import io.netris.model.response.TenantResponse;
28+
import io.netris.model.response.TenantsResponse;
29+
import org.junit.After;
2330
import org.junit.Assert;
31+
import org.junit.Before;
2432
import org.junit.Test;
33+
import org.mockito.InjectMocks;
34+
import org.mockito.MockedConstruction;
35+
import org.mockito.Mockito;
36+
import org.mockito.Spy;
2537

38+
import java.math.BigDecimal;
2639
import java.util.List;
2740

2841
public class NetrisApiClientImplTest {
2942

30-
private static final String endpointUrl = "https://shapeblue-ctl.netris.dev";
31-
private static final String username = "netris";
32-
private static final String password = "qHHa$CZ2oJv*@!7mwoSR";
43+
private static final String endpointUrl = "https://my-netris-controller.localdomain";
44+
private static final String username = "user";
45+
private static final String password = "password";
3346
private static final String siteName = "Datacenter-1";
3447
private static final String adminTenantName = "Admin";
48+
private static final int siteId = 1;
49+
private static final int adminTenantId = 1;
3550

36-
private static final NetrisApiClientImpl client = new NetrisApiClientImpl(endpointUrl, username, password, siteName, adminTenantName);
51+
private MockedConstruction<ApiClient> apiClientMockedConstruction;
3752

38-
@Test
39-
public void testNetrisAuthStatus() {
40-
Assert.assertTrue(client.isSessionAlive());
53+
@Spy
54+
@InjectMocks
55+
private NetrisApiClientImpl client;
56+
57+
@Before
58+
public void setUp() {
59+
GetSiteBody site = Mockito.mock(GetSiteBody.class);
60+
SitesApi sitesApiMock = Mockito.mock(SitesApi.class);
61+
Mockito.when(site.getName()).thenReturn(siteName);
62+
Mockito.when(site.getId()).thenReturn(siteId);
63+
TenantsApi tenantsApi = Mockito.mock(TenantsApi.class);
64+
TenantResponse tenant = Mockito.mock(TenantResponse.class);
65+
Mockito.when(tenant.getName()).thenReturn(adminTenantName);
66+
Mockito.when(tenant.getId()).thenReturn(new BigDecimal(adminTenantId));
67+
68+
apiClientMockedConstruction = Mockito.mockConstruction(ApiClient.class, (mock, context) -> {
69+
SitesResponseOK sitesResponse = Mockito.mock(SitesResponseOK.class);
70+
Mockito.when(sitesResponse.getData()).thenReturn(List.of(site));
71+
Mockito.when(sitesApiMock.apiSitesGet()).thenReturn(sitesResponse);
72+
Mockito.when(mock.getApiStubForMethod(SitesApi.class)).thenReturn(sitesApiMock);
73+
Mockito.when(mock.getApiStubForMethod(TenantsApi.class)).thenReturn(tenantsApi);
74+
ApiResponse<TenantsResponse> tenantsResponse = Mockito.mock(ApiResponse.class);
75+
Mockito.when(tenantsApi.apiTenantsGet()).thenReturn(tenantsResponse);
76+
TenantsResponse tenantsResponseData = Mockito.mock(TenantsResponse.class);
77+
Mockito.when(tenantsResponseData.getData()).thenReturn(List.of(tenant));
78+
Mockito.when(tenantsResponse.getData()).thenReturn(tenantsResponseData);
79+
AuthenticationApi authenticationApi = Mockito.mock(AuthenticationApi.class);
80+
Mockito.when(mock.getApiStubForMethod(AuthenticationApi.class)).thenReturn(authenticationApi);
81+
ApiResponse<AuthResponse> authResponseApiResponse = Mockito.mock(ApiResponse.class);
82+
Mockito.when(authenticationApi.apiAuthGet()).thenReturn(authResponseApiResponse);
83+
Mockito.when(authResponseApiResponse.getStatusCode()).thenReturn(200);
84+
});
85+
client = new NetrisApiClientImpl(endpointUrl, username, password, siteName, adminTenantName);
4186
}
4287

43-
@Test
44-
public void testListSites() {
45-
List<GetSiteBody> sites = client.listSites();
46-
Assert.assertTrue(sites.size() > 0);
88+
@After
89+
public void tearDown() {
90+
apiClientMockedConstruction.close();
4791
}
4892

4993
@Test
50-
public void testListVpcs() {
51-
List<VPCListing> vpcs = client.listVPCs();
52-
Assert.assertTrue(vpcs.size() > 0);
94+
public void testConstructor() {
95+
Assert.assertEquals(siteId, client.siteId);
96+
Assert.assertEquals(adminTenantId, client.tenantId);
5397
}
5498

5599
@Test
56-
public void testListTenants() throws ApiException {
57-
List<TenantResponse> tenants = client.listTenants();
58-
Assert.assertTrue(tenants.size() > 0);
100+
public void testNetrisAuthStatus() {
101+
Assert.assertTrue(client.isSessionAlive());
59102
}
60103
}

0 commit comments

Comments
 (0)