199939eda8a01bb7a2200a8e7e2ba62c6ab6c3fd
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / PathedURLTest.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.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;
49
50 import java.util.Optional;
51
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;
56
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class PathedURLTest extends AAISetup {
59
60     @Mock
61     private UrlBuilder urlBuilder;
62
63     private Graph graph;
64     private TransactionalGraphEngine dbEngine;
65     private Loader loader;
66     private PathedURL pathedURL;
67     private final ModelType factoryType = ModelType.MOXY;
68
69     @Autowired
70     private EdgeSerializer rules;
71
72     private SchemaVersion version;
73     private Vertex pserver;
74     private Vertex complex;
75     private DBSerializer serializer;
76
77     @Before
78     public void setUp() throws Exception {
79
80         version = schemaVersions.getDefaultVersion();
81
82         MockitoAnnotations.initMocks(this);
83
84         graph = TinkerGraph.open();
85
86         Vertex pserver1 =
87             graph.addVertex(
88                 T.label, "pserver",
89                 T.id, "2",
90                 "aai-node-type", "pserver",
91                 "hostname", "hostname-1",
92                 "resource-version", System.currentTimeMillis()
93             );
94
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");
97
98         GraphTraversalSource g = graph.traversal();
99         rules.addEdge(g, pserver1, complex1);
100
101         pserver = pserver1;
102         complex = complex1;
103
104         System.setProperty("AJSC_HOME", ".");
105         System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
106
107         createLoaderEngineSetup();
108     }
109
110     private void createLoaderEngineSetup() throws AAIException {
111
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");
116
117             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
118
119             when(dbEngine.tx()).thenReturn(graph);
120             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
121
122             when(spyAdmin.getReadOnlyTraversalSource())
123                 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
124             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
125         }
126     }
127
128     @Test
129     public void testPathedUrlReturnsResourceVersionWhenSet() throws AAIFormatVertexException, AAIException {
130
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);
134
135         if(!jsonObjectOptional.isPresent()){
136             fail("Expecting an json object returned from pathed url but returned none");
137         }
138
139         JsonObject pserverObject = jsonObjectOptional.get();
140
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"));
146     }
147
148     @Test
149     public void testPathedUrlReturnsResourceVersionWhenIncludeUrlIsNotSet() throws AAIFormatVertexException, AAIException {
150
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);
154
155         if(!jsonObjectOptional.isPresent()){
156             fail("Expecting an json object returned from pathed url but returned none");
157         }
158
159         JsonObject pserverObject = jsonObjectOptional.get();
160
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"));
166     }
167 }