Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 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.OpaqueLsaHeader; |
| 26 | |
| 27 | |
| 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.is; |
| 30 | import static org.hamcrest.Matchers.notNullValue; |
| 31 | |
| 32 | /** |
| 33 | * Unit test class for OpaqueLsa9. |
| 34 | */ |
| 35 | public class OpaqueLsa9Test { |
| 36 | |
| 37 | private final byte[] packet = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 38 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 39 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 40 | private byte[] result; |
| 41 | private String result1; |
| 42 | private OpaqueLsaHeader opqueHeader; |
| 43 | private OpaqueLsa9 opaqueLsa9; |
| 44 | private ChannelBuffer channelBuffer; |
| 45 | private OspfLsaType ospflsaType; |
| 46 | private int result2; |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() throws Exception { |
| 50 | opaqueLsa9 = new OpaqueLsa9(new OpaqueLsaHeader()); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | @After |
| 55 | public void tearDown() throws Exception { |
| 56 | opaqueLsa9 = null; |
| 57 | opqueHeader = null; |
| 58 | channelBuffer = null; |
| 59 | result = null; |
| 60 | ospflsaType = null; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Tests readFrom() method. |
| 65 | */ |
| 66 | @Test |
| 67 | public void testReadFrom() throws Exception { |
| 68 | opqueHeader = new OpaqueLsaHeader(); |
| 69 | opqueHeader.setLsType(1); |
| 70 | opqueHeader.setLsPacketLen(48); |
| 71 | opqueHeader.setLsCheckSum(10); |
| 72 | opqueHeader.setAge(4); |
| 73 | opqueHeader.setOpaqueId(9); |
| 74 | opqueHeader.setOpaqueType(9); |
| 75 | opqueHeader.setLsSequenceNo(250); |
| 76 | opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); |
| 77 | opqueHeader.setOptions(2); |
| 78 | opaqueLsa9 = new OpaqueLsa9(opqueHeader); |
| 79 | channelBuffer = ChannelBuffers.copiedBuffer(packet); |
| 80 | opaqueLsa9.readFrom(channelBuffer); |
| 81 | assertThat(opaqueLsa9, is(notNullValue())); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests asBytes() method. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testAsBytes() throws Exception { |
| 89 | opqueHeader = new OpaqueLsaHeader(); |
| 90 | opqueHeader.setLsType(9); |
| 91 | opqueHeader.setLsPacketLen(48); |
| 92 | opqueHeader.setLsCheckSum(10); |
| 93 | opqueHeader.setAge(4); |
| 94 | opqueHeader.setOpaqueId(9); |
| 95 | opqueHeader.setOpaqueType(9); |
| 96 | opqueHeader.setLsSequenceNo(250); |
| 97 | opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); |
| 98 | opqueHeader.setOptions(2); |
| 99 | opaqueLsa9 = new OpaqueLsa9(opqueHeader); |
| 100 | channelBuffer = ChannelBuffers.copiedBuffer(packet); |
| 101 | opaqueLsa9.readFrom(channelBuffer); |
| 102 | result = opaqueLsa9.asBytes(); |
| 103 | assertThat(result, is(notNullValue())); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests getLsaBodyAsByteArray() method. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testGetLsaBodyAsByteArray() throws Exception { |
| 111 | opqueHeader = new OpaqueLsaHeader(); |
| 112 | opqueHeader.setLsType(9); |
| 113 | opqueHeader.setLsPacketLen(48); |
| 114 | opqueHeader.setLsCheckSum(10); |
| 115 | opqueHeader.setAge(4); |
| 116 | opqueHeader.setOpaqueId(9); |
| 117 | opqueHeader.setOpaqueType(9); |
| 118 | opqueHeader.setLsSequenceNo(250); |
| 119 | opqueHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165")); |
| 120 | opqueHeader.setOptions(2); |
| 121 | opaqueLsa9 = new OpaqueLsa9(opqueHeader); |
| 122 | channelBuffer = ChannelBuffers.copiedBuffer(packet); |
| 123 | opaqueLsa9.readFrom(channelBuffer); |
| 124 | result = opaqueLsa9.getLsaBodyAsByteArray(); |
| 125 | assertThat(result, is(notNullValue())); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Tests hashCode() method. |
| 130 | */ |
| 131 | @Test |
| 132 | public void testHashcode() throws Exception { |
| 133 | |
| 134 | result2 = opaqueLsa9.hashCode(); |
| 135 | assertThat(result2, is(notNullValue())); |
| 136 | |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Tests to string method. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testToString() throws Exception { |
| 144 | |
| 145 | result1 = opaqueLsa9.toString(); |
| 146 | assertThat(result1, is(notNullValue())); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Tests to getOspfLsaType() getter method. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testGetOspfLsaType() throws Exception { |
| 155 | opaqueLsa9.setLsType(9); |
| 156 | ospflsaType = opaqueLsa9.getOspfLsaType(); |
| 157 | assertThat(ospflsaType, is(notNullValue())); |
| 158 | assertThat(ospflsaType, is(OspfLsaType.LINK_LOCAL_OPAQUE_LSA)); |
| 159 | } |
| 160 | } |