summaryrefslogtreecommitdiffstats
path: root/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImplTest.java
blob: 624a1906e9fc5967fce292e4f1a910fda0b936a4 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * Copyright (C) 2018 Bell Canada.
 *
 * 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.
 */
package org.onap.ccsdk.sli.adaptors.netbox.impl;

import static com.github.tomakehurst.wiremock.client.WireMock.created;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.givenThat;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.serverError;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.apache.http.HttpHeaders.ACCEPT;
import static org.apache.http.HttpHeaders.AUTHORIZATION;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.sql.rowset.CachedRowSet;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.ccsdk.sli.core.dblib.DbLibService;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
import org.slf4j.LoggerFactory;

@RunWith(MockitoJUnitRunner.class)
public class NetboxClientImplTest {

    private static final String APPLICATION_JSON = "application/json";

    @Rule
    public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort());
    @Mock
    private DbLibService dbLib;
    @Mock
    private SvcLogicContext svcLogicContext;
    @Mock
    private Appender<ILoggingEvent> appender;
    @Captor
    private ArgumentCaptor<ILoggingEvent> captor;


    private String token = "token";
    private String serviceInstanceId = UUID.randomUUID().toString();
    private String vfModuleId = UUID.randomUUID().toString();

    private Map<String, String> params = ImmutableMap
        .of("service_instance_id", serviceInstanceId,
            "vf_module_id", vfModuleId,
            "prefix_id", "3",
            "external_key", "3",
            "resource_name", "3"
        );

    private NetboxHttpClient httpClient;
    private NetboxClientImpl netboxClient;

    @Mock
    private NetboxHttpClient httpClientMock;
    @Mock
    private NetboxClientImpl netboxClientMock;


    @Before
    public void setup() {
        ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory
            .getLogger(NetboxClientImpl.class);
        logger.addAppender(appender);

        String baseUrl = "http://localhost:" + wm.port();

        httpClient = new NetboxHttpClient(baseUrl, token);

        netboxClient = new NetboxClientImpl(httpClient, dbLib);

        netboxClientMock = new NetboxClientImpl(httpClientMock, dbLib);

        wm.addMockServiceRequestListener(
            (request, response) -> {
                System.out.println("Request URL :" + request.getAbsoluteUrl());
                System.out.println("Request body :" + request.getBodyAsString());
                System.out.println("Response status :" + response.getStatus());
                System.out.println("Response body :" + response.getBodyAsString());
            });
    }

    @After
    public void tearDown() throws IOException {
        httpClient.close();
    }

    @Test
    public void unassignIpAddressTestNoParams() {
        QueryStatus status = netboxClient.unassignIpAddress(ImmutableMap.of(), svcLogicContext);
        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry(
            "This method requires the parameters [service_instance_id,external_key], but no parameters were passed in.");
    }

    @Test
    public void unassignIpAddressFailedRequest() throws IOException, SQLException {
        doThrow(new IOException("Failed request")).when(httpClientMock).delete(anyString());

        CachedRowSet crs = mock(CachedRowSet.class);
        doReturn("3").when(crs).getString(eq("ip_address_id"));
        doReturn(true).when(crs).next();
        doReturn(crs).when(dbLib).getData(anyString(), any(ArrayList.class), eq(null));

        QueryStatus status = netboxClientMock
            .unassignIpAddress(params, svcLogicContext);

        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry("Fail to unassign IP for IPAddress(id= 3). Failed request");
    }

    @Test
    public void unassignIpAddressServerError() throws SQLException {

        String expectedUrl = "/api/ipam/ip-addresses/3/";
        givenThat(delete(urlEqualTo(expectedUrl)).willReturn(serverError()));

        CachedRowSet crs = mock(CachedRowSet.class);
        doReturn("3").when(crs).getString(eq("ip_address_id"));
        doReturn(true).when(crs).next();
        doReturn(crs).when(dbLib).getData(anyString(), any(ArrayList.class), eq(null));

        QueryStatus status = netboxClient.unassignIpAddress(params, svcLogicContext);

        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry("Fail to unassign IP for IPAddress(id=3). HTTP code=500.");
    }

    @Test
    public void unassignIpAddressFailSQL() throws IOException, SQLException {

        String response = "{}";

        String expectedUrl = "/api/ipam/ip-addresses/3/";
        givenThat(delete(urlEqualTo(expectedUrl)).willReturn(created().withBody(response)));

        CachedRowSet crs = mock(CachedRowSet.class);
        doReturn(true).when(crs).next();
        doReturn("3").when(crs).getString(eq("ip_address_id"));
        doReturn(crs).when(dbLib).getData(anyString(), any(ArrayList.class), eq(null));

        doThrow(new SQLException("Failed")).when(dbLib).writeData(anyString(), any(ArrayList.class), eq(null));

        QueryStatus status = netboxClient.unassignIpAddress(params, svcLogicContext);

        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry("Caught SQL exception");
    }

    @Test
    public void unassignIpAddressSuccess() throws IOException, SQLException {
        String response = "{}";

        String expectedUrl = "/api/ipam/ip-addresses/3/";
        givenThat(delete(urlEqualTo(expectedUrl)).willReturn(created().withBody(response)));

        CachedRowSet crs = mock(CachedRowSet.class);
        doReturn("3").when(crs).getString(eq("ip_address_id"));
        doReturn(true).when(crs).next();
        doReturn(crs).when(dbLib).getData(anyString(), any(ArrayList.class), eq(null));

        QueryStatus status = netboxClient.unassignIpAddress(params, svcLogicContext);

        verify(deleteRequestedFor(urlEqualTo(expectedUrl))
            .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
            .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON))
            .withHeader(AUTHORIZATION, equalTo("Token " + token)));

        Mockito.verify(dbLib).writeData(anyString(), any(ArrayList.class), eq(null));
        Assert.assertEquals(QueryStatus.SUCCESS, status);
    }


    @Test
    public void nextAvailableIpInPrefixTestNoId() {
        QueryStatus status = netboxClient.assignIpAddress(ImmutableMap.of(), svcLogicContext);
        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry(
            "This method requires the parameters [service_instance_id,vf_module_id,prefix_id,resource_name,external_key], but no parameters were passed in.");
    }

    @Test
    public void nextAvailableIpInPrefixFailedRequest() throws IOException {
        doThrow(new IOException("Failed request")).when(httpClientMock).post(anyString(), anyString());
        QueryStatus status = netboxClientMock.assignIpAddress(params, svcLogicContext);

        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry("Fail to assign IP for Prefix(id=3). Failed request");
    }

    @Test
    public void nextAvailableIpInPrefixBadRespPayload() throws IOException {
        URL url = Resources.getResource("badResponse.json");
        String response = Resources.toString(url, Charsets.UTF_8);

        String expectedUrl = "/api/ipam/prefixes/3/available-ips/";
        givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response)));

        QueryStatus status = netboxClient.assignIpAddress(params, svcLogicContext);

        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry("Fail to parse IPAM JSON reponse to IPAddress POJO. IPAM JSON Response={\n"
            + "  \"id\": 8\n"
            + "  \"address\": \"192.168.20.7/32\"\n"
            + "}");
    }

    @Test
    public void nextAvailableIpInPrefixFailSQL() throws IOException, SQLException {
        URL url = Resources.getResource("nextAvailableIpResponse.json");
        String response = Resources.toString(url, Charsets.UTF_8);

        String expectedUrl = "/api/ipam/prefixes/3/available-ips/";
        givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response)));

        doThrow(new SQLException("Failed")).when(dbLib).writeData(anyString(), any(ArrayList.class), eq(null));

        QueryStatus status = netboxClient.assignIpAddress(params, svcLogicContext);

        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry("Caught SQL exception");
    }

    @Test
    public void nextAvailableIpInPrefixError500() throws IOException {
        URL url = Resources.getResource("nextAvailableIpResponse.json");
        String response = Resources.toString(url, Charsets.UTF_8);

        String expectedUrl = "/api/ipam/prefixes/3/available-ips/";
        givenThat(post(urlEqualTo(expectedUrl)).willReturn(serverError().withBody(response)));

        QueryStatus status = netboxClient.assignIpAddress(params, svcLogicContext);

        Assert.assertEquals(QueryStatus.FAILURE, status);
        verifyLogEntry("Fail to assign IP for Prefix(id=3). HTTP code 201!=500.");
    }

    @Test
    public void nextAvailableIpInPrefixSuccess() throws IOException, SQLException {
        URL url = Resources.getResource("nextAvailableIpResponse.json");
        String response = Resources.toString(url, Charsets.UTF_8);

        String expectedUrl = "/api/ipam/prefixes/3/available-ips/";
        givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response)));

        QueryStatus status = netboxClient.assignIpAddress(params, svcLogicContext);

        verify(postRequestedFor(urlEqualTo(expectedUrl))
            .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
            .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON))
            .withHeader(AUTHORIZATION, equalTo("Token " + token)));

        Mockito.verify(dbLib).writeData(anyString(), any(ArrayList.class), eq(null));
        Assert.assertEquals(QueryStatus.SUCCESS, status);
    }

    private void verifyLogEntry(String message) {
        Mockito.verify(appender, times(1)).doAppend(captor.capture());
        List<ILoggingEvent> allValues = captor.getAllValues();
        for (ILoggingEvent loggingEvent : allValues) {
            Assert.assertTrue(loggingEvent.getFormattedMessage().contains(message));
        }
    }

}