package com.krillsson.sysapi.resources;

import com.krillsson.sysapi.core.metrics.NetworkMetrics;
import io.dropwizard.testing.junit.ResourceTestRule;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import oshi.hardware.NetworkIF;

import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

public class NetworkInterfacesResourceTest {
    private static final NetworkMetrics provider = mock(NetworkMetrics.class);

    @ClassRule
    public static final ResourceTestRule RESOURCES = ResourceTestRule.builder()
            .addResource(new NetworkInterfacesResource(provider))
            .build();
    private com.krillsson.sysapi.core.domain.network.NetworkInterface networkInterfaceData;

    @Before
    public void setUp() throws Exception {
        networkInterfaceData = new com.krillsson.sysapi.core.domain.network.NetworkInterface(
                "en0",
                "",
                "",
                0,
                false,
                new ArrayList<>(),
                new ArrayList<>()
        );
    }

    @Test
    public void shouldReturnNotFoundIfNoDataPresent() throws Exception {
        when(provider.networkInterfaceById("en1")).thenReturn(Optional.empty());
        Response response = RESOURCES.getJerseyTest().target("/nics/en1")
                .request(MediaType.APPLICATION_JSON_TYPE)
                .get();
        assertThat(response.getStatus(), is(404));
    }

    @Test
    public void shouldReturnReasonableData() throws Exception {
        when(provider.networkInterfaceById("en0")).thenReturn(Optional.of(networkInterfaceData));
        com.krillsson.sysapi.dto.network.NetworkInterface networkInterfaceData = RESOURCES.getJerseyTest().target("/nics/en0")
                .request(MediaType.APPLICATION_JSON_TYPE)
                .get(com.krillsson.sysapi.dto.network.NetworkInterface.class);
        assertThat(networkInterfaceData, is(networkInterfaceData));
    }

    @Test
    public void shouldReturnReasonableArrayData() throws Exception {
        when(provider.networkInterfaces()).thenReturn(Arrays.asList(networkInterfaceData));
        List<com.krillsson.sysapi.dto.network.NetworkInterface> networkInterfaceData = RESOURCES.getJerseyTest().target("/nics")
                .request(MediaType.APPLICATION_JSON_TYPE)
                .get(new GenericType<List<com.krillsson.sysapi.dto.network.NetworkInterface>>(){});
        assertThat(networkInterfaceData.get(0), is(networkInterfaceData));
    }



    @After
    public void tearDown() throws Exception {
        reset(provider);
    }
}
@@ -247,40 +247,8 @@
 est;
-%0Aimport oshi.hardware.NetworkIF;
 %0A%0Aim
@@ -367,61 +367,70 @@
 ava.
-net.NetworkInterface;%0Aimport java.net.SocketException
+util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List
 ;%0Aim
@@ -444,17 +444,24 @@
 va.util.
-*
+Optional
 ;%0A%0Aimpor
@@ -541,51 +541,8 @@
 at;%0A
-import static org.junit.Assert.assertTrue;%0A
 impo
@@ -1965,32 +1965,49 @@
 .getJerseyTest()
+%0A                
 .target(%22/nics/e
@@ -2498,32 +2498,49 @@
 .getJerseyTest()
+%0A                
 .target(%22/nics%22)
@@ -2693,17 +2693,35 @@
 face%3E%3E()
-%7B
+ %7B%0A                
 %7D);%0A    
@@ -2799,17 +2799,16 @@
     %7D%0A%0A%0A
-%0A
     @Aft

Fix unit test