Update traversal from AJSC 2 to Spring Boot
[aai/traversal.git] / aai-traversal / src / test / java / org / onap / aai / rest / search / GroovyShellImplTest.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
23 package org.onap.aai.rest.search;
24
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;
45
46 import javax.ws.rs.core.*;
47 import java.net.URI;
48 import java.util.*;
49
50 import static org.mockito.Matchers.anyObject;
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.when;
53
54 public class GroovyShellImplTest {
55
56     GroovyShellImpl  groovyShellImpl ;
57
58     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
59
60     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
61
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;
66
67     static {
68         VALID_HTTP_STATUS_CODES.add(200);
69         VALID_HTTP_STATUS_CODES.add(201);
70         VALID_HTTP_STATUS_CODES.add(204);
71     }
72
73     private  GenericQueryProcessor genericQueryProcessor;
74     private HttpHeaders httpHeaders;
75
76     private UriInfo uriInfo;
77
78     private MultivaluedMap<String, String> headersMultiMap;
79     private MultivaluedMap<String, String> queryParameters;
80
81     private List<String> aaiRequestContextList;
82
83     private List<MediaType> outputMediaTypes;
84
85     private Loader loader;
86     private TitanGraph graph;
87
88     private Graph tx;
89
90     private GraphTraversalSource g;
91     private TransactionalGraphEngine dbEngine;
92
93
94
95 @Before
96     public void  setup()throws AAIException{
97
98         httpHeaders         = mock(HttpHeaders.class);
99         uriInfo             = mock(UriInfo.class);
100
101         headersMultiMap     = new MultivaluedHashMap<>();
102         queryParameters     = Mockito.spy(new MultivaluedHashMap<>());
103
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", "");
109
110         outputMediaTypes = new ArrayList<>();
111         outputMediaTypes.add(APPLICATION_JSON);
112
113         aaiRequestContextList = new ArrayList<>();
114         aaiRequestContextList.add("");
115
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"));
120
121         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
122
123
124         when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
125         when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
126
127         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very unreasonable
128         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
129
130         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
131         loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
132         dbEngine = new TitanDBEngine(
133                 queryStyle,
134                 type,
135                 loader);
136     GenericQueryProcessor.Builder builder=new GenericQueryProcessor.Builder(dbEngine);
137 builder.queryFrom(URI.create("te"));
138 builder.queryFrom("te", "gremlin");
139 builder.create();
140 builder.processWith(QueryProcessorType.GREMLIN_SERVER);
141     builder.processWith(QueryProcessorType.LOCAL_GROOVY);
142
143     groovyShellImpl= new GroovyShellImpl(builder);
144     }
145
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);
152 }
153
154
155
156
157
158 }