Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 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; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 23 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 24 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 26 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyReader; |
| 27 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.ReplyWriter; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 28 | |
| 29 | import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | import static org.hamcrest.Matchers.is; |
| 31 | |
| 32 | /** |
| 33 | * Unit tests for DefaultLispMapReply class. |
| 34 | */ |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 35 | public final class DefaultLispMapReplyTest { |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 36 | |
| 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 Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 52 | .withRecordCount((byte) 0) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 53 | .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 Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 63 | .withRecordCount((byte) 0) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 64 | .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 Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 92 | 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 Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 106 | } |
| 107 | } |