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