Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +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.types; |
| 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.controller.OspfLsaType; |
| 25 | import org.onosproject.ospf.protocol.lsa.LsaHeader; |
| 26 | import org.onosproject.ospf.protocol.lsa.subtypes.OspfLsaLink; |
| 27 | |
| 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.*; |
| 30 | |
| 31 | /** |
| 32 | * Unit test class for RouterLsa. |
| 33 | */ |
| 34 | public class RouterLsaTest { |
| 35 | |
| 36 | private RouterLsa routerLsa; |
| 37 | private int result1; |
| 38 | private OspfLsaLink ospflsaLink; |
| 39 | private byte[] inputArray; |
| 40 | private LsaHeader lsaHeader; |
| 41 | private ChannelBuffer channelBuffer; |
| 42 | private byte[] result2; |
| 43 | private OspfLsaType result3; |
| 44 | |
| 45 | @Before |
| 46 | public void setUp() throws Exception { |
| 47 | routerLsa = new RouterLsa(); |
| 48 | } |
| 49 | |
| 50 | @After |
| 51 | public void tearDown() throws Exception { |
| 52 | routerLsa = null; |
| 53 | ospflsaLink = null; |
| 54 | inputArray = null; |
| 55 | lsaHeader = null; |
| 56 | channelBuffer = null; |
| 57 | result2 = null; |
| 58 | result3 = null; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Tests virtualEndPoint() setter method. |
| 64 | */ |
| 65 | @Test |
| 66 | public void testSetVirtualEndPoint() throws Exception { |
| 67 | routerLsa.setVirtualEndPoint(true); |
| 68 | assertThat(routerLsa, is(notNullValue())); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Tests isAsBoundaryRouter() setter method. |
| 74 | */ |
| 75 | @Test |
| 76 | public void testSetAsBoundaryRouter() throws Exception { |
| 77 | routerLsa.setAsBoundaryRouter(true); |
| 78 | assertThat(routerLsa, is(notNullValue())); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Tests areaBorderRouter() setter method. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testSetAreaBorderRouter() throws Exception { |
| 86 | routerLsa.setAreaBorderRouter(true); |
| 87 | assertThat(routerLsa, is(notNullValue())); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests noLink() getter method. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testGetNoLink() throws Exception { |
| 95 | routerLsa.setNoLink(10); |
| 96 | result1 = routerLsa.noLink(); |
| 97 | assertThat(result1, is(10)); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests noLink() setter method. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testSetNoLink() throws Exception { |
| 105 | routerLsa.setNoLink(10); |
| 106 | result1 = routerLsa.noLink(); |
| 107 | assertThat(result1, is(10)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests addRouterLink() method. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testAddRouterLink() throws Exception { |
| 115 | routerLsa.setNoLink(0); |
| 116 | ospflsaLink = createOspfLsaLink(); |
| 117 | routerLsa.addRouterLink(ospflsaLink); |
| 118 | routerLsa.incrementLinkNo(); |
| 119 | result1 = routerLsa.noLink(); |
| 120 | assertThat(result1, is(1)); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Tests readFrom() method. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testReadFrom() throws Exception { |
| 130 | ospflsaLink = createOspfLsaLink(); |
| 131 | routerLsa.addRouterLink(ospflsaLink); |
| 132 | inputArray = createByteForRouterLsa(); |
| 133 | lsaHeader = createLsaHeader(); |
| 134 | routerLsa = new RouterLsa(lsaHeader); |
| 135 | channelBuffer = ChannelBuffers.copiedBuffer(inputArray); |
| 136 | routerLsa.readFrom(channelBuffer); |
| 137 | assertThat(routerLsa, is(notNullValue())); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tests readFrom() method. |
| 142 | */ |
| 143 | @Test(expected = Exception.class) |
| 144 | public void testReadFrom1() throws Exception { |
| 145 | byte[] temp = {0, 0, 0}; |
| 146 | ospflsaLink = createOspfLsaLink(); |
| 147 | routerLsa.addRouterLink(ospflsaLink); |
| 148 | inputArray = temp; |
| 149 | lsaHeader = createLsaHeader(); |
| 150 | routerLsa = new RouterLsa(lsaHeader); |
| 151 | channelBuffer = ChannelBuffers.copiedBuffer(inputArray); |
| 152 | routerLsa.readFrom(channelBuffer); |
| 153 | assertThat(routerLsa, is(notNullValue())); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests asBytes() method. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testAsBytes() throws Exception { |
| 161 | result2 = routerLsa.asBytes(); |
| 162 | assertThat(result2, is(notNullValue())); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Tests getLsaBodyAsByteArray() method. |
| 167 | */ |
| 168 | @Test |
| 169 | public void testGetLsaBodyAsByteArray() throws Exception { |
| 170 | routerLsa.setAreaBorderRouter(true); |
| 171 | routerLsa.setVirtualEndPoint(true); |
| 172 | routerLsa.setAreaBorderRouter(true); |
| 173 | ospflsaLink = createOspfLsaLink(); |
| 174 | routerLsa.addRouterLink(ospflsaLink); |
| 175 | result2 = routerLsa.getLsaBodyAsByteArray(); |
| 176 | assertThat(result2, is(notNullValue())); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Tests getOspfLsaType() getter method. |
| 181 | */ |
| 182 | @Test |
| 183 | public void testGetOspfLsaType() throws Exception { |
| 184 | routerLsa.setLsType(1); |
| 185 | result3 = routerLsa.getOspfLsaType(); |
| 186 | assertThat(result3, is(OspfLsaType.ROUTER)); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Tests incrementLinkNo() method. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testIncrementLinkNo() throws Exception { |
| 194 | routerLsa.setNoLink(1); |
| 195 | routerLsa.incrementLinkNo(); |
| 196 | assertThat(routerLsa.noLink(), is(2)); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Tests lsaHeader() method. |
| 201 | */ |
| 202 | @Test |
| 203 | public void testGetLsaHeader() throws Exception { |
| 204 | lsaHeader = (LsaHeader) routerLsa.lsaHeader(); |
| 205 | assertThat(lsaHeader, instanceOf(RouterLsa.class)); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Tests to string method. |
| 210 | */ |
| 211 | @Test |
| 212 | public void testToString() throws Exception { |
| 213 | assertThat(routerLsa.toString(), is(notNullValue())); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Utility method used by junit methods. |
| 219 | */ |
| 220 | private OspfLsaLink createOspfLsaLink() { |
| 221 | ospflsaLink = new OspfLsaLink(); |
| 222 | ospflsaLink.setLinkId("10.226.165.164"); |
| 223 | ospflsaLink.setMetric(10); |
| 224 | ospflsaLink.setTos(50); |
| 225 | ospflsaLink.setLinkType(2); |
| 226 | ospflsaLink.setLinkData("10.226.165.170"); |
| 227 | return ospflsaLink; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Utility method used by junit methods. |
| 232 | */ |
| 233 | private byte[] createByteForRouterLsa() { |
| 234 | byte[] packet = {2, 1, 1, 52, -64, -88, 56, 1, -64, -88, 56, 1, 0, 100, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, -64, |
| 235 | -88, 56, 1, 0, 10, 1, 1, 0, 0, 0, 40, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, 1, -64, -88, 56, |
| 236 | 1}; |
| 237 | return packet; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Utility method used by junit methods. |
| 242 | */ |
| 243 | private LsaHeader createLsaHeader() { |
| 244 | lsaHeader = new LsaHeader(); |
| 245 | lsaHeader.setLsType(1); |
| 246 | lsaHeader.setLsPacketLen(48); |
| 247 | lsaHeader.setLsCheckSum(10); |
| 248 | lsaHeader.setAge(4); |
| 249 | lsaHeader.setLinkStateId("10.226.165.164"); |
| 250 | lsaHeader.setLsSequenceNo(250); |
| 251 | lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); |
| 252 | lsaHeader.setOptions(2); |
| 253 | return lsaHeader; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Tests hashcode() method. |
| 258 | */ |
| 259 | @Test |
| 260 | public void testHashcode() throws Exception { |
| 261 | |
| 262 | result1 = routerLsa.hashCode(); |
| 263 | assertThat(result1, is(notNullValue())); |
| 264 | |
| 265 | } |
| 266 | } |