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.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;
 
  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;
 
  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;
 
  59 @RunWith(MockitoJUnitRunner.class)
 
  60 public class NetboxClientImplTest {
 
  62     private static final String APPLICATION_JSON = "application/json";
 
  65     public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort());
 
  67     private String token = "token";
 
  69     private NetboxHttpClient httpClient;
 
  70     private NetboxClientImpl netboxClient;
 
  74         String baseUrl = "http://localhost:" + wm.port();
 
  76         httpClient = new NetboxHttpClient(baseUrl, token);
 
  79         netboxClient = new NetboxClientImpl(httpClient);
 
  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());
 
  91     public void tearDown() throws IOException {
 
  96     public void nextAvailableIpInPrefixTestNoId() {
 
  97         Prefix prefix = mock(Prefix.class);
 
  98         doReturn(null).when(prefix).getId();
 
 100             netboxClient.assign(prefix);
 
 101         } catch (IpamException e) {
 
 102             Assert.assertEquals("Id must be set", e.getMessage());
 
 109     public void nextAvailableIpInPrefixTest() throws IOException, IpamException {
 
 111         Prefix prefix = mock(Prefix.class);
 
 112         doReturn(id).when(prefix).getId();
 
 114         URL url = Resources.getResource("nextAvailableIpResponse.json");
 
 115         String response = Resources.toString(url, Charsets.UTF_8);
 
 117         String expectedUrl = "/api/ipam/prefixes/" + id + "/available-ips/";
 
 118         givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response)));
 
 120         netboxClient.assign(prefix);
 
 122         verify(postRequestedFor(urlEqualTo(expectedUrl))
 
 123             .withHeader(ACCEPT, equalTo(APPLICATION_JSON))
 
 124             .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON))
 
 125             .withHeader(AUTHORIZATION, equalTo("Token " + token)));
 
 129     public void deleteIpTestError500() {
 
 131         IPAddress ipAddress = mock(IPAddress.class);
 
 132         doReturn(id).when(ipAddress).getId();
 
 134         String expectedUrl = "/api/ipam/ip-addresses/" + id + "/";
 
 135         givenThat(delete(urlEqualTo(expectedUrl)).willReturn(serverError()));
 
 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"));
 
 148     public void deleteIpTest() throws IpamException {
 
 150         IPAddress ipAddress = mock(IPAddress.class);
 
 151         doReturn(id).when(ipAddress).getId();
 
 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)));
 
 164     public void getIpAddressTest() throws IOException {
 
 165         StatusLine statusLine = mock(StatusLine.class);
 
 166         doReturn(201).when(statusLine).getStatusCode();
 
 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));
 
 172         HttpEntity entity = mock(HttpEntity.class);
 
 173         doReturn(stream).when(entity).getContent();
 
 175         HttpResponse httpResponse = mock(HttpResponse.class);
 
 176         doReturn(statusLine).when(httpResponse).getStatusLine();
 
 177         doReturn(entity).when(httpResponse).getEntity();
 
 179         IPAddress ipAddress = netboxClient.getIpAddress(httpResponse);
 
 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());