Add SvcLogicContext interaction with netbox-client
[ccsdk/sli/adaptors.git] / netbox-client / provider / src / test / java / org / onap / ccsdk / sli / adaptors / netbox / impl / NetboxHttpClientTest.java
1 /*
2  * Copyright (C) 2018 Bell Canada.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.ccsdk.sli.adaptors.netbox.impl;
17
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;
30
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;
37
38 public class NetboxHttpClientTest {
39
40     private static final String APPLICATION_JSON = "application/json";
41
42     @Rule
43     public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort());
44
45     private NetboxHttpClient httpClient;
46
47     @Before
48     public void setup() {
49         String baseUrl = "http://localhost:" + wm.port();
50         String token = "token";
51
52         httpClient = new NetboxHttpClient(baseUrl, token);
53
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());
60             });
61     }
62
63     @After
64     public void tearDown() throws IOException {
65         httpClient.close();
66     }
67
68     @Test
69     public void postTest() throws IOException {
70         String expectedUrl = "/testPost";
71         givenThat(post(urlEqualTo(expectedUrl)).willReturn(ok()));
72
73         httpClient.post(expectedUrl, "");
74
75         verify(postRequestedFor(urlEqualTo(expectedUrl))
76             .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
77             .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)));
78     }
79
80     @Test
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)));
88     }
89 }