Replaced all tabs with spaces in java and pom.xml
[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.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.ArgumentMatchers.isA;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Map;
38 import javax.ws.rs.core.GenericType;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.Spy;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.aai.domain.yang.Complex;
46 import org.onap.so.client.RestClient;
47 import org.onap.so.client.aai.entities.AAIResultWrapper;
48 import org.onap.so.client.aai.entities.CustomQuery;
49 import org.onap.so.client.aai.entities.Results;
50 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
51 import org.onap.so.client.aai.entities.uri.AAIUri;
52 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
53 import org.onap.so.client.graphinventory.Format;
54 import org.onap.so.client.graphinventory.GraphInventoryClient;
55 import org.onap.so.client.graphinventory.GraphInventorySubgraphType;
56 import org.onap.so.client.graphinventory.entities.Pathed;
57 import org.onap.so.client.graphinventory.entities.ResourceAndUrl;
58 import com.fasterxml.jackson.core.type.TypeReference;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60
61
62 @RunWith(MockitoJUnitRunner.class)
63 public class AAIQueryClientTest {
64
65     @Mock
66     private RestClient restClient;
67
68     @Mock
69     private GraphInventoryClient client;
70
71     @InjectMocks
72     @Spy
73     private AAIQueryClient aaiQueryClient = new AAIQueryClient();
74
75     private String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/query/";
76
77     private ObjectMapper mapper = new ObjectMapper();
78
79     @Test
80     public void testQuery() {
81         List<AAIResourceUri> uris = Arrays.asList(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY));
82
83         Format format = Format.SIMPLE;
84         CustomQuery query = new CustomQuery(uris);
85
86         doReturn(restClient).when(client).createClient(isA(AAIUri.class));
87         aaiQueryClient.query(format, query);
88         verify(client, times(1)).createClient(
89                 AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY).queryParam("format", format.toString()));
90         verify(restClient, times(1)).put(query, String.class);
91     }
92
93     @Test
94     public void testCreateClient() {
95         String depth = "testDepth";
96         GraphInventorySubgraphType subgraph = GraphInventorySubgraphType.STAR;
97
98         aaiQueryClient.depth(depth);
99         aaiQueryClient.nodesOnly();
100         aaiQueryClient.subgraph(subgraph);
101
102         AAIUri aaiUri = spy(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY));
103         doReturn(aaiUri).when(aaiUri).clone();
104         aaiQueryClient.setupQueryParams(aaiUri);
105
106         verify(aaiUri, times(1)).queryParam("depth", depth);
107         verify(aaiUri, times(1)).queryParam("nodesOnly", "");
108         verify(aaiUri, times(1)).queryParam("subgraph", subgraph.toString());
109     }
110
111     @Test
112     public void querySingleResourceTest() throws IOException {
113         doReturn(getJson("single-query-result.json")).when(aaiQueryClient).query(eq(Format.RESOURCE_AND_URL),
114                 any(CustomQuery.class));
115         List<Complex> result = aaiQueryClient.querySingleResource(
116                 new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(AAIObjectType.COMPLEX, "test"))),
117                 Complex.class);
118         assertEquals(2, result.size());
119         assertEquals("complex-id-15100-jc689q2", result.get(1).getPhysicalLocationId());
120     }
121
122     @Test
123     public void getResourceAndUrlTest() throws IOException {
124         doReturn(getJson("single-query-result.json")).when(aaiQueryClient).query(eq(Format.RESOURCE_AND_URL),
125                 any(CustomQuery.class));
126         List<ResourceAndUrl<AAIResultWrapper>> result = aaiQueryClient.getResourceAndUrl(
127                 new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(AAIObjectType.COMPLEX, "test"))));
128         assertEquals(2, result.size());
129
130         assertEquals(1,
131                 result.get(1).getWrapper().getRelationships().get().getRelatedUris(AAIObjectType.PSERVER).size());
132     }
133
134     @Test
135     public void querySingleTypeTest() throws IOException {
136         when(client.createClient(isA(AAIUri.class))).thenReturn(restClient);
137         when(restClient.put(any(Object.class), any(GenericType.class))).thenReturn(
138                 mapper.readValue(getJson("pathed-result.json"), new TypeReference<Results<Map<String, Object>>>() {}));
139
140
141         List<Pathed> results = aaiQueryClient.queryPathed(
142                 new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(AAIObjectType.COMPLEX, "test"))));
143
144         assertEquals(2, results.size());
145         assertEquals("service-instance", results.get(1).getResourceType());
146     }
147
148     private String getJson(String filename) throws IOException {
149         return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
150     }
151 }