package pl.grzeslowski.jsupla.protocol.impl.decoders;

import org.junit.Test;
import pl.grzeslowski.jsupla.protocol.api.decoders.ProtoWithSizeDecoder;
import pl.grzeslowski.jsupla.protocol.api.types.ProtoWithSize;

import static pl.grzeslowski.jsupla.protocol.common.RandomSupla.RANDOM_SUPLA;

public abstract class ProperDecoderTest<T extends ProtoWithSize> {
    public abstract ProtoWithSizeDecoder<T> getDecoder();

    public abstract int entitySize();

    @Test
    public void shouldParseEntity() throws Exception {

        // given
        int offset = RANDOM_SUPLA.nextInt(100);
        byte[] bytes = givenParseEntity(offset);

        // when
        final T entity = getDecoder().decode(bytes, offset);

        // then
        verifyParseEntity(entity);
    }

    protected abstract byte[] givenParseEntity(final int offset);

    protected abstract void verifyParseEntity(final T entity);

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowIllegalArgumentExceptionWhenBytesAreTooSmall() throws Exception {

        // given
        final byte[] bytes = new byte[entitySize() - 1];

        // when
        getDecoder().decode(bytes, 0);
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowIllegalArgumentExceptionWhenBytesAreTooSmallAfterAddingOffset() throws Exception {

        // given
        final byte[] bytes = new byte[entitySize()];

        // when
        getDecoder().decode(bytes, 1);
    }
}

@@ -208,16 +208,60 @@
 hSize;%0A%0A
+import static java.util.Arrays.copyOfRange;%0A
 import s
@@ -1133,42 +1133,114 @@
 e%5B%5D 
-bytes = new byte%5BentitySize()
+original = givenParseEntity(0);%0A        final byte%5B%5D bytes = copyOfRange(original, 0, original.length
  - 1
-%5D
+)
 ;%0A%0A 
@@ -1506,38 +1506,114 @@
 e%5B%5D 
-bytes = new byte%5BentitySize()%5D
+original = givenParseEntity(1);%0A        final byte%5B%5D bytes = copyOfRange(original, 0, original.length - 1)
 ;%0A%0A 

Fix for test checking when arryas are too small