1616// under the License.
1717package com .cloud .network .router ;
1818
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertFalse ;
21+ import static org .junit .Assert .assertNotNull ;
22+ import static org .junit .Assert .assertTrue ;
23+ import static org .mockito .ArgumentMatchers .any ;
24+ import static org .mockito .Mockito .doReturn ;
25+ import static org .mockito .Mockito .lenient ;
26+ import static org .mockito .Mockito .mock ;
27+ import static org .mockito .Mockito .spy ;
28+ import static org .mockito .Mockito .times ;
29+ import static org .mockito .Mockito .verify ;
30+ import static org .mockito .Mockito .when ;
31+
32+ import org .apache .cloudstack .engine .orchestration .service .NetworkOrchestrationService ;
33+ import org .apache .cloudstack .network .router .deployment .RouterDeploymentDefinition ;
34+ import org .junit .Before ;
35+ import org .junit .Test ;
36+ import org .junit .runner .RunWith ;
37+ import org .mockito .InjectMocks ;
38+ import org .mockito .Mock ;
39+ import org .mockito .junit .MockitoJUnitRunner ;
40+
1941import com .cloud .agent .AgentManager ;
2042import com .cloud .agent .api .Answer ;
2143import com .cloud .agent .api .Command ;
2244import com .cloud .agent .manager .Commands ;
2345import com .cloud .exception .AgentUnavailableException ;
2446import com .cloud .exception .OperationTimedoutException ;
2547import com .cloud .exception .ResourceUnavailableException ;
48+ import com .cloud .hypervisor .Hypervisor ;
2649import com .cloud .network .NetworkModel ;
50+ import com .cloud .network .VirtualRouterProvider ;
2751import com .cloud .network .dao .NetworkDao ;
52+ import com .cloud .service .ServiceOfferingVO ;
53+ import com .cloud .storage .VMTemplateVO ;
54+ import com .cloud .user .Account ;
55+ import com .cloud .vm .DomainRouterVO ;
56+ import com .cloud .vm .VirtualMachineName ;
57+ import com .cloud .vm .dao .DomainRouterDao ;
2858import com .cloud .vm .dao .NicDao ;
29- import org .apache .cloudstack .engine .orchestration .service .NetworkOrchestrationService ;
30- import org .junit .Before ;
31- import org .junit .Test ;
32- import org .junit .runner .RunWith ;
33- import org .mockito .InjectMocks ;
34- import org .mockito .ArgumentMatchers ;
35- import org .mockito .Mock ;
36- import org .mockito .junit .MockitoJUnitRunner ;
37-
38- import static org .junit .Assert .assertFalse ;
39- import static org .junit .Assert .assertTrue ;
40- import static org .mockito .Mockito .doReturn ;
41- import static org .mockito .Mockito .lenient ;
42- import static org .mockito .Mockito .mock ;
43- import static org .mockito .Mockito .spy ;
44- import static org .mockito .Mockito .times ;
45- import static org .mockito .Mockito .verify ;
46- import static org .mockito .Mockito .when ;
4759
4860
4961@ RunWith (MockitoJUnitRunner .class )
@@ -54,6 +66,9 @@ public class NetworkHelperImplTest {
5466 @ Mock
5567 protected AgentManager agentManager ;
5668
69+ @ Mock
70+ DomainRouterDao routerDao ;
71+
5772 @ InjectMocks
5873 protected NetworkHelperImpl nwHelper = new NetworkHelperImpl ();
5974 @ Mock
@@ -65,10 +80,27 @@ public class NetworkHelperImplTest {
6580 @ Mock
6681 NicDao nicDao ;
6782
83+ @ Mock
84+ private RouterDeploymentDefinition routerDeploymentDefinition ;
85+ @ Mock
86+ private VirtualRouterProvider virtualProvider ;
87+ @ Mock
88+ private Account owner ;
89+ @ Mock
90+ private ServiceOfferingVO routerOffering ;
91+ @ Mock
92+ private VMTemplateVO template ;
93+
6894 @ Before
6995 public void setUp () {
7096 nwHelper ._networkDao = networkDao ;
7197 nwHelper ._networkModel = networkModel ;
98+ when (template .getId ()).thenReturn (1L );
99+ when (template .isDynamicallyScalable ()).thenReturn (true );
100+ when (virtualProvider .getId ()).thenReturn (1L );
101+ when (routerDeploymentDefinition .getVirtualProvider ()).thenReturn (virtualProvider );
102+ when (routerDeploymentDefinition .isRedundant ()).thenReturn (true );
103+ when (routerOffering .getLimitCpuUse ()).thenReturn (true );
72104 }
73105
74106 @ Test (expected =ResourceUnavailableException .class )
@@ -83,7 +115,7 @@ public void testSendCommandsToRouterWrongRouterVersion()
83115 nwHelperUT .sendCommandsToRouter (vr , null );
84116
85117 // Assert
86- verify (this .agentManager , times (0 )).send ((Long ) ArgumentMatchers . any (), (Command ) ArgumentMatchers . any ());
118+ verify (this .agentManager , times (0 )).send ((Long ) any (), (Command ) any ());
87119 }
88120
89121 @ Test
@@ -187,4 +219,53 @@ public void testSendCommandsToRouterWithNoAnswers()
187219 verify (answer1 , times (0 )).getResult ();
188220 assertFalse (result );
189221 }
222+
223+ @ Test
224+ public void testCreateDomainRouter_New () {
225+ long id = 1L ;
226+ long userId = 800L ;
227+ boolean offerHA = false ;
228+ Long vpcId = 900L ;
229+ when (routerDao .persist (any (DomainRouterVO .class ))).thenAnswer (invocation -> invocation .getArgument (0 ));
230+ DomainRouterVO result = nwHelper .createOrUpdateDomainRouter (
231+ null , id , routerDeploymentDefinition , owner , userId , routerOffering , offerHA , vpcId , template );
232+ assertNotNull (result );
233+ assertEquals (id , result .getId ());
234+ assertEquals (routerOffering .getId (), result .getServiceOfferingId ());
235+ assertEquals (1L , result .getElementId ());
236+ assertEquals (VirtualMachineName .getRouterName (id , NetworkHelperImpl .s_vmInstanceName ), result .getInstanceName ());
237+ assertEquals (template .getId (), result .getTemplateId ());
238+ assertEquals (template .getHypervisorType (), result .getHypervisorType ());
239+ assertEquals (template .getGuestOSId (), result .getGuestOSId ());
240+ assertEquals (owner .getDomainId (), result .getDomainId ());
241+ assertEquals (owner .getId (), result .getAccountId ());
242+ assertEquals (userId , result .getUserId ());
243+ assertTrue (result .getIsRedundantRouter ());
244+ assertEquals (VirtualRouter .RedundantState .UNKNOWN , result .getRedundantState ());
245+ assertEquals (offerHA , result .isHaEnabled ());
246+ assertEquals (vpcId , result .getVpcId ());
247+ assertTrue (result .isDynamicallyScalable ());
248+ assertEquals (VirtualRouter .Role .VIRTUAL_ROUTER , result .getRole ());
249+ assertTrue (result .limitCpuUse ());
250+ verify (routerDao ).persist (any (DomainRouterVO .class ));
251+ }
252+
253+ @ Test
254+ public void testUpdateDomainRouter () {
255+ long id = 1L ;
256+ long userId = 800L ;
257+ boolean offerHA = false ;
258+ Long vpcId = 900L ;
259+ DomainRouterVO existing = new DomainRouterVO (id , routerOffering .getId (), virtualProvider .getId (),
260+ VirtualMachineName .getRouterName (id , NetworkHelperImpl .s_vmInstanceName ), 999L , Hypervisor .HypervisorType .KVM , 888L ,
261+ owner .getDomainId (), owner .getId (), userId , routerDeploymentDefinition .isRedundant (), VirtualRouter .RedundantState .UNKNOWN ,
262+ offerHA , false , vpcId );
263+ existing .setDynamicallyScalable (false );
264+ DomainRouterVO result = nwHelper .createOrUpdateDomainRouter (
265+ existing , id , routerDeploymentDefinition , owner , userId , routerOffering , offerHA , vpcId , template );
266+ verify (routerDao ).update (existing .getId (), existing );
267+ assertEquals (template .getId (), result .getTemplateId ());
268+ assertEquals (Hypervisor .HypervisorType .KVM , result .getHypervisorType ());
269+ assertTrue (result .isDynamicallyScalable ());
270+ }
190271}
0 commit comments