@Test public void shouldPositionTheByteBufferBeforeWriting() { InstanceV2 instanceV2 = new InstanceV2(mock(InstanceDomain.class), INSTANCE_NAME, INSTANCE_DOMAIN_ID, mock(PcpString.class)); ByteBuffer byteBuffer = mock(ByteBuffer.class); instanceV2.setOffset(MY_OFFSET); instanceV2.writeToMmv(byteBuffer); InOrder inOrder = Mockito.inOrder(byteBuffer); inOrder.verify(byteBuffer).position(MY_OFFSET); inOrder.verify(byteBuffer).putLong(anyLong()); }
@Test public void writeToMmvShouldWriteTheCorrectContentToTheByteBuffer() { InstanceDomain instanceDomain = mock(InstanceDomain.class); PcpString pcpString = mock(PcpString.class); ByteBuffer byteBuffer = ByteBuffer.allocate(EXPECTED_BYTE_SIZE); InstanceV2 instanceV2 = new InstanceV2(instanceDomain, INSTANCE_NAME, INSTANCE_DOMAIN_ID, pcpString); when(instanceDomain.getOffset()).thenReturn(INSTANCE_DOMAIN_OFFSET); when(pcpString.getOffset()).thenReturn(STRING_OFFSET); instanceV2.writeToMmv(byteBuffer); /* * As defined in mmv(5), this is the format of the v2 Instance data structure. * * |-------------------------------------| * | 4 bytes | 4 bytes | * |-------------------------------------| * | indom section offset | * |-------------------------------------| * | padding | external inst id | * |-------------------------------------| * | external inst name string offset | * |-------------------------------------| */ byte[] expected = { 0, 0, 0, 0, 0, 0, 0, 40, /* indom section offset */ 0, 0, 0, 0, 0, 0, 0, 123, /* padding, external inst id */ 0, 0, 0, 0, 0, 0, 0, 30, /* external inst name string offset */ }; assertArrayEquals(expected, byteBuffer.array()); }