19b178c9a9496b62f9728fb11f30aa608ddbe50d
[ccsdk/sli/adaptors.git] / netbox-client / provider / src / test / java / org / onap / ccsdk / sli / adaptors / netbox / impl / NetboxClientImplTest.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.created;
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.serverError;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
28 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
29 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
30 import static org.apache.http.HttpHeaders.ACCEPT;
31 import static org.apache.http.HttpHeaders.AUTHORIZATION;
32 import static org.apache.http.HttpHeaders.CONTENT_TYPE;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.mock;
35
36 import com.github.tomakehurst.wiremock.junit.WireMockRule;
37 import com.google.common.base.Charsets;
38 import com.google.common.io.Resources;
39 import java.io.ByteArrayInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.net.URL;
43 import java.nio.charset.StandardCharsets;
44 import org.apache.http.HttpEntity;
45 import org.apache.http.HttpResponse;
46 import org.apache.http.StatusLine;
47 import org.junit.After;
48 import org.junit.Assert;
49 import org.junit.Before;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.runners.MockitoJUnitRunner;
54 import org.onap.ccsdk.sli.adaptors.netbox.api.IpamException;
55 import org.onap.ccsdk.sli.adaptors.netbox.model.IPAddress;
56 import org.onap.ccsdk.sli.adaptors.netbox.model.Prefix;
57 import org.onap.ccsdk.sli.adaptors.netbox.model.Status.Values;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class NetboxClientImplTest {
61
62     private static final String APPLICATION_JSON = "application/json";
63
64     @Rule
65     public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort());
66
67     private String token = "token";
68
69     private NetboxHttpClient httpClient;
70     private NetboxClientImpl netboxClient;
71
72     @Before
73     public void setup() {
74         String baseUrl = "http://localhost:" + wm.port();
75
76         httpClient = new NetboxHttpClient(baseUrl, token);
77         httpClient.init();
78
79         netboxClient = new NetboxClientImpl(httpClient);
80
81         wm.addMockServiceRequestListener(
82             (request, response) -> {
83                 System.out.println("Request URL :" + request.getAbsoluteUrl());
84                 System.out.println("Request body :" + request.getBodyAsString());
85                 System.out.println("Response status :" + response.getStatus());
86                 System.out.println("Response body :" + response.getBodyAsString());
87             });
88     }
89
90     @After
91     public void tearDown() throws IOException {
92         httpClient.close();
93     }
94
95     @Test
96     public void nextAvailableIpInPrefixTestNoId() {
97         Prefix prefix = mock(Prefix.class);
98         doReturn(null).when(prefix).getId();
99         try {
100             netboxClient.assign(prefix);
101         } catch (IpamException e) {
102             Assert.assertEquals("Id must be set", e.getMessage());
103             return;
104         }
105         Assert.fail();
106     }
107
108     @Test
109     public void nextAvailableIpInPrefixTest() throws IOException, IpamException {
110         Integer id = 3;
111         Prefix prefix = mock(Prefix.class);
112         doReturn(id).when(prefix).getId();
113
114         URL url = Resources.getResource("nextAvailableIpResponse.json");
115         String response = Resources.toString(url, Charsets.UTF_8);
116
117         String expectedUrl = "/api/ipam/prefixes/" + id + "/available-ips/";
118         givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response)));
119
120         netboxClient.assign(prefix);
121
122         verify(postRequestedFor(urlEqualTo(expectedUrl))
123             .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
124             .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON))
125             .withHeader(AUTHORIZATION, equalTo("Token " + token)));
126     }
127
128     @Test
129     public void deleteIpTestError500() {
130         Integer id = 3;
131         IPAddress ipAddress = mock(IPAddress.class);
132         doReturn(id).when(ipAddress).getId();
133
134         String expectedUrl = "/api/ipam/ip-addresses/" + id + "/";
135         givenThat(delete(urlEqualTo(expectedUrl)).willReturn(serverError()));
136         try {
137             netboxClient.unassign(ipAddress);
138         } catch (IpamException e) {
139             Assert.assertEquals(IllegalStateException.class, e.getCause().getClass());
140             Assert.assertTrue(e.getMessage().contains(
141                 "Fail to unassign IP for IPAddress(id= 3). java.lang.IllegalStateException: Netbox request failed with status: HTTP/1.1 500 Server Error"));
142             return;
143         }
144         Assert.fail();
145     }
146
147     @Test
148     public void deleteIpTest() throws IpamException {
149         Integer id = 3;
150         IPAddress ipAddress = mock(IPAddress.class);
151         doReturn(id).when(ipAddress).getId();
152
153         String expectedUrl = "/api/ipam/ip-addresses/" + id + "/";
154         givenThat(delete(urlEqualTo(expectedUrl)).willReturn(ok()));
155         netboxClient.unassign(ipAddress);
156         verify(deleteRequestedFor(urlEqualTo(expectedUrl))
157             .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
158             .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON))
159             .withHeader(AUTHORIZATION, equalTo("Token " + token)));
160     }
161
162
163     @Test
164     public void getIpAddressTest() throws IOException {
165         StatusLine statusLine = mock(StatusLine.class);
166         doReturn(201).when(statusLine).getStatusCode();
167
168         URL url = Resources.getResource("nextAvailableIpResponse.json");
169         String response = Resources.toString(url, Charsets.UTF_8);
170         InputStream stream = new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8));
171
172         HttpEntity entity = mock(HttpEntity.class);
173         doReturn(stream).when(entity).getContent();
174
175         HttpResponse httpResponse = mock(HttpResponse.class);
176         doReturn(statusLine).when(httpResponse).getStatusLine();
177         doReturn(entity).when(httpResponse).getEntity();
178
179         IPAddress ipAddress = netboxClient.getIpAddress(httpResponse);
180
181         Assert.assertEquals("192.168.20.7/32", ipAddress.getAddress());
182         Assert.assertEquals(Integer.valueOf(8), ipAddress.getId());
183         Assert.assertEquals(Values.ACTIVE, ipAddress.getStatus());
184     }
185 }