Sho SHIMIZU | 5d62ba8 | 2014-08-21 10:23:47 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.newintent; |
| 2 | |
| 3 | import com.hazelcast.core.EntryEvent; |
| 4 | import com.hazelcast.core.EntryListener; |
| 5 | import net.onrc.onos.api.newintent.Intent; |
| 6 | import net.onrc.onos.api.newintent.IntentId; |
| 7 | import net.onrc.onos.core.datastore.hazelcast.DummySharedCollectionsService; |
| 8 | import org.junit.After; |
| 9 | import org.junit.Before; |
| 10 | import org.junit.Test; |
| 11 | |
| 12 | import java.util.concurrent.CountDownLatch; |
| 13 | |
| 14 | /** |
| 15 | * Suites of test of {@link IntentMap}. |
| 16 | */ |
| 17 | public class IntentMapTest { |
| 18 | |
| 19 | private final IntentId id1 = new IntentId(1); |
| 20 | private DummySharedCollectionsService service; |
| 21 | private IntentMap<Intent> sut; |
| 22 | |
| 23 | @Before |
| 24 | public void setUp() { |
| 25 | service = new DummySharedCollectionsService(); |
| 26 | sut = new IntentMap<>("test", Intent.class, service); |
| 27 | } |
| 28 | |
| 29 | @After |
| 30 | public void tearDown() { |
| 31 | sut.destroy(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Tests if listener is invoked when add/remove/update occurs. |
| 36 | * |
| 37 | * @throws InterruptedException if interrupt occurs |
| 38 | */ |
| 39 | @Test(timeout = 1000) |
| 40 | public void testListener() throws InterruptedException { |
| 41 | final CountDownLatch latch = new CountDownLatch(3); |
| 42 | |
| 43 | sut.addListener(new EntryListener<IntentId, Intent>() { |
| 44 | @Override |
| 45 | public void entryAdded(EntryEvent<IntentId, Intent> event) { |
| 46 | latch.countDown(); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void entryRemoved(EntryEvent<IntentId, Intent> event) { |
| 51 | latch.countDown(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void entryUpdated(EntryEvent<IntentId, Intent> event) { |
| 56 | latch.countDown(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void entryEvicted(EntryEvent<IntentId, Intent> event) { |
| 61 | } |
| 62 | }); |
| 63 | |
| 64 | sut.put(id1, new TestIntent(id1)); |
| 65 | sut.put(id1, new TestIntent(id1)); |
| 66 | sut.remove(id1); |
| 67 | |
| 68 | latch.await(); |
| 69 | } |
| 70 | } |