Ibft message tests (#367)

Jason Frame 6 years ago committed by GitHub
parent 391122b68e
commit d793a7ecae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 70
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/CommitMessageTest.java
  2. 70
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/NewRoundMessageTest.java
  3. 70
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrePrepareMessageTest.java
  4. 70
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/PrepareMessageTest.java
  5. 70
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/ibftmessage/RoundChangeMessageTest.java

@ -0,0 +1,70 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed 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 tech.pegasys.pantheon.consensus.ibft.ibftmessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.CommitPayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class CommitMessageTest {
@Mock private SignedData<CommitPayload> commitPayload;
@Mock private BytesValue messageBytes;
@Mock private MessageData messageData;
@Mock private CommitMessage commitMessage;
@Test
public void createMessageFromCommitMessageData() {
when(commitPayload.encode()).thenReturn(messageBytes);
CommitMessage commitMessage = CommitMessage.create(commitPayload);
assertThat(commitMessage.getData()).isEqualTo(messageBytes);
assertThat(commitMessage.getCode()).isEqualTo(IbftV2.COMMIT);
verify(commitPayload).encode();
}
@Test
public void createMessageFromCommitMessage() {
CommitMessage message = CommitMessage.fromMessage(commitMessage);
assertThat(message).isSameAs(commitMessage);
}
@Test
public void createMessageFromGenericMessageData() {
when(messageData.getData()).thenReturn(messageBytes);
when(messageData.getCode()).thenReturn(IbftV2.COMMIT);
CommitMessage commitMessage = CommitMessage.fromMessage(messageData);
assertThat(commitMessage.getData()).isEqualTo(messageData.getData());
assertThat(commitMessage.getCode()).isEqualTo(IbftV2.COMMIT);
}
@Test
public void createMessageFailsWhenIncorrectMessageCode() {
when(messageData.getCode()).thenReturn(42);
assertThatThrownBy(() -> CommitMessage.fromMessage(messageData))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Message has code 42 and thus is not a CommitMessage");
}
}

@ -0,0 +1,70 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed 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 tech.pegasys.pantheon.consensus.ibft.ibftmessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.NewRoundPayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class NewRoundMessageTest {
@Mock private SignedData<NewRoundPayload> newRoundPayload;
@Mock private BytesValue messageBytes;
@Mock private MessageData messageData;
@Mock private NewRoundMessage newRoundMessage;
@Test
public void createMessageFromNewRoundChangeMessageData() {
when(newRoundPayload.encode()).thenReturn(messageBytes);
NewRoundMessage prepareMessage = NewRoundMessage.create(newRoundPayload);
assertThat(prepareMessage.getData()).isEqualTo(messageBytes);
assertThat(prepareMessage.getCode()).isEqualTo(IbftV2.NEW_ROUND);
verify(newRoundPayload).encode();
}
@Test
public void createMessageFromNewRoundMessage() {
NewRoundMessage message = NewRoundMessage.fromMessage(newRoundMessage);
assertThat(message).isSameAs(newRoundMessage);
}
@Test
public void createMessageFromGenericMessageData() {
when(messageData.getData()).thenReturn(messageBytes);
when(messageData.getCode()).thenReturn(IbftV2.NEW_ROUND);
NewRoundMessage newRoundMessage = NewRoundMessage.fromMessage(messageData);
assertThat(newRoundMessage.getData()).isEqualTo(messageData.getData());
assertThat(newRoundMessage.getCode()).isEqualTo(IbftV2.NEW_ROUND);
}
@Test
public void createMessageFailsWhenIncorrectMessageCode() {
when(messageData.getCode()).thenReturn(42);
assertThatThrownBy(() -> NewRoundMessage.fromMessage(messageData))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Message has code 42 and thus is not a NewRoundMessage");
}
}

@ -0,0 +1,70 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed 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 tech.pegasys.pantheon.consensus.ibft.ibftmessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.ProposalPayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PrePrepareMessageTest {
@Mock private SignedData<ProposalPayload> prePrepareMessageData;
@Mock private BytesValue messageBytes;
@Mock private MessageData messageData;
@Mock private ProposalMessage proposalMessage;
@Test
public void createMessageFromPrePrepareMessageData() {
when(prePrepareMessageData.encode()).thenReturn(messageBytes);
ProposalMessage proposalMessage = ProposalMessage.create(prePrepareMessageData);
assertThat(proposalMessage.getData()).isEqualTo(messageBytes);
assertThat(proposalMessage.getCode()).isEqualTo(IbftV2.PROPOSAL);
verify(prePrepareMessageData).encode();
}
@Test
public void createMessageFromPrePrepareMessage() {
ProposalMessage message = ProposalMessage.fromMessage(proposalMessage);
assertThat(message).isSameAs(proposalMessage);
}
@Test
public void createMessageFromGenericMessageData() {
when(messageData.getCode()).thenReturn(IbftV2.PROPOSAL);
when(messageData.getData()).thenReturn(messageBytes);
ProposalMessage proposalMessage = ProposalMessage.fromMessage(messageData);
assertThat(proposalMessage.getData()).isEqualTo(messageData.getData());
assertThat(proposalMessage.getCode()).isEqualTo(IbftV2.PROPOSAL);
}
@Test
public void createMessageFailsWhenIncorrectMessageCode() {
when(messageData.getCode()).thenReturn(42);
assertThatThrownBy(() -> ProposalMessage.fromMessage(messageData))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Message has code 42 and thus is not a ProposalMessage");
}
}

@ -0,0 +1,70 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed 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 tech.pegasys.pantheon.consensus.ibft.ibftmessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.PreparePayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PrepareMessageTest {
@Mock private SignedData<PreparePayload> preparePayload;
@Mock private BytesValue messageBytes;
@Mock private MessageData messageData;
@Mock private PrepareMessage prepareMessage;
@Test
public void createMessageFromPrepareMessageData() {
when(preparePayload.encode()).thenReturn(messageBytes);
PrepareMessage prepareMessage = PrepareMessage.create(preparePayload);
assertThat(prepareMessage.getData()).isEqualTo(messageBytes);
assertThat(prepareMessage.getCode()).isEqualTo(IbftV2.PREPARE);
verify(preparePayload).encode();
}
@Test
public void createMessageFromPrepareMessage() {
PrepareMessage message = PrepareMessage.fromMessage(prepareMessage);
assertThat(message).isSameAs(prepareMessage);
}
@Test
public void createMessageFromGenericMessageData() {
when(messageData.getData()).thenReturn(messageBytes);
when(messageData.getCode()).thenReturn(IbftV2.PREPARE);
PrepareMessage prepareMessage = PrepareMessage.fromMessage(messageData);
assertThat(prepareMessage.getData()).isEqualTo(messageData.getData());
assertThat(prepareMessage.getCode()).isEqualTo(IbftV2.PREPARE);
}
@Test
public void createMessageFailsWhenIncorrectMessageCode() {
when(messageData.getCode()).thenReturn(42);
assertThatThrownBy(() -> PrepareMessage.fromMessage(messageData))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Message has code 42 and thus is not a PrepareMessage");
}
}

@ -0,0 +1,70 @@
/*
* Copyright 2018 ConsenSys AG.
*
* Licensed 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 tech.pegasys.pantheon.consensus.ibft.ibftmessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.RoundChangePayload;
import tech.pegasys.pantheon.consensus.ibft.ibftmessagedata.SignedData;
import tech.pegasys.pantheon.ethereum.p2p.api.MessageData;
import tech.pegasys.pantheon.util.bytes.BytesValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class RoundChangeMessageTest {
@Mock private SignedData<RoundChangePayload> roundChangePayload;
@Mock private BytesValue messageBytes;
@Mock private MessageData messageData;
@Mock private RoundChangeMessage roundChangeMessage;
@Test
public void createMessageFromRoundChangeMessageData() {
when(roundChangePayload.encode()).thenReturn(messageBytes);
RoundChangeMessage roundChangeMessage = RoundChangeMessage.create(roundChangePayload);
assertThat(roundChangeMessage.getData()).isEqualTo(messageBytes);
assertThat(roundChangeMessage.getCode()).isEqualTo(IbftV2.ROUND_CHANGE);
verify(roundChangePayload).encode();
}
@Test
public void createMessageFromRoundChangeMessage() {
RoundChangeMessage message = RoundChangeMessage.fromMessage(roundChangeMessage);
assertThat(message).isSameAs(roundChangeMessage);
}
@Test
public void createMessageFromGenericMessageData() {
when(messageData.getData()).thenReturn(messageBytes);
when(messageData.getCode()).thenReturn(IbftV2.ROUND_CHANGE);
RoundChangeMessage roundChangeMessage = RoundChangeMessage.fromMessage(messageData);
assertThat(roundChangeMessage.getData()).isEqualTo(messageData.getData());
assertThat(roundChangeMessage.getCode()).isEqualTo(IbftV2.ROUND_CHANGE);
}
@Test
public void createMessageFailsWhenIncorrectMessageCode() {
when(messageData.getCode()).thenReturn(42);
assertThatThrownBy(() -> RoundChangeMessage.fromMessage(messageData))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Message has code 42 and thus is not a RoundChangeMessage");
}
}
Loading…
Cancel
Save