diff options
author | Venkata Harish K Kajur <vk250x@att.com> | 2017-09-15 10:25:07 -0400 |
---|---|---|
committer | Venkata Harish K Kajur <vk250x@att.com> | 2017-09-15 10:25:32 -0400 |
commit | 14ea4802d70f479f8d777bb52cf42b3a644c896a (patch) | |
tree | 22eb3200a1c016dfb5c36f5abd59eaaae48be342 /aai-core/src/test/java | |
parent | 4bf722bff30e0b08586946eb973facf0022d3989 (diff) |
Increase the coverage to 35% for aai-common
Issue-ID: AAI-215
Change-Id: I783fc2207aa782a1b8bb55a730a341fa89b4c094
Signed-off-by: Venkata Harish K Kajur <vk250x@att.com>
Diffstat (limited to 'aai-core/src/test/java')
-rw-r--r-- | aai-core/src/test/java/org/openecomp/aai/serialization/db/DbSerializerTest.java | 280 |
1 files changed, 278 insertions, 2 deletions
diff --git a/aai-core/src/test/java/org/openecomp/aai/serialization/db/DbSerializerTest.java b/aai-core/src/test/java/org/openecomp/aai/serialization/db/DbSerializerTest.java index 3c3ec789..35894259 100644 --- a/aai-core/src/test/java/org/openecomp/aai/serialization/db/DbSerializerTest.java +++ b/aai-core/src/test/java/org/openecomp/aai/serialization/db/DbSerializerTest.java @@ -22,14 +22,20 @@ package org.openecomp.aai.serialization.db; import com.thinkaurelius.titan.core.TitanFactory; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Graph; import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.openecomp.aai.AAISetup; +import org.openecomp.aai.db.props.AAIProperties; import org.openecomp.aai.dbmap.DBConnectionType; import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.introspection.Introspector; import org.openecomp.aai.introspection.Loader; import org.openecomp.aai.introspection.LoaderFactory; import org.openecomp.aai.introspection.ModelType; @@ -38,11 +44,23 @@ import org.openecomp.aai.serialization.engines.QueryStyle; import org.openecomp.aai.serialization.engines.TitanDBEngine; import org.openecomp.aai.serialization.engines.TransactionalGraphEngine; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class DbSerializerTest extends AAISetup { + + //to use, set thrown.expect to whatever your test needs + //this line establishes default of expecting no exception to be thrown + @Rule + public ExpectedException thrown = ExpectedException.none(); protected Graph graph; protected final EdgeRules rules = EdgeRules.getInstance(); @@ -53,6 +71,8 @@ public class DbSerializerTest extends AAISetup { private final DBConnectionType type = DBConnectionType.REALTIME; private Loader loader; private TransactionalGraphEngine dbEngine; + private TransactionalGraphEngine engine; //for tests that aren't mocking the engine + private DBSerializer dbser; TransactionalGraphEngine spy; TransactionalGraphEngine.Admin adminSpy; @@ -65,6 +85,9 @@ public class DbSerializerTest extends AAISetup { adminSpy = spy(dbEngine.asAdmin()); createGraph(); + + engine = new TitanDBEngine(queryStyle, type, loader); + dbser = new DBSerializer(Version.getLatest(), engine, introspectorFactoryType, "AAI-TEST"); } public void createGraph() throws AAIException { @@ -156,5 +179,258 @@ public class DbSerializerTest extends AAISetup { return exceptionMessage; } + + @Test + public void createNewVertexTest() throws AAIException { + engine.startTransaction(); + + Introspector testObj = loader.introspectorFromName("generic-vnf"); + + Vertex testVertex = dbser.createNewVertex(testObj); + Vertex fromGraph = engine.tx().traversal().V().has("aai-node-type","generic-vnf").toList().get(0); + assertEquals(testVertex.id(), fromGraph.id()); + assertEquals("AAI-TEST", fromGraph.property(AAIProperties.SOURCE_OF_TRUTH.toString()).value()); + engine.rollback(); + } + + @Test + public void touchStandardVertexPropertiesTest() throws AAIException { + engine.startTransaction(); + DBSerializer dbser2 = new DBSerializer(Version.getLatest(), engine, introspectorFactoryType, "AAI-TEST-2"); + + Graph graph = TinkerGraph.open(); + Vertex vert = graph.addVertex("aai-node-type", "generic-vnf"); + + dbser.touchStandardVertexProperties(vert, true); + String resverStart = (String)vert.property(AAIProperties.RESOURCE_VERSION.toString()).value(); + String lastModTimeStart = (String)vert.property(AAIProperties.LAST_MOD_TS.toString()).value(); + + dbser2.touchStandardVertexProperties(vert, false); + assertFalse(resverStart.equals((String)vert.property(AAIProperties.RESOURCE_VERSION.toString()).value())); + assertFalse(lastModTimeStart.equals((String)vert.property(AAIProperties.LAST_MOD_TS.toString()).value())); + assertEquals("AAI-TEST-2", (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH.toString()).value()); + engine.rollback(); + } + + @Test + public void verifyResourceVersion_SunnyDayTest() throws AAIException { + engine.startTransaction(); + + assertTrue(dbser.verifyResourceVersion("delete", "vnfc", "abc", "abc", "vnfcs/vnfc/vnfcId")); + engine.rollback(); + } + + @Test + public void verifyResourceVersion_CreateWithRVTest() throws AAIException { + engine.startTransaction(); + + thrown.expect(AAIException.class); + thrown.expectMessage("resource-version passed for create of generic-vnfs/generic-vnf/myid"); + try { + dbser.verifyResourceVersion("create", "generic-vnf", null, "old-res-ver", "generic-vnfs/generic-vnf/myid"); + } finally { + engine.rollback(); + } + } + + @Test + public void verifyResourceVersion_MissingRVTest() throws AAIException { + engine.startTransaction(); + + thrown.expect(AAIException.class); + thrown.expectMessage("resource-version not passed for update of generic-vnfs/generic-vnf/myid"); + try { + dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", null, "generic-vnfs/generic-vnf/myid"); + } finally { + engine.rollback(); + } + } + + @Test + public void verifyResourceVersion_MismatchRVTest() throws AAIException { + engine.startTransaction(); + + thrown.expect(AAIException.class); + thrown.expectMessage("resource-version MISMATCH for update of generic-vnfs/generic-vnf/myid"); + try { + dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", "old-res-ver", "generic-vnfs/generic-vnf/myid"); + } finally { + engine.rollback(); + } + } + + @Test + public void trimClassNameTest() throws AAIException { + assertEquals("GenericVnf", dbser.trimClassName("GenericVnf")); + assertEquals("GenericVnf", dbser.trimClassName("org.onap.aai.GenericVnf")); + } + + @Test + public void getURIForVertexTest() throws AAIException, URISyntaxException, UnsupportedEncodingException { + engine.startTransaction(); + + Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123"); + Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453"); + EdgeRules rules = EdgeRules.getInstance(); + rules.addTreeEdge(engine.tx().traversal(), cr, ten); + + URI compare = new URI("/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453"); + assertEquals(compare, dbser.getURIForVertex(ten)); + + cr.property("aai-node-type").remove(); + URI compareFailure = new URI("/unknown-uri"); + assertEquals(compareFailure, dbser.getURIForVertex(ten)); + engine.rollback(); + } + + @Test + public void getVertexPropertiesTest() throws AAIException, UnsupportedEncodingException { + engine.startTransaction(); + + Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123"); + + Introspector crIntro = dbser.getVertexProperties(cr); + assertEquals("cloud-region", crIntro.getDbName()); + assertEquals("me", crIntro.getValue("cloud-owner")); + assertEquals("123", crIntro.getValue("cloud-region-id")); + engine.rollback(); + } + + @Test + public void setCachedURIsTest() throws AAIException, UnsupportedEncodingException, URISyntaxException { + engine.startTransaction(); + + Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123"); + Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453"); + Vertex vs = engine.tx().addVertex("aai-node-type", "vserver", "vserver-id", "vs1", + AAIProperties.AAI_URI.toString(), + "/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453/vservers/vserver/vs1"); + EdgeRules rules = EdgeRules.getInstance(); + rules.addTreeEdge(engine.tx().traversal(), cr, ten); + rules.addTreeEdge(engine.tx().traversal(), ten, vs); + + List<Vertex> vertices = new ArrayList<Vertex>(Arrays.asList(cr, ten, vs)); + Introspector crIn = dbser.getVertexProperties(cr); + Introspector tenIn = dbser.getVertexProperties(ten); + Introspector vsIn = dbser.getVertexProperties(vs); + List<Introspector> intros = new ArrayList<Introspector>(Arrays.asList(crIn, tenIn, vsIn)); + + dbser.setCachedURIs(vertices, intros); + + assertEquals("/cloud-infrastructure/cloud-regions/cloud-region/me/123", + (String)cr.property(AAIProperties.AAI_URI.toString()).value()); + assertEquals("/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453", + (String)ten.property(AAIProperties.AAI_URI.toString()).value()); + assertEquals("/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453/vservers/vserver/vs1", + (String)vs.property(AAIProperties.AAI_URI.toString()).value()); + engine.rollback(); + } + + @Test + public void getEdgeBetweenTest() throws AAIException { + engine.startTransaction(); + + Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123"); + Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453"); + EdgeRules rules = EdgeRules.getInstance(); + rules.addTreeEdge(engine.tx().traversal(), cr, ten); + + Edge e = dbser.getEdgeBetween(EdgeType.TREE, ten, cr); + assertEquals("has", e.label()); + engine.rollback(); + } + + @Test + public void deleteEdgeTest() throws AAIException, UnsupportedEncodingException { + engine.startTransaction(); + + Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf"); + Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name"); + EdgeRules rules = EdgeRules.getInstance(); + rules.addEdge(engine.tx().traversal(), gvnf, vnfc); + + Introspector relData = loader.introspectorFromName("relationship-data"); + relData.setValue("relationship-key", "vnfc.vnfc-name"); + relData.setValue("relationship-value", "a-name"); + Introspector relationship = loader.introspectorFromName("relationship"); + relationship.setValue("related-to", "vnfc"); + relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name"); + relationship.setValue("relationship-data",relData); + + assertTrue(dbser.deleteEdge(relationship, gvnf)); + + assertFalse(engine.tx().traversal().V(gvnf).both("uses").hasNext()); + assertFalse(engine.tx().traversal().V(vnfc).both("uses").hasNext()); + engine.rollback(); + } + + @Test + public void createEdgeTest() throws AAIException, UnsupportedEncodingException { + engine.startTransaction(); + + Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf"); + Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name"); + + //sunny day case + Introspector relData = loader.introspectorFromName("relationship-data"); + relData.setValue("relationship-key", "vnfc.vnfc-name"); + relData.setValue("relationship-value", "a-name"); + Introspector relationship = loader.introspectorFromName("relationship"); + relationship.setValue("related-to", "vnfc"); + relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name"); + relationship.setValue("relationship-data",relData); + + assertTrue(dbser.createEdge(relationship, gvnf)); + assertTrue(engine.tx().traversal().V(gvnf).both("uses").hasNext()); + assertTrue(engine.tx().traversal().V(vnfc).both("uses").hasNext()); + + //rainy day case, edge to nonexistant object + Introspector relData2 = loader.introspectorFromName("relationship-data"); + relData2.setValue("relationship-key", "vnfc.vnfc-name"); + relData2.setValue("relationship-value", "b-name"); + Introspector relationship2 = loader.introspectorFromName("relationship"); + relationship2.setValue("related-to", "vnfc"); + relationship2.setValue("related-link", "/network/vnfcs/vnfc/b-name"); + relationship2.setValue("relationship-data",relData2); + + thrown.expect(AAIException.class); + thrown.expectMessage("Node of type vnfc. Could not find object at: /network/vnfcs/vnfc/b-name"); + try { + dbser.createEdge(relationship2, gvnf); + } finally { + engine.rollback(); + } + } + + @Test + public void serializeSingleVertexTopLevelTest() throws AAIException, UnsupportedEncodingException { + engine.startTransaction(); + + Introspector gvnf = loader.introspectorFromName("generic-vnf"); + Vertex gvnfVert = dbser.createNewVertex(gvnf); + + gvnf.setValue("vnf-id", "myvnf"); + dbser.serializeSingleVertex(gvnfVert, gvnf, "test"); + assertTrue(engine.tx().traversal().V().has("aai-node-type","generic-vnf").has("vnf-id","myvnf").hasNext()); + engine.rollback(); + } + + @Test + public void serializeSingleVertexChildTest() throws AAIException, UnsupportedEncodingException { + engine.startTransaction(); + + Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123"); + Introspector tenIn = loader.introspectorFromName("tenant"); + Vertex ten = dbser.createNewVertex(tenIn); + EdgeRules rules = EdgeRules.getInstance(); + rules.addTreeEdge(engine.tx().traversal(), cr, ten); + + tenIn.setValue("tenant-id", "453"); + tenIn.setValue("tenant-name", "mytenant"); -} + dbser.serializeSingleVertex(ten, tenIn, "test"); + + assertTrue(engine.tx().traversal().V().has("aai-node-type","tenant").has("tenant-id","453").has("tenant-name","mytenant").hasNext()); + engine.rollback(); + } +}
\ No newline at end of file |