1919
2020import static org .mockito .Matchers .any ;
2121import static org .mockito .Matchers .anyLong ;
22+ import static org .mockito .Matchers .eq ;
23+ import static org .mockito .Matchers .isNull ;
2224import static org .mockito .Mockito .doReturn ;
2325import static org .mockito .Mockito .mock ;
26+ import static org .mockito .Mockito .never ;
27+ import static org .mockito .Mockito .times ;
28+ import static org .mockito .Mockito .verify ;
2429import static org .mockito .Mockito .when ;
2530import static org .mockito .Mockito .spy ;
2631
2732import java .util .ArrayList ;
33+ import java .util .Arrays ;
34+ import java .util .Collections ;
2835import java .util .HashMap ;
2936import java .util .HashSet ;
3037import java .util .List ;
3138import java .util .Map ;
3239import java .util .Set ;
3340
41+ import com .cloud .dc .DataCenter ;
42+ import com .cloud .dc .DataCenterVO ;
43+ import com .cloud .dc .dao .DataCenterDao ;
44+ import com .cloud .network .dao .PhysicalNetworkDao ;
45+ import com .cloud .network .dao .PhysicalNetworkServiceProviderDao ;
46+ import com .cloud .network .dao .PhysicalNetworkServiceProviderVO ;
47+ import com .cloud .network .dao .PhysicalNetworkVO ;
3448import junit .framework .Assert ;
3549
3650import org .junit .Before ;
4862import com .cloud .utils .db .SearchCriteria ;
4963import com .cloud .utils .net .Ip ;
5064import com .cloud .network .Network .Provider ;
65+ import org .mockito .InjectMocks ;
66+ import org .mockito .Mock ;
67+ import org .mockito .MockitoAnnotations ;
68+ import org .mockito .Spy ;
5169
5270public class NetworkModelTest {
71+
72+ @ Mock
73+ private DataCenterDao dataCenterDao ;
74+ @ Mock
75+ private PhysicalNetworkDao physicalNetworkDao ;
76+ @ Mock
77+ private PhysicalNetworkServiceProviderDao physicalNetworkServiceProviderDao ;
78+ @ Mock
79+ private NetworkService networkService ;
80+
81+ @ InjectMocks
82+ @ Spy
83+ private NetworkModelImpl networkModel = new NetworkModelImpl ();
84+
85+ @ Mock
86+ private DataCenterVO zone1 ;
87+ @ Mock
88+ private DataCenterVO zone2 ;
89+ @ Mock
90+ private PhysicalNetworkVO physicalNetworkZone1 ;
91+ @ Mock
92+ private PhysicalNetworkVO physicalNetworkZone2 ;
93+ @ Mock
94+ private PhysicalNetworkServiceProviderVO providerVO ;
95+
96+ private static final long ZONE_1_ID = 1L ;
97+ private static final long ZONE_2_ID = 2L ;
98+ private static final long PHYSICAL_NETWORK_1_ID = 1L ;
99+ private static final long PHYSICAL_NETWORK_2_ID = 2L ;
100+
53101 @ Before
54102 public void setUp () {
55-
103+ MockitoAnnotations .initMocks (this );
104+
105+ when (dataCenterDao .listEnabledZones ()).thenReturn (Arrays .asList (zone1 , zone2 ));
106+ when (physicalNetworkDao .listByZoneAndTrafficType (ZONE_1_ID , Networks .TrafficType .Guest )).
107+ thenReturn (Collections .singletonList (physicalNetworkZone1 ));
108+ when (physicalNetworkDao .listByZoneAndTrafficType (ZONE_2_ID , Networks .TrafficType .Guest )).
109+ thenReturn (Collections .singletonList (physicalNetworkZone2 ));
110+ when (physicalNetworkServiceProviderDao .findByServiceProvider (
111+ PHYSICAL_NETWORK_1_ID , Network .Provider .ConfigDrive .getName ())).thenReturn (null );
112+ when (physicalNetworkServiceProviderDao .findByServiceProvider (
113+ PHYSICAL_NETWORK_2_ID , Network .Provider .ConfigDrive .getName ())).thenReturn (null );
114+
115+ when (zone1 .getNetworkType ()).thenReturn (DataCenter .NetworkType .Advanced );
116+ when (zone1 .getId ()).thenReturn (ZONE_1_ID );
117+
118+ when (zone2 .getNetworkType ()).thenReturn (DataCenter .NetworkType .Advanced );
119+ when (zone2 .getId ()).thenReturn (ZONE_2_ID );
120+
121+ when (physicalNetworkZone1 .getId ()).thenReturn (PHYSICAL_NETWORK_1_ID );
122+ when (physicalNetworkZone2 .getId ()).thenReturn (PHYSICAL_NETWORK_2_ID );
56123 }
57124
58125 @ Test
@@ -69,7 +136,7 @@ public void testGetSourceNatIpAddressForGuestNetwork() {
69136 when (fakeVlanDao .findById (anyLong ())).thenReturn (mock (VlanVO .class ));
70137 modelImpl ._vlanDao = fakeVlanDao ;
71138 when (fakeSearch .create ()).thenReturn (mock (SearchCriteria .class ));
72- when (ipAddressDao .search (any (SearchCriteria .class ), (Filter )org . mockito . Matchers . isNull ())).thenReturn (fakeList );
139+ when (ipAddressDao .search (any (SearchCriteria .class ), (Filter ) isNull ())).thenReturn (fakeList );
73140 when (ipAddressDao .findById (anyLong ())).thenReturn (fakeIp );
74141 Account fakeAccount = mock (Account .class );
75142 when (fakeAccount .getId ()).thenReturn (1L );
@@ -134,5 +201,53 @@ public void testCapabilityForProvider() {
134201 }
135202 }
136203
204+ @ Test
205+ public void testVerifyDisabledConfigDriveEntriesOnZonesBothEnabledZones () {
206+ networkModel .verifyDisabledConfigDriveEntriesOnEnabledZones ();
207+ verify (networkModel , times (2 )).addDisabledConfigDriveEntriesOnZone (any (DataCenterVO .class ));
208+ }
209+
210+ @ Test
211+ public void testVerifyDisabledConfigDriveEntriesOnZonesOneEnabledZone () {
212+ when (dataCenterDao .listEnabledZones ()).thenReturn (Collections .singletonList (zone1 ));
213+
214+ networkModel .verifyDisabledConfigDriveEntriesOnEnabledZones ();
215+ verify (networkModel ).addDisabledConfigDriveEntriesOnZone (any (DataCenterVO .class ));
216+ }
217+
218+ @ Test
219+ public void testVerifyDisabledConfigDriveEntriesOnZonesNoEnabledZones () {
220+ when (dataCenterDao .listEnabledZones ()).thenReturn (null );
221+
222+ networkModel .verifyDisabledConfigDriveEntriesOnEnabledZones ();
223+ verify (networkModel , never ()).addDisabledConfigDriveEntriesOnZone (any (DataCenterVO .class ));
224+ }
225+
226+ @ Test
227+ public void testAddDisabledConfigDriveEntriesOnZoneBasicZone () {
228+ when (zone1 .getNetworkType ()).thenReturn (DataCenter .NetworkType .Basic );
229+
230+ networkModel .addDisabledConfigDriveEntriesOnZone (zone1 );
231+ verify (physicalNetworkDao , never ()).listByZoneAndTrafficType (ZONE_1_ID , Networks .TrafficType .Guest );
232+ verify (networkService , never ()).
233+ addProviderToPhysicalNetwork (anyLong (), eq (Provider .ConfigDrive .getName ()), isNull (Long .class ), isNull (List .class ));
234+ }
235+
236+ @ Test
237+ public void testAddDisabledConfigDriveEntriesOnZoneAdvancedZoneExistingConfigDrive () {
238+ when (physicalNetworkServiceProviderDao .findByServiceProvider (
239+ PHYSICAL_NETWORK_1_ID , Network .Provider .ConfigDrive .getName ())).thenReturn (providerVO );
240+
241+ networkModel .addDisabledConfigDriveEntriesOnZone (zone1 );
242+ verify (networkService , never ()).
243+ addProviderToPhysicalNetwork (anyLong (), eq (Provider .ConfigDrive .getName ()), isNull (Long .class ), isNull (List .class ));
244+ }
245+
246+ @ Test
247+ public void testAddDisabledConfigDriveEntriesOnZoneAdvancedZoneNonExistingConfigDrive () {
248+ networkModel .addDisabledConfigDriveEntriesOnZone (zone1 );
249+ verify (networkService ).
250+ addProviderToPhysicalNetwork (anyLong (), eq (Provider .ConfigDrive .getName ()), isNull (Long .class ), isNull (List .class ));
251+ }
137252
138253}
0 commit comments