1717package org .apache .cloudstack .resource ;
1818
1919import com .cloud .network .dao .NetworkVO ;
20+ import com .cloud .utils .exception .CloudRuntimeException ;
2021import com .vmware .nsx .model .TransportZone ;
2122import com .vmware .nsx .model .TransportZoneListResult ;
2223import com .vmware .nsx_policy .model .EnforcementPoint ;
3132import org .apache .cloudstack .agent .api .CreateNsxSegmentCommand ;
3233import org .apache .cloudstack .agent .api .CreateNsxStaticNatCommand ;
3334import org .apache .cloudstack .agent .api .CreateNsxTier1GatewayCommand ;
35+ import org .apache .cloudstack .agent .api .CreateOrUpdateNsxTier1NatRuleCommand ;
3436import org .apache .cloudstack .agent .api .DeleteNsxDistributedFirewallRulesCommand ;
3537import org .apache .cloudstack .agent .api .DeleteNsxNatRuleCommand ;
3638import org .apache .cloudstack .agent .api .DeleteNsxSegmentCommand ;
3739import org .apache .cloudstack .agent .api .DeleteNsxTier1GatewayCommand ;
3840import org .apache .cloudstack .agent .api .NsxCommand ;
3941import org .apache .cloudstack .service .NsxApiClient ;
42+ import org .apache .cloudstack .utils .NsxControllerUtils ;
4043import org .junit .After ;
4144import org .junit .Before ;
4245import org .junit .Test ;
4346import org .junit .runner .RunWith ;
4447
4548import org .mockito .Mock ;
49+ import org .mockito .Mockito ;
4650import org .mockito .MockitoAnnotations ;
4751import org .mockito .junit .MockitoJUnitRunner ;
4852
5155import java .util .List ;
5256import java .util .Map ;
5357
58+ import static org .junit .Assert .assertFalse ;
5459import static org .junit .Assert .assertTrue ;
5560import static org .junit .Assert .assertThrows ;
61+ import static org .mockito .ArgumentMatchers .anyBoolean ;
62+ import static org .mockito .ArgumentMatchers .anyLong ;
5663import static org .mockito .ArgumentMatchers .anyString ;
64+ import static org .mockito .Mockito .doThrow ;
5765import static org .mockito .Mockito .mock ;
5866import static org .mockito .Mockito .when ;
5967
@@ -72,12 +80,22 @@ public class NsxResourceTest {
7280 @ Mock
7381 TransportZoneListResult transportZoneListResult ;
7482
83+ private static final String transportZone = "Overlay" ;
84+ private static final String tier0Gateway = "Tier0-GW01" ;
85+ private static final String edgeCluster = "EdgeCluster" ;
86+
87+ private static final long domainId = 1L ;
88+ private static final long accountId = 2L ;
89+ private static final long zoneId = 1L ;
90+
7591 @ Before
7692 public void setup () {
7793 closeable = MockitoAnnotations .openMocks (this );
7894 nsxResource = new NsxResource ();
7995 nsxResource .nsxApiClient = nsxApi ;
80- nsxResource .transportZone = "Overlay" ;
96+ nsxResource .transportZone = transportZone ;
97+ nsxResource .tier0Gateway = tier0Gateway ;
98+ nsxResource .edgeCluster = edgeCluster ;
8199 }
82100
83101 @ After
@@ -94,9 +112,9 @@ public void testConfigure() throws ConfigurationException {
94112 params .put ("hostname" , "host1" );
95113 params .put ("username" , "admin" );
96114 params .put ("password" , "password" );
97- params .put ("tier0Gateway" , "Tier0-GW01" );
98- params .put ("edgeCluster" , "EdgeCluster" );
99- params .put ("transportZone" , "Overlay" );
115+ params .put ("tier0Gateway" , tier0Gateway );
116+ params .put ("edgeCluster" , edgeCluster );
117+ params .put ("transportZone" , transportZone );
100118 params .put ("port" , "443" );
101119
102120 Assert .assertTrue (nsxResource .configure ("nsx" , params ));
@@ -111,22 +129,39 @@ public void testConfigure_MissingParameter() throws ConfigurationException {
111129
112130 @ Test
113131 public void testCreateNsxTier1Gateway () {
114- NsxCommand command = new CreateNsxTier1GatewayCommand (1L , 2L ,
115- 1L , 3L , "VPC01" , true , false );
116-
132+ NsxCommand command = new CreateNsxTier1GatewayCommand (domainId , accountId , zoneId ,
133+ 3L , "VPC01" , true , false );
117134 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
118135 assertTrue (answer .getResult ());
119136 }
120137
121138 @ Test
122- public void testDeleteTier1Gateway () {
123- NsxCommand command = new DeleteNsxTier1GatewayCommand (1L , 1L ,
124- 1L , 2L , "VPC01" , true );
139+ public void testCreateNsxTier1GatewayError () {
140+ NsxCommand command = new CreateNsxTier1GatewayCommand (domainId , accountId , zoneId ,
141+ 3L , "VPC01" , true , false );
142+ Mockito .doThrow (new CloudRuntimeException ("ERROR" ))
143+ .when (nsxApi ).createTier1Gateway (anyString (), anyString (), anyString (), anyBoolean ());
144+ NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
145+ assertFalse (answer .getResult ());
146+ }
125147
148+ @ Test
149+ public void testDeleteTier1Gateway () {
150+ NsxCommand command = new DeleteNsxTier1GatewayCommand (domainId , accountId , zoneId ,
151+ 2L , "VPC01" , true );
126152 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
127153 assertTrue (answer .getResult ());
128154 }
129155
156+ @ Test
157+ public void testDeleteTier1GatewayError () {
158+ NsxCommand command = new DeleteNsxTier1GatewayCommand (domainId , accountId , zoneId ,
159+ 2L , "VPC01" , true );
160+ Mockito .doThrow (new CloudRuntimeException ("ERROR" )).when (nsxApi ).deleteTier1Gateway (anyString ());
161+ NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
162+ assertFalse (answer .getResult ());
163+ }
164+
130165 @ Test
131166 public void testCreateNsxSegment () {
132167 NetworkVO tierNetwork = new NetworkVO ();
@@ -137,10 +172,10 @@ public void testCreateNsxSegment() {
137172 List <Site > siteList = List .of (site );
138173 EnforcementPoint enforcementPoint = mock (EnforcementPoint .class );
139174 List <EnforcementPoint > enforcementPointList = List .of (enforcementPoint );
140- List <TransportZone > transportZoneList = List .of (new TransportZone .Builder ().setDisplayName ("Overlay" ).build ());
175+ List <TransportZone > transportZoneList = List .of (new TransportZone .Builder ().setDisplayName (transportZone ).build ());
141176
142- NsxCommand command = new CreateNsxSegmentCommand (1L , 1L ,
143- 1L , 2L , "VPC01" , 3L , "Web" , "10.10.10.1" , "10.10.10.0/24" );
177+ NsxCommand command = new CreateNsxSegmentCommand (domainId , accountId , zoneId ,
178+ 2L , "VPC01" , 3L , "Web" , "10.10.10.1" , "10.10.10.0/24" );
144179
145180 when (nsxApi .getSites ()).thenReturn (siteListResult );
146181 when (siteListResult .getResults ()).thenReturn (siteList );
@@ -157,33 +192,77 @@ public void testCreateNsxSegment() {
157192 assertTrue (answer .getResult ());
158193 }
159194
195+ @ Test
196+ public void testCreateNsxSegmentEmptySites () {
197+ when (nsxApi .getSites ()).thenReturn (null );
198+ CreateNsxSegmentCommand command = Mockito .mock (CreateNsxSegmentCommand .class );
199+ NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
200+ assertFalse (answer .getResult ());
201+ }
202+
203+ @ Test
204+ public void testCreateNsxSegmentEmptyEnforcementPoints () {
205+ Site site = mock (Site .class );
206+ List <Site > siteList = List .of (site );
207+ when (nsxApi .getSites ()).thenReturn (siteListResult );
208+ when (siteListResult .getResults ()).thenReturn (siteList );
209+ when (siteList .get (0 ).getId ()).thenReturn ("site1" );
210+ when (nsxApi .getEnforcementPoints (anyString ())).thenReturn (null );
211+ CreateNsxSegmentCommand command = Mockito .mock (CreateNsxSegmentCommand .class );
212+ NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
213+ assertFalse (answer .getResult ());
214+ }
215+
216+ @ Test
217+ public void testCreateNsxSegmentEmptyTransportZones () {
218+ Site site = mock (Site .class );
219+ List <Site > siteList = List .of (site );
220+ when (nsxApi .getSites ()).thenReturn (siteListResult );
221+ when (siteListResult .getResults ()).thenReturn (siteList );
222+ when (siteList .get (0 ).getId ()).thenReturn ("site1" );
223+ CreateNsxSegmentCommand command = Mockito .mock (CreateNsxSegmentCommand .class );
224+ NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
225+ assertFalse (answer .getResult ());
226+ }
227+
160228 @ Test
161229 public void testDeleteNsxSegment () {
162230 NetworkVO tierNetwork = new NetworkVO ();
163231 tierNetwork .setName ("tier1" );
164- DeleteNsxSegmentCommand command = new DeleteNsxSegmentCommand (1L , 1L , 1L , 3L , "VPC01" , 2L , "Web" );
165-
232+ DeleteNsxSegmentCommand command = new DeleteNsxSegmentCommand (domainId , accountId , zoneId ,
233+ 3L , "VPC01" , 2L , "Web" );
166234 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
167235 assertTrue (answer .getResult ());
168236 }
169237
238+ @ Test
239+ public void testDeleteNsxSegmentError () {
240+ NetworkVO tierNetwork = new NetworkVO ();
241+ tierNetwork .setName ("tier1" );
242+ DeleteNsxSegmentCommand command = new DeleteNsxSegmentCommand (domainId , accountId , zoneId ,
243+ 3L , "VPC01" , 2L , "Web" );
244+ doThrow (new CloudRuntimeException ("ERROR" )).when (nsxApi ).deleteSegment (anyLong (), anyLong (), anyLong (), anyLong (), anyLong (), anyString ());
245+ NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
246+ assertFalse (answer .getResult ());
247+ }
248+
170249 @ Test
171250 public void testCreateStaticNat () {
172- CreateNsxStaticNatCommand cmd = new CreateNsxStaticNatCommand (1L , 1L , 1L , 3L , "VPC01" , true , 2L , "10.1.12.10" , "172.30.20.12" );
251+ CreateNsxStaticNatCommand cmd = new CreateNsxStaticNatCommand (domainId , accountId , zoneId , 3L , "VPC01" , true , 2L , "10.1.12.10" , "172.30.20.12" );
173252 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (cmd );
174253 assertTrue (answer .getResult ());
175254 }
176255
177256 @ Test
178257 public void testCreatePortForwardRule () {
179- CreateNsxPortForwardRuleCommand cmd = new CreateNsxPortForwardRuleCommand (1L , 1L , 1L , 3L , "VPC01" , true , 2L , 5L , "10.1.12.10" , "172.30.20.12" , "2222" , "22" , "tcp" );
258+ CreateNsxPortForwardRuleCommand cmd = new CreateNsxPortForwardRuleCommand (domainId , accountId , zoneId , 3L , "VPC01" , true , 2L , 5L , "10.1.12.10" , "172.30.20.12" , "2222" , "22" , "tcp" );
180259 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (cmd );
181260 assertTrue (answer .getResult ());
182261 }
183262
184263 @ Test
185264 public void testDeleteNsxNatRule () {
186- DeleteNsxNatRuleCommand cmd = new DeleteNsxNatRuleCommand (1L , 1L , 1L , 3L , "VPC01" , true , 2L , 5L , "22" , "tcp" );
265+ DeleteNsxNatRuleCommand cmd = new DeleteNsxNatRuleCommand (domainId , accountId , zoneId , 3L , "VPC01" , true , 2L , 5L , "22" , "tcp" );
187266 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (cmd );
188267 assertTrue (answer .getResult ());
189268 }
@@ -193,7 +272,7 @@ public void testCreateNsxLoadBalancerRule() {
193272 List <NsxLoadBalancerMember > loadBalancerMembers = List .of (new NsxLoadBalancerMember (
194273 1L , "172.30.20.12" , 6443
195274 ));
196- CreateNsxLoadBalancerRuleCommand cmd = new CreateNsxLoadBalancerRuleCommand (1L , 1L , 1L ,
275+ CreateNsxLoadBalancerRuleCommand cmd = new CreateNsxLoadBalancerRuleCommand (domainId , accountId , zoneId ,
197276 3L , "VPC01" , true , loadBalancerMembers , 1L , "6443" , "6443" , "RoundRobin" , "TCP" );
198277 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (cmd );
199278 assertTrue (answer .getResult ());
@@ -203,7 +282,7 @@ public void testCreateNsxLoadBalancerRule() {
203282 @ Test
204283 public void testCreateNsxDistributedFirewallRule () {
205284 List <NsxNetworkRule > networkRules = List .of (new NsxNetworkRule ());
206- CreateNsxDistributedFirewallRulesCommand cmd = new CreateNsxDistributedFirewallRulesCommand (1L , 1L , 1L ,
285+ CreateNsxDistributedFirewallRulesCommand cmd = new CreateNsxDistributedFirewallRulesCommand (domainId , accountId , zoneId ,
207286 3L , 1L , networkRules );
208287 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (cmd );
209288 assertTrue (answer .getResult ());
@@ -212,11 +291,19 @@ public void testCreateNsxDistributedFirewallRule() {
212291 @ Test
213292 public void testDeleteNsxDistributedFirewallRule () {
214293 List <NsxNetworkRule > networkRules = List .of (new NsxNetworkRule ());
215- DeleteNsxDistributedFirewallRulesCommand cmd = new DeleteNsxDistributedFirewallRulesCommand (1L , 1L , 1L ,
294+ DeleteNsxDistributedFirewallRulesCommand cmd = new DeleteNsxDistributedFirewallRulesCommand (domainId , accountId , zoneId ,
216295 3L , 1L , networkRules );
217296 NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (cmd );
218297 assertTrue (answer .getResult ());
219298 }
220299
221-
300+ @ Test
301+ public void testCreateTier1NatRule () {
302+ long vpcId = 5L ;
303+ String tier1GatewayName = NsxControllerUtils .getTier1GatewayName (domainId , accountId , zoneId , vpcId , true );
304+ CreateOrUpdateNsxTier1NatRuleCommand command = new CreateOrUpdateNsxTier1NatRuleCommand (domainId , accountId , zoneId ,
305+ tier1GatewayName , "SNAT" , "10.1.10.10" , "natRuleId" );
306+ NsxAnswer answer = (NsxAnswer ) nsxResource .executeRequest (command );
307+ assertTrue (answer .getResult ());
308+ }
222309}
0 commit comments