6e9c38517d0a548205e2b3983056806741c5a968
[so.git] / common / src / test / java / org / onap / so / client / aai / AAIQueryClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 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
21 package org.onap.so.client.aai;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Matchers.isA;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31
32 import java.util.Arrays;
33 import java.util.List;
34
35 import javax.ws.rs.core.Response;
36
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.Spy;
42 import org.mockito.runners.MockitoJUnitRunner;
43 import org.onap.so.client.RestClient;
44 import org.onap.so.client.aai.entities.CustomQuery;
45 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
46 import org.onap.so.client.aai.entities.uri.AAIUri;
47 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
48 import org.onap.so.client.graphinventory.Format;
49
50
51 @RunWith(MockitoJUnitRunner.class) 
52 public class AAIQueryClientTest {
53         
54         @Mock
55         Response response;
56         
57         @Mock
58         RestClient restClient;
59         
60         @Spy
61         AAIQueryClient aaiQueryClient = new AAIQueryClient();
62         
63         @Test
64         public void testQuery() {
65                 List<AAIResourceUri> uris = Arrays.asList(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY));
66                 
67                 Format format = Format.SIMPLE;
68                 CustomQuery query = new CustomQuery(uris);
69                 
70                 doReturn(restClient).when(aaiQueryClient).createClient(isA(AAIUri.class));
71                 aaiQueryClient.query(format, query);
72                 verify(aaiQueryClient, times(1)).createClient(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY).queryParam("format", format.toString()));
73                 verify(restClient, times(1)).put(query, String.class);
74         }
75         
76         @Test
77         public void testCreateClient() {
78                 String depth = "testDepth";
79                 AAISubgraphType subgraph = AAISubgraphType.STAR;
80                 
81                 aaiQueryClient.depth(depth);
82                 aaiQueryClient.nodesOnly();
83                 aaiQueryClient.subgraph(subgraph);
84                 
85                 AAIUri aaiUri = spy(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY));
86                 doReturn(aaiUri).when(aaiUri).clone();
87                 aaiQueryClient.setupQueryParams(aaiUri);
88                 
89                 verify(aaiUri, times(1)).queryParam("depth", depth);
90                 verify(aaiUri, times(1)).queryParam("nodesOnly", "");
91                 verify(aaiUri, times(1)).queryParam("subgraph", subgraph.toString());
92         }
93 }