f88de4fb1892d4ad9db81eb8185cf0ec92d0398f
[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 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;
48
49 import java.util.*;
50
51 import static org.junit.Assert.*;
52 import static org.mockito.Mockito.spy;
53 import static org.mockito.Mockito.when;
54
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class RawFormatTest extends AAISetup {
57
58     @Mock
59     private UrlBuilder urlBuilder;
60
61     private Graph graph;
62     private TransactionalGraphEngine dbEngine;
63     private Loader loader;
64     private RawFormat rawFormat;
65     private final ModelType factoryType = ModelType.MOXY;
66
67     @Autowired
68     private EdgeSerializer rules;
69
70     private SchemaVersion version;
71     private Vertex pserver;
72     private Vertex complex;
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 =
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");
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()
102             throws AAIFormatVertexException {
103         assertEquals("org.onap.relationships.inventory.LocatedIn", rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("relationship-label").getAsString());
104     }
105
106     @Test
107     public void verifyPserverRelatedToComplexLabel()
108             throws AAIFormatVertexException {
109         assertEquals("complex", rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString());
110     }
111
112     @Test
113     public void verifyComplexRelatedToHasEdgeLabel()
114             throws AAIFormatVertexException {
115         assertEquals("org.onap.relationships.inventory.LocatedIn", rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("relationship-label").getAsString());
116     }
117
118     @Test
119     public void verifyComplexRelatedToPserverLabel()
120             throws AAIFormatVertexException {
121         assertEquals("pserver", rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString());
122     }
123
124     private void createLoaderEngineSetup() throws AAIException {
125
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();
131
132             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
133
134             when(dbEngine.tx()).thenReturn(graph);
135             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
136
137             when(spyAdmin.getReadOnlyTraversalSource())
138                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
139             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
140         }
141     }
142
143     @Test
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"));
158     }
159
160
161 }