Ray Milkey
Committed by Gerrit Code Review

Add unit tests for cluster messaging classes

Change-Id: I77dd4155956dca77e86e9990c7d4f5de394c0e87
...@@ -24,7 +24,7 @@ import java.util.Objects; ...@@ -24,7 +24,7 @@ import java.util.Objects;
24 * Cluster messages have associated subjects that dictate how they get handled 24 * Cluster messages have associated subjects that dictate how they get handled
25 * on the receiving side. 25 * on the receiving side.
26 */ 26 */
27 -public class MessageSubject { 27 +public final class MessageSubject {
28 28
29 private final String value; 29 private final String value;
30 30
......
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 +}
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 +
19 +import org.junit.Test;
20 +import org.onlab.packet.IpAddress;
21 +
22 +import com.google.common.testing.EqualsTester;
23 +
24 +import static org.hamcrest.MatcherAssert.assertThat;
25 +import static org.hamcrest.Matchers.is;
26 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
27 +
28 +/**
29 + * Unit tests for the Endpoint class.
30 + */
31 +public class EndpointTest {
32 + IpAddress host1 = IpAddress.valueOf("1.2.3.4");
33 + IpAddress host2 = IpAddress.valueOf("1.2.3.5");
34 +
35 + private final Endpoint endpoint1 = new Endpoint(host1, 1);
36 + private final Endpoint sameAsEndpoint1 = new Endpoint(host1, 1);
37 + private final Endpoint endpoint2 = new Endpoint(host2, 1);
38 + private final Endpoint endpoint3 = new Endpoint(host1, 2);
39 +
40 + /**
41 + * Checks that the MessageSubject class is immutable.
42 + */
43 + @Test
44 + public void testImmutability() {
45 + assertThatClassIsImmutable(Endpoint.class);
46 + }
47 +
48 + /**
49 + * Checks the operation of equals(), hashCode() and toString() methods.
50 + */
51 + @Test
52 + public void testEquals() {
53 + new EqualsTester()
54 + .addEqualityGroup(endpoint1, sameAsEndpoint1)
55 + .addEqualityGroup(endpoint2)
56 + .addEqualityGroup(endpoint3)
57 + .testEquals();
58 + }
59 +
60 + /**
61 + * Checks the construction of a MessageSubject object.
62 + */
63 + @Test
64 + public void testConstruction() {
65 + assertThat(endpoint2.host(), is(host2));
66 + assertThat(endpoint2.port(), is(1));
67 + }
68 +}
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 +
20 +import com.google.common.testing.EqualsTester;
21 +
22 +import static org.hamcrest.MatcherAssert.assertThat;
23 +import static org.hamcrest.Matchers.is;
24 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
25 +
26 +/**
27 + * Unit tests for MessageSubject class.
28 + */
29 +public class MessageSubjectTest {
30 + private final MessageSubject subject1 = new MessageSubject("Message 1");
31 + private final MessageSubject sameAsSubject1 = new MessageSubject("Message 1");
32 + private final MessageSubject subject2 = new MessageSubject("Message 2");
33 + private final MessageSubject subject3 = new MessageSubject("Message 3");
34 +
35 + /**
36 + * Checks that the MessageSubject class is immutable.
37 + */
38 + @Test
39 + public void testImmutability() {
40 + assertThatClassIsImmutable(MessageSubject.class);
41 + }
42 +
43 + /**
44 + * Checks the operation of equals(), hashCode() and toString() methods.
45 + */
46 + @Test
47 + public void testEquals() {
48 + new EqualsTester()
49 + .addEqualityGroup(subject1, sameAsSubject1)
50 + .addEqualityGroup(subject2)
51 + .addEqualityGroup(subject3)
52 + .testEquals();
53 + }
54 +
55 + /**
56 + * Checks the construction of a MessageSubject object.
57 + */
58 + @Test
59 + public void testConstruction() {
60 + assertThat(subject3.value(), is("Message 3"));
61 + MessageSubject serializerObject = new MessageSubject();
62 + assertThat(serializerObject.value(), is(""));
63 + }
64 +}