package uk.ac.ebi.atlas.commons.streams;

import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ObjectInputStreamsTest {


    ObjectInputStream<Integer> oneTwoThreeNull(){
        ObjectInputStream<Integer> inputStream = (ObjectInputStream<Integer>) mock(ObjectInputStream.class);
        when(inputStream.readNext())
                .thenReturn(1, 2, 3, null);
        return inputStream;
    }

    @Test
    public void alwaysTrue(){
        ObjectInputStream<Integer> out = ObjectInputStreams.filter(oneTwoThreeNull(), x -> true);
        assertThat(out.readNext(), is(1));
        assertThat(out.readNext(), is(2));
        assertThat(out.readNext(), is(3));
        assertNull(out.readNext());
    }

    @Test
    public void alwaysFalse(){
        ObjectInputStream<Integer> out = ObjectInputStreams.filter(oneTwoThreeNull(), x -> true);
        assertNull(out.readNext());
    }

    void keepOnlyOne(final Integer toKeep){
        ObjectInputStream<Integer> out = ObjectInputStreams.filter(oneTwoThreeNull(), x -> true);
        assertThat(out.readNext(), is(toKeep));
        assertNull(out.readNext());
    }

    @Test
    public void keepOne(){
        keepOnlyOne(1);
        keepOnlyOne(2);
        keepOnlyOne(3);
    }
}
@@ -52,20 +52,196 @@
 g.junit.
-Test
+Before;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.mockito.Mock;%0Aimport org.mockito.junit.MockitoJUnitRunner;%0A%0Aimport java.util.concurrent.ThreadLocalRandom
 ;%0A%0Aimpor
@@ -297,31 +297,35 @@
 org.
-junit.Assert.assertNull
+hamcrest.Matchers.nullValue
 ;%0Aim
@@ -369,48 +369,8 @@
 at;%0A
-import static org.mockito.Mockito.mock;%0A
 impo
@@ -406,16 +406,51 @@
 .when;%0A%0A
+@RunWith(MockitoJUnitRunner.class)%0A
 public c
@@ -480,16 +480,25 @@
 Test %7B%0A%0A
+    @Mock
 %0A    Obj
@@ -525,135 +525,179 @@
 er%3E 
-oneTwoThreeNull()%7B%0A        ObjectInputStream%3CInteger%3E inputStream = (ObjectInputStream%3CInteger%3E) mock(ObjectInputStream.class);
+inputStreamMock;%0A%0A    private ObjectInputStream%3CInteger%3E oneTwoThreeNull() %7B%0A        return inputStreamMock;%0A    %7D%0A%0A    @Before%0A    public void setUp() throws Exception %7B%0A
 %0A   
@@ -717,16 +717,20 @@
 utStream
+Mock
 .readNex
@@ -737,25 +737,8 @@
 t())
-%0A                
 .the
@@ -765,36 +765,8 @@
 l);%0A
-        return inputStream;%0A
     
@@ -1041,36 +1041,36 @@
 ;%0A        assert
-Null
+That
 (out.readNext())
@@ -1060,32 +1060,49 @@
 t(out.readNext()
+, is(nullValue())
 );%0A    %7D%0A%0A    @T
@@ -1219,35 +1219,36 @@
 reeNull(), x -%3E 
-tru
+fals
 e);%0A        asse
@@ -1241,36 +1241,36 @@
 ;%0A        assert
-Null
+That
 (out.readNext())
@@ -1272,61 +1272,135 @@
 xt()
-);%0A    %7D%0A%0A    void keepOnlyOne(final Integer toKeep)%7B
+, is(nullValue()));%0A    %7D%0A%0A    @Test%0A    public void keepOne()%7B%0A        int toKeep = ThreadLocalRandom.current().nextInt(1, 4);
 %0A   
@@ -1479,36 +1479,48 @@
 reeNull(), x -%3E 
-true
+x.equals(toKeep)
 );%0A        asser
@@ -1568,20 +1568,20 @@
   assert
-Null
+That
 (out.rea
@@ -1591,124 +1591,25 @@
 xt()
-);%0A    %7D%0A%0A    @Test%0A    public void keepOne()%7B%0A        keepOnlyOne(1);%0A        keepOnlyOne(2);%0A        keepOnlyOne(3
+, is(nullValue())
 );%0A 

Remove warning, randomise test keepOne