Jian Li
Committed by Gerrit Code Review

Add unit test for LISP msg to check object equality and construction

Change-Id: I5365f7654df10f4cf157c79f098c7ce26c29a300
...@@ -15,8 +15,11 @@ ...@@ -15,8 +15,11 @@
15 */ 15 */
16 package org.onosproject.lisp.msg.protocols; 16 package org.onosproject.lisp.msg.protocols;
17 17
18 +import com.google.common.base.Objects;
18 import org.onosproject.lisp.msg.types.LispAfiAddress; 19 import org.onosproject.lisp.msg.types.LispAfiAddress;
19 20
21 +import static com.google.common.base.MoreObjects.toStringHelper;
22 +
20 /** 23 /**
21 * Default implementation of LispMapRecord. 24 * Default implementation of LispMapRecord.
22 */ 25 */
...@@ -88,6 +91,42 @@ public final class DefaultLispMapRecord implements LispMapRecord { ...@@ -88,6 +91,42 @@ public final class DefaultLispMapRecord implements LispMapRecord {
88 return eidPrefixAfi; 91 return eidPrefixAfi;
89 } 92 }
90 93
94 + @Override
95 + public String toString() {
96 + return toStringHelper(this)
97 + .add("record TTL", recordTtl)
98 + .add("locatorCount", locatorCount)
99 + .add("maskLength", maskLength)
100 + .add("action", action)
101 + .add("authoritative", authoritative)
102 + .add("mapVersionNumber", mapVersionNumber)
103 + .add("EID prefix AFI address", eidPrefixAfi).toString();
104 + }
105 +
106 + @Override
107 + public boolean equals(Object o) {
108 + if (this == o) {
109 + return true;
110 + }
111 + if (o == null || getClass() != o.getClass()) {
112 + return false;
113 + }
114 + DefaultLispMapRecord that = (DefaultLispMapRecord) o;
115 + return Objects.equal(recordTtl, that.recordTtl) &&
116 + Objects.equal(locatorCount, that.locatorCount) &&
117 + Objects.equal(maskLength, that.maskLength) &&
118 + Objects.equal(action, that.action) &&
119 + Objects.equal(authoritative, that.authoritative) &&
120 + Objects.equal(mapVersionNumber, that.mapVersionNumber) &&
121 + Objects.equal(eidPrefixAfi, that.eidPrefixAfi);
122 + }
123 +
124 + @Override
125 + public int hashCode() {
126 + return Objects.hashCode(recordTtl, locatorCount, maskLength, action,
127 + authoritative, mapVersionNumber, eidPrefixAfi);
128 + }
129 +
91 public static final class DefaultMapRecordBuilder implements MapRecordBuilder { 130 public static final class DefaultMapRecordBuilder implements MapRecordBuilder {
92 131
93 private int recordTtl; 132 private int recordTtl;
......
1 +/*
2 + * Copyright 2016-present 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.lisp.msg.protocols;
17 +
18 +import com.google.common.testing.EqualsTester;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.hamcrest.MatcherAssert.assertThat;
23 +import static org.hamcrest.Matchers.is;
24 +
25 +/**
26 + * Unit tests for DefaultLispMapNotify class.
27 + */
28 +public class DefaultLispMapNotifyTest {
29 +
30 + private LispMapNotify notify1;
31 + private LispMapNotify sameAsNotify1;
32 + private LispMapNotify notify2;
33 +
34 + @Before
35 + public void setup() {
36 +
37 + LispMapNotify.NotifyBuilder builder1 =
38 + new DefaultLispMapNotify.DefaultNotifyBuilder();
39 +
40 + notify1 = builder1
41 + .withKeyId((short) 1)
42 + .withNonce(1L)
43 + .withRecordCount((byte) 0x01)
44 + .build();
45 +
46 + LispMapNotify.NotifyBuilder builder2 =
47 + new DefaultLispMapNotify.DefaultNotifyBuilder();
48 +
49 + sameAsNotify1 = builder2
50 + .withKeyId((short) 1)
51 + .withNonce(1L)
52 + .withRecordCount((byte) 0x01)
53 + .build();
54 +
55 + LispMapNotify.NotifyBuilder builder3 =
56 + new DefaultLispMapNotify.DefaultNotifyBuilder();
57 +
58 + notify2 = builder3
59 + .withKeyId((short) 2)
60 + .withNonce(2L)
61 + .withRecordCount((byte) 0x02)
62 + .build();
63 + }
64 +
65 + @Test
66 + public void testEquality() {
67 + new EqualsTester()
68 + .addEqualityGroup(notify1, sameAsNotify1)
69 + .addEqualityGroup(notify2).testEquals();
70 + }
71 +
72 + @Test
73 + public void testConstruction() {
74 + DefaultLispMapNotify notify = (DefaultLispMapNotify) notify1;
75 +
76 + assertThat(notify.getKeyId(), is((short) 1));
77 + assertThat(notify.getNonce(), is(1L));
78 + assertThat(notify.getRecordCount(), is((byte) 0x01));
79 + }
80 +}
1 +/*
2 + * Copyright 2016-present 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.lisp.msg.protocols;
17 +
18 +import com.google.common.testing.EqualsTester;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.hamcrest.MatcherAssert.assertThat;
23 +import static org.hamcrest.Matchers.is;
24 +
25 +/**
26 + * Unit tests for DefaultLispMapRecord class.
27 + */
28 +public class DefaultLispMapRecordTest {
29 +
30 + private LispMapRecord record1;
31 + private LispMapRecord sameAsRecord1;
32 + private LispMapRecord record2;
33 +
34 + @Before
35 + public void setup() {
36 +
37 + LispMapRecord.MapRecordBuilder builder1 =
38 + new DefaultLispMapRecord.DefaultMapRecordBuilder();
39 +
40 + record1 = builder1
41 + .withRecordTtl(100)
42 + .withAuthoritative(true)
43 + .withLocatorCount(100)
44 + .withMapVersionNumber((short) 1)
45 + .withMaskLength((byte) 0x01)
46 + .build();
47 +
48 + LispMapRecord.MapRecordBuilder builder2 =
49 + new DefaultLispMapRecord.DefaultMapRecordBuilder();
50 +
51 + sameAsRecord1 = builder2
52 + .withRecordTtl(100)
53 + .withAuthoritative(true)
54 + .withLocatorCount(100)
55 + .withMapVersionNumber((short) 1)
56 + .withMaskLength((byte) 0x01)
57 + .build();
58 +
59 + LispMapRecord.MapRecordBuilder builder3 =
60 + new DefaultLispMapRecord.DefaultMapRecordBuilder();
61 +
62 + record2 = builder3
63 + .withRecordTtl(200)
64 + .withAuthoritative(false)
65 + .withLocatorCount(200)
66 + .withMapVersionNumber((short) 2)
67 + .withMaskLength((byte) 0x02)
68 + .build();
69 + }
70 +
71 + @Test
72 + public void testEquality() {
73 + new EqualsTester()
74 + .addEqualityGroup(record1, sameAsRecord1)
75 + .addEqualityGroup(record2).testEquals();
76 + }
77 +
78 + @Test
79 + public void testConstruction() {
80 + DefaultLispMapRecord record = (DefaultLispMapRecord) record1;
81 +
82 + assertThat(record.getRecordTtl(), is(100));
83 + assertThat(record.isAuthoritative(), is(true));
84 + assertThat(record.getLocatorCount(), is(100));
85 + assertThat(record.getMapVersionNumber(), is((short) 1));
86 + assertThat(record.getMaskLength(), is((byte) 0x01));
87 + }
88 +}
1 +/*
2 + * Copyright 2016-present 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.lisp.msg.protocols;
17 +
18 +import com.google.common.testing.EqualsTester;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.hamcrest.MatcherAssert.assertThat;
23 +import static org.hamcrest.Matchers.is;
24 +
25 +/**
26 + * Unit tests for DefaultLispMapRegister class.
27 + */
28 +public class DefaultLispMapRegisterTest {
29 +
30 + private LispMapRegister register1;
31 + private LispMapRegister sameAsRegister1;
32 + private LispMapRegister register2;
33 +
34 + @Before
35 + public void setup() {
36 +
37 + LispMapRegister.RegisterBuilder builder1 =
38 + new DefaultLispMapRegister.DefaultRegisterBuilder();
39 +
40 + register1 = builder1
41 + .withIsProxyMapReply(true)
42 + .withIsWantMapNotify(false)
43 + .withKeyId((short) 1)
44 + .withNonce(1L)
45 + .withRecordCount((byte) 0x01)
46 + .build();
47 +
48 + LispMapRegister.RegisterBuilder builder2 =
49 + new DefaultLispMapRegister.DefaultRegisterBuilder();
50 +
51 + sameAsRegister1 = builder2
52 + .withIsProxyMapReply(true)
53 + .withIsWantMapNotify(false)
54 + .withKeyId((short) 1)
55 + .withNonce(1L)
56 + .withRecordCount((byte) 0x01)
57 + .build();
58 +
59 + LispMapRegister.RegisterBuilder builder3 =
60 + new DefaultLispMapRegister.DefaultRegisterBuilder();
61 +
62 + register2 = builder3
63 + .withIsProxyMapReply(true)
64 + .withIsWantMapNotify(false)
65 + .withKeyId((short) 2)
66 + .withNonce(2L)
67 + .withRecordCount((byte) 0x02)
68 + .build();
69 + }
70 +
71 + @Test
72 + public void testEquality() {
73 + new EqualsTester()
74 + .addEqualityGroup(register1, sameAsRegister1)
75 + .addEqualityGroup(register2).testEquals();
76 + }
77 +
78 + @Test
79 + public void testConstruction() {
80 + DefaultLispMapRegister register = (DefaultLispMapRegister) register1;
81 +
82 + assertThat(register.isProxyMapReply(), is(true));
83 + assertThat(register.isWantMapNotify(), is(false));
84 + assertThat(register.getKeyId(), is((short) 1));
85 + assertThat(register.getNonce(), is(1L));
86 + assertThat(register.getRecordCount(), is((byte) 0x01));
87 + }
88 +}
1 +/*
2 + * Copyright 2016-present 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.lisp.msg.protocols;
17 +
18 +import com.google.common.testing.EqualsTester;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.hamcrest.MatcherAssert.assertThat;
23 +import static org.hamcrest.Matchers.is;
24 +
25 +/**
26 + * Unit tests for DefaultLispMapReply class.
27 + */
28 +public class DefaultLispMapReplyTest {
29 +
30 + private LispMapReply reply1;
31 + private LispMapReply sameAsReply1;
32 + private LispMapReply reply2;
33 +
34 + @Before
35 + public void setup() {
36 +
37 + LispMapReply.ReplyBuilder builder1 =
38 + new DefaultLispMapReply.DefaultReplyBuilder();
39 +
40 + reply1 = builder1
41 + .withIsEtr(true)
42 + .withIsProbe(false)
43 + .withIsSecurity(true)
44 + .withNonce(1L)
45 + .withRecordCount((byte) 0x01)
46 + .build();
47 +
48 + LispMapReply.ReplyBuilder builder2 =
49 + new DefaultLispMapReply.DefaultReplyBuilder();
50 +
51 + sameAsReply1 = builder2
52 + .withIsEtr(true)
53 + .withIsProbe(false)
54 + .withIsSecurity(true)
55 + .withNonce(1L)
56 + .withRecordCount((byte) 0x01)
57 + .build();
58 +
59 + LispMapReply.ReplyBuilder builder3 =
60 + new DefaultLispMapReply.DefaultReplyBuilder();
61 + reply2 = builder3
62 + .withIsEtr(false)
63 + .withIsProbe(true)
64 + .withIsSecurity(false)
65 + .withNonce(2L)
66 + .withRecordCount((byte) 0x02)
67 + .build();
68 + }
69 +
70 + @Test
71 + public void testEquality() {
72 + new EqualsTester()
73 + .addEqualityGroup(reply1, sameAsReply1)
74 + .addEqualityGroup(reply2).testEquals();
75 + }
76 +
77 + @Test
78 + public void testConstruction() {
79 + DefaultLispMapReply reply = (DefaultLispMapReply) reply1;
80 +
81 + assertThat(reply.isEtr(), is(true));
82 + assertThat(reply.isProbe(), is(false));
83 + assertThat(reply.isSecurity(), is(true));
84 + assertThat(reply.getNonce(), is(1L));
85 + assertThat(reply.getRecordCount(), is((byte) 0x01));
86 + }
87 +}
1 +/*
2 + * Copyright 2016-present 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.lisp.msg.protocols;
17 +
18 +import com.google.common.testing.EqualsTester;
19 +import org.junit.Before;
20 +import org.junit.Test;
21 +
22 +import static org.hamcrest.MatcherAssert.assertThat;
23 +import static org.hamcrest.Matchers.is;
24 +
25 +/**
26 + * Unit tests for DefaultLispMapRequest class.
27 + */
28 +public class DefaultLispMapRequestTest {
29 +
30 + private LispMapRequest request1;
31 + private LispMapRequest sameAsRequest1;
32 + private LispMapRequest request2;
33 +
34 + @Before
35 + public void setup() {
36 +
37 + LispMapRequest.RequestBuilder builder1 =
38 + new DefaultLispMapRequest.DefaultRequestBuilder();
39 +
40 + request1 = builder1
41 + .withIsAuthoritative(true)
42 + .withIsMapDataPresent(true)
43 + .withIsPitr(false)
44 + .withIsProbe(false)
45 + .withIsSmr(true)
46 + .withIsSmrInvoked(false)
47 + .withNonce(1L)
48 + .withRecordCount((byte) 0x01)
49 + .build();
50 +
51 + LispMapRequest.RequestBuilder builder2 =
52 + new DefaultLispMapRequest.DefaultRequestBuilder();
53 +
54 + sameAsRequest1 = builder2
55 + .withIsAuthoritative(true)
56 + .withIsMapDataPresent(true)
57 + .withIsPitr(false)
58 + .withIsProbe(false)
59 + .withIsSmr(true)
60 + .withIsSmrInvoked(false)
61 + .withNonce(1L)
62 + .withRecordCount((byte) 0x01)
63 + .build();
64 +
65 + LispMapRequest.RequestBuilder builder3 =
66 + new DefaultLispMapRequest.DefaultRequestBuilder();
67 +
68 + request2 = builder3
69 + .withIsAuthoritative(false)
70 + .withIsMapDataPresent(false)
71 + .withIsPitr(true)
72 + .withIsProbe(true)
73 + .withIsSmr(false)
74 + .withIsSmrInvoked(true)
75 + .withNonce(2L)
76 + .withRecordCount((byte) 0x02)
77 + .build();
78 + }
79 +
80 + @Test
81 + public void testEquality() {
82 + new EqualsTester()
83 + .addEqualityGroup(request1, sameAsRequest1)
84 + .addEqualityGroup(request2).testEquals();
85 + }
86 +
87 + @Test
88 + public void testConstruction() {
89 + DefaultLispMapRequest request = (DefaultLispMapRequest) request1;
90 +
91 + assertThat(request.isAuthoritative(), is(true));
92 + assertThat(request.isMapDataPresent(), is(true));
93 + assertThat(request.isPitr(), is(false));
94 + assertThat(request.isProbe(), is(false));
95 + assertThat(request.isSmr(), is(true));
96 + assertThat(request.isSmrInvoked(), is(false));
97 + assertThat(request.getNonce(), is(1L));
98 + assertThat(request.getRecordCount(), is((byte) 0x01));
99 + }
100 +}