2 * Copyright (C) 2018 Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.sli.adaptors.netbox.impl;
18 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
20 import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor;
21 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
22 import static com.github.tomakehurst.wiremock.client.WireMock.givenThat;
23 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
24 import static com.github.tomakehurst.wiremock.client.WireMock.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
27 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
28 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
29 import static org.apache.http.HttpHeaders.ACCEPT;
30 import static org.apache.http.HttpHeaders.CONTENT_TYPE;
32 import com.github.tomakehurst.wiremock.http.Fault;
33 import com.github.tomakehurst.wiremock.junit.WireMockRule;
34 import java.io.IOException;
35 import java.util.concurrent.CompletionException;
36 import org.junit.After;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.onap.ccsdk.sli.adaptors.netbox.api.IpamException;
43 public class NetboxHttpClientTest {
45 private static final String APPLICATION_JSON = "application/json";
48 public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort());
50 private NetboxHttpClient httpClient;
54 String baseUrl = "http://localhost:" + wm.port();
55 String token = "token";
57 httpClient = new NetboxHttpClient(baseUrl, token);
60 wm.addMockServiceRequestListener(
61 (request, response) -> {
62 System.out.println("Request URL :" + request.getAbsoluteUrl());
63 System.out.println("Request body :" + request.getBodyAsString());
64 System.out.println("Response status :" + response.getStatus());
65 System.out.println("Response body :" + response.getBodyAsString());
70 public void tearDown() throws IOException {
75 public void postTest() {
76 String expectedUrl = "/testPost";
77 givenThat(post(urlEqualTo(expectedUrl)).willReturn(ok()));
79 httpClient.post(expectedUrl, "").toCompletableFuture().join();
81 verify(postRequestedFor(urlEqualTo(expectedUrl))
82 .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
83 .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)));
87 public void postTestException() {
88 String expectedUrl = "/testPost";
89 givenThat(post(urlEqualTo(expectedUrl)).willReturn(aResponse().withFault(Fault.MALFORMED_RESPONSE_CHUNK)));
92 httpClient.post(expectedUrl, "").toCompletableFuture().join();
93 } catch (CompletionException e) {
94 Assert.assertEquals(IpamException.class, e.getCause().getClass());
95 Assert.assertEquals("Netbox request failed", e.getCause().getMessage());
102 public void deleteTest() {
103 String expectedUrl = "/testDelete";
104 givenThat(delete(urlEqualTo(expectedUrl)).willReturn(ok()));
106 httpClient.delete(expectedUrl).toCompletableFuture().join();
108 verify(deleteRequestedFor(urlEqualTo(expectedUrl))
109 .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
110 .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)));
114 public void deleteTestException() {
115 String expectedUrl = "/testDelete";
116 givenThat(delete(urlEqualTo(expectedUrl)).willReturn(aResponse().withFault(Fault.MALFORMED_RESPONSE_CHUNK)));
119 httpClient.delete(expectedUrl).toCompletableFuture().join();
120 } catch (CompletionException e) {
121 Assert.assertEquals(IpamException.class, e.getCause().getClass());
122 Assert.assertEquals("Netbox request failed", e.getCause().getMessage());