PartialFetchBodyElement implements BodyElement { @Override public long size() throws IOException { final long size = delegate.size(); final long lastOctet = this.numberOfOctets + firstOctet; if (firstOctet > size) { return 0; } else if (size > lastOctet) { return numberOfOctets; } else { return size - firstOctet; } } PartialFetchBodyElement(BodyElement delegate, long firstOctet, long numberOfOctets); @Override String getName(); @Override long size(); @Override InputStream getInputStream();  }
@Test public void testSizeShouldBeNumberOfOctetsWhenSizeMoreWhenStartIsZero() throws Exception { final long moreThanNumberOfOctets = NUMBER_OF_OCTETS + 1; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 0, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(moreThanNumberOfOctets); assertThat(element.size()).describedAs("Size is more than number of octets so should be number of octets").isEqualTo(NUMBER_OF_OCTETS); }
@Test public void testSizeShouldBeSizeWhenNumberOfOctetsMoreWhenStartIsZero() throws Exception { final long lessThanNumberOfOctets = NUMBER_OF_OCTETS - 1; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 0, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(lessThanNumberOfOctets); assertThat(element.size()).describedAs("Size is less than number of octets so should be size").isEqualTo(lessThanNumberOfOctets); }
@Test public void testWhenStartPlusNumberOfOctetsIsMoreThanSizeSizeShouldBeSizeMinusStart() throws Exception { final long size = 60; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 10, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(size); assertThat(element.size()).describedAs("Size is less than number of octets so should be size").isEqualTo(50); }
@Test public void testWhenStartPlusNumberOfOctetsIsLessThanSizeSizeShouldBeNumberOfOctetsMinusStart() throws Exception { final long size = 100; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 10, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(size); assertThat(element.size()).describedAs("Size is less than number of octets so should be size").isEqualTo(90); }
@Test public void testSizeShouldBeZeroWhenStartIsMoreThanSize() throws Exception { final long size = 100; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 1000, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(size); assertThat(element.size()).describedAs("Size is less than number of octets so should be size").isEqualTo(0); }
@Test public void testSizeShouldBeNumberOfOctetsWhenStartMoreThanOctets() throws Exception { final long size = 2000; PartialFetchBodyElement element = new PartialFetchBodyElement(mockBodyElement, 1000, NUMBER_OF_OCTETS); when(mockBodyElement.size()).thenReturn(size); assertThat(element.size()).describedAs("Content size is less than start. Size should be zero.").isEqualTo(NUMBER_OF_OCTETS); }