Create an implementaion of IntentIdGenerator
- Define interface to allocate IdBlock in IdBlockAllocator
- Implement IdBlockAllocatorBasedIntentIdGenerator
- Refactor IdBlock class
Change-Id: I21fa21ae625e3d7e137a7f846bb5a0c1bdb8df9a
diff --git a/src/test/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGeneratorTest.java b/src/test/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGeneratorTest.java
new file mode 100644
index 0000000..97394bd
--- /dev/null
+++ b/src/test/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGeneratorTest.java
@@ -0,0 +1,48 @@
+package net.onrc.onos.core.newintent;
+
+import net.onrc.onos.api.newintent.IntentId;
+import net.onrc.onos.core.registry.IdBlock;
+import net.onrc.onos.core.util.IdBlockAllocator;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Suites of test of {@link IdBlockAllocatorBasedIntentIdGenerator}.
+ */
+public class IdBlockAllocatorBasedIntentIdGeneratorTest {
+ private IdBlockAllocator allocator;
+ private IdBlockAllocatorBasedIntentIdGenerator sut;
+
+ @Before
+ public void setUp() {
+ allocator = createMock(IdBlockAllocator.class);
+
+ }
+
+ /**
+ * Tests generated IntentId sequences using two {@link IdBlock blocks}.
+ */
+ @Test
+ public void testIds() {
+ expect(allocator.allocateUniqueIdBlock())
+ .andReturn(new IdBlock(0, 3))
+ .andReturn(new IdBlock(4, 3));
+
+ replay(allocator);
+ sut = new IdBlockAllocatorBasedIntentIdGenerator(allocator);
+
+ assertThat(sut.getNewId(), is(new IntentId(0)));
+ assertThat(sut.getNewId(), is(new IntentId(1)));
+ assertThat(sut.getNewId(), is(new IntentId(2)));
+
+ assertThat(sut.getNewId(), is(new IntentId(4)));
+ assertThat(sut.getNewId(), is(new IntentId(5)));
+ assertThat(sut.getNewId(), is(new IntentId(6)));
+ }
+}
diff --git a/src/test/java/net/onrc/onos/core/registry/IdBlockTest.java b/src/test/java/net/onrc/onos/core/registry/IdBlockTest.java
new file mode 100644
index 0000000..aec55fa
--- /dev/null
+++ b/src/test/java/net/onrc/onos/core/registry/IdBlockTest.java
@@ -0,0 +1,33 @@
+package net.onrc.onos.core.registry;
+
+import net.onrc.onos.core.util.UnavailableIdException;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.*;
+
+/**
+ * Suites of test of {@link IdBlock}.
+ */
+public class IdBlockTest {
+
+ private final IdBlock sut = new IdBlock(0, 3);
+
+ /**
+ * Tests generated sequences. Also checks occurrence of {@link UnavailableIdException},
+ * when the number of generated IDs exceeds the block size.
+ */
+ @Test
+ public void basics() {
+ assertThat(sut.getNextId(), is(0L));
+ assertThat(sut.getNextId(), is(1L));
+ assertThat(sut.getNextId(), is(2L));
+
+ try {
+ sut.getNextId();
+ fail("UnavailableIdException should be thrown");
+ } catch (UnavailableIdException e) {
+ assertTrue(true);
+ }
+ }
+}