ProtoUtils { public static Entry convert(RaftEntry.Entry input) { return new Entry(input.getCommand().toByteArray(), input.getTerm()); }  static Entry convert(RaftEntry.Entry input); static RaftEntry.Entry convert(Entry input); static RequestVote convert(RaftProto.RequestVote request); static RaftProto.RequestVoteResponse convert(RequestVoteResponse requestVoteResponse); static RequestVoteResponse convert(RaftProto.RequestVoteResponse requestVoteResponse); static AppendEntries convert(RaftProto.AppendEntries request); static RaftProto.AppendEntries convert(AppendEntries request); static RaftProto.AppendEntriesResponse convert(AppendEntriesResponse response); static AppendEntriesResponse convert(RaftProto.AppendEntriesResponse response); static RaftProto.RequestVote convert(RequestVote request); static Function<? super RaftProto.AppendEntriesResponse, ? extends AppendEntriesResponse> convertAppendResponse; static Function<? super RaftProto.RequestVoteResponse, ? extends RequestVoteResponse> convertVoteResponse; }
@Test public void canConvertRequestVoteMessageToAndFromProtobuf() throws Exception { RequestVote pojo = new RequestVote(12, "foo", 43, 44); RaftProto.RequestVote proto = RaftProto.RequestVote.newBuilder() .setTerm(12) .setCandidateId("foo") .setLastLogIndex(43) .setLastLogTerm(44) .build(); assertThat(ProtoUtils.convert(pojo)).isEqualTo(proto); assertThat(ProtoUtils.convert(proto)).isEqualTo(pojo); }
@Test public void canConvertRequestVoteResponseMessageToAndFromProtobuf() throws Exception { RequestVoteResponse pojo = new RequestVoteResponse(12, true); RaftProto.RequestVoteResponse proto = RaftProto.RequestVoteResponse.newBuilder() .setTerm(12) .setVoteGranted(true) .build(); assertThat(ProtoUtils.convert(pojo)).isEqualTo(proto); assertThat(ProtoUtils.convert(proto)).isEqualTo(pojo); }
@Test public void canConvertAppendEntriesToAndFromProtobuf() throws Exception { List<Entry> entries = Lists.newArrayList(new Entry("bar".getBytes(), 12), new Entry("baz".getBytes(), 12)); AppendEntries pojo = new AppendEntries(12, "foo", 13, 14, 15, entries); RaftProto.AppendEntries proto = RaftProto.AppendEntries.newBuilder() .setTerm(12) .setLeaderId("foo") .setPrevLogIndex(13) .setPrevLogTerm(14) .setCommitIndex(15) .addEntries(RaftEntry.Entry.newBuilder() .setTerm(12) .setCommand(ByteString.copyFromUtf8("bar")) .build()) .addEntries(RaftEntry.Entry.newBuilder() .setTerm(12) .setCommand(ByteString.copyFromUtf8("baz")) .build()) .build(); assertThat(ProtoUtils.convert(pojo)).isEqualTo(proto); assertThat(ProtoUtils.convert(proto)).isEqualTo(pojo); }
@Test public void canConvertAppendEntriesResponseToAndFromProtobuf() throws Exception { AppendEntriesResponse pojo = new AppendEntriesResponse(12,true,43); RaftProto.AppendEntriesResponse proto = RaftProto.AppendEntriesResponse.newBuilder() .setTerm(12) .setLastLogIndex(43) .setSuccess(true) .build(); assertThat(ProtoUtils.convert(pojo)).isEqualTo(proto); assertThat(ProtoUtils.convert(proto)).isEqualTo(pojo); }