/* * 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 */ package org.opendaylight.controller.remote.rpc.registry.gossip; import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.Address; import akka.actor.Props; import akka.testkit.JavaTestKit; import akka.testkit.TestActorRef; import com.typesafe.config.ConfigFactory; import java.util.HashMap; import java.util.Map; import java.util.Optional; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.remote.rpc.RemoteRpcProviderConfig; import org.opendaylight.controller.remote.rpc.TerminationMonitor; public class BucketStoreTest { /** * Dummy class to eliminate rawtype warnings. * * @author gwu * */ private static class T implements BucketData { @Override public Optional getWatchActor() { return Optional.empty(); } } private static ActorSystem system; @BeforeClass public static void setup() { system = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("unit-test")); system.actorOf(Props.create(TerminationMonitor.class), "termination-monitor"); } @AfterClass public static void teardown() { JavaTestKit.shutdownActorSystem(system); } /** * Given remote buckets, should merge with local copy of remote buckets. */ @Test public void testReceiveUpdateRemoteBuckets() { final BucketStore store = createStore(); Address localAddress = system.provider().getDefaultAddress(); Bucket localBucket = new BucketImpl<>(new T()); Address a1 = new Address("tcp", "system1"); Address a2 = new Address("tcp", "system2"); Address a3 = new Address("tcp", "system3"); Bucket b1 = new BucketImpl<>(new T()); Bucket b2 = new BucketImpl<>(new T()); Bucket b3 = new BucketImpl<>(new T()); Map> remoteBuckets = new HashMap<>(3); remoteBuckets.put(a1, b1); remoteBuckets.put(a2, b2); remoteBuckets.put(a3, b3); remoteBuckets.put(localAddress, localBucket); //Given remote buckets store.receiveUpdateRemoteBuckets(remoteBuckets); //Should NOT contain local bucket //Should contain ONLY 3 entries i.e a1, a2, a3 Map> remoteBucketsInStore = store.getRemoteBuckets(); Assert.assertFalse("remote buckets contains local bucket", remoteBucketsInStore.containsKey(localAddress)); Assert.assertTrue(remoteBucketsInStore.size() == 3); //Add a new remote bucket Address a4 = new Address("tcp", "system4"); Bucket b4 = new BucketImpl<>(new T()); remoteBuckets.clear(); remoteBuckets.put(a4, b4); store.receiveUpdateRemoteBuckets(remoteBuckets); //Should contain a4 //Should contain 4 entries now i.e a1, a2, a3, a4 remoteBucketsInStore = store.getRemoteBuckets(); Assert.assertTrue("Does not contain a4", remoteBucketsInStore.containsKey(a4)); Assert.assertTrue(remoteBucketsInStore.size() == 4); //Update a bucket Bucket b3New = new BucketImpl<>(new T()); remoteBuckets.clear(); remoteBuckets.put(a3, b3New); remoteBuckets.put(a1, null); remoteBuckets.put(a2, null); store.receiveUpdateRemoteBuckets(remoteBuckets); //Should only update a3 remoteBucketsInStore = store.getRemoteBuckets(); Bucket b3InStore = remoteBucketsInStore.get(a3); Assert.assertEquals(b3New.getVersion(), b3InStore.getVersion()); //Should NOT update a1 and a2 Bucket b1InStore = remoteBucketsInStore.get(a1); Bucket b2InStore = remoteBucketsInStore.get(a2); Assert.assertEquals(b1.getVersion(), b1InStore.getVersion()); Assert.assertEquals(b2.getVersion(), b2InStore.getVersion()); Assert.assertTrue(remoteBucketsInStore.size() == 4); //Should update versions map //versions map contains versions for all remote buckets (4). Map versionsInStore = store.getVersions(); Assert.assertEquals(4, versionsInStore.size()); Assert.assertEquals((Long)b1.getVersion(), versionsInStore.get(a1)); Assert.assertEquals((Long)b2.getVersion(), versionsInStore.get(a2)); Assert.assertEquals((Long)b3New.getVersion(), versionsInStore.get(a3)); Assert.assertEquals((Long)b4.getVersion(), versionsInStore.get(a4)); //Send older version of bucket remoteBuckets.clear(); remoteBuckets.put(a3, b3); store.receiveUpdateRemoteBuckets(remoteBuckets); //Should NOT update a3 remoteBucketsInStore = store.getRemoteBuckets(); b3InStore = remoteBucketsInStore.get(a3); Assert.assertEquals(b3InStore.getVersion(), b3New.getVersion()); } /** * Create BucketStore actor and returns the underlying instance of BucketStore class. * * @return instance of BucketStore class */ private static BucketStore createStore() { final Props props = Props.create(BucketStore.class, new RemoteRpcProviderConfig(system.settings().config()), new T()); final TestActorRef> testRef = TestActorRef.create(system, props, "testStore"); return testRef.underlyingActor(); } }