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.delete;
19 import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor;
20 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
21 import static com.github.tomakehurst.wiremock.client.WireMock.givenThat;
22 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
23 import static com.github.tomakehurst.wiremock.client.WireMock.post;
24 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
26 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
27 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
28 import static org.apache.http.HttpHeaders.ACCEPT;
29 import static org.apache.http.HttpHeaders.CONTENT_TYPE;
31 import com.github.tomakehurst.wiremock.junit.WireMockRule;
32 import java.io.IOException;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
38 public class NetboxHttpClientTest {
40 private static final String APPLICATION_JSON = "application/json";
43 public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort());
45 private NetboxHttpClient httpClient;
49 String baseUrl = "http://localhost:" + wm.port();
50 String token = "token";
52 httpClient = new NetboxHttpClient(baseUrl, token);
54 wm.addMockServiceRequestListener(
55 (request, response) -> {
56 System.out.println("Request URL :" + request.getAbsoluteUrl());
57 System.out.println("Request body :" + request.getBodyAsString());
58 System.out.println("Response status :" + response.getStatus());
59 System.out.println("Response body :" + response.getBodyAsString());
64 public void tearDown() throws IOException {
69 public void postTest() throws IOException {
70 String expectedUrl = "/testPost";
71 givenThat(post(urlEqualTo(expectedUrl)).willReturn(ok()));
73 httpClient.post(expectedUrl, "");
75 verify(postRequestedFor(urlEqualTo(expectedUrl))
76 .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
77 .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)));
81 public void deleteTest() throws IOException {
82 String expectedUrl = "/testDelete";
83 givenThat(delete(urlEqualTo(expectedUrl)).willReturn(ok()));
84 httpClient.delete(expectedUrl);
85 verify(deleteRequestedFor(urlEqualTo(expectedUrl))
86 .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
87 .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)));