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