Add new query format count and
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.onap.aai.serialization.queryformats;
24
25 import com.google.gson.JsonObject;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
28 import org.apache.tinkerpop.gremlin.structure.Graph;
29 import org.apache.tinkerpop.gremlin.structure.T;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.aai.AAISetup;
38 import org.onap.aai.dbmap.DBConnectionType;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.Loader;
41 import org.onap.aai.introspection.LoaderFactory;
42 import org.onap.aai.introspection.ModelType;
43 import org.onap.aai.introspection.Version;
44 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
45 import org.onap.aai.serialization.db.DBSerializer;
46 import org.onap.aai.serialization.engines.QueryStyle;
47 import org.onap.aai.serialization.engines.TitanDBEngine;
48 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
49 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
50 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
51
52 import java.io.UnsupportedEncodingException;
53
54 import static org.junit.Assert.*;
55 import static org.mockito.Matchers.anyBoolean;
56 import static org.mockito.Matchers.anyInt;
57 import static org.mockito.Matchers.anyObject;
58 import static org.mockito.Matchers.anyString;
59 import static org.mockito.Mockito.*;
60
61 public class SimpleFormatTest extends AAISetup {
62
63         @Mock
64         private UrlBuilder urlBuilder;
65
66         private Graph graph;
67         private TransactionalGraphEngine dbEngine;
68         private Loader loader;
69         private DBSerializer serializer;
70         private RawFormat simpleFormat;
71         private Vertex vfModule;
72         private final ModelType factoryType = ModelType.MOXY;
73
74         @Before
75         public void setUp() throws Exception {
76
77                 MockitoAnnotations.initMocks(this);
78
79                 graph = TinkerGraph.open();
80
81                 vfModule = graph.addVertex(
82                                 T.label, "vf-module",
83                                 T.id, "5",
84                                 "aai-node-type", "vf-module",
85                                 "vf-module-id", "vf-module-id-val-68205",
86                                 "vf-module-name", "example-vf-module-name-val-68205",
87                                 "heat-stack-id", "example-heat-stack-id-val-68205",
88                                 "orchestration-status", "example-orchestration-status-val-68205",
89                                 "is-base-vf-module", "true",
90                                 "resource-version", "1498166571906",
91                                 "model-invariant-id", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
92                                 "model-invariant-id-local", "fe8aac07-ce6c-4f9f-aa0d-b561c77da9e8",
93                                 "model-version-id", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
94                                 "model-version-id-local", "0d23052d-8ffe-433e-a25d-da5da027bb7c",
95                                 "widget-model-id", "example-widget-model-id-val-68205",
96                                 "widget-model-version", "example-widget--model-version-val-68205",
97                                 "contrail-service-instance-fqdn", "example-contrail-service-instance-fqdn-val-68205"
98                 );
99         }
100
101         @Test
102         public void testCreatePropertiesObjectReturnsProperProperties() throws AAIFormatVertexException, AAIException {
103
104             createLoaderEngineSetup();
105                 serializer = new DBSerializer(Version.v10, dbEngine, factoryType, "Junit");
106                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).modelDriven().build();
107
108                 assertNotNull(dbEngine.tx());
109                 assertNotNull(dbEngine.asAdmin());
110
111                 JsonObject json = simpleFormat.createPropertiesObject(vfModule);
112
113                 assertTrue(json.has("model-invariant-id"));
114                 assertTrue(json.has("model-version-id"));
115
116                 assertFalse(json.has("model-invariant-id-local"));
117                 assertFalse(json.has("model-version-id-local"));
118
119         }
120
121         @Ignore
122         @Test(expected = AAIFormatVertexException.class)
123         public void testCreatePropertiesObjectThrowsExceptionIfSerializationFails() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
124
125                 serializer = mock(DBSerializer.class);
126                 loader = mock(Loader.class);
127
128                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
129
130                 when(serializer.dbToObject(anyObject(), anyObject(), anyInt(), anyBoolean(), anyString()))
131                         .thenThrow(new AAIException("Test Exception"));
132
133                 simpleFormat.createPropertiesObject(vfModule);
134         }
135
136         @Ignore
137         @Test(expected = AAIFormatVertexException.class)
138         public void testCreatePropertiesObjectThrowsExceptionIfUnknownObject() throws AAIFormatVertexException, AAIException, UnsupportedEncodingException {
139
140                 loader = mock(Loader.class);
141                 serializer = mock(DBSerializer.class);
142
143                 simpleFormat = new RawFormat.Builder(loader, serializer, urlBuilder).nodesOnly(true).depth(0).build();
144
145                 when(loader.introspectorFromName(anyString()))
146                                 .thenThrow(new AAIUnknownObjectException("Test Exception"));
147
148                 simpleFormat.createPropertiesObject(vfModule);
149         }
150
151         public void createLoaderEngineSetup(){
152
153                 if(loader == null){
154                         loader = LoaderFactory.createLoaderForVersion(factoryType, Version.v10);
155                         dbEngine = spy(new TitanDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.CACHED, loader));
156
157                         TransactionalGraphEngine.Admin spyAdmin = spy(dbEngine.asAdmin());
158
159                         when(dbEngine.tx()).thenReturn(graph);
160                         when(dbEngine.asAdmin()).thenReturn(spyAdmin);
161
162                         when(spyAdmin.getReadOnlyTraversalSource()).thenReturn(graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())));
163                         when(spyAdmin.getTraversalSource()).thenReturn(graph.traversal());
164                 }
165         }
166 }