Update spring boot to 2.4
[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.*;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
27 import com.google.gson.JsonObject;
28
29 import java.util.*;
30
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
33 import org.apache.tinkerpop.gremlin.structure.Graph;
34 import org.apache.tinkerpop.gremlin.structure.T;
35 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.exceptions.AAIException;
43 import org.onap.aai.introspection.Loader;
44 import org.onap.aai.introspection.ModelType;
45 import org.onap.aai.serialization.db.DBSerializer;
46 import org.onap.aai.serialization.db.EdgeSerializer;
47 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
48 import org.onap.aai.serialization.engines.QueryStyle;
49 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
50 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
51 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
52 import org.onap.aai.setup.SchemaVersion;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.test.annotation.DirtiesContext;
55
56 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
57 public class RawFormatTest extends AAISetup {
58
59     @Mock
60     private UrlBuilder urlBuilder;
61
62     private Graph graph;
63     private TransactionalGraphEngine dbEngine;
64     private Loader loader;
65     private RawFormat rawFormat;
66     private final ModelType factoryType = ModelType.MOXY;
67
68     @Autowired
69     private EdgeSerializer rules;
70
71     private SchemaVersion version;
72     private Vertex pserver;
73     private Vertex complex;
74
75     @Before
76     public void setUp() throws Exception {
77
78         version = schemaVersions.getDefaultVersion();
79
80         MockitoAnnotations.openMocks(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() throws AAIFormatVertexException {
103         assertEquals("org.onap.relationships.inventory.LocatedIn", rawFormat.createRelationshipObject(pserver).get(0)
104                 .getAsJsonObject().get("relationship-label").getAsString());
105     }
106
107     @Test
108     public void verifyPserverRelatedToComplexLabel() throws AAIFormatVertexException {
109         assertEquals("complex",
110                 rawFormat.createRelationshipObject(pserver).get(0).getAsJsonObject().get("node-type").getAsString());
111     }
112
113     @Test
114     public void verifyComplexRelatedToHasEdgeLabel() throws AAIFormatVertexException {
115         assertEquals("org.onap.relationships.inventory.LocatedIn", rawFormat.createRelationshipObject(complex).get(0)
116                 .getAsJsonObject().get("relationship-label").getAsString());
117     }
118
119     @Test
120     public void verifyComplexRelatedToPserverLabel() throws AAIFormatVertexException {
121         assertEquals("pserver",
122                 rawFormat.createRelationshipObject(complex).get(0).getAsJsonObject().get("node-type").getAsString());
123     }
124
125     private void createLoaderEngineSetup() throws AAIException {
126
127         if (loader == null) {
128             loader = loaderFactory.createLoaderForVersion(factoryType, version);
129             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
130             DBSerializer serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
131             rawFormat = new RawFormat.Builder(loader, serializer, urlBuilder).build();
132
133             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
134
135             when(dbEngine.tx()).thenReturn(graph);
136             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
137
138             when(spyAdmin.getReadOnlyTraversalSource())
139                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
140             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
141         }
142     }
143
144     @Test
145     public void run() throws AAIFormatVertexException {
146         assertNotNull(dbEngine.tx());
147         System.out.println(dbEngine.tx());
148         assertNotNull(graph.traversal());
149         JsonObject json = rawFormat.createPropertiesObject(pserver).get();
150         json.entrySet().forEach((System.out::println));
151         assertTrue(json.has("hostname"));
152         Map<String, List<String>> propMap = new HashMap<>();
153         List<String> selectedProps = new ArrayList<String>(Arrays.asList("'physical-location-id'"));
154         propMap.put("complex", selectedProps);
155         JsonObject json1 = rawFormat.createSelectedPropertiesObject(complex, propMap).get();
156         json1.entrySet().forEach((System.out::println));
157         assertFalse(json1.has("aai-node-type"));
158         assertTrue(json1.has("physical-location-id"));
159     }
160
161 }