2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import com.google.gson.JsonObject;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
26 import org.apache.tinkerpop.gremlin.structure.Graph;
27 import org.apache.tinkerpop.gremlin.structure.T;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.aai.AAISetup;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.ModelType;
38 import org.onap.aai.serialization.db.DBSerializer;
39 import org.onap.aai.serialization.db.EdgeSerializer;
40 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
41 import org.onap.aai.serialization.engines.QueryStyle;
42 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
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;
51 import static org.junit.Assert.*;
52 import static org.mockito.Mockito.spy;
53 import static org.mockito.Mockito.when;
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class RawFormatTest extends AAISetup {
59 private UrlBuilder urlBuilder;
62 private TransactionalGraphEngine dbEngine;
63 private Loader loader;
64 private RawFormat rawFormat;
65 private final ModelType factoryType = ModelType.MOXY;
68 private EdgeSerializer rules;
70 private SchemaVersion version;
71 private Vertex pserver;
72 private Vertex complex;
75 public void setUp() throws Exception {
77 version = schemaVersions.getDefaultVersion();
79 MockitoAnnotations.initMocks(this);
81 graph = TinkerGraph.open();
84 graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname", "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");
88 GraphTraversalSource g = graph.traversal();
89 rules.addEdge(g, pserver1, complex1);
94 System.setProperty("AJSC_HOME", ".");
95 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
97 createLoaderEngineSetup();
101 public void verifyPserverRelatedToHasEdgeLabel()
102 throws AAIFormatVertexException {
103 assertEquals("org.onap.relationships.inventory.LocatedIn", rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString());
107 public void verifyPserverRelatedToComplexLabel()
108 throws AAIFormatVertexException {
109 assertEquals("complex", rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString());
113 public void verifyComplexRelatedToHasEdgeLabel()
114 throws AAIFormatVertexException {
115 assertEquals("org.onap.relationships.inventory.LocatedIn", rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString());
119 public void verifyComplexRelatedToPserverLabel()
120 throws AAIFormatVertexException {
121 assertEquals("pserver", rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString());
124 private void createLoaderEngineSetup() throws AAIException {
126 if (loader == null) {
127 loader = loaderFactory.createLoaderForVersion(factoryType, version);
128 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
129 DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
130 rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
132 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
134 when(dbEngine.tx()).thenReturn(graph);
135 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
137 when(spyAdmin.getReadOnlyTraversalSource())
138 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
139 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
144 public void run() throws AAIFormatVertexException {
145 assertNotNull(dbEngine.tx());
146 System.out.println(dbEngine.tx());
147 assertNotNull(graph.traversal());
148 JsonObject json = rawFormat.createPropertiesObject(pserver).get();
149 json.entrySet().forEach((System.out::println));
150 assertTrue(json.has("hostname"));
151 Map<String, List<String>> propMap = new HashMap<>();
152 List<String> selectedProps = new ArrayList<String>( Arrays.asList("'physical-location-id'"));
153 propMap.put("complex",selectedProps);
154 JsonObject json1 = rawFormat.createSelectedPropertiesObject(complex, propMap).get();
155 json1.entrySet().forEach((System.out::println));
156 assertFalse(json1.has("aai-node-type"));
157 assertTrue(json1.has("physical-location-id"));