EdgeRules throws descriptive error on invalid rule
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / serialization / queryformats / QueryFormatTestHelper.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 org.apache.tinkerpop.gremlin.structure.Graph;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
26 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 import org.openecomp.aai.db.props.AAIProperties;
30 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
31 import org.openecomp.aai.serialization.queryformats.utils.UrlBuilder;
32
33 import java.io.IOException;
34 import java.lang.reflect.Field;
35 import java.lang.reflect.Modifier;
36
37 import static org.mockito.Matchers.isA;
38 import static org.mockito.Mockito.when;
39
40 public class QueryFormatTestHelper {
41
42         
43         public static final String testResources = "src/test/resources/org/openecomp/aai/serialization/queryformats/";
44         public static final String graphsonResources = "src/test/resources/org/openecomp/aai/serialization/queryformats/graphson/";
45
46         
47         public static void mockPathed(UrlBuilder mock) throws AAIFormatVertexException {
48                 Answer<String> answer = new Answer<String>() {
49                         public String answer(InvocationOnMock invocation) throws Throwable {
50                                 Vertex v = invocation.getArgumentAt(0, Vertex.class);
51                                 
52                                 return v.<String>property(AAIProperties.AAI_URI).orElse("urimissing");
53                         }
54                 };
55                 when(mock.pathed(isA(Vertex.class))).thenAnswer(answer);
56
57         }
58         
59         public static Graph loadGraphson(String fileName) throws IOException {
60                 final Graph graph = TinkerGraph.open();
61                 graph.io(IoCore.graphson()).readGraph(QueryFormatTestHelper.graphsonResources + fileName);
62                 
63                 return graph;
64         }
65         
66         public static void setFinalStatic(Field field, Object newValue) throws Exception {
67                 field.setAccessible(true);
68                 // remove final modifier from field
69                 Field modifiersField = Field.class.getDeclaredField("modifiers");
70                 modifiersField.setAccessible(true);
71                 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
72                 field.set(null, newValue);
73         }
74         
75 }