Gbp coverage increased in ./neutron/mapper/mapping/rule 71/28371/1
authorVladimir Lavor <vladimir.lavor@pantheon.sk>
Mon, 5 Oct 2015 10:10:53 +0000 (12:10 +0200)
committerVladimir Lavor <vladimir.lavor@pantheon.sk>
Wed, 14 Oct 2015 08:50:16 +0000 (08:50 +0000)
- NeutronSecurityRuleAware.java (+35%, 87% overall)

Signed-off-by: Vladimir Lavor <vladimir.lavor@pantheon.sk>
Change-Id: Iecdf2dc507d76d9e58b253d7ee9ada50b3aed70c

neutron-mapper/src/test/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/rule/NeutronSecurityRuleAwareTest.java

index b8539f1bb3cad12d0e87066877c59acc2149fac7..ad9ca3fb4c9cc73f45af1c2075c9ca13fecbebdc 100644 (file)
@@ -1,12 +1,26 @@
 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.StatusCode;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.group.SecGroupDao;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.test.ConfigDataStoreReader;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.test.GbpDataBrokerTest;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.test.PolicyAssert;
+import org.opendaylight.neutron.spi.NeutronSecurityRule;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
 
-public class NeutronSecurityRuleAwareTest {
+public class NeutronSecurityRuleAwareTest extends GbpDataBrokerTest {
+
+    private static final String RULE_ID = "00000000-0000-0000-0000-000000000001";
+    private static final String RULE_TENANT_ID = "00000000-0000-0000-0000-000000000002";
+    private static final String RULE_GROUP_ID = "00000000-0000-0000-0000-000000000003";
 
     @Test
     public final void testIsDirectionOpposite_InIn() {
@@ -28,4 +42,52 @@ public class NeutronSecurityRuleAwareTest {
         assertTrue(NeutronSecurityRuleAware.isDirectionOpposite(Direction.Out, Direction.In));
     }
 
+    @Test
+    public void testNeutronSecurityRuleCreatedAndDeleted() throws Exception {
+        DataBroker dataProvider = getDataBroker();
+        SecGroupDao secGroupDao = new SecGroupDao();
+        SecRuleDao secRuleDao = new SecRuleDao();
+        NeutronSecurityRuleAware neutronSecurityRuleAware =
+                new NeutronSecurityRuleAware(dataProvider, secRuleDao, secGroupDao);
+
+        //create security rule and put to DS
+        NeutronSecurityRule neutronRule = buildNeutronSecurityRule();
+        assertEquals(neutronSecurityRuleAware.canCreateNeutronSecurityRule(neutronRule),
+                StatusCode.OK);
+        neutronSecurityRuleAware.neutronSecurityRuleCreated(neutronRule);
+
+        //read security rule
+        PolicyAssert.assertContractExists(dataProvider, RULE_TENANT_ID, RULE_ID);
+
+        //compare
+        Contract readContract = ConfigDataStoreReader.readContract(dataProvider, RULE_TENANT_ID, RULE_ID).get();
+        assertNotNull(readContract);
+        assertEquals(readContract.getId().getValue(), RULE_ID);
+
+        assertEquals(neutronSecurityRuleAware.canUpdateNeutronSecurityRule(neutronRule, neutronRule),
+                StatusCode.BAD_REQUEST);
+
+        //delete rule
+        assertEquals(neutronSecurityRuleAware.canDeleteNeutronSecurityRule(neutronRule),
+                StatusCode.OK);
+        neutronSecurityRuleAware.neutronSecurityRuleDeleted(neutronRule);
+        PolicyAssert.assertContractNotExists(dataProvider, RULE_TENANT_ID, RULE_ID);
+    }
+
+    //create neutron security rule
+    private NeutronSecurityRule buildNeutronSecurityRule() {
+        NeutronSecurityRule neutronSecurityRule = new NeutronSecurityRule();
+        neutronSecurityRule.setSecurityRuleUUID(RULE_ID);
+        neutronSecurityRule.setSecurityRuleTenantID(RULE_TENANT_ID);
+        neutronSecurityRule.setSecurityRuleGroupID(RULE_GROUP_ID);
+        neutronSecurityRule.setSecurityRuleRemoteIpPrefix("192.0.0.1/24");
+        neutronSecurityRule.setSecurityRulePortMin(1000);
+        neutronSecurityRule.setSecurityRulePortMax(5000);
+        neutronSecurityRule.setSecurityRuleProtocol("tcp");
+        neutronSecurityRule.setSecurityRuleEthertype("IPv4");
+        neutronSecurityRule.setSecurityRuleDirection("ingress");
+
+        return neutronSecurityRule;
+    }
+
 }