Github user afine commented on a diff in the pull request: https://github.com/apache/zookeeper/pull/415#discussion_r149832062 --- Diff: src/java/test/org/apache/zookeeper/server/util/SerializeUtilsTest.java --- @@ -0,0 +1,73 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zookeeper.server.util; + +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Test; +import org.mockito.InOrder; + +import java.io.IOException; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class SerializeUtilsTest { + + @Test + public void testSerializeRequest_RequestIsNull() { + byte[] data = SerializeUtils.serializeRequest(null); + assertNull(data); + } + + @Test + public void testSerializeRequest_RequestHeaderIsNull() { + Request request = new Request(0, 0, 0, null, null, 0); + byte[] data = SerializeUtils.serializeRequest(request); + assertNull(data); + } + + @Test + public void testSerializeRequest_WithoutTxn() throws IOException { + TxnHeader header = mock(TxnHeader.class); + 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")); + } + + @Test + public void testSerializeRequest_WithTxn() throws IOException { + Record txn = mock(Record.class); + TxnHeader header = mock(TxnHeader.class); + Request request = new Request(1, 2, 3, header, txn, 4); + byte[] data = SerializeUtils.serializeRequest(request); + assertNotNull(data); + InOrder inOrder = inOrder(header, txn); --- End diff -- I'm concerned that we do not actually check that the result of serializing the header and transaction ever make it into `data`. Would it be possible to write this test to mock out the serialization of the `header` and `txn` then make sure we get the `data` we expect?
---