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.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.when;
29 import com.google.gson.JsonObject;
31 import java.util.Arrays;
33 import javax.ws.rs.core.MultivaluedHashMap;
34 import javax.ws.rs.core.MultivaluedMap;
36 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
37 import org.apache.tinkerpop.gremlin.structure.Graph;
38 import org.apache.tinkerpop.gremlin.structure.T;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.aai.AAISetup;
46 import org.onap.aai.exceptions.AAIException;
47 import org.onap.aai.introspection.Loader;
48 import org.onap.aai.introspection.ModelType;
49 import org.onap.aai.serialization.db.DBSerializer;
50 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
51 import org.onap.aai.serialization.engines.QueryStyle;
52 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
53 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
54 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
55 import org.springframework.test.annotation.DirtiesContext;
57 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
58 public class ResourceFormatTest extends AAISetup {
61 private UrlBuilder urlBuilder;
64 private TransactionalGraphEngine dbEngine;
65 private Loader loader;
66 private DBSerializer serializer;
67 private Resource resource;
68 private Vertex vfModule;
69 private Vertex unknown;
70 private final ModelType factoryType = ModelType.MOXY;
73 public void setUp() throws Exception {
75 MockitoAnnotations.openMocks(this);
77 graph = TinkerGraph.open();
79 vfModule = graph.addVertex(T.label, "vf-module", T.id, "5", "aai-node-type", "vf-module", "vf-module-id",
80 "vf-module-id-val-68205", "vf-module-name", "example-vf-module-name-val-68205", "heat-stack-id",
81 "example-heat-stack-id-val-68205", "orchestration-status", "example-orchestration-status-val-68205",
82 "is-base-vf-module", "true", "resource-version", "1498166571906", "model-invariant-id",
83 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-invariant-id-local",
84 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
85 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c", "widget-model-id",
86 "example-widget-model-id-val-68205", "widget-model-version", "example-widget--model-version-val-68205",
87 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205");
89 unknown = graph.addVertex(T.label, "unknown", T.id, "1", "aai-node-type", "unknown", "vserver-id",
90 "vserver-id-1", "vserver-name", "vserver-name-1");
94 public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
96 createLoaderEngineSetup();
97 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
98 resource = new Resource.Builder(loader, serializer, urlBuilder).build();
100 assertNotNull(dbEngine.tx());
101 assertNotNull(dbEngine.asAdmin());
103 JsonObject json = resource.getJsonFromVertex(vfModule).get();
105 assertTrue(json.getAsJsonObject("vf-module").has("model-invariant-id"));
106 assertTrue(json.getAsJsonObject("vf-module").has("model-version-id"));
108 assertFalse(json.getAsJsonObject("vf-module").has("model-invariant-id-local"));
109 assertFalse(json.getAsJsonObject("vf-module").has("model-version-id-local"));
114 public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
116 createLoaderEngineSetup();
117 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
118 resource = new Resource.Builder(loader, serializer, urlBuilder).build();
120 assertNotNull(dbEngine.tx());
121 assertNotNull(dbEngine.asAdmin());
123 assertFalse(resource.getJsonFromVertex(unknown).isPresent());
128 public void testFormattingUnknownVertex() throws AAIFormatVertexException, AAIException {
130 createLoaderEngineSetup();
131 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
134 new FormatFactory(loader, serializer, schemaVersions, basePath, "https://localhost:8447/aai/");
135 MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
136 mvm.add("depth", "0");
137 Formatter formatter = ff.get(Format.resource, mvm);
139 JsonObject json = formatter.output(Arrays.asList(unknown, vfModule));
140 System.out.println(json);
144 public void createLoaderEngineSetup() {
146 if (loader == null) {
147 loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getAppRootVersion());
148 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
150 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
152 when(dbEngine.tx()).thenReturn(graph);
153 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
155 when(spyAdmin.getReadOnlyTraversalSource())
156 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
157 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());