Ray Milkey | 96495ca | 2015-07-07 11:02:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.cluster.messaging; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import org.onosproject.cluster.NodeId; |
| 20 | |
| 21 | import com.google.common.testing.EqualsTester; |
| 22 | |
| 23 | import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | |
| 26 | /** |
| 27 | * Units tests for ClusterMessage class. |
| 28 | */ |
| 29 | public class ClusterMessageTest { |
| 30 | private final MessageSubject subject1 = new MessageSubject("Message 1"); |
| 31 | private final MessageSubject subject2 = new MessageSubject("Message 2"); |
| 32 | |
| 33 | private final byte[] payload1 = {0, 1, 2, 3, 4, 5}; |
| 34 | private final byte[] payload2 = {0, 1, 2, 3, 4, 5, 6}; |
| 35 | |
| 36 | private final NodeId nodeId = new NodeId("node"); |
| 37 | |
| 38 | private final ClusterMessage message1 = |
| 39 | new ClusterMessage(nodeId, subject1, payload1); |
| 40 | private final ClusterMessage sameAsMessage1 = |
| 41 | new ClusterMessage(nodeId, subject1, payload1); |
| 42 | private final ClusterMessage message2 = |
| 43 | new ClusterMessage(nodeId, subject1, payload2); |
| 44 | private final ClusterMessage message3 = |
| 45 | new ClusterMessage(nodeId, subject2, payload1); |
| 46 | |
| 47 | /** |
| 48 | * Checks the operation of equals(), hashCode() and toString() methods. |
| 49 | */ |
| 50 | @Test |
| 51 | public void testEquals() { |
| 52 | new EqualsTester() |
| 53 | .addEqualityGroup(message1, sameAsMessage1) |
| 54 | .addEqualityGroup(message2) |
| 55 | .addEqualityGroup(message3) |
| 56 | .testEquals(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Checks the construction of a FlowId object. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testConstruction() { |
| 64 | assertThat(message1.payload(), is(payload1)); |
| 65 | assertThat(message1.sender(), is(nodeId)); |
| 66 | assertThat(message1.subject(), is(subject1)); |
| 67 | |
| 68 | byte[] response = {2, 2, 2, 2, 2, 2, 2, 2}; |
| 69 | message1.respond(response); |
| 70 | assertThat(message1.response(), is(response)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests the toBytes and fromBytes methods. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testByteMethods() { |
| 78 | byte[] fromBytes = message3.getBytes(); |
| 79 | ClusterMessage message = ClusterMessage.fromBytes(fromBytes); |
| 80 | assertThat(message, is(message3)); |
| 81 | } |
| 82 | } |