Skip to content

Commit

Permalink
Clean up SerialId class and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Schwartz-Matthew-bah committed Dec 7, 2018
1 parent af057f3 commit 03683d0
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 89 deletions.
101 changes: 12 additions & 89 deletions jpo-ode-core/src/main/java/us/dot/its/jpo/ode/model/SerialId.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class SerialId {


public SerialId() {
super();
streamId = UUID.randomUUID().toString();
}

Expand All @@ -35,41 +34,26 @@ public SerialId(String streamId, int bundleSize,
this.serialNumber = calculateSerialNumber();
}

public SerialId (String serialId) {
public SerialId (String serialId) throws Exception {

String[] splitId = serialId.split(
"[" + UUID_DELIMITER + SERIAL_NUMBER_DELIMITER +"]+");
if (splitId.length >= 1)
this.streamId = splitId[0];
else
this.streamId = serialId;

if (splitId.length >= 3)
this.serialNumber = Integer.parseInt(splitId[2]);
else
this.serialNumber = -1;
"[" + UUID_DELIMITER + SERIAL_NUMBER_DELIMITER + BUNDLE_RECORD_DELIMITER +"]+");

if (splitId.length >= 2)
splitId = splitId[1].split(
"[" + BUNDLE_RECORD_DELIMITER +"]+");

if (splitId.length >= 1)
this.bundleSize = Integer.parseInt(splitId[0]);

if (splitId.length >= 2)
this.bundleId = Long.parseLong(splitId[1]);
if (splitId.length != 5) {
throw new Exception("Invalid serialId! Expected length 5 but was " + splitId.length);
}

if (splitId.length >= 3)
this.recordId = Integer.parseInt(splitId[2]);

if (this.serialNumber == -1)
this.serialNumber = calculateSerialNumber();
this.streamId = splitId[0];
this.bundleSize = Integer.parseInt(splitId[1]);
this.bundleId = Integer.parseInt(splitId[2]);
this.recordId = Integer.parseInt(splitId[3]);
this.serialNumber = Integer.parseInt(splitId[4]);
}

public SerialId(String streamId,
int bundleSize, long bundleId, int recordId,
long serialNumber) {
super();

this.streamId = streamId;
this.bundleSize = bundleSize;
this.bundleId = bundleId;
Expand All @@ -86,68 +70,7 @@ public SerialId(JsonNode jsonNode) {
}

private long calculateSerialNumber() {
return this.bundleId * this.bundleSize + this.recordId;
}

public static SerialId create(String serialIdStr) throws Exception {
int bundleSize;
long bundleId;
int recordId;
try {
if (serialIdStr != null && !serialIdStr.equals("")) {
String[] splitId = serialIdStr.split(
"[" + UUID_DELIMITER + SERIAL_NUMBER_DELIMITER +"]+");

if (splitId.length == 3) {
String streamId = splitId[0];
long serialNumber;
try {
serialNumber = Integer.parseInt(splitId[2]);
} catch (Exception e) {
throw new Exception("SerialId has Non-Numeric Serial Number: "
+ splitId[2], e);
}
splitId = splitId[1].split(
"[" + BUNDLE_RECORD_DELIMITER +"]+");

if (splitId.length == 3) {
try {
bundleSize = Integer.parseInt(splitId[0]);
} catch (Exception e) {
throw new Exception("SerialId has Non-Numeric Bundle Size: "
+ splitId[0], e);
}
try {
bundleId = Long.parseLong(splitId[1]);
} catch (Exception e) {
throw new Exception("SerialId has Non-Numeric Bundle ID: "
+ splitId[1], e);
}
try {
recordId = Integer.parseInt(splitId[2]);
} catch (Exception e) {
throw new Exception("SerialId has Non-Numeric Record ID: "
+ splitId[2], e);
}
} else {
throw new Exception("Serial ID missing BundleSize.BundleId.RecordId");
}

return new SerialId(streamId,
bundleSize, bundleId, recordId,
serialNumber);
} else {
throw new Exception("Serial ID missing StreamId, BundleSize.BundleId.RecordId and SerialNumber components");
}
} else {
throw new Exception("SerialId cannot be null or blank");
}
} catch (Exception e) {
throw new Exception("Serial ID Format Must be "
+ "StreamId_BundleSize.BundleId.RecordId#SerialNumber"
+ "where BundleSize, BundleId, RecordId and SerialNumber components"
+ "are integer values", e);
}
return (this.bundleId * this.bundleSize) + this.recordId;
}

public int nextRecordId() {
Expand Down
222 changes: 222 additions & 0 deletions jpo-ode-core/src/test/java/us/dot/its/jpo/ode/model/SerialIdTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package us.dot.its.jpo.ode.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.Test;

import com.fasterxml.jackson.databind.node.ObjectNode;

import us.dot.its.jpo.ode.util.JsonUtils;

public class SerialIdTest {

@Test
public void testNoArgConstructor() {
SerialId testSerialId = new SerialId();

assertNotNull(testSerialId.getStreamId());
}

@Test
public void testFourArgConstructorNullStreamId() {
// streamId = null
// bundleSize = 3
// bundleId = 6L
// recordId = 12
SerialId testSerialId = new SerialId(null, 3, 6L, 12);

assertNotNull(testSerialId.getStreamId());
assertTrue(testSerialId.getStreamId().endsWith("_null"));
assertEquals(3, testSerialId.getBundleSize()); // bundle size = bundle size
assertEquals(10L, testSerialId.getBundleId()); // bundle id = bundle id + (record id / bundle size) = 6L + ( 12 /
// 3) = 10L
assertEquals(0, testSerialId.getRecordId()); // record id = record id % bundle size = 12 % 3 = 0
assertEquals(30L, testSerialId.getSerialNumber()); // serial number = (bundle id * bundle size) + record id = (6L
// * 3) + 12 = 30L
}

@Test
public void testFourArgConstructorWithStreamId() {
SerialId testSerialId = new SerialId("bob", 3, 6L, 12);

assertEquals("bob", testSerialId.getStreamId());
assertEquals(3, testSerialId.getBundleSize());
assertEquals(10L, testSerialId.getBundleId());
assertEquals(0, testSerialId.getRecordId());
assertEquals(30L, testSerialId.getSerialNumber());

assertEquals("bob_3.10.0#30", testSerialId.toString());
}

@Test
public void testSingleArgConstructor() throws Exception {
SerialId testSerialId = new SerialId("bob_3.10.0#30");

assertEquals("bob", testSerialId.getStreamId());
assertEquals(3, testSerialId.getBundleSize());
assertEquals(10L, testSerialId.getBundleId());
assertEquals(0, testSerialId.getRecordId());
assertEquals(30L, testSerialId.getSerialNumber());
}

@Test
public void testSingleArgConstructorInvalidSerialId() {
try {
new SerialId("bob_3.10.0");
fail("Expected Exception for invalid length");
} catch (Exception e) {
assertEquals("Invalid serialId! Expected length 5 but was 4", e.getMessage());
}
}

@Test
public void testFiveArgConstructor() {
SerialId testSerialId = new SerialId("allison", 6, 12L, 24, 15);

assertEquals("allison", testSerialId.getStreamId());
assertEquals(6, testSerialId.getBundleSize());
assertEquals(12L, testSerialId.getBundleId());
assertEquals(24, testSerialId.getRecordId());
assertEquals(15L, testSerialId.getSerialNumber());

assertEquals("allison_6.12.24#15", testSerialId.toString());
}

@Test
public void testJsonNodeConstructor() {
ObjectNode testNode = JsonUtils.newNode();
JsonUtils.addNode(testNode, "streamId", "bob");
JsonUtils.addNode(testNode, "bundleSize", 3);
JsonUtils.addNode(testNode, "bundleId", 10);
JsonUtils.addNode(testNode, "recordId", 0);
JsonUtils.addNode(testNode, "serialNumber", 30);

SerialId testSerialId = new SerialId(testNode);

assertEquals("bob", testSerialId.getStreamId());
assertEquals(3, testSerialId.getBundleSize());
assertEquals(10L, testSerialId.getBundleId());
assertEquals(0, testSerialId.getRecordId());
assertEquals(30L, testSerialId.getSerialNumber());

assertEquals("bob_3.10.0#30", testSerialId.toString());
}

@Test
public void testNextRecordId() {
SerialId testSerialId = new SerialId("bob", 3, 6L, 12);

assertEquals(1, testSerialId.nextRecordId()); // nextRecordId = (recordId + 1) % bundleSize = (0 + 1) % 3 = 1
}

@Test
public void testNextSerialNumber() {
SerialId testSerialId = new SerialId("bob", 3, 6L, 12);

assertEquals(31L, testSerialId.nextSerialNumber()); // nextSerialNumber = nextSerialNumber + 1
}

@Test
public void testNextSerialId() {
SerialId testSerialId = new SerialId("bob", 3, 6L, 12);

// nextSerialId:
// bundleId = bundleId + ((recordId + 1) / bundleSize) = 10 + ((0 + 1) / 3) = 10
// recordId = (recordId + 1) % bundleSize = (0 + 1) % 3 = 1

SerialId nextSerialId = testSerialId.nextSerialId();

assertEquals(10L, nextSerialId.getBundleId());
assertEquals(1, nextSerialId.getRecordId());
}

@Test
public void testIsRightAfter() {
SerialId testSerialId0 = new SerialId("bob", 3, 6L, 12);
SerialId testSerialId1 = new SerialId("bob", 3, 6L, 13);

assertTrue(testSerialId1.isRightAfter(testSerialId0));
assertFalse(testSerialId0.isRightAfter(testSerialId1));
}

@Test
public void testIsRightBefore() {
SerialId testSerialId0 = new SerialId("bob", 3, 6L, 12);
SerialId testSerialId1 = new SerialId("bob", 3, 6L, 13);

assertTrue(testSerialId0.isRightBefore(testSerialId1));
assertFalse(testSerialId1.isRightBefore(testSerialId0));
}

@Test
public void testAddBundleId() {
SerialId testSerialId = new SerialId("bob", 3, 6L, 12);
testSerialId.addBundleId(5L);

assertEquals(15L, testSerialId.getBundleId());
}

@Test
public void testAddRecordId() {
SerialId testSerialId = new SerialId("bob", 3, 6L, 12);
testSerialId.addRecordId(7);

assertEquals(7, testSerialId.getRecordId());
}

@Test
public void testHashCode() {
SerialId testSerialId0 = new SerialId("bob", 3, 6L, 12);
SerialId testSerialId1 = new SerialId("bob", 3, 6L, 12);
SerialId testSerialId2 = new SerialId("bob", 3, 6L, 16);

assertEquals(testSerialId0.hashCode(), testSerialId1.hashCode());
assertNotEquals(testSerialId0.hashCode(), testSerialId2.hashCode());

testSerialId2.setStreamId(null);
assertNotEquals(testSerialId1.hashCode(), testSerialId2.hashCode());
}

@Test
public void testEquals() {
SerialId testSerialId0 = new SerialId("bob", 3, 6L, 12);
SerialId testSerialId1 = new SerialId("bob", 3, 6L, 12);

assertTrue(testSerialId0.equals(testSerialId0));
assertFalse(testSerialId0.equals(null));
assertFalse(testSerialId0.equals(new Integer(4)));

assertTrue(testSerialId0.equals(testSerialId1));
testSerialId1.setBundleId(7L);
assertFalse(testSerialId0.equals(testSerialId1));
testSerialId1.setBundleId(10L);

assertTrue(testSerialId0.equals(testSerialId1));
testSerialId1.setBundleSize(7);
assertFalse(testSerialId0.equals(testSerialId1));
testSerialId1.setBundleSize(3);

assertTrue(testSerialId0.equals(testSerialId1));
testSerialId1.setRecordId(1);
assertFalse(testSerialId0.equals(testSerialId1));
testSerialId1.setRecordId(0);

assertTrue(testSerialId0.equals(testSerialId1));
testSerialId1.setStreamId(null);
assertFalse(testSerialId0.equals(testSerialId1));
testSerialId0.setStreamId(null);
assertTrue(testSerialId0.equals(testSerialId1));

testSerialId0.setStreamId("bob");
assertFalse(testSerialId1.equals(testSerialId0));

testSerialId1.setStreamId("allison");
assertFalse(testSerialId0.equals(testSerialId1));
}

}

0 comments on commit 03683d0

Please sign in to comment.