blob: eb061925b173703f8f65edfd5b842078aefa0ef2 [file] [log] [blame]
Mohamed Rahila3b9e992016-02-16 20:26:49 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Mohamed Rahila3b9e992016-02-16 20:26:49 +05303 *
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 SummaryLsa.
34 */
35public class SummaryLsaTest {
36
37 private SummaryLsa summaryLsa;
38 private Ip4Address result;
39 private int result1;
40 private byte[] inputByteArray;
41 private LsaHeader lsaHeader;
42 private ChannelBuffer channelBuffer;
43 private byte[] result2;
44 private OspfLsaType ospflsaType;
45
46 @Before
47 public void setUp() throws Exception {
48 summaryLsa = new SummaryLsa(new LsaHeader());
49 }
50
51 @After
52 public void tearDown() throws Exception {
53 summaryLsa = null;
54 result = null;
55 inputByteArray = null;
56 lsaHeader = null;
57 channelBuffer = null;
58 result2 = null;
59 ospflsaType = null;
60 }
61
62 /**
63 * Tests networkMask() getter method.
64 */
65 @Test
66 public void testGetNetworkMask() throws Exception {
67 summaryLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
68 result = summaryLsa.networkMask();
69 assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
70 }
71
72 /**
73 * Tests networkMask() setter method.
74 */
75 @Test
76 public void testSetNetworkMask() throws Exception {
77 summaryLsa.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
78 result = summaryLsa.networkMask();
79 assertThat(result, is(Ip4Address.valueOf("10.226.165.164")));
80 }
81
82 /**
83 * Tests metric() getter method.
84 */
85 @Test
86 public void testGetMetric() throws Exception {
87 summaryLsa.setMetric(10);
88 result1 = summaryLsa.metric();
89 assertThat(result1, is(10));
90 }
91
92 /**
93 * Tests metric() setter method.
94 */
95 @Test
96 public void testSetMetric() throws Exception {
97 summaryLsa.setMetric(20);
98 result1 = summaryLsa.metric();
99 assertThat(result1, is(20));
100 }
101
102 /**
103 * Tests readFrom() method.
104 */
105 @Test
106 public void testReadFrom() throws Exception {
107 inputByteArray = createByteForNetworkLsa();
108 lsaHeader = createLsaHeader();
109 summaryLsa = new SummaryLsa(lsaHeader);
110 channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
111 summaryLsa.readFrom(channelBuffer);
112 assertThat(summaryLsa, is(notNullValue()));
113 }
114
115 /**
116 * Tests readFrom() method.
117 */
118 @Test(expected = Exception.class)
119 public void testReadFrom1() throws Exception {
120 byte[] temp = {0, 0, 0};
121 inputByteArray = temp;
122 lsaHeader = createLsaHeader();
123 summaryLsa = new SummaryLsa(lsaHeader);
124 channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
125 summaryLsa.readFrom(channelBuffer);
126 assertThat(summaryLsa, is(notNullValue()));
127 }
128
129 /**
130 * Tests asBytes() method.
131 */
132 @Test
133 public void testAsBytes() throws Exception {
134 result2 = summaryLsa.asBytes();
135 assertThat(result2, is(notNullValue()));
136 }
137
138 /**
139 * Tests getLsaBodyAsByteArray() method.
140 */
141 @Test
142 public void testGetLsaBodyAsByteArray() throws Exception {
143 result2 = summaryLsa.getLsaBodyAsByteArray();
144 assertThat(result2, is(notNullValue()));
145 }
146
147 /**
148 * Tests getOspfLsaType() getter method.
149 */
150 @Test
151 public void testGetOspfLsaType() throws Exception {
152 ospflsaType = summaryLsa.getOspfLsaType();
153 assertThat(ospflsaType, is(notNullValue()));
154 assertThat(ospflsaType, is(OspfLsaType.SUMMARY));
155 }
156
157 /**
158 * Utility method used by junit methods.
159 */
160 private byte[] createByteForNetworkLsa() {
161 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,
162 -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,
163 1};
164 return packet;
165 }
166
167 /**
168 * Utility method used by junit methods.
169 */
170 private LsaHeader createLsaHeader() {
171 lsaHeader = new LsaHeader();
172 lsaHeader.setLsType(3);
173 lsaHeader.setLsPacketLen(48);
174 lsaHeader.setLsCheckSum(10);
175 lsaHeader.setAge(4);
176 lsaHeader.setLinkStateId("10.226.165.164");
177 lsaHeader.setLsSequenceNo(250);
178 lsaHeader.setAdvertisingRouter(Ip4Address.valueOf("100.226.165.165"));
179 lsaHeader.setOptions(2);
180 return lsaHeader;
181 }
182
183 /**
184 * Tests hashcode() method.
185 */
186 @Test
187 public void testHashcode() throws Exception {
188
189 result1 = summaryLsa.hashCode();
190 assertThat(result1, is(notNullValue()));
191
192 }
193}