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 static org.hamcrest.MatcherAssert.assertThat;
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
28 import com.google.gson.JsonObject;
30 import java.util.Optional;
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
33 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
34 import org.apache.tinkerpop.gremlin.structure.Graph;
35 import org.apache.tinkerpop.gremlin.structure.T;
36 import org.apache.tinkerpop.gremlin.structure.Vertex;
37 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
38 import org.hamcrest.CoreMatchers;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.aai.AAISetup;
44 import org.onap.aai.exceptions.AAIException;
45 import org.onap.aai.introspection.Loader;
46 import org.onap.aai.introspection.ModelType;
47 import org.onap.aai.serialization.db.DBSerializer;
48 import org.onap.aai.serialization.db.EdgeSerializer;
49 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
50 import org.onap.aai.serialization.engines.QueryStyle;
51 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
52 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
53 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
54 import org.onap.aai.setup.SchemaVersion;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.test.annotation.DirtiesContext;
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
59 public class PathedURLTest extends AAISetup {
62 private UrlBuilder urlBuilder;
65 private TransactionalGraphEngine dbEngine;
66 private Loader loader;
67 private PathedURL pathedURL;
68 private final ModelType factoryType = ModelType.MOXY;
71 private EdgeSerializer rules;
73 private SchemaVersion version;
74 private Vertex pserver;
75 private Vertex complex;
76 private DBSerializer serializer;
79 public void setUp() throws Exception {
81 version = schemaVersions.getDefaultVersion();
83 MockitoAnnotations.initMocks(this);
85 graph = TinkerGraph.open();
87 Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
88 "hostname-1", "resource-version", System.currentTimeMillis());
90 Vertex complex1 = graph.addVertex(T.label, "complex", T.id, "3", "aai-node-type", "complex",
91 "physical-location-id", "physical-location-id-1", "country", "US");
93 GraphTraversalSource g = graph.traversal();
94 rules.addEdge(g, pserver1, complex1);
99 System.setProperty("AJSC_HOME", ".");
100 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
102 createLoaderEngineSetup();
105 private void createLoaderEngineSetup() throws AAIException {
107 if (loader == null) {
108 loader = loaderFactory.createLoaderForVersion(factoryType, version);
109 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
110 serializer = new DBSerializer(version, dbEngine, factoryType, "Junit");
112 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
114 when(dbEngine.tx()).thenReturn(graph);
115 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
117 when(spyAdmin.getReadOnlyTraversalSource())
118 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
119 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
124 public void testPathedUrlReturnsResourceVersionWhenSet() throws AAIFormatVertexException, AAIException {
126 pathedURL = new PathedURL.Builder(loader, serializer, urlBuilder).includeUrl().build();
127 when(urlBuilder.pathed(pserver)).thenReturn("/aai/v14/cloud-infrastructure/pservers/pserver/hostname-1");
128 Optional<JsonObject> jsonObjectOptional = pathedURL.getJsonFromVertex(pserver);
130 if (!jsonObjectOptional.isPresent()) {
131 fail("Expecting an json object returned from pathed url but returned none");
134 JsonObject pserverObject = jsonObjectOptional.get();
136 assertNotNull("Expecting the pserver object to contain resource type", pserverObject.get("resource-type"));
137 assertThat(pserverObject.get("resource-type").getAsString(), CoreMatchers.is("pserver"));
138 assertNotNull("Expecting the pserver object to contain resource link", pserverObject.get("resource-link"));
139 assertThat(pserverObject.get("resource-link").getAsString(),
140 CoreMatchers.is("/aai/v14/cloud-infrastructure/pservers/pserver/hostname-1"));
141 assertNotNull("Expecting the pserver object to contain resource version",
142 pserverObject.get("resource-version"));
146 public void testPathedUrlReturnsResourceVersionWhenIncludeUrlIsNotSet()
147 throws AAIFormatVertexException, AAIException {
149 pathedURL = new PathedURL.Builder(loader, serializer, urlBuilder).build();
150 when(urlBuilder.pathed(pserver)).thenReturn("/aai/v14/cloud-infrastructure/pservers/pserver/hostname-1");
151 Optional<JsonObject> jsonObjectOptional = pathedURL.getJsonFromVertex(pserver);
153 if (!jsonObjectOptional.isPresent()) {
154 fail("Expecting an json object returned from pathed url but returned none");
157 JsonObject pserverObject = jsonObjectOptional.get();
159 assertNotNull("Expecting the pserver object to contain resource type", pserverObject.get("resource-type"));
160 assertThat(pserverObject.get("resource-type").getAsString(), CoreMatchers.is("pserver"));
161 assertNotNull("Expecting the pserver object to contain resource link", pserverObject.get("resource-link"));
162 assertThat(pserverObject.get("resource-link").getAsString(),
163 CoreMatchers.is("/aai/v14/cloud-infrastructure/pservers/pserver/hostname-1"));
164 assertNull("Expecting the pserver object to not contain resource version",
165 pserverObject.get("resource-version"));