Update spring boot to 2.3
[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 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;
27
28 import com.google.gson.JsonObject;
29
30 import java.util.Optional;
31
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;
57
58 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
59 public class PathedURLTest extends AAISetup {
60
61     @Mock
62     private UrlBuilder urlBuilder;
63
64     private Graph graph;
65     private TransactionalGraphEngine dbEngine;
66     private Loader loader;
67     private PathedURL pathedURL;
68     private final ModelType factoryType = ModelType.MOXY;
69
70     @Autowired
71     private EdgeSerializer rules;
72
73     private SchemaVersion version;
74     private Vertex pserver;
75     private Vertex complex;
76     private DBSerializer serializer;
77
78     @Before
79     public void setUp() throws Exception {
80
81         version = schemaVersions.getDefaultVersion();
82
83         MockitoAnnotations.initMocks(this);
84
85         graph = TinkerGraph.open();
86
87         Vertex pserver1 = graph.addVertex(T.label, "pserver", T.id, "2", "aai-node-type", "pserver", "hostname",
88                 "hostname-1", "resource-version", System.currentTimeMillis());
89
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");
92
93         GraphTraversalSource g = graph.traversal();
94         rules.addEdge(g, pserver1, complex1);
95
96         pserver = pserver1;
97         complex = complex1;
98
99         System.setProperty("AJSC_HOME", ".");
100         System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
101
102         createLoaderEngineSetup();
103     }
104
105     private void createLoaderEngineSetup() throws AAIException {
106
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");
111
112             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
113
114             when(dbEngine.tx()).thenReturn(graph);
115             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
116
117             when(spyAdmin.getReadOnlyTraversalSource())
118                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
119             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
120         }
121     }
122
123     @Test
124     public void testPathedUrlReturnsResourceVersionWhenSet() throws AAIFormatVertexException, AAIException {
125
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);
129
130         if (!jsonObjectOptional.isPresent()) {
131             fail("Expecting an json object returned from pathed url but returned none");
132         }
133
134         JsonObject pserverObject = jsonObjectOptional.get();
135
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"));
143     }
144
145     @Test
146     public void testPathedUrlReturnsResourceVersionWhenIncludeUrlIsNotSet()
147             throws AAIFormatVertexException, AAIException {
148
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);
152
153         if (!jsonObjectOptional.isPresent()) {
154             fail("Expecting an json object returned from pathed url but returned none");
155         }
156
157         JsonObject pserverObject = jsonObjectOptional.get();
158
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"));
166     }
167 }