1313// See the License for the specific language governing permissions and
1414// limitations under the License.
1515
16- package com .cloud .vpc ;
16+ package com .cloud .network . vpc ;
1717
1818import static org .mockito .ArgumentMatchers .any ;
1919import static org .mockito .ArgumentMatchers .anyList ;
3030
3131import javax .inject .Inject ;
3232
33+ import com .cloud .server .ResourceTag ;
3334import org .apache .cloudstack .context .CallContext ;
3435import org .apache .cloudstack .engine .orchestration .service .NetworkOrchestrationService ;
3536import org .apache .cloudstack .framework .messagebus .MessageBus ;
5859import com .cloud .network .dao .NetworkServiceMapDao ;
5960import com .cloud .network .dao .NetworkVO ;
6061import com .cloud .network .element .NetworkACLServiceProvider ;
61- import com .cloud .network .vpc .NetworkACLItem ;
6262import com .cloud .network .vpc .NetworkACLItem .State ;
63- import com .cloud .network .vpc .NetworkACLItemDao ;
64- import com .cloud .network .vpc .NetworkACLItemVO ;
65- import com .cloud .network .vpc .NetworkACLManager ;
66- import com .cloud .network .vpc .NetworkACLManagerImpl ;
67- import com .cloud .network .vpc .NetworkACLVO ;
68- import com .cloud .network .vpc .PrivateGateway ;
69- import com .cloud .network .vpc .VpcGateway ;
70- import com .cloud .network .vpc .VpcGatewayVO ;
71- import com .cloud .network .vpc .VpcManager ;
72- import com .cloud .network .vpc .VpcService ;
7363import com .cloud .network .vpc .dao .NetworkACLDao ;
7464import com .cloud .network .vpc .dao .VpcGatewayDao ;
7565import com .cloud .offerings .dao .NetworkOfferingDao ;
8878@ ContextConfiguration (loader = AnnotationConfigContextLoader .class )
8979public class NetworkACLManagerTest extends TestCase {
9080 @ Inject
91- NetworkACLManager _aclMgr ;
81+ NetworkACLManagerImpl _aclMgr ;
9282
9383 @ Inject
9484 AccountManager _accountMgr ;
@@ -103,17 +93,15 @@ public class NetworkACLManagerTest extends TestCase {
10393 @ Inject
10494 NetworkOfferingDao networkOfferingDao ;
10595 @ Inject
106- ConfigurationManager _configMgr ;
107- @ Inject
108- EntityManager _entityMgr ;
109- @ Inject
11096 NetworkModel _networkModel ;
11197 @ Inject
11298 List <NetworkACLServiceProvider > _networkAclElements ;
11399 @ Inject
114100 VpcService _vpcSvc ;
115101 @ Inject
116102 VpcGatewayDao _vpcGatewayDao ;
103+ @ Inject
104+ private ResourceTagDao resourceTagDao ;
117105
118106 private NetworkACLVO acl ;
119107 private NetworkACLItemVO aclItem ;
@@ -154,9 +142,17 @@ public void testApplyACL() throws Exception {
154142 }
155143
156144 @ Test
157- public void testApplyNetworkACL () throws Exception {
145+ public void testApplyNetworkACLsOnGatewayAndInGeneral () throws Exception {
158146 driveTestApplyNetworkACL (true , true , true );
147+ }
148+
149+ @ Test
150+ public void testApplyNetworkACLsOnGatewayOnly () throws Exception {
159151 driveTestApplyNetworkACL (false , false , true );
152+ }
153+
154+ @ Test
155+ public void testApplyNetworkACLsButNotOnGateway () throws Exception {
160156 driveTestApplyNetworkACL (false , true , false );
161157 }
162158
@@ -168,11 +164,12 @@ public void driveTestApplyNetworkACL(final boolean result, final boolean applyNe
168164 // Prepare
169165 // Reset mocked objects to reuse
170166 Mockito .reset (_networkACLItemDao );
167+ Mockito .reset (_networkDao );
171168
172169 // Make sure it is handled
173170 final long aclId = 1L ;
174171 final NetworkVO network = Mockito .mock (NetworkVO .class );
175- final List <NetworkVO > networks = new ArrayList <NetworkVO >();
172+ final List <NetworkVO > networks = new ArrayList <>();
176173 networks .add (network );
177174
178175 NetworkServiceMapDao ntwkSrvcDao = mock (NetworkServiceMapDao .class );
@@ -194,7 +191,7 @@ public void driveTestApplyNetworkACL(final boolean result, final boolean applyNe
194191
195192 // Create 4 rules to test all 4 scenarios: only revoke should
196193 // be deleted, only add should update
197- final List <NetworkACLItemVO > rules = new ArrayList <NetworkACLItemVO >();
194+ final List <NetworkACLItemVO > rules = new ArrayList <>();
198195 final NetworkACLItemVO ruleActive = Mockito .mock (NetworkACLItemVO .class );
199196 final NetworkACLItemVO ruleStaged = Mockito .mock (NetworkACLItemVO .class );
200197 final NetworkACLItemVO rule2Revoke = Mockito .mock (NetworkACLItemVO .class );
@@ -224,7 +221,6 @@ public void driveTestApplyNetworkACL(final boolean result, final boolean applyNe
224221
225222 // Assert if conditions met, network ACL was applied
226223 final int timesProcessingDone = applyNetworkACLs && applyACLToPrivateGw ? 1 : 0 ;
227- Mockito .verify (_networkACLItemDao , Mockito .times (timesProcessingDone )).remove (revokeId );
228224 Mockito .verify (rule2Add , Mockito .times (timesProcessingDone )).setState (NetworkACLItem .State .Active );
229225 Mockito .verify (_networkACLItemDao , Mockito .times (timesProcessingDone )).update (addId , rule2Add );
230226 }
@@ -235,9 +231,20 @@ public void testRevokeACLItem() throws Exception {
235231 assertTrue (_aclMgr .revokeNetworkACLItem (1L ));
236232 }
237233
234+ @ Test
235+ public void testRemoveRule () {
236+ NetworkACLItem aclItem = Mockito .mock (NetworkACLItemVO .class );
237+ when (aclItem .getId ()).thenReturn (1l );
238+ Mockito .when (resourceTagDao .removeByIdAndType (1l , ResourceTag .ResourceObjectType .NetworkACL )).thenReturn (true );
239+ Mockito .when (_networkACLItemDao .remove (1l )).thenReturn (true );
240+ assertTrue (_aclMgr .removeRule (aclItem ));
241+
242+ }
243+
238244 @ Test
239245 public void deleteNonEmptyACL () throws Exception {
240- final List <NetworkACLItemVO > aclItems = new ArrayList <NetworkACLItemVO >();
246+ Mockito .reset (_networkDao );
247+ final List <NetworkACLItemVO > aclItems = new ArrayList <>();
241248 aclItems .add (aclItem );
242249 Mockito .when (_networkACLItemDao .listByACL (anyLong ())).thenReturn (aclItems );
243250 Mockito .when (acl .getId ()).thenReturn (3l );
@@ -342,5 +349,4 @@ public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1)
342349 }
343350 }
344351 }
345-
346352}
0 commit comments