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