Integrate aai-schema-ingest library into aai-core
[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 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
23 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
24 import org.apache.tinkerpop.gremlin.structure.Graph;
25 import org.apache.tinkerpop.gremlin.structure.T;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.onap.aai.AAISetup;
33 import org.onap.aai.dbmap.DBConnectionType;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.serialization.db.DBSerializer;
38 import org.onap.aai.serialization.db.EdgeSerializer;
39 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
40 import org.onap.aai.serialization.engines.QueryStyle;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
42 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
43 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
44 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
45 import org.onap.aai.setup.SchemaVersion;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.test.annotation.DirtiesContext;
48
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.Mockito.spy;
51 import static org.mockito.Mockito.when;
52
53 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
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
65         @Autowired
66         private EdgeSerializer rules;
67
68         private SchemaVersion version;
69         private Vertex pserver;
70         private Vertex complex;
71
72         private DBSerializer serializer;
73         
74         @Before
75         public void setUp() throws Exception {
76
77             version = schemaVersions.getDefaultVersion();
78
79                 MockitoAnnotations.initMocks(this);
80
81                 graph = TinkerGraph.open();
82
83                 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
84                                 "hostname-1");
85                 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
86                                 "physical-location-id", "physical-location-id-1", "country", "US");
87
88                 GraphTraversalSource g = graph.traversal();
89                 rules.addEdge(g, pserver1, complex1);
90                 
91                 pserver = pserver1;
92                 complex = complex1;
93
94                 System.setProperty("AJSC_HOME", ".");
95                 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
96
97                 createLoaderEngineSetup();
98         }
99
100         @Test
101         public void verifyPserverRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
102                 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("org.onap.relationships.inventory.LocatedIn"));
103         }
104         
105         @Test
106         public void verifyPserverRelatedToComplexLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
107                 assertTrue(rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString().equals("complex"));
108         }
109         
110         @Test
111         public void verifyComplexRelatedToHasEdgeLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
112                 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString().equals("org.onap.relationships.inventory.LocatedIn"));
113         }
114         
115         @Test
116         public void verifyComplexRelatedToPserverLabel () throws AAIFormatVertexException, AAIException, AAIFormatQueryResultFormatNotSupported {
117                 assertTrue(rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString().equals("pserver"));
118         }
119
120         public void createLoaderEngineSetup() throws AAIException {
121
122                 if (loader == null) {
123                         loader = loaderFactory.createLoaderForVersion(factoryType, version);
124                         //loader = LoaderFactory.createLoaderForVersion(factoryType, version);
125                         dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
126                         serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
127                         rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
128
129                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
130
131                         when(dbEngine.tx()).thenReturn(graph);
132                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
133
134                         when(spyAdmin.getReadOnlyTraversalSource())
135                                         .thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
136                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
137                 }
138         }
139 }