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; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 26 | |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.Matchers.is; |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 29 | import static org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.*; |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Unit tests for DefaultLispMapNotify class. |
| 33 | */ |
Jian Li | 4767190 | 2016-08-11 01:18:18 +0900 | [diff] [blame] | 34 | public final class DefaultLispMapNotifyTest { |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 35 | |
| 36 | private LispMapNotify notify1; |
| 37 | private LispMapNotify sameAsNotify1; |
| 38 | private LispMapNotify notify2; |
| 39 | |
| 40 | @Before |
| 41 | public void setup() { |
| 42 | |
| 43 | LispMapNotify.NotifyBuilder builder1 = |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 44 | new DefaultNotifyBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 45 | |
| 46 | notify1 = builder1 |
| 47 | .withKeyId((short) 1) |
| 48 | .withNonce(1L) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 49 | .withRecordCount((byte) 0) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 50 | .build(); |
| 51 | |
| 52 | LispMapNotify.NotifyBuilder builder2 = |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 53 | new DefaultNotifyBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 54 | |
| 55 | sameAsNotify1 = builder2 |
| 56 | .withKeyId((short) 1) |
| 57 | .withNonce(1L) |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 58 | .withRecordCount((byte) 0) |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 59 | .build(); |
| 60 | |
| 61 | LispMapNotify.NotifyBuilder builder3 = |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 62 | new DefaultNotifyBuilder(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 63 | |
| 64 | notify2 = builder3 |
| 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(notify1, sameAsNotify1) |
| 75 | .addEqualityGroup(notify2).testEquals(); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testConstruction() { |
| 80 | DefaultLispMapNotify notify = (DefaultLispMapNotify) notify1; |
| 81 | |
| 82 | assertThat(notify.getKeyId(), is((short) 1)); |
| 83 | assertThat(notify.getNonce(), is(1L)); |
Jian Li | e4ba2a4 | 2016-08-29 20:24:15 +0900 | [diff] [blame^] | 84 | assertThat(notify.getRecordCount(), is((byte) 0)); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testSerialization() throws LispReaderException, LispWriterException, LispParseError { |
| 89 | ByteBuf byteBuf = Unpooled.buffer(); |
| 90 | |
| 91 | NotifyWriter writer = new NotifyWriter(); |
| 92 | writer.writeTo(byteBuf, notify1); |
| 93 | |
| 94 | NotifyReader reader = new NotifyReader(); |
| 95 | LispMapNotify deserialized = reader.readFrom(byteBuf); |
| 96 | |
| 97 | new EqualsTester() |
| 98 | .addEqualityGroup(notify1, deserialized).testEquals(); |
Jian Li | 18f3bce | 2016-08-04 17:36:41 +0900 | [diff] [blame] | 99 | } |
| 100 | } |