blob: 5b3c8991f4d3ac74b0170fce1d7ba5beec6429e3 [file] [log] [blame]
Jian Li18f3bce2016-08-04 17:36:41 +09001/*
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 */
16package org.onosproject.lisp.msg.protocols;
17
18import com.google.common.testing.EqualsTester;
Jian Lie4ba2a42016-08-29 20:24:15 +090019import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Jian Li18f3bce2016-08-04 17:36:41 +090021import org.junit.Before;
22import org.junit.Test;
Jian Lie4ba2a42016-08-29 20:24:15 +090023import org.onosproject.lisp.msg.exceptions.LispParseError;
24import org.onosproject.lisp.msg.exceptions.LispReaderException;
25import org.onosproject.lisp.msg.exceptions.LispWriterException;
26import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyReader;
27import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyWriter;
Jian Li18f3bce2016-08-04 17:36:41 +090028
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.is;
31
32/**
33 * Unit tests for DefaultLispMapReply class.
34 */
Jian Li47671902016-08-11 01:18:18 +090035public final class DefaultLispMapReplyTest {
Jian Li18f3bce2016-08-04 17:36:41 +090036
37 private LispMapReply reply1;
38 private LispMapReply sameAsReply1;
39 private LispMapReply reply2;
40
41 @Before
42 public void setup() {
43
44 LispMapReply.ReplyBuilder builder1 =
45 new DefaultLispMapReply.DefaultReplyBuilder();
46
47 reply1 = builder1
48 .withIsEtr(true)
49 .withIsProbe(false)
50 .withIsSecurity(true)
51 .withNonce(1L)
Jian Lie4ba2a42016-08-29 20:24:15 +090052 .withRecordCount((byte) 0)
Jian Li18f3bce2016-08-04 17:36:41 +090053 .build();
54
55 LispMapReply.ReplyBuilder builder2 =
56 new DefaultLispMapReply.DefaultReplyBuilder();
57
58 sameAsReply1 = builder2
59 .withIsEtr(true)
60 .withIsProbe(false)
61 .withIsSecurity(true)
62 .withNonce(1L)
Jian Lie4ba2a42016-08-29 20:24:15 +090063 .withRecordCount((byte) 0)
Jian Li18f3bce2016-08-04 17:36:41 +090064 .build();
65
66 LispMapReply.ReplyBuilder builder3 =
67 new DefaultLispMapReply.DefaultReplyBuilder();
68 reply2 = builder3
69 .withIsEtr(false)
70 .withIsProbe(true)
71 .withIsSecurity(false)
72 .withNonce(2L)
73 .withRecordCount((byte) 0x02)
74 .build();
75 }
76
77 @Test
78 public void testEquality() {
79 new EqualsTester()
80 .addEqualityGroup(reply1, sameAsReply1)
81 .addEqualityGroup(reply2).testEquals();
82 }
83
84 @Test
85 public void testConstruction() {
86 DefaultLispMapReply reply = (DefaultLispMapReply) reply1;
87
88 assertThat(reply.isEtr(), is(true));
89 assertThat(reply.isProbe(), is(false));
90 assertThat(reply.isSecurity(), is(true));
91 assertThat(reply.getNonce(), is(1L));
Jian Lie4ba2a42016-08-29 20:24:15 +090092 assertThat(reply.getRecordCount(), is((byte) 0));
93 }
94
95 @Test
96 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
97 ByteBuf byteBuf = Unpooled.buffer();
98 ReplyWriter writer = new ReplyWriter();
99 writer.writeTo(byteBuf, reply1);
100
101 ReplyReader reader = new ReplyReader();
102 LispMapReply deserialized = reader.readFrom(byteBuf);
103
104 new EqualsTester()
105 .addEqualityGroup(reply1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900106 }
107}