blob: 13f97fd8d60fb6c11e6ee2077d38fcb5c1cc89a1 [file] [log] [blame]
Mohamed Rahila3b9e992016-02-16 20:26:49 +05301/*
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 */
16package org.onosproject.ospf.protocol.lsa.types;
17
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.Ip4Address;
24import org.onosproject.ospf.controller.OspfLsaType;
25import org.onosproject.ospf.protocol.lsa.LsaHeader;
26
27
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.is;
30import static org.hamcrest.Matchers.notNullValue;
31
32/**
33 * Unit test class for AsbrSummaryLsa.
34 */
35public class AsbrSummaryLsaTest {
36
37 private final Ip4Address ipAddress = Ip4Address.valueOf("10.226.165.164");
38 private AsbrSummaryLsa asbrSummaryLsa;
39 private Ip4Address result;
40 private int result1;
41 private int num;
42 private byte[] inputByteArray;
43 private byte[] result2;
44 private ChannelBuffer channelBuffer;
45 private LsaHeader lsaHeader;
46 private OspfLsaType ospflsaType;
47 private String result3;
48 private boolean result4;
49
50 @Before
51 public void setUp() throws Exception {
52 asbrSummaryLsa = new AsbrSummaryLsa(new LsaHeader());
53 }
54
55 @After
56 public void tearDown() throws Exception {
57 asbrSummaryLsa = null;
58 }
59
60 /**
61 * Tests networkMask() getter method.
62 */
63 @Test
64 public void testGetNetworkMask() throws Exception {
65 asbrSummaryLsa.setNetworkMask(ipAddress);
66 result = asbrSummaryLsa.networkMask();
67 assertThat(result, is(notNullValue()));
68 assertThat(result, is(ipAddress));
69 }
70
71 /**
72 * Tests networkMask() setter method.
73 */
74 @Test
75 public void testSetNetworkMask() throws Exception {
76 asbrSummaryLsa.setNetworkMask(ipAddress);
77 result = asbrSummaryLsa.networkMask();
78 assertThat(result, is(notNullValue()));
79 assertThat(result, is(ipAddress));
80 }
81
82 /**
83 * Tests metric() getter method.
84 */
85 @Test
86 public void testGetMetric() throws Exception {
87 num = 10;
88 asbrSummaryLsa.setMetric(num);
89 result1 = asbrSummaryLsa.metric();
90 assertThat(result1, is(notNullValue()));
91 assertThat(result1, is(num));
92 }
93
94 /**
95 * Tests metric() setter method.
96 */
97 @Test
98 public void testSetMetric() throws Exception {
99 num = 20;
100 asbrSummaryLsa.setMetric(num);
101 result1 = asbrSummaryLsa.metric();
102 assertThat(result1, is(notNullValue()));
103 assertThat(result1, is(num));
104 }
105
106 /**
107 * Tests readFrom() method.
108 */
109 @Test
110 public void testReadFrom() throws Exception {
111 inputByteArray = createByteForNetworkLsa();
112 lsaHeader = createLsaHeader();
113 asbrSummaryLsa = new AsbrSummaryLsa(lsaHeader);
114 channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
115 asbrSummaryLsa.readFrom(channelBuffer);
116 assertThat(asbrSummaryLsa, is(notNullValue()));
117 }
118
119 /**
120 * Tests readFrom() method.
121 */
122 @Test(expected = Exception.class)
123 public void testReadFrom1() throws Exception {
124 byte[] temp = {0, 1, 2, 3};
125 inputByteArray = temp;
126 lsaHeader = createLsaHeader();
127 asbrSummaryLsa = new AsbrSummaryLsa(lsaHeader);
128 channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
129 asbrSummaryLsa.readFrom(channelBuffer);
130 assertThat(asbrSummaryLsa, is(notNullValue()));
131 }
132
133 /**
134 * Tests asBytes() method.
135 */
136 @Test(expected = Exception.class)
137 public void testAsBytes() throws Exception {
138 result2 = asbrSummaryLsa.asBytes();
139 assertThat(result2, is(notNullValue()));
140 }
141
142 /**
143 * Tests getLsaBodyAsByteArray() method.
144 */
145 @Test(expected = Exception.class)
146 public void testGetLsaBodyAsByteArray() throws Exception {
147 result2 = asbrSummaryLsa.getLsaBodyAsByteArray();
148 assertThat(result2, is(notNullValue()));
149 }
150
151 /**
152 * Tests ospfLsaType() getter method.
153 */
154 @Test
155 public void testGetOspfLsaType() throws Exception {
156
157 ospflsaType = asbrSummaryLsa.getOspfLsaType();
158 assertThat(ospflsaType, is(notNullValue()));
159 assertThat(ospflsaType, is(OspfLsaType.ASBR_SUMMARY));
160 }
161
162 /**
163 * Tests to string method.
164 */
165 @Test
166 public void testToString() throws Exception {
167
168 result3 = asbrSummaryLsa.toString();
169 assertThat(result3, is(notNullValue()));
170
171 }
172
173 /**
174 * Tests hashcode() method.
175 */
176 @Test
177 public void testHashcode() throws Exception {
178
179 result1 = asbrSummaryLsa.hashCode();
180 assertThat(result1, is(notNullValue()));
181
182 }
183
184 /**
185 * Tests equals() method.
186 */
187 @Test
188 public void testEqual() throws Exception {
189
190 result4 = asbrSummaryLsa.equals(new AsbrSummaryLsa(new LsaHeader()));
191 assertThat(result4, is(true));
192
193 }
194
195 /**
196 * Utility method used by junit methods.
197 */
198 private byte[] createByteForNetworkLsa() {
199 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,
200 -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,
201 1};
202 return packet;
203 }
204
205 /**
206 * Utility method used by junit methods.
207 */
208 private LsaHeader createLsaHeader() {
209 lsaHeader = new LsaHeader();
210 lsaHeader.setLsType(1);
211 lsaHeader.setLsPacketLen(48);
212 lsaHeader.setLsCheckSum(10);
213 lsaHeader.setAge(4);
214 lsaHeader.setLinkStateId("10.226.165.164");
215 lsaHeader.setLsSequenceNo(250);
216 lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
217 lsaHeader.setOptions(2);
218 return lsaHeader;
219 }
220}