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