2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.rest.search;
22 import com.thinkaurelius.titan.core.TitanGraph;
23 import groovy.lang.MissingPropertyException;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.apache.tinkerpop.gremlin.structure.Graph;
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;
43 import javax.ws.rs.core.*;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
51 public class GroovyShellImplTest {
53 GroovyShellImpl groovyShellImpl ;
55 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
57 private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
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;
65 VALID_HTTP_STATUS_CODES.add(200);
66 VALID_HTTP_STATUS_CODES.add(201);
67 VALID_HTTP_STATUS_CODES.add(204);
70 private GenericQueryProcessor genericQueryProcessor;
71 private HttpHeaders httpHeaders;
73 private UriInfo uriInfo;
75 private MultivaluedMap<String, String> headersMultiMap;
76 private MultivaluedMap<String, String> queryParameters;
78 private List<String> aaiRequestContextList;
80 private List<MediaType> outputMediaTypes;
82 private Loader loader;
83 private TitanGraph graph;
87 private GraphTraversalSource g;
88 private TransactionalGraphEngine dbEngine;
93 public void setup()throws AAIException{
95 httpHeaders = mock(HttpHeaders.class);
96 uriInfo = mock(UriInfo.class);
98 headersMultiMap = new MultivaluedHashMap<>();
99 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
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", "");
107 outputMediaTypes = new ArrayList<>();
108 outputMediaTypes.add(APPLICATION_JSON);
110 aaiRequestContextList = new ArrayList<>();
111 aaiRequestContextList.add("");
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"));
118 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
121 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
122 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
124 // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
125 Mockito.doReturn(null).when(queryParameters).remove(anyObject());
127 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
128 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
129 dbEngine = new TitanDBEngine(
133 GenericQueryProcessor.Builder builder=new GenericQueryProcessor.Builder(dbEngine);
134 builder.queryFrom(URI.create("te"));
135 builder.queryFrom("te", "gremlin");
137 builder.processWith(QueryProcessorType.GREMLIN_SERVER);
138 builder.processWith(QueryProcessorType.LOCAL_GROOVY);
140 groovyShellImpl= new GroovyShellImpl(builder);
143 @Test(expected = MissingPropertyException.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 Map<String, Object> params = new HashMap<>();
148 groovyShellImpl.runQuery("vnfs-fromServiceInstance",params);