diff options
-rw-r--r-- | aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java | 8 | ||||
-rw-r--r-- | aai-core/src/test/java/org/onap/aai/serialization/db/DbSerializerTest.java | 55 |
2 files changed, 52 insertions, 11 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java b/aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java index e8c03bdd..7a2c447e 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java @@ -96,6 +96,7 @@ public class DBSerializer { private final Loader loader; private final String baseURL; private double dbTimeMsecs = 0; + private long currentTimeMillis; private SchemaVersions schemaVersions; /** @@ -117,6 +118,7 @@ public class DBSerializer { this.version = version; this.loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectionType, version); this.baseURL = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE); + this.currentTimeMillis = System.currentTimeMillis(); initBeans(); } @@ -156,14 +158,14 @@ public class DBSerializer { * @param isNewVertex the is new vertex */ public void touchStandardVertexProperties(Vertex v, boolean isNewVertex) { - - String timeNowInSec = Long.toString(System.currentTimeMillis()); + String timeNowInSec = Long.toString(currentTimeMillis); + if (isNewVertex) { v.property(AAIProperties.SOURCE_OF_TRUTH, this.sourceOfTruth); v.property(AAIProperties.CREATED_TS, timeNowInSec); v.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString()); } - v.property(AAIProperties.RESOURCE_VERSION, timeNowInSec ); + v.property(AAIProperties.RESOURCE_VERSION, timeNowInSec); v.property(AAIProperties.LAST_MOD_TS, timeNowInSec); v.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, this.sourceOfTruth); diff --git a/aai-core/src/test/java/org/onap/aai/serialization/db/DbSerializerTest.java b/aai-core/src/test/java/org/onap/aai/serialization/db/DbSerializerTest.java index af6afac2..a5b968b3 100644 --- a/aai-core/src/test/java/org/onap/aai/serialization/db/DbSerializerTest.java +++ b/aai-core/src/test/java/org/onap/aai/serialization/db/DbSerializerTest.java @@ -340,25 +340,64 @@ public class DbSerializerTest extends AAISetup { } - @Test + @Test public void touchStandardVertexPropertiesTest() throws AAIException, InterruptedException { engine.startTransaction(); + + //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value + Thread.sleep(2); DBSerializer dbser2 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-2"); Vertex vert = graph.addVertex("aai-node-type", "generic-vnf"); + // Upon first creation of the Vertex and the DBSerializer + // the source of truth and created-ts should be the same as their modified counterparts + dbser2.touchStandardVertexProperties(vert, true); + String createTS = (String)vert.property(AAIProperties.CREATED_TS).value(); + String modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value(); + String sot = (String)vert.property(AAIProperties.SOURCE_OF_TRUTH).value(); + String lastModSOT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value(); + assertTrue(createTS.equals(modTS)); + assertTrue(sot.equals(lastModSOT)); + + //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value + Thread.sleep(2); + + // Not new vertex && new DBSerializer (A new serializer since a new one will be created per transaction) + // Here the vertex will be modified by a different source of truth + DBSerializer dbser3 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-3"); + dbser3.touchStandardVertexProperties(vert, false); + createTS = (String)vert.property(AAIProperties.CREATED_TS).value(); + modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value(); + sot = (String)vert.property(AAIProperties.SOURCE_OF_TRUTH).value(); + lastModSOT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value(); + assertFalse(createTS.equals(modTS)); + assertFalse(sot.equals(lastModSOT)); + + //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value + Thread.sleep(2); + + // The currentTimeMillis used for the created-ts and modified-ts is created at DBSerializer instantiation + // Every REST transaction should create a new DBSerializer - thus a new currentTimeMillis is used at the time of transaction. + // Using an existing vertex, but treating it as new && using an older DBSerializer dbser.touchStandardVertexProperties(vert, true); String resverStart = (String)vert.property(AAIProperties.RESOURCE_VERSION).value(); - String lastModTimeStart = (String)vert.property(AAIProperties.LAST_MOD_TS).value(); + String lastModTimeStart = (String)vert.property(AAIProperties.LAST_MOD_TS).value(); + createTS = (String)vert.property(AAIProperties.CREATED_TS).value(); + modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value(); + assertTrue(createTS.equals(modTS)); + assertEquals("AAI-TEST", vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value()); - Thread.sleep(10); //bc the resource version is set based on current time in milliseconds, - //if this test runs through too fast the value may not change - //causing the test to fail. sleeping ensures a different value + //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value + Thread.sleep(2); dbser2.touchStandardVertexProperties(vert, false); - assertFalse(resverStart.equals(vert.property(AAIProperties.RESOURCE_VERSION).value())); - assertFalse(lastModTimeStart.equals(vert.property(AAIProperties.LAST_MOD_TS).value())); - assertEquals("AAI-TEST-2", vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value()); + String resourceVer = (String)vert.property(AAIProperties.RESOURCE_VERSION).value(); + String lastModTs = (String)vert.property(AAIProperties.LAST_MOD_TS).value(); + String lastModSoT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value(); + assertFalse(resverStart.equals(resourceVer)); + assertFalse(lastModTimeStart.equals(lastModTs)); + assertEquals("AAI-TEST-2", lastModSoT); } @Test |