2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.rest.search;
25 import com.thinkaurelius.titan.core.TitanGraph;
26 import groovy.lang.MissingPropertyException;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
29 import org.apache.tinkerpop.gremlin.structure.Graph;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.aai.dbmap.DBConnectionType;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.introspection.LoaderFactory;
39 import org.onap.aai.introspection.ModelType;
40 import org.onap.aai.introspection.Version;
41 import org.onap.aai.serialization.engines.QueryStyle;
42 import org.onap.aai.serialization.engines.TitanDBEngine;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
44 import org.onap.aai.serialization.queryformats.SubGraphStyle;
46 import javax.ws.rs.core.*;
50 import static org.mockito.Matchers.anyObject;
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.when;
54 public class GroovyShellImplTest {
56 GroovyShellImpl groovyShellImpl ;
58 protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
60 private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
62 private final static Version version = Version.getLatest();
63 private final static ModelType introspectorFactoryType = ModelType.MOXY;
64 private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
65 private final static DBConnectionType type = DBConnectionType.REALTIME;
68 VALID_HTTP_STATUS_CODES.add(200);
69 VALID_HTTP_STATUS_CODES.add(201);
70 VALID_HTTP_STATUS_CODES.add(204);
73 private GenericQueryProcessor genericQueryProcessor;
74 private HttpHeaders httpHeaders;
76 private UriInfo uriInfo;
78 private MultivaluedMap<String, String> headersMultiMap;
79 private MultivaluedMap<String, String> queryParameters;
81 private List<String> aaiRequestContextList;
83 private List<MediaType> outputMediaTypes;
85 private Loader loader;
86 private TitanGraph graph;
90 private GraphTraversalSource g;
91 private TransactionalGraphEngine dbEngine;
96 public void setup()throws AAIException{
98 httpHeaders = mock(HttpHeaders.class);
99 uriInfo = mock(UriInfo.class);
101 headersMultiMap = new MultivaluedHashMap<>();
102 queryParameters = Mockito.spy(new MultivaluedHashMap<>());
104 headersMultiMap.add("X-FromAppId", "JUNIT");
105 headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
106 headersMultiMap.add("Real-Time", "true");
107 headersMultiMap.add("Accept", "application/json");
108 headersMultiMap.add("aai-request-context", "");
110 outputMediaTypes = new ArrayList<>();
111 outputMediaTypes.add(APPLICATION_JSON);
113 aaiRequestContextList = new ArrayList<>();
114 aaiRequestContextList.add("");
116 when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
117 when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
118 when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
119 when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
121 when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
124 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
125 when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
127 // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
128 Mockito.doReturn(null).when(queryParameters).remove(anyObject());
130 when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
131 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
132 dbEngine = new TitanDBEngine(
136 GenericQueryProcessor.Builder builder=new GenericQueryProcessor.Builder(dbEngine);
137 builder.queryFrom(URI.create("te"));
138 builder.queryFrom("te", "gremlin");
140 builder.processWith(QueryProcessorType.GREMLIN_SERVER);
141 builder.processWith(QueryProcessorType.LOCAL_GROOVY);
143 groovyShellImpl= new GroovyShellImpl(builder);
146 @Test(expected = MissingPropertyException.class)
147 public void processSubGraphTest() throws Exception{
148 GraphTraversal<Vertex, Vertex> g=Mockito.mock(GraphTraversal.class);
149 g.has("cloud-region-id", "cloud-region-id-1");
150 Map<String, Object> params = new HashMap<>();
151 groovyShellImpl.runQuery("vnfs-fromServiceInstance",params);