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