### Question:
SerializeUtils { public static byte[] serializeRequest(Request request) { if (request == null || request.getHdr() == null) { return null; } byte[] data = new byte[32]; try { data = Util.marshallTxnEntry(request.getHdr(), request.getTxn(), request.getTxnDigest()); } catch (IOException e) { LOG.error("This really should be impossible", e); } return data; }  static TxnLogEntry deserializeTxn(byte[] txnBytes); static void deserializeSnapshot(DataTree dt, InputArchive ia, Map<Long, Integer> sessions); static void serializeSnapshot(DataTree dt, OutputArchive oa, Map<Long, Integer> sessions); static byte[] serializeRequest(Request request);  }### Answer:
@Test public void testSerializeRequestRequestIsNull() { byte[] data = SerializeUtils.serializeRequest(null); assertNull(data); }
@Test public void testSerializeRequestRequestHeaderIsNull() { Request request = new Request(0, 0, 0, null, null, 0); byte[] data = SerializeUtils.serializeRequest(request); assertNull(data); }
@Test public void testSerializeRequestWithoutTxn() throws IOException { TxnHeader header = mock(TxnHeader.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); OutputArchive oa = (OutputArchive) args[0]; oa.writeString("header", "test"); return null; } }).when(header).serialize(any(OutputArchive.class), anyString()); Request request = new Request(1, 2, 3, header, null, 4); byte[] data = SerializeUtils.serializeRequest(request); assertNotNull(data); verify(header).serialize(any(OutputArchive.class), eq("hdr")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); boa.writeString("header", "test"); baos.close(); assertArrayEquals(baos.toByteArray(), data); }
@Test public void testSerializeRequestWithTxn() throws IOException { TxnHeader header = mock(TxnHeader.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); OutputArchive oa = (OutputArchive) args[0]; oa.writeString("header", "test"); return null; } }).when(header).serialize(any(OutputArchive.class), anyString()); Record txn = mock(Record.class); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); OutputArchive oa = (OutputArchive) args[0]; oa.writeString("record", "test"); return null; } }).when(txn).serialize(any(OutputArchive.class), anyString()); Request request = new Request(1, 2, 3, header, txn, 4); byte[] data = SerializeUtils.serializeRequest(request); assertNotNull(data); InOrder inOrder = inOrder(header, txn); inOrder.verify(header).serialize(any(OutputArchive.class), eq("hdr")); inOrder.verify(txn).serialize(any(OutputArchive.class), eq("txn")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); boa.writeString("header", "test"); boa.writeString("record", "test"); baos.close(); assertArrayEquals(baos.toByteArray(), data); }