aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/aai/AaiOverTLSClientServerTest.java
blob: 7ae8ac83292cf6a2a359665f6975b1b36b6667fa (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2018 Nokia 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.vid.aai;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xebialabs.restito.semantics.Action;
import io.joshworks.restclient.http.HttpResponse;
import io.joshworks.restclient.http.mapper.ObjectMapper;
import java.io.IOException;
import org.assertj.core.api.Assertions;
import org.glassfish.grizzly.http.util.HttpStatus;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.vid.aai.model.AaiNodeQueryResponse;
import org.onap.vid.aai.model.ResourceType;
import org.onap.vid.client.SyncRestClient;
import org.onap.vid.model.SubscriberList;
import org.onap.vid.testUtils.StubServerUtil;

@RunWith(MockitoJUnitRunner.class)
public class AaiOverTLSClientServerTest {

    @Mock
    private AaiOverTLSPropertySupplier propertySupplier;

    private static StubServerUtil serverUtil;

    private String searchNodesQueryResponsePayload =
        "{\n"
            + "\"result-data\": [\n"
            + "  {\n"
            + "\"resource-type\": \"generic-vnf\",\n"
            + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6eac8e69-c98d-4ac5-ab90-69fe0cabda76\"\n"
            + "},\n"
            + "  {\n"
            + "\"resource-type\": \"generic-vnf\",\n"
            + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/e3766bc5-40a7-4dbe-9d4a-1d8c8f284913\"\n"
            + "},\n"
            + "  {\n"
            + "\"resource-type\": \"generic-vnf\",\n"
            + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/6aa153ee-6637-4b49-beb5-a5e756e00393\"\n"
            + "},\n"
            + "  {\n"
            + "\"resource-type\": \"generic-vnf\",\n"
            + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/5a981c30-de25-4ea9-98fa-ed398f13ea41\"\n"
            + "},\n"
            + "  {\n"
            + "\"resource-type\": \"generic-vnf\",\n"
            + "\"resource-link\": \"/aai/v13/network/generic-vnfs/generic-vnf/b0ef2271-8ac0-4268-b9a5-09cb50c20c85\"\n"
            + "}\n"
            + "],\n"
            + "}";

    private String subscribersResponsePayload =
        "{\n"
        + "\"customer\": [\n"
        + "  {\n"
        + "\"global-customer-id\": \"DemoCust_752df078-d8e9-4731-abf6-8ae7348075bb\",\n"
        + "\"subscriber-name\": \"DemoCust_752df078-d8e9-4731-abf6-8ae7348075bb\",\n"
        + "\"subscriber-type\": \"INFRA\",\n"
        + "\"resource-version\": \"1536158347585\"\n"
        + "},\n"
        + "  {\n"
        + "\"global-customer-id\": \"DemoCust_62bf43a3-4888-4c82-ae98-3ebc3d782761\",\n"
        + "\"subscriber-name\": \"DemoCust_62bf43a3-4888-4c82-ae98-3ebc3d782761\",\n"
        + "\"subscriber-type\": \"INFRA\",\n"
        + "\"resource-version\": \"1536240894581\"\n"
        + "},\n"
        + "  {\n"
        + "\"global-customer-id\": \"DemoCust_e84256d6-ef3e-4a28-9741-9987019c3a8f\",\n"
        + "\"subscriber-name\": \"DemoCust_e84256d6-ef3e-4a28-9741-9987019c3a8f\",\n"
        + "\"subscriber-type\": \"INFRA\",\n"
        + "\"resource-version\": \"1536330956393\"\n"
        + "},\n"
        + "  {\n"
        + "\"global-customer-id\": \"ETE_Customer_377bb124-2638-4025-a315-cdae04f52bce\",\n"
        + "\"subscriber-name\": \"ETE_Customer_377bb124-2638-4025-a315-cdae04f52bce\",\n"
        + "\"subscriber-type\": \"INFRA\",\n"
        + "\"resource-version\": \"1536088625538\"\n"
        + "}\n"
        + "],\n"
        + "}";

    @BeforeClass
    public static void setUpClass(){
        serverUtil = new StubServerUtil();
        serverUtil.runServer();
    }

    @AfterClass
    public static void tearDown(){
        serverUtil.stopServer();
    }

    @Test
    public void shouldSearchNodeTypeByName() throws IOException, ParseException {
        ObjectMapper objectMapper = getFasterXmlObjectMapper();
        AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper),  propertySupplier, serverUtil.constructTargetUrl("http", ""));

        serverUtil.prepareGetCall("/search/nodes-query", new JSONParser().parse(searchNodesQueryResponsePayload), Action.status(HttpStatus.OK_200));

        HttpResponse<AaiNodeQueryResponse> aaiNodeQueryResponseHttpResponse = aaiOverTLSClient
            .searchNodeTypeByName("any", ResourceType.GENERIC_VNF);

        AaiNodeQueryResponse body = aaiNodeQueryResponseHttpResponse.getBody();
        Assertions.assertThat(body.resultData.size()).isEqualTo(5);
        Assertions.assertThat(aaiNodeQueryResponseHttpResponse.getStatus()).isEqualTo(200);
    }

    @Test
    public void shouldGetSubscribers() throws ParseException, JsonProcessingException {
        ObjectMapper objectMapper = getCodehausObjectMapper();
        AaiOverTLSClient aaiOverTLSClient = new AaiOverTLSClient(new SyncRestClient(objectMapper),  propertySupplier, serverUtil.constructTargetUrl("http", ""));

        serverUtil.prepareGetCall("/business/customers", new JSONParser().parse(subscribersResponsePayload), Action.status(HttpStatus.OK_200));

        HttpResponse<SubscriberList> allSubscribers = aaiOverTLSClient.getAllSubscribers();

        SubscriberList subscriberList = allSubscribers.getBody();
        Assertions.assertThat(subscriberList.customer.size()).isEqualTo(4);
        Assertions.assertThat(allSubscribers.getStatus()).isEqualTo(200);
    }

    private ObjectMapper getCodehausObjectMapper() {
        return new ObjectMapper() {

            org.codehaus.jackson.map.ObjectMapper om = new org.codehaus.jackson.map.ObjectMapper();

            @Override
            public <T> T readValue(String s, Class<T> aClass) {
                try {
                    return om.readValue(s, aClass);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public String writeValue(Object o) {
                try {
                    return om.writeValueAsString(o);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    private ObjectMapper getFasterXmlObjectMapper() {
        return new ObjectMapper() {

            com.fasterxml.jackson.databind.ObjectMapper om = new com.fasterxml.jackson.databind.ObjectMapper();

            @Override
            public <T> T readValue(String s, Class<T> aClass) {
                try {
                    return om.readValue(s, aClass);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public String writeValue(Object o) {
                try {
                    return om.writeValueAsString(o);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

}