Bug 3243 increased test coverage #1 37/20737/7
authorKinsey Nietzsche <knietzsc@cisco.com>
Tue, 19 May 2015 14:21:19 +0000 (16:21 +0200)
committerMartin Sunal <msunal@cisco.com>
Tue, 26 May 2015 08:21:12 +0000 (08:21 +0000)
- increased test coverage in groupbasedpolicy base
- FIX EgKey.compareTo()

Change-Id: I189169fe1be6e60ed781c016d707f04429a1f950
Signed-off-by: Kinsey Nietzsche <knietzsc@cisco.com>
groupbasedpolicy/pom.xml
groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/resolver/EgKey.java
groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndPointRpcRegistryTest.java [new file with mode: 0644]
groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/EgKeyTest.java [new file with mode: 0644]
groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/IndexedTenantTest.java
groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyResolverTest.java
groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyTest.java [new file with mode: 0644]
groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/RuleGroupTest.java [new file with mode: 0644]

index 4385e34467222d55f8b614798ff7f8b3429215ee..6d48b113eb9cda3a3ddab8f29055209f244c20a8 100644 (file)
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <!-- project build -->
index 9081d0a1fcc5de5809c36f373274010061e53751..a585e35675d2bbc07a8c44de295a64dc5aaccf73 100644 (file)
@@ -59,7 +59,7 @@ public class EgKey implements Comparable<EgKey> {
         String otid = null;
         if (o.tenantId != null) otid = o.tenantId.getValue();
         String egid = null;
-        if (egId != null) tid = egId.getValue();
+        if (egId != null) egid = egId.getValue();
         String oegid = null;
         if (o.egId != null) oegid = o.egId.getValue();
         return ComparisonChain.start()
diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndPointRpcRegistryTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/endpoint/EndPointRpcRegistryTest.java
new file mode 100644 (file)
index 0000000..03d2fca
--- /dev/null
@@ -0,0 +1,77 @@
+package org.opendaylight.groupbasedpolicy.endpoint;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.RpcService;
+
+import com.google.common.util.concurrent.CheckedFuture;
+
+public class EndPointRpcRegistryTest {
+
+    private DataBroker dataProvider;
+    private RpcProviderRegistry rpcRegistry;
+    private RpcRegistration<EndpointService> rpcRegistration;
+    private WriteTransaction t;
+    private EpRendererAugmentation epRendererAugmentation;
+
+    @SuppressWarnings("unchecked")
+    @Before
+    public void initialisation() {
+        dataProvider = mock(DataBroker.class);
+        rpcRegistry = mock(RpcProviderRegistry.class);
+        epRendererAugmentation = mock(EpRendererAugmentation.class);
+
+        t = mock(WriteTransaction.class);
+        when(dataProvider.newWriteOnlyTransaction()).thenReturn(t);
+        CheckedFuture<Void, TransactionCommitFailedException> f = mock(CheckedFuture.class);
+        when(t.submit()).thenReturn(f);
+
+        rpcRegistration = mock(RpcRegistration.class);
+        when(rpcRegistry.addRpcImplementation(any(Class.class), any(RpcService.class))).thenReturn(rpcRegistration);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void registerTest() throws Exception {
+        EndpointRpcRegistry.register(dataProvider, rpcRegistry, epRendererAugmentation);
+        verify(rpcRegistry).addRpcImplementation(any(Class.class), any(RpcService.class));
+        verify(t).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Endpoints.class));
+
+        EndpointRpcRegistry.unregister(epRendererAugmentation);
+        verify(rpcRegistration).close();
+    }
+
+    @Test
+    public void registerTestSafelyFail() {
+        EndpointRpcRegistry.register(null, rpcRegistry, epRendererAugmentation);
+        EndpointRpcRegistry.register(dataProvider, null, epRendererAugmentation);
+        EndpointRpcRegistry.register(null, rpcRegistry, null);
+        EndpointRpcRegistry.register(dataProvider, null, null);
+        EndpointRpcRegistry.register(dataProvider, rpcRegistry, null);
+    }
+
+    @Test
+    public void unregisterTestFail() throws Exception {
+        EndpointRpcRegistry.unregister(null);
+        verify(rpcRegistration, never()).close();
+
+        EpRendererAugmentation epRendererAugmentation = mock(EpRendererAugmentation.class);
+        EndpointRpcRegistry.unregister(epRendererAugmentation);
+        verify(rpcRegistration, never()).close();
+    }
+}
diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/EgKeyTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/EgKeyTest.java
new file mode 100644 (file)
index 0000000..d45053e
--- /dev/null
@@ -0,0 +1,95 @@
+package org.opendaylight.groupbasedpolicy.resolver;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
+
+public class EgKeyTest {
+
+    private EgKey egKey;
+    private TenantId tenantId;
+    private EndpointGroupId egId;
+    private String value;
+
+    @Before
+    public void initialisation() {
+        tenantId = mock(TenantId.class);
+        egId = mock(EndpointGroupId.class);
+
+        value = "value";
+        when(tenantId.getValue()).thenReturn(value);
+        when(egId.getValue()).thenReturn(value);
+
+        egKey = new EgKey(tenantId, egId);
+    }
+
+    @Test
+    public void constructorTest() {
+        Assert.assertEquals(tenantId, egKey.getTenantId());
+        Assert.assertEquals(egId, egKey.getEgId());
+    }
+
+    @Test
+    public void equalsTest() {
+        Assert.assertTrue(egKey.equals(egKey));
+        Assert.assertFalse(egKey.equals(null));
+        Assert.assertFalse(egKey.equals(new Object()));
+
+        EgKey other;
+        other = new EgKey(null, egId);
+        Assert.assertFalse(egKey.equals(other));
+        Assert.assertFalse(other.equals(egKey));
+
+        other = new EgKey(tenantId, null);
+        Assert.assertFalse(egKey.equals(other));
+        Assert.assertFalse(other.equals(egKey));
+
+        other = new EgKey(tenantId, egId);
+        Assert.assertTrue(egKey.equals(other));
+
+        egKey = new EgKey(null, null);
+        other = new EgKey(null, null);
+        Assert.assertTrue(egKey.equals(other));
+    }
+
+    @Test
+    public void compareToTest() {
+        EgKey other = new EgKey(tenantId, egId);
+        Assert.assertEquals(0, egKey.compareTo(other));
+
+        other = new EgKey(null, null);
+        Assert.assertEquals(-1, egKey.compareTo(other));
+        Assert.assertEquals(1, other.compareTo(egKey));
+
+        String valueOther = "valu";
+        TenantId tenantIdOther = mock(TenantId.class);
+        when(tenantIdOther.getValue()).thenReturn(valueOther);
+        other = new EgKey(tenantIdOther, egId);
+        Assert.assertEquals(1, egKey.compareTo(other));
+        Assert.assertEquals(-1, other.compareTo(egKey));
+
+        EndpointGroupId egIdOther = mock(EndpointGroupId.class);
+        when(egIdOther.getValue()).thenReturn(valueOther);
+        other = new EgKey(tenantId, egIdOther);
+        Assert.assertEquals(1, egKey.compareTo(other));
+        Assert.assertEquals(-1, other.compareTo(egKey));
+
+        egKey = new EgKey(tenantIdOther, egId);
+        Assert.assertEquals(-1, egKey.compareTo(other));
+        Assert.assertEquals(1, other.compareTo(egKey));
+    }
+
+    @Test
+    public void toStringTest() {
+        String string = egKey.toString();
+        Assert.assertNotNull(string);
+        Assert.assertFalse(string.isEmpty());
+        Assert.assertTrue(string.contains(tenantId.toString()));
+        Assert.assertTrue(string.contains(egId.toString()));
+    }
+}
index 1ffc6f91b0bc0e565f4b569203fa1a5d8dfeadc2..d6cc6d72ee558d6dc18307f963c89eaee5ebf01d 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
- *
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ * 
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
@@ -8,53 +8,69 @@
 
 package org.opendaylight.groupbasedpolicy.resolver;
 
+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 static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.List;
 
+import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomain;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2BridgeDomainBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomain;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L2FloodDomainBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3Context;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.L3ContextBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.SubjectFeatureInstances;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Subnet;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.SubnetBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ActionInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.subject.feature.instances.ClassifierInstance;
 
 import com.google.common.collect.ImmutableList;
 
-import static org.junit.Assert.*;
-
 public class IndexedTenantTest {
 
+    private Tenant tenant;
+
+    @Before
+    public void before() {
+        tenant = mock(Tenant.class);
+    }
+
     @Test
     public void testResolveND() throws Exception {
         SubnetId sid = new SubnetId("dd25397d-d829-4c8d-8c01-31f129b8de8f");
         SubnetId sid2 = new SubnetId("c752ba40-40aa-4a47-8138-9b7175b854fa");
         L3ContextId l3id = new L3ContextId("f2311f52-890f-4095-8b85-485ec8b92b3c");
-        L2BridgeDomainId bdid= new L2BridgeDomainId("70aeb9ea-4ca1-4fb9-9780-22b04b84a0d6");
+        L2BridgeDomainId bdid = new L2BridgeDomainId("70aeb9ea-4ca1-4fb9-9780-22b04b84a0d6");
         L2FloodDomainId fdid = new L2FloodDomainId("252fbac6-bb6e-4d16-808d-6f56d20e5cca");
 
         L3Context l3c = new L3ContextBuilder().setId(l3id).build();
-        L2BridgeDomain bd = new L2BridgeDomainBuilder()
-            .setParent(l3id)
-            .setId(bdid).build();
-        L2FloodDomain fd = new L2FloodDomainBuilder()
-            .setParent(bdid)
-            .setId(fdid).build();
-        Subnet s = new SubnetBuilder()
-            .setParent(fdid)
-            .setId(sid).build();
-        Subnet s2 = new SubnetBuilder()
-            .setParent(bdid)
-            .setId(sid2).build();
-        Tenant t = new TenantBuilder()
-            .setSubnet(ImmutableList.of(s, s2))
+        L2BridgeDomain bd = new L2BridgeDomainBuilder().setParent(l3id).setId(bdid).build();
+        L2FloodDomain fd = new L2FloodDomainBuilder().setParent(bdid).setId(fdid).build();
+        Subnet s = new SubnetBuilder().setParent(fdid).setId(sid).build();
+        Subnet s2 = new SubnetBuilder().setParent(bdid).setId(sid2).build();
+        Tenant t = new TenantBuilder().setSubnet(ImmutableList.of(s, s2))
             .setL2BridgeDomain(ImmutableList.of(bd))
             .setL3Context(ImmutableList.of(l3c))
             .setL2FloodDomain(ImmutableList.of(fd))
@@ -69,4 +85,116 @@ public class IndexedTenantTest {
         assertEquals(bdid, it.resolveL2BridgeDomain(sid).getId());
         assertEquals(fdid, it.resolveL2FloodDomain(sid).getId());
     }
+
+    @Test
+    public void constructorTest() {
+        EndpointGroup eg = mock(EndpointGroup.class);
+        List<EndpointGroup> egList = Arrays.asList(eg);
+        when(tenant.getEndpointGroup()).thenReturn(egList);
+        EndpointGroupId egId = mock(EndpointGroupId.class);
+        when(eg.getId()).thenReturn(egId);
+
+        Contract contract = mock(Contract.class);
+        List<Contract> contractList = Arrays.asList(contract);
+        when(tenant.getContract()).thenReturn(contractList);
+        ContractId contractId = mock(ContractId.class);
+        when(contract.getId()).thenReturn(contractId);
+
+        L3Context l3Context = mock(L3Context.class);
+        List<L3Context> l3ContextList = Arrays.asList(l3Context);
+        when(tenant.getL3Context()).thenReturn(l3ContextList);
+        L3ContextId l3ContextId = mock(L3ContextId.class);
+        when(l3Context.getId()).thenReturn(l3ContextId);
+        String l3ContextValue = "contextID";
+        when(l3ContextId.getValue()).thenReturn(l3ContextValue);
+
+        L2BridgeDomain l2BridgeDomain = mock(L2BridgeDomain.class);
+        List<L2BridgeDomain> l2BridgeDomainList = Arrays.asList(l2BridgeDomain);
+        when(tenant.getL2BridgeDomain()).thenReturn(l2BridgeDomainList);
+        L2BridgeDomainId l2BridgeDomainId = mock(L2BridgeDomainId.class);
+        when(l2BridgeDomain.getId()).thenReturn(l2BridgeDomainId);
+        String l2BridgeDomainIdValue = "bridgeDomainID";
+        when(l2BridgeDomainId.getValue()).thenReturn(l2BridgeDomainIdValue);
+
+        L2FloodDomain l2FloodDomain = mock(L2FloodDomain.class);
+        List<L2FloodDomain> l2FloodDomainList = Arrays.asList(l2FloodDomain);
+        when(tenant.getL2FloodDomain()).thenReturn(l2FloodDomainList);
+        L2FloodDomainId l2FloodDomainId = mock(L2FloodDomainId.class);
+        when(l2FloodDomain.getId()).thenReturn(l2FloodDomainId);
+        String cValue = "floodDomainID";
+        when(l2FloodDomainId.getValue()).thenReturn(cValue);
+
+        Subnet subnet = mock(Subnet.class);
+        List<Subnet> subnetList = Arrays.asList(subnet);
+        when(tenant.getSubnet()).thenReturn(subnetList);
+        SubnetId subnetId = mock(SubnetId.class);
+        when(subnet.getId()).thenReturn(subnetId);
+        String subnetIdValue = "subnetID";
+        when(subnetId.getValue()).thenReturn(subnetIdValue);
+        ContextId sParent = mock(ContextId.class);
+        when(subnet.getParent()).thenReturn(sParent);
+        String sParentValue = "sParentValue";
+        when(sParent.getValue()).thenReturn(sParentValue);
+
+        SubjectFeatureInstances sfi = mock(SubjectFeatureInstances.class);
+        when(tenant.getSubjectFeatureInstances()).thenReturn(sfi);
+
+        ClassifierInstance ci = mock(ClassifierInstance.class);
+        List<ClassifierInstance> ciList = Arrays.asList(ci);
+        when(sfi.getClassifierInstance()).thenReturn(ciList);
+        ClassifierName ciName = mock(ClassifierName.class);
+        when(ci.getName()).thenReturn(ciName);
+
+        ActionInstance ai = mock(ActionInstance.class);
+        List<ActionInstance> actionList = Arrays.asList(ai);
+        when(sfi.getActionInstance()).thenReturn(actionList);
+        ActionName actionName = mock(ActionName.class);
+        when(ai.getName()).thenReturn(actionName);
+
+        IndexedTenant it = new IndexedTenant(tenant);
+
+        assertEquals(tenant.hashCode(), it.hashCode());
+        assertEquals(tenant, it.getTenant());
+        assertEquals(eg, it.getEndpointGroup(egId));
+        assertEquals(contract, it.getContract(contractId));
+        assertEquals(l3Context, it.getNetworkDomain(l3ContextId));
+        assertEquals(l2BridgeDomain, it.getNetworkDomain(l2BridgeDomainId));
+        assertEquals(l2FloodDomain, it.getNetworkDomain(l2FloodDomainId));
+        assertEquals(ci, it.getClassifier(ciName));
+        assertEquals(ai, it.getAction(actionName));
+    }
+
+    @Test
+    public void constructorTestNullValues() {
+        when(tenant.getL3Context()).thenReturn(null);
+        when(tenant.getL2BridgeDomain()).thenReturn(null);
+        when(tenant.getL2FloodDomain()).thenReturn(null);
+        when(tenant.getSubnet()).thenReturn(null);
+
+        SubjectFeatureInstances sfi = mock(SubjectFeatureInstances.class);
+        when(tenant.getSubjectFeatureInstances()).thenReturn(sfi);
+        when(sfi.getClassifierInstance()).thenReturn(null);
+        when(sfi.getActionInstance()).thenReturn(null);
+
+        IndexedTenant it = new IndexedTenant(tenant);
+        assertEquals(tenant.hashCode(), it.hashCode());
+        assertEquals(tenant, it.getTenant());
+    }
+
+    @Test
+    public void equalsTest() {
+        Tenant tenant = mock(Tenant.class);
+        IndexedTenant indexedTenant = new IndexedTenant(tenant);
+
+        assertTrue(indexedTenant.equals(indexedTenant));
+        assertFalse(indexedTenant.equals(null));
+        assertFalse(indexedTenant.equals(new Object()));
+
+        Tenant tenantOther = mock(Tenant.class);
+        IndexedTenant other;
+        other = new IndexedTenant(tenantOther);
+        assertFalse(indexedTenant.equals(other));
+        other = new IndexedTenant(tenant);
+        assertTrue(indexedTenant.equals(other));
+    }
 }
index a4d1be121d162abd3ccfba2ac1e109c4283ac2df..9446ce027b961725bf3e02f8e6a3c24ba3681004 100644 (file)
@@ -11,15 +11,25 @@ package org.opendaylight.groupbasedpolicy.resolver;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ScheduledExecutorService;
 
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.groupbasedpolicy.resolver.ContractResolverUtils.ContractMatch;
 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver.TenantContext;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.CapabilityMatcherName;
@@ -84,10 +94,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderNamedSelectorBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderTargetSelector;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.endpoint.group.ProviderTargetSelectorBuilder;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Table;
+import com.google.common.util.concurrent.CheckedFuture;
 
 public class PolicyResolverTest {
     Quality q1 = new QualityBuilder()
@@ -316,6 +329,47 @@ public class PolicyResolverTest {
         resolver = new PolicyResolver(null, null);
     }
 
+    @Test
+    public void listenerTest() {
+        PolicyScope policyScope;
+        PolicyListener policyListener = mock(PolicyListener.class);
+        Assert.assertTrue(resolver.policyListenerScopes.isEmpty());
+
+        policyScope = resolver.registerListener(policyListener);
+        Assert.assertNotNull(policyScope);
+        Assert.assertFalse(resolver.policyListenerScopes.isEmpty());
+
+        resolver.removeListener(policyScope);
+        Assert.assertTrue(resolver.policyListenerScopes.isEmpty());
+    }
+
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    @Test
+    public void tenantTest() {
+        DataBroker dataProvider = mock(DataBroker.class);
+        ScheduledExecutorService executor = mock(ScheduledExecutorService.class);
+        resolver = new PolicyResolver(dataProvider, executor);
+
+        TenantId tenantId = mock(TenantId.class);
+        Assert.assertTrue(resolver.resolvedTenants.isEmpty());
+
+        ListenerRegistration<DataChangeListener> registration = mock(ListenerRegistration.class);
+        when(
+                dataProvider.registerDataChangeListener(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
+                        any(DataChangeListener.class), any(DataChangeScope.class))).thenReturn(registration);
+
+        ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class);
+        when(dataProvider.newReadOnlyTransaction()).thenReturn(transaction);
+        CheckedFuture unresolved = mock(CheckedFuture.class);
+        when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(unresolved);
+
+        resolver.subscribeTenant(tenantId);
+        Assert.assertFalse(resolver.resolvedTenants.isEmpty());
+
+        resolver.unsubscribeTenant(tenantId);
+        Assert.assertTrue(resolver.resolvedTenants.isEmpty());
+    }
+
     public void verifyMatches(List<ContractId> contrids,
                               List<TenantId> contrtids,
                               List<ContractMatch> matches) {
diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/PolicyTest.java
new file mode 100644 (file)
index 0000000..644e452
--- /dev/null
@@ -0,0 +1,30 @@
+package org.opendaylight.groupbasedpolicy.resolver;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class PolicyTest {
+
+    private Policy policy;
+
+    @Before
+    public void initialisation() {
+        policy = new Policy(null);
+    }
+
+    @Test
+    public void equalsTest() {
+        Assert.assertTrue(policy.equals(policy));
+        Assert.assertFalse(policy.equals(null));
+        Assert.assertFalse(policy.equals(new Object()));
+
+        Policy other = new Policy(null);
+        Assert.assertTrue(policy.equals(other));
+    }
+
+    @Test
+    public void toStringTest() {
+        Assert.assertNotNull(policy.toString());
+    }
+}
diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/RuleGroupTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/resolver/RuleGroupTest.java
new file mode 100644 (file)
index 0000000..7049737
--- /dev/null
@@ -0,0 +1,123 @@
+package org.opendaylight.groupbasedpolicy.resolver;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;
+
+public class RuleGroupTest {
+
+    private Rule rule;
+    private List<Rule> rules;
+    private Integer order;
+    private Tenant contractTenant;
+    private Contract contract;
+    private SubjectName subject;
+    private String subjectValue;
+
+    private RuleGroup ruleGroup;
+
+    @Before
+    public void initialisation() {
+        rule = mock(Rule.class);
+        rules = Arrays.asList(rule);
+        order = Integer.valueOf(5);
+        contractTenant = mock(Tenant.class);
+        contract = mock(Contract.class);
+        subject = mock(SubjectName.class);
+
+        subjectValue = "value";
+        when(subject.getValue()).thenReturn(subjectValue);
+
+        ruleGroup = new RuleGroup(rules, order, contractTenant, contract, subject);
+    }
+
+    @Test
+    public void constructorTest() {
+        Assert.assertNotNull(ruleGroup);
+        Assert.assertEquals(rules, ruleGroup.getRules());
+        Assert.assertEquals(order, ruleGroup.getOrder());
+        Assert.assertEquals(contractTenant, ruleGroup.getContractTenant());
+        Assert.assertEquals(contract, ruleGroup.getRelatedContract());
+        Assert.assertEquals(subject, ruleGroup.getRelatedSubject());
+    }
+
+    @Test
+    public void equalsTest() {
+        Assert.assertTrue(ruleGroup.equals(ruleGroup));
+        Assert.assertFalse(ruleGroup.equals(null));
+        Assert.assertFalse(ruleGroup.equals(new Object()));
+
+        RuleGroup other;
+        Integer orderOther = Integer.valueOf(3);
+        other = new RuleGroup(rules, orderOther, contractTenant, contract, subject);
+        Assert.assertFalse(ruleGroup.equals(other));
+
+        Rule ruleOther = mock(Rule.class);
+        List<Rule> rulesOther = Arrays.asList(ruleOther);
+        other = new RuleGroup(rulesOther, order, contractTenant, contract, subject);
+        Assert.assertFalse(ruleGroup.equals(other));
+
+        SubjectName subjectOther = mock(SubjectName.class);
+        other = new RuleGroup(rules, order, contractTenant, contract, subjectOther);
+        Assert.assertFalse(ruleGroup.equals(other));
+
+        other = new RuleGroup(rules, order, contractTenant, contract, subject);
+        Assert.assertTrue(ruleGroup.equals(other));
+
+        ruleGroup = new RuleGroup(rules, null, contractTenant, contract, subject);
+        Assert.assertFalse(ruleGroup.equals(other));
+        other = new RuleGroup(rules, null, contractTenant, contract, subject);
+        Assert.assertTrue(ruleGroup.equals(other));
+
+        other = new RuleGroup(rules, order, contractTenant, contract, subject);
+        ruleGroup = new RuleGroup(rules, order, contractTenant, contract, null);
+        Assert.assertFalse(ruleGroup.equals(other));
+        other = new RuleGroup(rules, order, contractTenant, contract, null);
+        Assert.assertTrue(ruleGroup.equals(other));
+    }
+
+    @Test
+    public void compareToTest() {
+        RuleGroup other;
+        other = new RuleGroup(rules, order, contractTenant, contract, subject);
+        Assert.assertEquals(0, ruleGroup.compareTo(other));
+
+        Integer orderOther;
+        orderOther = Integer.valueOf(3);
+        other = new RuleGroup(rules, orderOther, contractTenant, contract, subject);
+        Assert.assertEquals(1, ruleGroup.compareTo(other));
+
+        orderOther = Integer.valueOf(8);
+        other = new RuleGroup(rules, orderOther, contractTenant, contract, subject);
+        Assert.assertEquals(-1, ruleGroup.compareTo(other));
+
+        SubjectName subjectOther = mock(SubjectName.class);
+
+        when(subjectOther.getValue()).thenReturn("valu");
+        other = new RuleGroup(rules, order, contractTenant, contract, subjectOther);
+        Assert.assertEquals(1, ruleGroup.compareTo(other));
+
+        when(subjectOther.getValue()).thenReturn("valuee");
+        other = new RuleGroup(rules, order, contractTenant, contract, subjectOther);
+        Assert.assertEquals(-1, ruleGroup.compareTo(other));
+    }
+
+    @Test
+    public void toStringTest() {
+        String string = ruleGroup.toString();
+        Assert.assertNotNull(string);
+        Assert.assertFalse(string.isEmpty());
+        Assert.assertTrue(string.contains(rules.toString()));
+        Assert.assertTrue(string.contains(order.toString()));
+    }
+}