diff options
Diffstat (limited to 'netbox-client/provider/src/test')
4 files changed, 169 insertions, 119 deletions
diff --git a/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImplTest.java b/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImplTest.java index f1eda736a..0f887a630 100644 --- a/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImplTest.java +++ b/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxClientImplTest.java @@ -20,7 +20,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.delete; import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; import static com.github.tomakehurst.wiremock.client.WireMock.givenThat; -import static com.github.tomakehurst.wiremock.client.WireMock.ok; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.serverError; @@ -33,37 +32,37 @@ import static org.apache.http.HttpHeaders.CONTENT_TYPE; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.times; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.Appender; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableMap; import com.google.common.io.Resources; -import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.InputStream; import java.net.URL; -import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.util.ArrayList; +import java.util.List; +import java.util.Map; import java.util.UUID; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import org.onap.ccsdk.sli.adaptors.netbox.api.IpamException; -import org.onap.ccsdk.sli.adaptors.netbox.model.IPAddress; -import org.onap.ccsdk.sli.adaptors.netbox.model.Prefix; -import org.onap.ccsdk.sli.adaptors.netbox.model.Status.Values; import org.onap.ccsdk.sli.core.dblib.DbLibService; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus; +import org.slf4j.LoggerFactory; @RunWith(MockitoJUnitRunner.class) public class NetboxClientImplTest { @@ -72,26 +71,50 @@ public class NetboxClientImplTest { @Rule public WireMockRule wm = new WireMockRule(wireMockConfig().dynamicPort()); - @Mock private DbLibService dbLib; + @Mock + private SvcLogicContext svcLogicContext; + @Mock + private Appender<ILoggingEvent> appender; + @Captor + private ArgumentCaptor<ILoggingEvent> captor; + private String token = "token"; private String serviceInstanceId = UUID.randomUUID().toString(); private String vfModuleId = UUID.randomUUID().toString(); + private Map<String, String> params = ImmutableMap + .of("service_instance_id", serviceInstanceId, + "vf_module_id", vfModuleId, + "prefix_id", "3", + "ip_address_id", "3" + ); + private NetboxHttpClient httpClient; private NetboxClientImpl netboxClient; + @Mock + private NetboxHttpClient httpClientMock; + @Mock + private NetboxClientImpl netboxClientMock; + + @Before public void setup() { + ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory + .getLogger(NetboxClientImpl.class); + logger.addAppender(appender); + String baseUrl = "http://localhost:" + wm.port(); httpClient = new NetboxHttpClient(baseUrl, token); - httpClient.init(); netboxClient = new NetboxClientImpl(httpClient, dbLib); + netboxClientMock = new NetboxClientImpl(httpClientMock, dbLib); + wm.addMockServiceRequestListener( (request, response) -> { System.out.println("Request URL :" + request.getAbsoluteUrl()); @@ -107,95 +130,157 @@ public class NetboxClientImplTest { } @Test - public void nextAvailableIpInPrefixTestNoId() throws SQLException { - Prefix prefix = mock(Prefix.class); - doReturn(null).when(prefix).getId(); - try { - netboxClient.assign(prefix, serviceInstanceId, vfModuleId); - } catch (IpamException e) { - Assert.assertEquals("Id must be set", e.getMessage()); - return; - } - Assert.fail(); + public void unassignIpAddressTestNoParams() { + QueryStatus status = netboxClient.unassignIpAddress(ImmutableMap.of(), svcLogicContext); + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("This method requires the parameters [service_instance_id,vf_module_id,ip_address_id], but no parameters were passed in"); } @Test - public void nextAvailableIpInPrefixTest() throws IOException, IpamException, SQLException { - Integer id = 3; - Prefix prefix = mock(Prefix.class); - doReturn(id).when(prefix).getId(); + public void unassignIpAddressFailedRequest() throws IOException { + doThrow(new IOException("Failed request")).when(httpClientMock).delete(anyString()); + QueryStatus status = netboxClientMock + .unassignIpAddress(params, svcLogicContext); - URL url = Resources.getResource("nextAvailableIpResponse.json"); - String response = Resources.toString(url, Charsets.UTF_8); + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("Fail to unassign IP for IPAddress(id= 3). Failed request"); + } - String expectedUrl = "/api/ipam/prefixes/" + id + "/available-ips/"; - givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response))); + @Test + public void unassignIpAddressServerError() { - netboxClient.assign(prefix, serviceInstanceId, vfModuleId); + String expectedUrl = "/api/ipam/ip-addresses/3/"; + givenThat(delete(urlEqualTo(expectedUrl)).willReturn(serverError())); - verify(postRequestedFor(urlEqualTo(expectedUrl)) - .withHeader(ACCEPT, equalTo(APPLICATION_JSON)) - .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)) - .withHeader(AUTHORIZATION, equalTo("Token " + token))); - Mockito.verify(dbLib).writeData(anyString(), any(ArrayList.class), eq((null))); + QueryStatus status = netboxClient.unassignIpAddress(params, svcLogicContext); + + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("Fail to unassign IP for IPAddress(id=3). HTTP code=500."); } @Test - public void deleteIpTestError500() throws SQLException { - Integer id = 3; - IPAddress ipAddress = mock(IPAddress.class); - doReturn(id).when(ipAddress).getId(); + public void unassignIpAddressFailSQL() throws IOException, SQLException { - String expectedUrl = "/api/ipam/ip-addresses/" + id + "/"; - givenThat(delete(urlEqualTo(expectedUrl)).willReturn(serverError())); - try { - netboxClient.unassign(ipAddress, serviceInstanceId, vfModuleId); - } catch (IpamException e) { - Assert.assertEquals(IllegalStateException.class, e.getCause().getClass()); - Assert.assertTrue(e.getMessage().contains( - "Fail to unassign IP for IPAddress(id= 3). java.lang.IllegalStateException: Netbox request failed with status: HTTP/1.1 500 Server Error")); - return; - } - Assert.fail(); + String response = "{}"; + + String expectedUrl = "/api/ipam/ip-addresses/3/"; + givenThat(delete(urlEqualTo(expectedUrl)).willReturn(created().withBody(response))); + + doThrow(new SQLException("Failed")).when(dbLib).writeData(anyString(), any(ArrayList.class), eq(null)); + + QueryStatus status = netboxClient.unassignIpAddress(params, svcLogicContext); + + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("Caught SQL exception"); } @Test - public void deleteIpTest() throws IpamException, SQLException { - Integer id = 3; - IPAddress ipAddress = mock(IPAddress.class); - doReturn(id).when(ipAddress).getId(); - - String expectedUrl = "/api/ipam/ip-addresses/" + id + "/"; - givenThat(delete(urlEqualTo(expectedUrl)).willReturn(ok())); - netboxClient.unassign(ipAddress, serviceInstanceId, vfModuleId); + public void unassignIpAddressSuccess() throws IOException, SQLException { + String response = "{}"; + + String expectedUrl = "/api/ipam/ip-addresses/3/"; + givenThat(delete(urlEqualTo(expectedUrl)).willReturn(created().withBody(response))); + + QueryStatus status = netboxClient.unassignIpAddress(params, svcLogicContext); + verify(deleteRequestedFor(urlEqualTo(expectedUrl)) .withHeader(ACCEPT, equalTo(APPLICATION_JSON)) .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)) .withHeader(AUTHORIZATION, equalTo("Token " + token))); - Mockito.verify(dbLib).writeData(anyString(), any(ArrayList.class), eq((null))); + + Mockito.verify(dbLib).writeData(anyString(), any(ArrayList.class), eq(null)); + Assert.assertEquals(QueryStatus.SUCCESS, status); + } + + + @Test + public void nextAvailableIpInPrefixTestNoId() { + QueryStatus status = netboxClient.assignIpAddress(ImmutableMap.of(), svcLogicContext); + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("This method requires the parameters [service_instance_id,vf_module_id,prefix_id], but no parameters were passed in"); } + @Test + public void nextAvailableIpInPrefixFailedRequest() throws IOException { + doThrow(new IOException("Failed request")).when(httpClientMock).post(anyString(), anyString()); + QueryStatus status = netboxClientMock.assignIpAddress(params, svcLogicContext); + + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("Fail to assign IP for Prefix(id=3). Failed request"); + } @Test - public void getIpAddressTest() throws IOException { - StatusLine statusLine = mock(StatusLine.class); - doReturn(201).when(statusLine).getStatusCode(); + public void nextAvailableIpInPrefixBadRespPayload() throws IOException { + URL url = Resources.getResource("badResponse.json"); + String response = Resources.toString(url, Charsets.UTF_8); + + String expectedUrl = "/api/ipam/prefixes/3/available-ips/"; + givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response))); + + QueryStatus status = netboxClient.assignIpAddress(params, svcLogicContext); + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("Fail to parse IPAM JSON reponse to IPAddress POJO. IPAM JSON Response={\n" + + " \"id\": 8\n" + + " \"address\": \"192.168.20.7/32\"\n" + + "}"); + } + + @Test + public void nextAvailableIpInPrefixFailSQL() throws IOException, SQLException { URL url = Resources.getResource("nextAvailableIpResponse.json"); String response = Resources.toString(url, Charsets.UTF_8); - InputStream stream = new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)); - HttpEntity entity = mock(HttpEntity.class); - doReturn(stream).when(entity).getContent(); + String expectedUrl = "/api/ipam/prefixes/3/available-ips/"; + givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response))); + + doThrow(new SQLException("Failed")).when(dbLib).writeData(anyString(), any(ArrayList.class), eq(null)); + + QueryStatus status = netboxClient.assignIpAddress(params, svcLogicContext); + + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("Caught SQL exception"); + } + + @Test + public void nextAvailableIpInPrefixError500() throws IOException { + URL url = Resources.getResource("nextAvailableIpResponse.json"); + String response = Resources.toString(url, Charsets.UTF_8); - HttpResponse httpResponse = mock(HttpResponse.class); - doReturn(statusLine).when(httpResponse).getStatusLine(); - doReturn(entity).when(httpResponse).getEntity(); + String expectedUrl = "/api/ipam/prefixes/3/available-ips/"; + givenThat(post(urlEqualTo(expectedUrl)).willReturn(serverError().withBody(response))); - IPAddress ipAddress = netboxClient.getIpAddress(httpResponse); + QueryStatus status = netboxClient.assignIpAddress(params, svcLogicContext); - Assert.assertEquals("192.168.20.7/32", ipAddress.getAddress()); - Assert.assertEquals(Integer.valueOf(8), ipAddress.getId()); - Assert.assertEquals(Values.ACTIVE, ipAddress.getStatus()); + Assert.assertEquals(QueryStatus.FAILURE, status); + verifyLogEntry("Fail to assign IP for Prefix(id=3). HTTP code 201!=500."); } + + @Test + public void nextAvailableIpInPrefixSuccess() throws IOException, SQLException { + URL url = Resources.getResource("nextAvailableIpResponse.json"); + String response = Resources.toString(url, Charsets.UTF_8); + + String expectedUrl = "/api/ipam/prefixes/3/available-ips/"; + givenThat(post(urlEqualTo(expectedUrl)).willReturn(created().withBody(response))); + + QueryStatus status = netboxClient.assignIpAddress(params, svcLogicContext); + + verify(postRequestedFor(urlEqualTo(expectedUrl)) + .withHeader(ACCEPT, equalTo(APPLICATION_JSON)) + .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON)) + .withHeader(AUTHORIZATION, equalTo("Token " + token))); + + Mockito.verify(dbLib).writeData(anyString(), any(ArrayList.class), eq(null)); + Assert.assertEquals(QueryStatus.SUCCESS, status); + } + + private void verifyLogEntry(String message) { + Mockito.verify(appender, times(1)).doAppend(captor.capture()); + List<ILoggingEvent> allValues = captor.getAllValues(); + for (ILoggingEvent loggingEvent : allValues) { + Assert.assertTrue(loggingEvent.getFormattedMessage().contains(message)); + } + } + }
\ No newline at end of file diff --git a/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxHttpClientTest.java b/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxHttpClientTest.java index ee2861c0a..8634742ae 100644 --- a/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxHttpClientTest.java +++ b/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/impl/NetboxHttpClientTest.java @@ -15,7 +15,6 @@ */ package org.onap.ccsdk.sli.adaptors.netbox.impl; -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.delete; import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor; import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; @@ -29,16 +28,12 @@ import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMoc import static org.apache.http.HttpHeaders.ACCEPT; import static org.apache.http.HttpHeaders.CONTENT_TYPE; -import com.github.tomakehurst.wiremock.http.Fault; import com.github.tomakehurst.wiremock.junit.WireMockRule; import java.io.IOException; -import java.util.concurrent.CompletionException; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.onap.ccsdk.sli.adaptors.netbox.api.IpamException; public class NetboxHttpClientTest { @@ -55,7 +50,6 @@ public class NetboxHttpClientTest { String token = "token"; httpClient = new NetboxHttpClient(baseUrl, token); - httpClient.init(); wm.addMockServiceRequestListener( (request, response) -> { @@ -72,11 +66,11 @@ public class NetboxHttpClientTest { } @Test - public void postTest() { + public void postTest() throws IOException { String expectedUrl = "/testPost"; givenThat(post(urlEqualTo(expectedUrl)).willReturn(ok())); - httpClient.post(expectedUrl, "").toCompletableFuture().join(); + httpClient.post(expectedUrl, ""); verify(postRequestedFor(urlEqualTo(expectedUrl)) .withHeader(ACCEPT, equalTo(APPLICATION_JSON)) @@ -84,44 +78,12 @@ public class NetboxHttpClientTest { } @Test - public void postTestException() { - String expectedUrl = "/testPost"; - givenThat(post(urlEqualTo(expectedUrl)).willReturn(aResponse().withFault(Fault.MALFORMED_RESPONSE_CHUNK))); - - try { - httpClient.post(expectedUrl, "").toCompletableFuture().join(); - } catch (CompletionException e) { - Assert.assertEquals(IpamException.class, e.getCause().getClass()); - Assert.assertEquals("Netbox request failed", e.getCause().getMessage()); - return; - } - Assert.fail(); - } - - @Test - public void deleteTest() { + public void deleteTest() throws IOException { String expectedUrl = "/testDelete"; givenThat(delete(urlEqualTo(expectedUrl)).willReturn(ok())); - - httpClient.delete(expectedUrl).toCompletableFuture().join(); - + httpClient.delete(expectedUrl); verify(deleteRequestedFor(urlEqualTo(expectedUrl)) .withHeader(ACCEPT, equalTo(APPLICATION_JSON)) .withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON))); } - - @Test - public void deleteTestException() { - String expectedUrl = "/testDelete"; - givenThat(delete(urlEqualTo(expectedUrl)).willReturn(aResponse().withFault(Fault.MALFORMED_RESPONSE_CHUNK))); - - try { - httpClient.delete(expectedUrl).toCompletableFuture().join(); - } catch (CompletionException e) { - Assert.assertEquals(IpamException.class, e.getCause().getClass()); - Assert.assertEquals("Netbox request failed", e.getCause().getMessage()); - return; - } - Assert.fail(); - } } diff --git a/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxPropertiesTest.java b/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxPropertiesTest.java index d9916707f..be5aabfc2 100644 --- a/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxPropertiesTest.java +++ b/netbox-client/provider/src/test/java/org/onap/ccsdk/sli/adaptors/netbox/property/NetboxPropertiesTest.java @@ -56,7 +56,6 @@ public class NetboxPropertiesTest { "Missing configuration properties resource for Netbox: netbox.properties"); } - private void verifyLogEntry(String message) { verify(appender, times(1)).doAppend(captor.capture()); List<ILoggingEvent> allValues = captor.getAllValues(); diff --git a/netbox-client/provider/src/test/resources/badResponse.json b/netbox-client/provider/src/test/resources/badResponse.json new file mode 100644 index 000000000..713109fd9 --- /dev/null +++ b/netbox-client/provider/src/test/resources/badResponse.json @@ -0,0 +1,4 @@ +{ + "id": 8 + "address": "192.168.20.7/32" +}
\ No newline at end of file |