PcpMetricInfoV2 extends PcpMetricInfo { @Override public void writeToMmv(ByteBuffer byteBuffer) { byteBuffer.position(offset); byteBuffer.putLong(getStringOffset(nameAsPcpString)); byteBuffer.putInt(getId()); byteBuffer.putInt(typeHandler.getMetricType().getIdentifier()); byteBuffer.putInt(getSemantics().getPcpValue()); byteBuffer.putInt(UnitMapping.getDimensions(getUnit(), metricName)); if (domain != null) { byteBuffer.putInt(domain.getId()); } else { byteBuffer.putInt(DEFAULT_INSTANCE_DOMAIN_ID); } byteBuffer.putInt(0); byteBuffer.putLong(getStringOffset(shortHelpText)); byteBuffer.putLong(getStringOffset(longHelpText)); } PcpMetricInfoV2(String metricName, int id, PcpString nameAsPcpString); @Override void writeToMmv(ByteBuffer byteBuffer); @Override int byteSize();  }
@Test public void writeToMmvWritesACorrectlyStructuredMetricsV2Record() { ByteBuffer byteBuffer = ByteBuffer.allocate(48); TypeHandler<Object> typeHandler = mock(TypeHandler.class); MmvMetricType mmvMetricType = mock(MmvMetricType.class); PcpString shortHelpPcpString = mock(PcpString.class); PcpString longHelpPcpString = mock(PcpString.class); Semantics semantics = mock(Semantics.class); when(nameAsPcpString.getOffset()).thenReturn(STRING_OFFSET); when(typeHandler.getMetricType()).thenReturn(mmvMetricType); when(mmvMetricType.getIdentifier()).thenReturn(METRIC_TYPE_IDENTIFIER); when(shortHelpPcpString.getOffset()).thenReturn(SHORT_HELP_TEXT_OFFSET); when(longHelpPcpString.getOffset()).thenReturn(LONG_HELP_TEXT_OFFSET); when(semantics.getPcpValue()).thenReturn(SEMANTICS_VALUE); pcpMetricInfoV2.setTypeHandler(typeHandler); pcpMetricInfoV2.setHelpText(shortHelpPcpString, longHelpPcpString); pcpMetricInfoV2.setSemantics(semantics); pcpMetricInfoV2.setUnit(HOUR); pcpMetricInfoV2.writeToMmv(byteBuffer); byte[] expectedBytes = { 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 3, -115, 0, 0, 0, 32, 1, 0, 80, 0, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 12, }; assertArrayEquals(expectedBytes, byteBuffer.array()); }
@Test public void writeToMmvShouldSetThePositionOfTheBufferBeforeWriting() { ByteBuffer byteBuffer = mock(ByteBuffer.class); TypeHandler<Object> typeHandler = mock(TypeHandler.class); when(typeHandler.getMetricType()).thenReturn(mock(MmvMetricType.class)); pcpMetricInfoV2.setOffset(THIS_PCP_METRIC_INFO_OFFSET); pcpMetricInfoV2.setTypeHandler(typeHandler); pcpMetricInfoV2.writeToMmv(byteBuffer); InOrder inOrder = Mockito.inOrder(byteBuffer); inOrder.verify(byteBuffer).position(THIS_PCP_METRIC_INFO_OFFSET); inOrder.verify(byteBuffer).putLong(anyLong()); }