2424import static org .mockito .Mockito .mock ;
2525import static org .mockito .Mockito .times ;
2626import static org .mockito .Mockito .when ;
27+ import static org .mockito .Mockito .doReturn ;
2728
2829import java .lang .reflect .Field ;
2930import java .lang .reflect .Modifier ;
6162import com .cloud .dc .DataCenterVO ;
6263import com .cloud .dc .dao .DataCenterDao ;
6364import com .cloud .exception .InsufficientCapacityException ;
64- import com .cloud .exception .InvalidParameterValueException ;
6565import com .cloud .exception .ResourceAllocationException ;
6666import com .cloud .network .dao .IPAddressDao ;
6767import com .cloud .network .dao .IPAddressVO ;
9696import com .cloud .vm .VirtualMachine ;
9797import com .cloud .vm .dao .DomainRouterDao ;
9898import com .cloud .vm .dao .NicDao ;
99-
99+ import com .cloud .offering .ServiceOffering ;
100+ import com .cloud .service .ServiceOfferingVO ;
101+ import com .cloud .service .dao .ServiceOfferingDao ;
102+ import com .cloud .exception .InvalidParameterValueException ;
100103
101104@ PowerMockIgnore ("javax.management.*" )
102105@ RunWith (PowerMockRunner .class )
@@ -154,6 +157,10 @@ public class NetworkServiceImplTest {
154157 AccountService accountService ;
155158 @ Mock
156159 NetworkHelper networkHelper ;
160+ @ Mock
161+ ServiceOfferingDao serviceOfferingDaoMock ;
162+ @ Mock
163+ ServiceOfferingVO serviceOfferingVoMock ;
157164
158165 @ InjectMocks
159166 AccountManagerImpl accountManagerImpl ;
@@ -171,7 +178,8 @@ public class NetworkServiceImplTest {
171178 @ Mock
172179 private Account accountMock ;
173180 @ InjectMocks
174- private NetworkServiceImpl service = new NetworkServiceImpl ();
181+ NetworkServiceImpl service = new NetworkServiceImpl ();
182+
175183 private static final String VLAN_ID_900 = "900" ;
176184 private static final String VLAN_ID_901 = "901" ;
177185 private static final String VLAN_ID_902 = "902" ;
@@ -200,6 +208,8 @@ private void registerCallContext() {
200208 CallContext .register (user , account );
201209 }
202210
211+ Class <InvalidParameterValueException > expectedException = InvalidParameterValueException .class ;
212+
203213 @ Before
204214 public void setup () throws Exception {
205215 MockitoAnnotations .initMocks (this );
@@ -241,6 +251,7 @@ public void setup() throws Exception {
241251 Mockito .lenient ().doNothing ().when (accountMgr ).checkAccess (accountMock , networkOffering , dc );
242252 Mockito .when (accountMgr .isRootAdmin (accountMock .getId ())).thenReturn (true );
243253 }
254+
244255 @ Test
245256 public void testGetPrivateVlanPairNoVlans () {
246257 Pair <String , Network .PVlanType > pair = service .getPrivateVlanPair (null , null , null );
@@ -713,4 +724,50 @@ public void testCheckAndUpdateNetworkResetSuccess() {
713724 Assert .assertNull (networkVO .getIp6Dns1 ());
714725 Assert .assertNull (networkVO .getIp6Dns2 ());
715726 }
727+ @ Test
728+ public void validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouterTestMustThrowInvalidParameterValueExceptionWhenServiceOfferingIsNull () {
729+ doReturn (null ).when (serviceOfferingDaoMock ).findById (anyLong ());
730+
731+ String expectedMessage = String .format ("Could not find specified service offering [%s]." , 1l );
732+ InvalidParameterValueException assertThrows = Assert .assertThrows (expectedException , () -> {
733+ service .validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouter (1l );
734+ });
735+
736+ Assert .assertEquals (expectedMessage , assertThrows .getMessage ());
737+ }
738+
739+ @ Test
740+ public void validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouterTestMustThrowInvalidParameterValueExceptionWhenServiceOfferingStateIsInactive () {
741+ doReturn (serviceOfferingVoMock ).when (serviceOfferingDaoMock ).findById (anyLong ());
742+ doReturn (ServiceOffering .State .Inactive ).when (serviceOfferingVoMock ).getState ();
743+
744+ String expectedMessage = String .format ("The specified service offering [%s] is inactive." , serviceOfferingVoMock );
745+ InvalidParameterValueException assertThrows = Assert .assertThrows (expectedException , () -> {
746+ service .validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouter (1l );
747+ });
748+
749+ Assert .assertEquals (expectedMessage , assertThrows .getMessage ());
750+ }
751+
752+ @ Test
753+ public void validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouterTestMustThrowInvalidParameterValueExceptionWhenSystemVmTypeIsNotDomainRouter () {
754+ doReturn (serviceOfferingVoMock ).when (serviceOfferingDaoMock ).findById (anyLong ());
755+ doReturn (ServiceOffering .State .Active ).when (serviceOfferingVoMock ).getState ();
756+ doReturn (VirtualMachine .Type .ElasticLoadBalancerVm .toString ()).when (serviceOfferingVoMock ).getSystemVmType ();
757+
758+ String expectedMessage = String .format ("The specified service offering [%s] is of type [%s]. Virtual routers can only be created with service offering of type [%s]." ,
759+ serviceOfferingVoMock , serviceOfferingVoMock .getSystemVmType (), VirtualMachine .Type .DomainRouter .toString ().toLowerCase ());
760+ InvalidParameterValueException assertThrows = Assert .assertThrows (expectedException , () -> {
761+ service .validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouter (1l );
762+ });
763+
764+ Assert .assertEquals (expectedMessage , assertThrows .getMessage ());
765+ }
766+
767+ @ Test
768+ public void validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouterTestMustNotThrowInvalidParameterValueExceptionWhenSystemVmTypeIsDomainRouter () {
769+ NetworkServiceImpl networkServiceImplMock = mock (NetworkServiceImpl .class );
770+
771+ networkServiceImplMock .validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouter (1l );
772+ }
716773}
0 commit comments