Release 1.14.3 docker artifact of aai-traversal
[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-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 static org.mockito.ArgumentMatchers.anyObject;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25
26 import groovy.lang.MissingPropertyException;
27
28 import java.net.URI;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.UUID;
37
38 import javax.ws.rs.core.HttpHeaders;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.MultivaluedHashMap;
41 import javax.ws.rs.core.MultivaluedMap;
42 import javax.ws.rs.core.UriInfo;
43
44 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
45 import org.apache.tinkerpop.gremlin.structure.Vertex;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.Mockito;
49 import org.onap.aai.AAISetup;
50 import org.onap.aai.introspection.Loader;
51 import org.onap.aai.introspection.ModelType;
52 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
53 import org.onap.aai.serialization.engines.QueryStyle;
54 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
55 import org.onap.aai.setup.SchemaVersion;
56
57 public class GroovyShellImplTest extends AAISetup {
58
59     GroovyShellImpl groovyShellImpl;
60
61     protected static final MediaType APPLICATION_JSON = MediaType.valueOf("application/json");
62
63     private static final Set<Integer> VALID_HTTP_STATUS_CODES = new HashSet<>();
64
65     private SchemaVersion version;
66
67     private final static ModelType introspectorFactoryType = ModelType.MOXY;
68     private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
69
70     static {
71         VALID_HTTP_STATUS_CODES.add(200);
72         VALID_HTTP_STATUS_CODES.add(201);
73         VALID_HTTP_STATUS_CODES.add(204);
74     }
75
76     private HttpHeaders httpHeaders;
77
78     private UriInfo uriInfo;
79
80     private MultivaluedMap<String, String> headersMultiMap;
81     private MultivaluedMap<String, String> queryParameters;
82
83     private List<String> aaiRequestContextList;
84
85     private List<MediaType> outputMediaTypes;
86
87     private Loader loader;
88     private TransactionalGraphEngine dbEngine;
89
90     @Before
91     public void setup() {
92
93         version = schemaVersions.getDefaultVersion();
94         httpHeaders = mock(HttpHeaders.class);
95         uriInfo = mock(UriInfo.class);
96
97         headersMultiMap = new MultivaluedHashMap<>();
98         queryParameters = Mockito.spy(new MultivaluedHashMap<>());
99
100         headersMultiMap.add("X-FromAppId", "JUNIT");
101         headersMultiMap.add("X-TransactionId", UUID.randomUUID().toString());
102         headersMultiMap.add("Real-Time", "true");
103         headersMultiMap.add("Accept", "application/json");
104         headersMultiMap.add("aai-request-context", "");
105
106         outputMediaTypes = new ArrayList<>();
107         outputMediaTypes.add(APPLICATION_JSON);
108
109         aaiRequestContextList = new ArrayList<>();
110         aaiRequestContextList.add("");
111
112         when(httpHeaders.getAcceptableMediaTypes()).thenReturn(outputMediaTypes);
113         when(httpHeaders.getRequestHeaders()).thenReturn(headersMultiMap);
114         when(httpHeaders.getRequestHeader("X-FromAppId")).thenReturn(Arrays.asList("JUNIT"));
115         when(httpHeaders.getRequestHeader("X-TransactionId")).thenReturn(Arrays.asList("JUNIT"));
116
117         when(httpHeaders.getRequestHeader("aai-request-context")).thenReturn(aaiRequestContextList);
118
119         when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
120         when(uriInfo.getQueryParameters(false)).thenReturn(queryParameters);
121
122         // TODO - Check if this is valid since RemoveDME2QueryParameters seems to be very
123         // unreasonable
124         Mockito.doReturn(null).when(queryParameters).remove(anyObject());
125
126         when(httpHeaders.getMediaType()).thenReturn(APPLICATION_JSON);
127         loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
128         dbEngine = new JanusGraphDBEngine(queryStyle, loader);
129         GenericQueryProcessor.Builder builder =
130             new GenericQueryProcessor.Builder(dbEngine, gremlinServerSingleton);
131         builder.queryFrom(URI.create("te"));
132         builder.queryFrom("te", "gremlin");
133         builder.create();
134         builder.processWith(QueryProcessorType.GREMLIN_SERVER);
135         builder.processWith(QueryProcessorType.LOCAL_GROOVY);
136
137         groovyShellImpl = new GroovyShellImpl(builder);
138     }
139
140     @Test(expected = MissingPropertyException.class)
141     public void processSubGraphTest() throws Exception {
142         GraphTraversal<Vertex, Vertex> g = Mockito.mock(GraphTraversal.class);
143         g.has("cloud-region-id", "cloud-region-id-1");
144         Map<String, Object> params = new HashMap<>();
145         groovyShellImpl.runQuery("vnfs-fromServiceInstance", params,
146             dbEngine.asAdmin().getTraversalSource());
147     }
148
149 }