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.DefaultLispMapRegister.RegisterReader; |
| 27 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRegister.RegisterWriter; |
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 DefaultLispMapRegister class. |
| 34 | */ |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 35 | public final class DefaultLispMapRegisterTest { |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 36 | |
| 37 | private LispMapRegister register1; |
| 38 | private LispMapRegister sameAsRegister1; |
| 39 | private LispMapRegister register2; |
| 40 | |
| 41 | @Before |
| 42 | public void setup() { |
| 43 | |
| 44 | LispMapRegister.RegisterBuilder builder1 = |
| 45 | new DefaultLispMapRegister.DefaultRegisterBuilder(); |
| 46 | |
| 47 | register1 = builder1 |
| 48 | .withIsProxyMapReply(true) |
| 49 | .withIsWantMapNotify(false) |
| 50 | .withKeyId((short) 1) |
| 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 | LispMapRegister.RegisterBuilder builder2 = |
| 56 | new DefaultLispMapRegister.DefaultRegisterBuilder(); |
| 57 | |
| 58 | sameAsRegister1 = builder2 |
| 59 | .withIsProxyMapReply(true) |
| 60 | .withIsWantMapNotify(false) |
| 61 | .withKeyId((short) 1) |
| 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 | LispMapRegister.RegisterBuilder builder3 = |
| 67 | new DefaultLispMapRegister.DefaultRegisterBuilder(); |
| 68 | |
| 69 | register2 = builder3 |
| 70 | .withIsProxyMapReply(true) |
| 71 | .withIsWantMapNotify(false) |
| 72 | .withKeyId((short) 2) |
| 73 | .withNonce(2L) |
| 74 | .withRecordCount((byte) 0x02) |
| 75 | .build(); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testEquality() { |
| 80 | new EqualsTester() |
| 81 | .addEqualityGroup(register1, sameAsRegister1) |
| 82 | .addEqualityGroup(register2).testEquals(); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testConstruction() { |
| 87 | DefaultLispMapRegister register = (DefaultLispMapRegister) register1; |
| 88 | |
| 89 | assertThat(register.isProxyMapReply(), is(true)); |
| 90 | assertThat(register.isWantMapNotify(), is(false)); |
| 91 | assertThat(register.getKeyId(), is((short) 1)); |
| 92 | assertThat(register.getNonce(), is(1L)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 93 | assertThat(register.getRecordCount(), is((byte) 0)); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 98 | ByteBuf byteBuf = Unpooled.buffer(); |
| 99 | |
| 100 | RegisterWriter writer = new RegisterWriter(); |
| 101 | writer.writeTo(byteBuf, register1); |
| 102 | |
| 103 | RegisterReader reader = new RegisterReader(); |
| 104 | LispMapRegister deserialized = reader.readFrom(byteBuf); |
| 105 | |
| 106 | new EqualsTester() |
| 107 | .addEqualityGroup(register1, deserialized).testEquals(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 108 | } |
| 109 | } |