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.hamcrest.CoreMatchers;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.aai.AAISetup;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.introspection.ModelType;
39 import org.onap.aai.serialization.db.DBSerializer;
40 import org.onap.aai.serialization.db.EdgeSerializer;
41 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
42 import org.onap.aai.serialization.engines.QueryStyle;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
44 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
45 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
46 import org.onap.aai.setup.SchemaVersion;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.test.annotation.DirtiesContext;
50 import java.util.Optional;
52 import static org.hamcrest.MatcherAssert.assertThat;
53 import static org.junit.Assert.*;
54 import static org.mockito.Mockito.spy;
55 import static org.mockito.Mockito.when;
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class PathedURLTest extends AAISetup {
61 private UrlBuilder urlBuilder;
64 private TransactionalGraphEngine dbEngine;
65 private Loader loader;
66 private PathedURL pathedURL;
67 private final ModelType factoryType = ModelType.MOXY;
70 private EdgeSerializer rules;
72 private SchemaVersion version;
73 private Vertex pserver;
74 private Vertex complex;
75 private DBSerializer serializer;
78 public void setUp() throws Exception {
80 version = schemaVersions.getDefaultVersion();
82 MockitoAnnotations.initMocks(this);
84 graph = TinkerGraph.open();
90 "aai-node-type", "pserver",
91 "hostname", "hostname-1",
92 "resource-version", System.currentTimeMillis()
95 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
96 "physical-location-id", "physical-location-id-1", "country", "US");
98 GraphTraversalSource g = graph.traversal();
99 rules.addEdge(g, pserver1, complex1);
104 System.setProperty("AJSC_HOME", ".");
105 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
107 createLoaderEngineSetup();
110 private void createLoaderEngineSetup() throws AAIException {
112 if (loader == null) {
113 loader = loaderFactory.createLoaderForVersion(factoryType, version);
114 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
115 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
117 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
119 when(dbEngine.tx()).thenReturn(graph);
120 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
122 when(spyAdmin.getReadOnlyTraversalSource())
123 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
124 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
129 public void testPathedUrlReturnsResourceVersionWhenSet() throws AAIFormatVertexException, AAIException {
131 pathedURL = new PathedURL.Builder(loader, serializer, urlBuilder).includeUrl().build();
132 when(urlBuilder.pathed(pserver)).thenReturn("/aai/v14/cloud-infrastructure/pservers/pserver/hostname-1");
133 Optional<JsonObject> jsonObjectOptional = pathedURL.getJsonFromVertex(pserver);
135 if(!jsonObjectOptional.isPresent()){
136 fail("Expecting an json object returned from pathed url but returned none");
139 JsonObject pserverObject = jsonObjectOptional.get();
141 assertNotNull("Expecting the pserver object to contain resource type", pserverObject.get("resource-type"));
142 assertThat(pserverObject.get("resource-type").getAsString(), CoreMatchers.is("pserver"));
143 assertNotNull("Expecting the pserver object to contain resource link", pserverObject.get("resource-link"));
144 assertThat(pserverObject.get("resource-link").getAsString(), CoreMatchers.is("/aai/v14/cloud-infrastructure/pservers/pserver/hostname-1"));
145 assertNotNull("Expecting the pserver object to contain resource version", pserverObject.get("resource-version"));
149 public void testPathedUrlReturnsResourceVersionWhenIncludeUrlIsNotSet() throws AAIFormatVertexException, AAIException {
151 pathedURL = new PathedURL.Builder(loader, serializer, urlBuilder).build();
152 when(urlBuilder.pathed(pserver)).thenReturn("/aai/v14/cloud-infrastructure/pservers/pserver/hostname-1");
153 Optional<JsonObject> jsonObjectOptional = pathedURL.getJsonFromVertex(pserver);
155 if(!jsonObjectOptional.isPresent()){
156 fail("Expecting an json object returned from pathed url but returned none");
159 JsonObject pserverObject = jsonObjectOptional.get();
161 assertNotNull("Expecting the pserver object to contain resource type", pserverObject.get("resource-type"));
162 assertThat(pserverObject.get("resource-type").getAsString(), CoreMatchers.is("pserver"));
163 assertNotNull("Expecting the pserver object to contain resource link", pserverObject.get("resource-link"));
164 assertThat(pserverObject.get("resource-link").getAsString(), CoreMatchers.is("/aai/v14/cloud-infrastructure/pservers/pserver/hostname-1"));
165 assertNull("Expecting the pserver object to not contain resource version", pserverObject.get("resource-version"));