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