Merge "Fix minor sonar issues in SchemaGenerator4Hist"
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / queryformats / SimpleFormatTest.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.strategy.verification.ReadOnlyStrategy;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.T;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
29 import org.junit.Before;
30 import org.junit.Ignore;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.onap.aai.AAISetup;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.ModelType;
38 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
39 import org.onap.aai.serialization.db.DBSerializer;
40 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
41 import org.onap.aai.serialization.engines.QueryStyle;
42 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
43 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
44 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
45 import org.springframework.test.annotation.DirtiesContext;
46
47 import javax.ws.rs.core.MultivaluedHashMap;
48 import javax.ws.rs.core.MultivaluedMap;
49 import java.io.UnsupportedEncodingException;
50 import java.util.Arrays;
51
52 import static org.junit.Assert.*;
53 import static org.mockito.Matchers.*;
54 import static org.mockito.Mockito.*;
55
56 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
57 public class SimpleFormatTest extends AAISetup {
58
59     @Mock
60     private UrlBuilder urlBuilder;
61
62     private Graph graph;
63     private TransactionalGraphEngine dbEngine;
64     private Loader loader;
65     private DBSerializer serializer;
66     private RawFormat simpleFormat;
67     private Vertex vfModule;
68     private Vertex unknown;
69     private final ModelType factoryType = ModelType.MOXY;
70
71     @Before
72     public void setUp() throws Exception {
73
74         MockitoAnnotations.initMocks(this);
75
76         graph = TinkerGraph.open();
77
78         vfModule = graph.addVertex(T.label, "vf-module", T.id, "5", "aai-node-type", "vf-module", "vf-module-id",
79                 "vf-module-id-val-68205", "vf-module-name", "example-vf-module-name-val-68205", "heat-stack-id",
80                 "example-heat-stack-id-val-68205", "orchestration-status", "example-orchestration-status-val-68205",
81                 "is-base-vf-module", "true", "resource-version", "1498166571906", "model-invariant-id",
82                 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-invariant-id-local",
83                 "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8", "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
84                 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c", "widget-model-id",
85                 "example-widget-model-id-val-68205", "widget-model-version", "example-widget--model-version-val-68205",
86                 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205");
87
88         unknown = graph.addVertex(T.label, "unknown", T.id, "1", "aai-node-type", "unknown", "vserver-id",
89                 "vserver-id-1", "vserver-name", "vserver-name-1");
90     }
91
92     @Test
93     public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
94
95         createLoaderEngineSetup();
96         serializer = new DBSerializer(schemaVersions.getRelatedLinkVersion(), dbEngine, factoryType, "Junit");
97         simpleFormat =
98                 new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).modelDriven().build();
99
100         assertNotNull(dbEngine.tx());
101         assertNotNull(dbEngine.asAdmin());
102
103         JsonObject json = simpleFormat.createPropertiesObject(vfModule).get();
104
105         assertTrue(json.has("model-invariant-id"));
106         assertTrue(json.has("model-version-id"));
107
108         assertFalse(json.has("model-invariant-id-local"));
109         assertFalse(json.has("model-version-id-local"));
110
111     }
112
113     @Test
114     public void testUnknownVertex() throws AAIFormatVertexException, AAIException {
115
116         createLoaderEngineSetup();
117         serializer = new DBSerializer(schemaVersions.getRelatedLinkVersion(), dbEngine, factoryType, "Junit");
118         simpleFormat =
119                 new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).modelDriven().build();
120
121         assertNotNull(dbEngine.tx());
122         assertNotNull(dbEngine.asAdmin());
123
124         assertFalse(simpleFormat.getJsonFromVertex(unknown).isPresent());
125
126     }
127
128     @Test
129     public void testFormattingUnknownVertex() throws AAIException {
130
131         createLoaderEngineSetup();
132         serializer = new DBSerializer(schemaVersions.getRelatedLinkVersion(), dbEngine, factoryType, "Junit");
133
134         FormatFactory ff = new FormatFactory(loader, serializer, schemaVersions, basePath, "https://localhost:8447/aai/");
135         MultivaluedMap mvm = new MultivaluedHashMap();
136         mvm.add("depth", "0");
137         Formatter formatter = ff.get(Format.simple, mvm);
138
139         JsonObject json = formatter.output(Arrays.asList(unknown, vfModule));
140
141     }
142
143     @Ignore
144     @Test(expected = AAIFormatVertexException.class)
145     public void testCreatePropertiesObjectThrowsExceptionIfSerializationFails()
146             throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
147
148         serializer = mock(DBSerializer.class);
149         loader = mock(Loader.class);
150
151         simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
152
153         when(serializer.dbToObject(anyObject(), anyObject(), anyInt(), anyBoolean(), anyString()))
154                 .thenThrow(new AAIException("Test Exception"));
155
156         simpleFormat.createPropertiesObject(vfModule);
157     }
158
159     @Ignore
160     @Test(expected = AAIFormatVertexException.class)
161     public void testCreatePropertiesObjectThrowsExceptionIfUnknownObject()
162             throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
163
164         loader = mock(Loader.class);
165         serializer = mock(DBSerializer.class);
166
167         simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
168
169         when(loader.introspectorFromName(anyString())).thenThrow(new AAIUnknownObjectException("Test Exception"));
170
171         simpleFormat.createPropertiesObject(vfModule);
172     }
173
174     public void createLoaderEngineSetup() {
175
176         if (loader == null) {
177             loader = loaderFactory.createLoaderForVersion(factoryType, schemaVersions.getRelatedLinkVersion());
178             dbEngine = spy(new JanusGraphDBEngine(QueryStyle.TRAVERSAL, loader));
179
180             TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
181
182             when(dbEngine.tx()).thenReturn(graph);
183             when(dbEngine.asAdmin()).thenReturn(spyAdmin);
184
185             when(spyAdmin.getReadOnlyTraversalSource())
186                     .thenReturn(graph.traversal().withStrategies(ReadOnlyStrategy.instance()));
187             when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
188         }
189     }
190 }