a33349fd13eeb65216dfce77628076f174214a72
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / RawFormatTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.queryformats;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
29 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
30 import org.apache.tinkerpop.gremlin.structure.Graph;
31 import org.apache.tinkerpop.gremlin.structure.T;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.aai.AAISetup;
39 import org.onap.aai.dbmap.DBConnectionType;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.introspection.Loader;
42 import org.onap.aai.introspection.LoaderFactory;
43 import org.onap.aai.introspection.ModelType;
44 import org.onap.aai.introspection.Version;
45 import org.onap.aai.serialization.db.DBSerializer;
46 import org.onap.aai.serialization.db.EdgeRules;
47 import org.onap.aai.serialization.engines.QueryStyle;
48 import org.onap.aai.serialization.engines.TitanDBEngine;
49 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
50 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
51 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
52 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
53
54 public class RawFormatTest extends AAISetup {
55
56         @Mock
57         private UrlBuilder urlBuilder;
58
59         private Graph graph;
60         private TransactionalGraphEngine dbEngine;
61         private Loader loader;
62         private RawFormat rawFormat;
63         private final ModelType factoryType = ModelType.MOXY;
64         private final EdgeRules rules = EdgeRules.getInstance();
65         private Version version = Version.getLatest();
66         private Vertex pserver;
67         private Vertex complex;
68
69         private DBSerializer serializer;
70
71         @Before
72         public void setUp() throws Exception {
73
74                 MockitoAnnotations.initMocks(this);
75
76                 graph = TinkerGraph.open();
77
78                 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
79                                 "hostname-1");
80                 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
81                                 "physical-location-id", "physical-location-id-1", "country", "US");
82
83                 GraphTraversalSource g = graph.traversal();
84                 rules.addEdge(g, pserver1, complex1);
85                 
86                 pserver = pserver1;
87                 complex = complex1;
88                 
89                 createLoaderEngineSetup();
90
91         }
92
93         @Test
94         public void verifyPserverRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
95                 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("locatedIn"));
96         }
97         
98         @Test
99         public void verifyPserverRelatedToComplexLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
100                 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString().equals("complex"));
101         }
102         
103         @Test
104         public void verifyComplexRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
105                 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("locatedIn"));
106         }
107         
108         @Test
109         public void verifyComplexRelatedToPserverLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
110                 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString().equals("pserver"));
111         }
112
113         public void createLoaderEngineSetup() throws AAIException {
114
115                 if (loader == null) {
116                         loader = LoaderFactory.createLoaderForVersion(factoryType, version);
117                         dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
118                         serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
119                         rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
120
121                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
122
123                         when(dbEngine.tx()).thenReturn(graph);
124                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
125
126                         when(spyAdmin.getReadOnlyTraversalSource())
127                                         .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
128                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
129                 }
130         }
131 }