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.*;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
27 import com.google.gson.JsonObject;
29 import java.util.Arrays;
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
34 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
35 import org.apache.tinkerpop.gremlin.structure.Graph;
36 import org.apache.tinkerpop.gremlin.structure.T;
37 import org.apache.tinkerpop.gremlin.structure.Vertex;
38 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
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.engines.JanusGraphDBEngine;
49 import org.onap.aai.serialization.engines.QueryStyle;
50 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
51 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
52 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
53 import org.springframework.test.annotation.DirtiesContext;
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class ResourceFormatTest extends AAISetup {
59 private UrlBuilder urlBuilder;
62 private TransactionalGraphEngine dbEngine;
63 private Loader loader;
64 private DBSerializer serializer;
65 private Resource resource;
66 private Vertex vfModule;
67 private Vertex unknown;
68 private final ModelType factoryType = ModelType.MOXY;
71 public void setUp() throws Exception {
73 MockitoAnnotations.initMocks(this);
75 graph = TinkerGraph.open();
77 vfModule = graph.addVertex(T.label, "vf-module", T.id, "5", "aai-node-type", "vf-module", "vf-module-id",
78 "vf-module-id-val-68205", "vf-module-name", "example-vf-module-name-val-68205", "heat-stack-id",
79 "example-heat-stack-id-val-68205", "orchestration-status", "example-orchestration-status-val-68205",
80 "is-base-vf-module", "true", "resource-version", "1498166571906", "model-invariant-id",
81 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-invariant-id-local",
82 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
83 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c", "widget-model-id",
84 "example-widget-model-id-val-68205", "widget-model-version", "example-widget--model-version-val-68205",
85 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205");
87 unknown = graph.addVertex(T.label, "unknown", T.id, "1", "aai-node-type", "unknown", "vserver-id",
88 "vserver-id-1", "vserver-name", "vserver-name-1");
92 public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
94 createLoaderEngineSetup();
95 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
96 resource = new Resource.Builder(loader, serializer, urlBuilder).build();
98 assertNotNull(dbEngine.tx());
99 assertNotNull(dbEngine.asAdmin());
101 JsonObject json = resource.getJsonFromVertex(vfModule).get();
103 assertTrue(json.getAsJsonObject("vf-module").has("model-invariant-id"));
104 assertTrue(json.getAsJsonObject("vf-module").has("model-version-id"));
106 assertFalse(json.getAsJsonObject("vf-module").has("model-invariant-id-local"));
107 assertFalse(json.getAsJsonObject("vf-module").has("model-version-id-local"));
112 public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
114 createLoaderEngineSetup();
115 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
116 resource = new Resource.Builder(loader, serializer, urlBuilder).build();
118 assertNotNull(dbEngine.tx());
119 assertNotNull(dbEngine.asAdmin());
121 assertFalse(resource.getJsonFromVertex(unknown).isPresent());
126 public void testFormattingUnknownVertex() throws AAIFormatVertexException, AAIException {
128 createLoaderEngineSetup();
129 serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
132 new FormatFactory(loader, serializer, schemaVersions, basePath, "https://localhost:8447/aai/");
133 MultivaluedMap mvm = new MultivaluedHashMap();
134 mvm.add("depth", "0");
135 Formatter formatter = ff.get(Format.resource, mvm);
137 JsonObject json = formatter.output(Arrays.asList(unknown, vfModule));
138 System.out.println(json);
142 public void createLoaderEngineSetup() {
144 if (loader == null) {
145 loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getAppRootVersion());
146 dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
148 TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
150 when(dbEngine.tx()).thenReturn(graph);
151 when(dbEngine.asAdmin()).thenReturn(spyAdmin);
153 when(spyAdmin.getReadOnlyTraversalSource())
154 .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
155 when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());