Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / search / GremlinServerImplTest.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.rest.search;
21
22 import com.thinkaurelius.titan.core.TitanGraph;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.structure.Graph;
26 import org.apache.tinkerpop.gremlin.structure.T;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.onap.aai.dbmap.DBConnectionType;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Loader;
35 import org.onap.aai.introspection.LoaderFactory;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.introspection.Version;
38 import org.onap.aai.serialization.engines.QueryStyle;
39 import org.onap.aai.serialization.engines.TitanDBEngine;
40 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
41 import org.onap.aai.serialization.queryformats.SubGraphStyle;
42
43 import javax.ws.rs.core.*;
44 import java.net.URI;
45 import java.util.*;
46
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
50
51 public class GremlinServerImplTest {
52
53     GremlinServerImpl gremlinServerImpl;
54
55     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
56
57     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
58
59     private final static Version version = Version.getLatest();
60     private final static ModelType introspectorFactoryType = ModelType.MOXY;
61     private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
62     private final static DBConnectionType type = DBConnectionType.REALTIME;
63
64     static {
65         VALID_HTTP_STATUS_CODES.add(200);
66         VALID_HTTP_STATUS_CODES.add(201);
67         VALID_HTTP_STATUS_CODES.add(204);
68     }
69
70     private  GenericQueryProcessor genericQueryProcessor;
71     private HttpHeaders httpHeaders;
72
73     private UriInfo uriInfo;
74
75     private MultivaluedMap<String, String> headersMultiMap;
76     private MultivaluedMap<String, String> queryParameters;
77
78     private List<String> aaiRequestContextList;
79
80     private List<MediaType> outputMediaTypes;
81
82     private Loader loader;
83     private TitanGraph graph;
84
85     private Graph tx;
86
87     private GraphTraversalSource g;
88     private TransactionalGraphEngine dbEngine;
89
90
91
92 @Before
93     public void  setup()throws AAIException{
94
95         httpHeaders         = mock(HttpHeaders.class);
96         uriInfo             = mock(UriInfo.class);
97
98         headersMultiMap     = new MultivaluedHashMap<>();
99         queryParameters     = Mockito.spy(new MultivaluedHashMap<>());
100
101         headersMultiMap.add("X-FromAppId", "JUNIT");
102         headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
103         headersMultiMap.add("Real-Time", "true");
104         headersMultiMap.add("Accept", "application/json");
105         headersMultiMap.add("aai-request-context", "");
106
107         outputMediaTypes = new ArrayList<>();
108         outputMediaTypes.add(APPLICATION_JSON);
109
110         aaiRequestContextList = new ArrayList<>();
111         aaiRequestContextList.add("");
112
113         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
114         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
115         when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
116         when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
117
118         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
119
120
121         when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
122         when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
123
124         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
125         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
126
127         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
128         loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
129         dbEngine = new TitanDBEngine(
130                 queryStyle,
131                 type,
132                 loader);
133     GenericQueryProcessor.Builder builder=new GenericQueryProcessor.Builder(dbEngine);
134 builder.queryFrom(URI.create("te"));
135 builder.queryFrom("te", "gremlin");
136 builder.create();
137 builder.processWith(QueryProcessorType.GREMLIN_SERVER);
138     builder.processWith(QueryProcessorType.LOCAL_GROOVY);
139
140     gremlinServerImpl= new GremlinServerImpl(builder);
141     }
142
143     @Test(expected = NullPointerException.class)
144     public void processSubGraphTest() throws Exception{
145         GraphTraversal<Vertex, Vertex> g=Mockito.mock(GraphTraversal.class);
146         g.has("cloud-region-id", "cloud-region-id-1");
147         gremlinServerImpl.processSubGraph(SubGraphStyle.prune,g);
148 }
149
150     @Test(expected = RuntimeException.class)
151     public void executeTest() throws Exception{
152         GraphTraversal<Vertex, Vertex> g=Mockito.mock(GraphTraversal.class);
153         g.has("cloud-region-id", "cloud-region-id-1");
154         //Mockito.when(etPath()).thenReturn("testpath");
155     List<Object> re=    gremlinServerImpl.execute(SubGraphStyle.prune);
156         Assert.assertNotNull(re);
157     }
158
159
160
161 }