Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / ResourceFormatTest.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.junit.Assert.*;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26
27 import com.google.gson.JsonObject;
28
29 import java.util.Arrays;
30
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
33
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;
54
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class ResourceFormatTest extends AAISetup {
57
58     @Mock
59     private UrlBuilder urlBuilder;
60
61     private Graph graph;
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;
69
70     @Before
71     public void setUp() throws Exception {
72
73         MockitoAnnotations.initMocks(this);
74
75         graph = TinkerGraph.open();
76
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");
86
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");
89     }
90
91     @Test
92     public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
93
94         createLoaderEngineSetup();
95         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
96         resource = new Resource.Builder(loader, serializer, urlBuilder).build();
97
98         assertNotNull(dbEngine.tx());
99         assertNotNull(dbEngine.asAdmin());
100
101         JsonObject json = resource.getJsonFromVertex(vfModule).get();
102
103         assertTrue(json.getAsJsonObject("vf-module").has("model-invariant-id"));
104         assertTrue(json.getAsJsonObject("vf-module").has("model-version-id"));
105
106         assertFalse(json.getAsJsonObject("vf-module").has("model-invariant-id-local"));
107         assertFalse(json.getAsJsonObject("vf-module").has("model-version-id-local"));
108
109     }
110
111     @Test
112     public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
113
114         createLoaderEngineSetup();
115         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
116         resource = new Resource.Builder(loader, serializer, urlBuilder).build();
117
118         assertNotNull(dbEngine.tx());
119         assertNotNull(dbEngine.asAdmin());
120
121         assertFalse(resource.getJsonFromVertex(unknown).isPresent());
122
123     }
124
125     @Test
126     public void testFormattingUnknownVertex() throws AAIFormatVertexException, AAIException {
127
128         createLoaderEngineSetup();
129         serializer = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine, factoryType, "Junit");
130
131         FormatFactory ff =
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);
136
137         JsonObject json = formatter.output(Arrays.asList(unknown, vfModule));
138         System.out.println(json);
139
140     }
141
142     public void createLoaderEngineSetup() {
143
144         if (loader == null) {
145             loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getAppRootVersion());
146             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
147
148             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
149
150             when(dbEngine.tx()).thenReturn(graph);
151             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
152
153             when(spyAdmin.getReadOnlyTraversalSource())
154                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
155             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
156         }
157     }
158 }