ee2861c0a24a4e8491810efedf2d537bfeb6e205
[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.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;
31
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;
42
43 public class NetboxHttpClientTest {
44
45     private static final String APPLICATION_JSON = "application/json";
46
47     @Rule
48     public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort());
49
50     private NetboxHttpClient httpClient;
51
52     @Before
53     public void setup() {
54         String baseUrl = "http://localhost:" + wm.port();
55         String token = "token";
56
57         httpClient = new NetboxHttpClient(baseUrl, token);
58         httpClient.init();
59
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());
66             });
67     }
68
69     @After
70     public void tearDown() throws IOException {
71         httpClient.close();
72     }
73
74     @Test
75     public void postTest() {
76         String expectedUrl = "/testPost";
77         givenThat(post(urlEqualTo(expectedUrl)).willReturn(ok()));
78
79         httpClient.post(expectedUrl, "").toCompletableFuture().join();
80
81         verify(postRequestedFor(urlEqualTo(expectedUrl))
82             .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
83             .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)));
84     }
85
86     @Test
87     public void postTestException() {
88         String expectedUrl = "/testPost";
89         givenThat(post(urlEqualTo(expectedUrl)).willReturn(aResponse().withFault(Fault.MALFORMED_RESPONSE_CHUNK)));
90
91         try {
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());
96             return;
97         }
98         Assert.fail();
99     }
100
101     @Test
102     public void deleteTest() {
103         String expectedUrl = "/testDelete";
104         givenThat(delete(urlEqualTo(expectedUrl)).willReturn(ok()));
105
106         httpClient.delete(expectedUrl).toCompletableFuture().join();
107
108         verify(deleteRequestedFor(urlEqualTo(expectedUrl))
109             .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
110             .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)));
111     }
112
113     @Test
114     public void deleteTestException() {
115         String expectedUrl = "/testDelete";
116         givenThat(delete(urlEqualTo(expectedUrl)).willReturn(aResponse().withFault(Fault.MALFORMED_RESPONSE_CHUNK)));
117
118         try {
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());
123             return;
124         }
125         Assert.fail();
126     }
127 }