Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.ospf.protocol.lsa.tlvtypes; |
| 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.buffer.ChannelBuffers; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onosproject.ospf.protocol.lsa.TlvHeader; |
| 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.*; |
| 28 | |
| 29 | /** |
| 30 | * Unit test class for RouterTlv. |
| 31 | */ |
| 32 | public class RouterTlvTest { |
| 33 | |
| 34 | private final byte[] packet = {1, 1, 1, 1}; |
| 35 | private final byte[] packet1 = {1, 1, 1}; |
| 36 | private RouterTlv rtlv; |
| 37 | private TlvHeader header; |
| 38 | private ChannelBuffer channelBuffer; |
| 39 | private byte[] result; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() throws Exception { |
| 43 | rtlv = new RouterTlv(new TlvHeader()); |
| 44 | } |
| 45 | |
| 46 | @After |
| 47 | public void tearDown() throws Exception { |
| 48 | rtlv = null; |
| 49 | header = null; |
| 50 | channelBuffer = null; |
| 51 | result = null; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests routerAddress() getter method. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testGetRouterAddress() throws Exception { |
| 59 | rtlv.setRouterAddress(Ip4Address.valueOf("1.1.1.1")); |
| 60 | assertThat(rtlv.routerAddress(), is(Ip4Address.valueOf("1.1.1.1"))); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Tests routerAddress() setter method. |
| 65 | */ |
| 66 | @Test |
| 67 | public void testSetRouterAddress() throws Exception { |
| 68 | rtlv.setRouterAddress(Ip4Address.valueOf("1.1.1.1")); |
| 69 | assertThat(rtlv.routerAddress(), is(Ip4Address.valueOf("1.1.1.1"))); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Tests readFrom() method. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testReadFrom() throws Exception { |
| 77 | header = new TlvHeader(); |
| 78 | header.setTlvType(1); |
| 79 | header.setTlvLength(4); |
| 80 | rtlv = new RouterTlv(header); |
| 81 | channelBuffer = ChannelBuffers.copiedBuffer(packet); |
| 82 | rtlv.readFrom(channelBuffer); |
| 83 | assertThat(rtlv, is(notNullValue())); |
| 84 | assertThat(rtlv, instanceOf(RouterTlv.class)); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests readFrom() method. |
| 89 | */ |
| 90 | @Test(expected = Exception.class) |
| 91 | public void testReadFrom1() throws Exception { |
| 92 | header = new TlvHeader(); |
| 93 | header.setTlvType(1); |
| 94 | header.setTlvLength(4); |
| 95 | rtlv = new RouterTlv(header); |
| 96 | channelBuffer = ChannelBuffers.copiedBuffer(packet1); |
| 97 | rtlv.readFrom(channelBuffer); |
| 98 | assertThat(rtlv, is(notNullValue())); |
| 99 | assertThat(rtlv, instanceOf(RouterTlv.class)); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Tests asBytes() method. |
| 104 | */ |
| 105 | @Test(expected = Exception.class) |
| 106 | public void testAsBytes() throws Exception { |
| 107 | result = rtlv.asBytes(); |
| 108 | assertThat(result, is(notNullValue())); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Tests getTlvBodyAsByteArray() method. |
| 113 | */ |
| 114 | @Test(expected = Exception.class) |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 115 | public void testGetTlvBodyAsByteArray() throws Exception { |
Kiran Ramachandra | 959353a | 2016-02-16 22:12:07 +0530 | [diff] [blame] | 116 | result = rtlv.getTlvBodyAsByteArray(); |
| 117 | assertThat(result, is(notNullValue())); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tests to string method. |
| 122 | */ |
| 123 | @Test |
| 124 | public void testToString() throws Exception { |
| 125 | assertThat(rtlv.toString(), is(notNullValue())); |
| 126 | } |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 127 | } |