Added support for Multiple Edges
[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 (C) 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
21 package org.onap.aai.serialization.queryformats;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.when;
29
30 import java.io.UnsupportedEncodingException;
31
32 import org.apache.tinkerpop.gremlin.process.traversal.Path;
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
34 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
35 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
36 import org.apache.tinkerpop.gremlin.structure.Graph;
37 import org.apache.tinkerpop.gremlin.structure.T;
38 import org.apache.tinkerpop.gremlin.structure.Vertex;
39 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.aai.AAISetup;
45 import org.onap.aai.dbmap.DBConnectionType;
46 import org.onap.aai.exceptions.AAIException;
47 import org.onap.aai.introspection.Loader;
48 import org.onap.aai.introspection.LoaderFactory;
49 import org.onap.aai.introspection.ModelType;
50 import org.onap.aai.introspection.Version;
51 import org.onap.aai.serialization.db.DBSerializer;
52 import org.onap.aai.serialization.db.EdgeRules;
53 import org.onap.aai.serialization.engines.QueryStyle;
54 import org.onap.aai.serialization.engines.TitanDBEngine;
55 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
56 import org.onap.aai.serialization.queryformats.RawFormat.Builder;
57 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
58 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
59 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
60
61 import com.google.gson.JsonObject;
62 import com.google.gson.JsonParser;
63
64 public class RawFormatTest extends AAISetup {
65
66         @Mock
67         private UrlBuilder urlBuilder;
68
69         private Graph graph;
70         private TransactionalGraphEngine dbEngine;
71         private Loader loader;
72         private RawFormat rawFormat;
73         private final ModelType factoryType = ModelType.MOXY;
74         private final EdgeRules rules = EdgeRules.getInstance();
75         private Version version = Version.getLatest();
76         private Vertex pserver;
77         private Vertex complex;
78
79         private DBSerializer serializer;
80
81         @Before
82         public void setUp() throws Exception {
83
84                 MockitoAnnotations.initMocks(this);
85
86                 graph = TinkerGraph.open();
87
88                 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
89                                 "hostname-1");
90                 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
91                                 "physical-location-id", "physical-location-id-1", "country", "US");
92
93                 GraphTraversalSource g = graph.traversal();
94                 rules.addEdge(g, pserver1, complex1);
95                 
96                 pserver = pserver1;
97                 complex = complex1;
98                 
99                 createLoaderEngineSetup();
100
101         }
102
103         @Test
104         public void verifyPserverRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
105                 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("locatedIn"));
106         }
107         
108         @Test
109         public void verifyPserverRelatedToComplexLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
110                 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString().equals("complex"));
111         }
112         
113         @Test
114         public void verifyComplexRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
115                 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("locatedIn"));
116         }
117         
118         @Test
119         public void verifyComplexRelatedToPserverLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
120                 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString().equals("pserver"));
121         }
122
123         public void createLoaderEngineSetup() throws AAIException {
124
125                 if (loader == null) {
126                         loader = LoaderFactory.createLoaderForVersion(factoryType, version);
127                         dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
128                         serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
129                         rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
130
131                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
132
133                         when(dbEngine.tx()).thenReturn(graph);
134                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
135
136                         when(spyAdmin.getReadOnlyTraversalSource())
137                                         .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
138                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
139                 }
140         }
141 }