aboutsummaryrefslogtreecommitdiffstats
path: root/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIQueryClientTest.java
blob: 29d862478c19e5097bc3c4328ffca8aca555bff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aaiclient.client.aai;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.GenericType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.aai.domain.yang.Complex;
import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
import org.onap.aaiclient.client.aai.entities.CustomQuery;
import org.onap.aaiclient.client.aai.entities.Results;
import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
import org.onap.aaiclient.client.graphinventory.Format;
import org.onap.aaiclient.client.graphinventory.GraphInventoryClient;
import org.onap.aaiclient.client.graphinventory.GraphInventorySubgraphType;
import org.onap.aaiclient.client.graphinventory.entities.Pathed;
import org.onap.aaiclient.client.graphinventory.entities.ResourceAndUrl;
import org.onap.so.client.RestClient;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;


@RunWith(MockitoJUnitRunner.class)
public class AAIQueryClientTest {

    @Mock
    private RestClient restClient;

    @Mock
    private GraphInventoryClient client;

    @InjectMocks
    @Spy
    private AAIQueryClient aaiQueryClient = new AAIQueryClient();

    private String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/query/";

    private ObjectMapper mapper = new ObjectMapper();

    @Test
    public void testQuery() {
        List<AAIResourceUri> uris = Arrays.asList(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY));

        Format format = Format.SIMPLE;
        CustomQuery query = new CustomQuery(uris);

        doReturn(restClient).when(client).createClient(isA(AAIResourceUri.class));
        aaiQueryClient.query(format, query);
        verify(client, times(1))
                .createClient(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY).format(format));
        verify(restClient, times(1)).put(query, String.class);
    }

    @Test
    public void testCreateClient() {
        String depth = "testDepth";
        GraphInventorySubgraphType subgraph = GraphInventorySubgraphType.STAR;

        aaiQueryClient.depth(depth);
        aaiQueryClient.nodesOnly();
        aaiQueryClient.subgraph(subgraph);

        AAIResourceUri aaiUri = spy(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY));
        doReturn(aaiUri).when(aaiUri).clone();
        aaiQueryClient.setupQueryParams(aaiUri);

        verify(aaiUri, times(1)).queryParam("depth", depth);
        verify(aaiUri, times(1)).queryParam("nodesOnly", "");
        verify(aaiUri, times(1)).queryParam("subgraph", subgraph.toString());
    }

    @Test
    public void querySingleResourceTest() throws IOException {
        doReturn(getJson("single-query-result.json")).when(aaiQueryClient).query(eq(Format.RESOURCE_AND_URL),
                any(CustomQuery.class));
        List<Complex> result = aaiQueryClient.querySingleResource(
                new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(Types.COMPLEX.getFragment("test")))),
                Complex.class);
        assertEquals(2, result.size());
        assertEquals("complex-id-15100-jc689q2", result.get(1).getPhysicalLocationId());
    }

    @Test
    public void getResourceAndUrlTest() throws IOException {
        doReturn(getJson("single-query-result.json")).when(aaiQueryClient).query(eq(Format.RESOURCE_AND_URL),
                any(CustomQuery.class));
        List<ResourceAndUrl<AAIResultWrapper>> result = aaiQueryClient.getResourceAndUrl(
                new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(Types.COMPLEX.getFragment("test")))));
        assertEquals(2, result.size());

        assertEquals(1, result.get(1).getWrapper().getRelationships().get().getRelatedUris(Types.PSERVER).size());
    }

    @Test
    public void querySingleTypeTest() throws IOException {
        when(client.createClient(isA(AAIResourceUri.class))).thenReturn(restClient);
        when(restClient.put(any(Object.class), any(GenericType.class))).thenReturn(
                mapper.readValue(getJson("pathed-result.json"), new TypeReference<Results<Map<String, Object>>>() {}));


        List<Pathed> results = aaiQueryClient.queryPathed(
                new CustomQuery(Arrays.asList(AAIUriFactory.createNodesUri(Types.COMPLEX.getFragment("test")))));

        assertEquals(2, results.size());
        assertEquals("service-instance", results.get(1).getResourceType());
    }

    private String getJson(String filename) throws IOException {
        return new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + filename)));
    }
}