GBP core tests improvements
[groupbasedpolicy.git] / groupbasedpolicy / src / test / java / org / opendaylight / groupbasedpolicy / statistics / StatisticManagerImplTest.java
index 4d2f69437aef0c8cf1d9de4f522ac00f8897ae76..1268854e7f52e0649aee32f8e5d21bcaa546b02c 100755 (executable)
@@ -8,13 +8,22 @@
 
 package org.opendaylight.groupbasedpolicy.statistics;
 
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
 
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
-import org.mockito.Mockito;
+import org.junit.rules.ExpectedException;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
@@ -35,6 +44,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.StatRecordsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.EpToEpStatisticBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.EpToEpStatisticKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatistic;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatisticBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatisticKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.ep.epg.to.ep.epg.statistic.MatchedRuleStatisticBuilder;
@@ -51,68 +61,98 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.statistics.store.statistic.record.StatisticKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
-
 public class StatisticManagerImplTest {
 
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
+    private static final String READING_EXCEPTION_MSG = "Reading throws exception";
+
     private DataBroker dataBroker;
     private StatisticsManagerImpl manager;
     private WriteTransaction wtx;
     private ReadOnlyTransaction rtx;
+    private MacAddress srcMac;
+    private MacAddress dstMac;
+    private L2BridgeDomainId srcL2C;
+    private L2BridgeDomainId dstL2C;
+    private EndpointGroupId srcEPG;
+    private EndpointGroupId dstEPG;
+    private TenantId srcTenant;
+    private TenantId dstTenant;
+    private ContractId contract;
+    private SubjectName subject;
+    private RuleName rule;
+    private ActionName action;
+    private ClassifierName classifier;
 
     @Before
     public void init() {
-        dataBroker = Mockito.mock(DataBroker.class);
-        wtx = Mockito.mock(WriteTransaction.class);
-        rtx = Mockito.mock(ReadOnlyTransaction.class);
-        Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx);
-        Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rtx);
+        srcMac = new MacAddress("00:00:00:00:00:01");
+        dstMac = new MacAddress("00:00:00:00:00:02");
+        srcL2C = new L2BridgeDomainId("srcL2C");
+        dstL2C = new L2BridgeDomainId("dstL2C");
+        srcEPG = new EndpointGroupId("srcEPG");
+        dstEPG = new EndpointGroupId("dstEPG");
+        srcTenant = new TenantId("srcTenant");
+        dstTenant = new TenantId("dstTenant");
+        contract = new ContractId("contract");
+        subject = new SubjectName("subject");
+        rule = new RuleName("rule");
+        action = new ActionName("action");
+        classifier = new ClassifierName("classifier");
+
+        dataBroker = mock(DataBroker.class);
+        wtx = mock(WriteTransaction.class);
+        rtx = mock(ReadOnlyTransaction.class);
+        when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx);
+        when(dataBroker.newReadOnlyTransaction()).thenReturn(rtx);
+
         manager = new StatisticsManagerImpl(dataBroker);
     }
 
     @Test
-    public void writeStatTest() {
-        MacAddress srcMac = new MacAddress("00:00:00:00:00:01");
-        MacAddress dstMac = new MacAddress("00:00:00:00:00:02");
-        L2BridgeDomainId srcL2C = new L2BridgeDomainId("srcL2C");
-        L2BridgeDomainId dstL2C = new L2BridgeDomainId("dstL2C");
-        EndpointGroupId srcEPG = new EndpointGroupId("srcEPG");
-        EndpointGroupId dstEPG = new EndpointGroupId("dstEPG");
-        TenantId srcTenant = new TenantId("srcTenant");
-        TenantId dstTenant = new TenantId("dstTenant");
-        ContractId contract = new ContractId("contract");
-        SubjectName subject = new SubjectName("subject");
-        RuleName rule = new RuleName("rule");
-        ActionName action = new ActionName("action");
-        ClassifierName classifier = new ClassifierName("classifier");
-        // input
-        StatRecordsBuilder recordsBuilder = new StatRecordsBuilder();
-        EpToEpStatisticBuilder epBuilder = new EpToEpStatisticBuilder();
-        EpEpgToEpEpgStatisticBuilder epgBuilder = new EpEpgToEpEpgStatisticBuilder();
-        epgBuilder.setSrcEpg(srcEPG)
-            .setDstEpg(dstEPG)
-            .setKey(new EpEpgToEpEpgStatisticKey(dstEPG, srcEPG))
-            .setMatchedRuleStatistic(Collections.singletonList(new MatchedRuleStatisticBuilder()
-                .setKey(new MatchedRuleStatisticKey(contract, rule, subject))
-                .setContract(contract)
-                .setSubject(subject)
-                .setMatchedRule(rule)
-                .setAction(Collections.singletonList(action))
-                .setClassifier(Collections.singletonList(classifier))
-                .setByteCount(25l)
-                .setPacketCount(5l)
-                .build()));
-        epBuilder.setSrcMacAddress(srcMac)
-            .setDstMacAddress(dstMac)
-            .setSrcL2c(srcL2C)
-            .setDstL2c(dstL2C)
-            .setSrcTenant(srcTenant)
-            .setDstTenant(dstTenant)
-            .setKey(new EpToEpStatisticKey(dstL2C, dstMac, srcL2C, srcMac))
-            .setEpEpgToEpEpgStatistic(Collections.singletonList(epgBuilder.build()));
-        recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build()));
-        // output
+    public void testWriteStat() {
+        StatRecords input = inputForWriting();
+        StatisticRecordKey key = new StatisticRecordKey(new RecordId(0l));
+        StatisticRecord output = outputForWriting(key);
+
+        CheckedFuture<Void, TransactionCommitFailedException> future = mock(CheckedFuture.class);
+        when(wtx.submit()).thenReturn(future);
+        when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx);
+
+        manager.writeStat(input);
+        verify(wtx).put(LogicalDatastoreType.OPERATIONAL, IidFactory.statisticRecordIid(key), output, true);
+    }
+
+    @Test
+    public void testReadStats() throws Exception {
+        List<StatisticRecord> stats = inputForReading();
+        StatRecords statRecords = outputForReading();
+
+        CheckedFuture<Optional<StatisticsStore>, ReadFailedException> future = mock(CheckedFuture.class);
+        when(future.get()).thenReturn(Optional.of(new StatisticsStoreBuilder().setStatisticRecord(stats).build()));
+        when(rtx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(StatisticsStore.class).build()))
+            .thenReturn(future);
+
+        StatRecords read = manager.readStats();
+
+        Assert.assertEquals(statRecords, read);
+    }
+
+    @Test
+    public void testReadStats_throwsException() throws ExecutionException, InterruptedException {
+        CheckedFuture<Optional<StatisticsStore>, ReadFailedException> future = mock(CheckedFuture.class);
+        when(future.get()).thenThrow(new RuntimeException(READING_EXCEPTION_MSG));
+        when(rtx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(StatisticsStore.class).build()))
+            .thenReturn(future);
+
+        thrown.expect(RuntimeException.class);
+        thrown.expectMessage(READING_EXCEPTION_MSG);
+        manager.readStats();
+    }
+
+    private List<StatisticRecord> inputForReading() {
         SrcEndpointBuilder srcBuilder = new SrcEndpointBuilder();
         DstEndpointBuilder dstBuilder = new DstEndpointBuilder();
         srcBuilder.setMacAddress(srcMac).setL2Context(srcL2C).setTenant(srcTenant);
@@ -132,37 +172,33 @@ public class StatisticManagerImplTest {
             .setStatistic(Collections.singletonList(statBuilder.build()))
             .setSrcEndpoint(srcBuilder.build())
             .setDstEndpoint(dstBuilder.build());
+        List<StatisticRecord> stats = new ArrayList<>();
+        stats.add(statRecord.build());
+        statRecord.setKey(new StatisticRecordKey(new RecordId(1l)));
+        stats.add(statRecord.build());
 
-//        manager.writeStat(recordsBuilder.build());
-//        Mockito.verify(wtx).put(LogicalDatastoreType.OPERATIONAL,
-//                IidFactory.statisticRecordIid(key),
-//                statRecord.build());
-        CheckedFuture<Void,TransactionCommitFailedException> future = Mockito.mock(CheckedFuture.class);
-        Mockito.when(wtx.submit()).thenReturn(future);
-        Mockito.when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx);
-
-        manager.writeStat(recordsBuilder.build());
-        Mockito.verify(wtx).put(LogicalDatastoreType.OPERATIONAL,
-                IidFactory.statisticRecordIid(key),
-                statRecord.build(), true);
+        return stats;
     }
 
-    @Test
-    public void readStatsTest() {
-        MacAddress srcMac = new MacAddress("00:00:00:00:00:01");
-        MacAddress dstMac = new MacAddress("00:00:00:00:00:02");
-        L2BridgeDomainId srcL2C = new L2BridgeDomainId("srcL2C");
-        L2BridgeDomainId dstL2C = new L2BridgeDomainId("dstL2C");
-        EndpointGroupId srcEPG = new EndpointGroupId("srcEPG");
-        EndpointGroupId dstEPG = new EndpointGroupId("dstEPG");
-        TenantId srcTenant = new TenantId("srcTenant");
-        TenantId dstTenant = new TenantId("dstTenant");
-        ContractId contract = new ContractId("contract");
-        SubjectName subject = new SubjectName("subject");
-        RuleName rule = new RuleName("rule");
-        ActionName action = new ActionName("action");
-        ClassifierName classifier = new ClassifierName("classifier");
-        // input
+    private StatRecords outputForReading() {
+        StatRecordsBuilder recordsBuilder = new StatRecordsBuilder();
+        EpEpgToEpEpgStatisticBuilder epgBuilder = newEpEpgToEpEpgStatisticBuilder(50, 10);
+        EpToEpStatisticBuilder epBuilder = newEpToEpStatisticBuilder(Collections.singletonList(epgBuilder.build()));
+        recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build()));
+
+        return recordsBuilder.build();
+    }
+
+    private StatRecords inputForWriting() {
+        StatRecordsBuilder recordsBuilder = new StatRecordsBuilder();
+        EpEpgToEpEpgStatisticBuilder epgBuilder = newEpEpgToEpEpgStatisticBuilder(25, 5);
+        EpToEpStatisticBuilder epBuilder = newEpToEpStatisticBuilder(Collections.singletonList(epgBuilder.build()));
+        recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build()));
+
+        return recordsBuilder.build();
+    }
+
+    private StatisticRecord outputForWriting(StatisticRecordKey key) {
         SrcEndpointBuilder srcBuilder = new SrcEndpointBuilder();
         DstEndpointBuilder dstBuilder = new DstEndpointBuilder();
         srcBuilder.setMacAddress(srcMac).setL2Context(srcL2C).setTenant(srcTenant);
@@ -177,53 +213,39 @@ public class StatisticManagerImplTest {
             .setAction(Collections.singletonList(action))
             .setByteCount(25l)
             .setPacketCount(5l);
-        StatisticRecordKey key = new StatisticRecordKey(new RecordId(0l));
         StatisticRecordBuilder statRecord = new StatisticRecordBuilder().setKey(key)
             .setStatistic(Collections.singletonList(statBuilder.build()))
             .setSrcEndpoint(srcBuilder.build())
             .setDstEndpoint(dstBuilder.build());
-        ArrayList<StatisticRecord> stats = new ArrayList<>();
-        stats.add(statRecord.build());
-        statRecord.setKey(new StatisticRecordKey(new RecordId(1l)));
-        stats.add(statRecord.build());
-        // output
-        StatRecordsBuilder recordsBuilder = new StatRecordsBuilder();
-        EpToEpStatisticBuilder epBuilder = new EpToEpStatisticBuilder();
-        EpEpgToEpEpgStatisticBuilder epgBuilder = new EpEpgToEpEpgStatisticBuilder();
-        epgBuilder.setSrcEpg(srcEPG)
+
+        return statRecord.build();
+    }
+
+    private EpEpgToEpEpgStatisticBuilder newEpEpgToEpEpgStatisticBuilder(long byteCount, long packetCount) {
+        return new EpEpgToEpEpgStatisticBuilder().setSrcEpg(srcEPG)
             .setDstEpg(dstEPG)
             .setKey(new EpEpgToEpEpgStatisticKey(dstEPG, srcEPG))
-            .setMatchedRuleStatistic(Collections.singletonList(new MatchedRuleStatisticBuilder()
-                .setKey(new MatchedRuleStatisticKey(contract, rule, subject))
-                .setContract(contract)
-                .setSubject(subject)
-                .setMatchedRule(rule)
-                .setAction(Collections.singletonList(action))
-                .setClassifier(Collections.singletonList(classifier))
-                .setByteCount(50l)
-                .setPacketCount(10l)
-                .build()));
-        epBuilder.setSrcMacAddress(srcMac)
+            .setMatchedRuleStatistic(Collections.singletonList(
+                    new MatchedRuleStatisticBuilder().setKey(new MatchedRuleStatisticKey(contract, rule, subject))
+                        .setContract(contract)
+                        .setSubject(subject)
+                        .setMatchedRule(rule)
+                        .setAction(Collections.singletonList(action))
+                        .setClassifier(Collections.singletonList(classifier))
+                        .setByteCount(byteCount)
+                        .setPacketCount(packetCount)
+                        .build()));
+    }
+
+    private EpToEpStatisticBuilder newEpToEpStatisticBuilder(List<EpEpgToEpEpgStatistic> list) {
+        return new EpToEpStatisticBuilder().setSrcMacAddress(srcMac)
             .setDstMacAddress(dstMac)
             .setSrcL2c(srcL2C)
             .setDstL2c(dstL2C)
             .setSrcTenant(srcTenant)
             .setDstTenant(dstTenant)
             .setKey(new EpToEpStatisticKey(dstL2C, dstMac, srcL2C, srcMac))
-            .setEpEpgToEpEpgStatistic(Collections.singletonList(epgBuilder.build()));
-        recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build()));
-
-        CheckedFuture<Optional<StatisticsStore>, ReadFailedException> future = Mockito.mock(CheckedFuture.class);
-        try {
-            Mockito.when(future.get())
-                .thenReturn(Optional.of(new StatisticsStoreBuilder().setStatisticRecord(stats).build()));
-            Mockito.when(rtx.read(LogicalDatastoreType.OPERATIONAL,
-                    InstanceIdentifier.builder(StatisticsStore.class).build()))
-                .thenReturn(future);
-            StatRecords read = manager.readStats();
-            Assert.assertEquals(recordsBuilder.build(), read);
-        } catch (Exception e) {
-        }
-
+            .setEpEpgToEpEpgStatistic(list);
     }
+
 }