Merge changes I936ddd89,I1f20b8df
[bgpcep.git] / pcep / api / src / test / java / org / opendaylight / protocol / pcep / api / MessagesTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.pcep.api;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.junit.Test;
18
19 import org.opendaylight.protocol.concepts.IPv4Address;
20 import org.opendaylight.protocol.pcep.PCEPErrors;
21 import org.opendaylight.protocol.pcep.message.PCEPCloseMessage;
22 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
23 import org.opendaylight.protocol.pcep.message.PCEPKeepAliveMessage;
24 import org.opendaylight.protocol.pcep.message.PCEPNotificationMessage;
25 import org.opendaylight.protocol.pcep.message.PCEPOpenMessage;
26 import org.opendaylight.protocol.pcep.message.PCEPReplyMessage;
27 import org.opendaylight.protocol.pcep.message.PCEPReportMessage;
28 import org.opendaylight.protocol.pcep.message.PCEPRequestMessage;
29 import org.opendaylight.protocol.pcep.message.PCEPUpdateRequestMessage;
30 import org.opendaylight.protocol.pcep.object.CompositeErrorObject;
31 import org.opendaylight.protocol.pcep.object.CompositeNotifyObject;
32 import org.opendaylight.protocol.pcep.object.CompositeRequestObject;
33 import org.opendaylight.protocol.pcep.object.CompositeResponseObject;
34 import org.opendaylight.protocol.pcep.object.CompositeStateReportObject;
35 import org.opendaylight.protocol.pcep.object.CompositeUpdateRequestObject;
36 import org.opendaylight.protocol.pcep.object.PCEPCloseObject;
37 import org.opendaylight.protocol.pcep.object.PCEPCloseObject.Reason;
38 import org.opendaylight.protocol.pcep.object.PCEPEndPointsObject;
39 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
40 import org.opendaylight.protocol.pcep.object.PCEPLspObject;
41 import org.opendaylight.protocol.pcep.object.PCEPNotificationObject;
42 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
43 import org.opendaylight.protocol.pcep.object.PCEPRequestParameterObject;
44
45 public class MessagesTest {
46
47         @Test
48         public void errorMessageTest() {
49                 final List<PCEPErrorObject> errorObjs = new ArrayList<PCEPErrorObject>() {
50                         private static final long serialVersionUID = 1L;
51
52                         {
53                                 this.add(new PCEPErrorObject(PCEPErrors.ATTEMPT_2ND_SESSION));
54                                 this.add(new PCEPErrorObject(PCEPErrors.ATTEMPT_2ND_SESSION));
55                         }
56                 };
57                 final List<CompositeErrorObject> errors = new ArrayList<CompositeErrorObject>() {
58                         private static final long serialVersionUID = 1L;
59
60                         {
61                                 this.add(new CompositeErrorObject(errorObjs));
62                         }
63                 };
64
65                 final List<?> objs = new ArrayList<Object>() {
66                         private static final long serialVersionUID = 1L;
67
68                         {
69                                 this.add(new CompositeErrorObject(errorObjs));
70                                 this.add(new PCEPErrorObject(PCEPErrors.BANDWIDTH_MISSING));
71                         }
72                 };
73
74                 final PCEPErrorMessage m = new PCEPErrorMessage(new PCEPOpenObject(10, 10, 1), errorObjs, errors);
75                 final PCEPErrorMessage m2 = new PCEPErrorMessage(new PCEPOpenObject(10, 10, 1), errorObjs, errors);
76                 final PCEPErrorMessage m3 = new PCEPErrorMessage(objs);
77
78                 assertEquals(m, m2);
79                 assertEquals(m.toString(), m2.toString());
80                 assertEquals(m.hashCode(), m2.hashCode());
81                 assertEquals(m.getOpenObject(), m2.getOpenObject());
82                 assertEquals(m.getErrors(), m2.getErrors());
83                 assertEquals(m.getErrorObjects(), m2.getErrorObjects());
84                 assertFalse(m.equals(null));
85                 assertFalse(m.equals(m3));
86                 assertFalse(m.equals(new PCEPCloseMessage(new PCEPCloseObject(Reason.EXP_DEADTIMER))));
87                 assertTrue(m.equals(m));
88         }
89
90         @Test(expected = IllegalArgumentException.class)
91         public void closeMessageTest() {
92                 final PCEPCloseMessage m = new PCEPCloseMessage(new PCEPCloseObject(Reason.EXP_DEADTIMER));
93                 final PCEPCloseMessage m2 = new PCEPCloseMessage(new PCEPCloseObject(Reason.EXP_DEADTIMER));
94                 final PCEPCloseMessage m3 = new PCEPCloseMessage(new PCEPCloseObject(Reason.MALFORMED_MSG));
95
96                 assertEquals(m, m2);
97                 assertEquals(m.toString(), m2.toString());
98                 assertEquals(m.hashCode(), m2.hashCode());
99                 assertEquals(m.getCloseObject(), m2.getCloseObject());
100                 assertFalse(m.equals(null));
101                 assertFalse(m.equals(m3));
102                 assertFalse(m.equals(new PCEPOpenMessage(new PCEPOpenObject(10, 10, 1))));
103                 assertTrue(m.equals(m));
104
105                 new PCEPCloseMessage(null);
106         }
107
108         @Test(expected = IllegalArgumentException.class)
109         public void openMessageTest() {
110                 final PCEPOpenMessage m = new PCEPOpenMessage(new PCEPOpenObject(10, 10, 1));
111                 final PCEPOpenMessage m2 = new PCEPOpenMessage(new PCEPOpenObject(10, 10, 1));
112                 final PCEPOpenMessage m3 = new PCEPOpenMessage(new PCEPOpenObject(5, 5, 1));
113
114                 assertEquals(m, m2);
115                 assertEquals(m.toString(), m2.toString());
116                 assertEquals(m.hashCode(), m2.hashCode());
117                 assertEquals(m.getOpenObject(), m2.getOpenObject());
118                 assertFalse(m.equals(null));
119                 assertFalse(m.equals(m3));
120                 assertFalse(m.equals(new PCEPCloseMessage(new PCEPCloseObject(Reason.EXP_DEADTIMER))));
121                 assertTrue(m.equals(m));
122
123                 new PCEPOpenMessage(null);
124         }
125
126         @Test(expected = IllegalArgumentException.class)
127         public void keepAliveMessageTest() {
128                 final PCEPKeepAliveMessage m = new PCEPKeepAliveMessage();
129                 final PCEPKeepAliveMessage m2 = new PCEPKeepAliveMessage();
130
131                 assertEquals(m, m2);
132                 assertEquals(m.toString(), m2.toString());
133                 assertEquals(m.hashCode(), m2.hashCode());
134                 assertFalse(m.equals(null));
135                 assertFalse(m.equals(new PCEPCloseMessage(new PCEPCloseObject(Reason.EXP_DEADTIMER))));
136                 assertTrue(m.equals(m));
137
138                 new PCEPOpenMessage(null);
139         }
140
141         @Test(expected = IllegalArgumentException.class)
142         public void notifyMessageTest() {
143                 final List<CompositeNotifyObject> notifications = new ArrayList<CompositeNotifyObject>() {
144                         private static final long serialVersionUID = 1L;
145
146                         {
147                                 this.add(new CompositeNotifyObject(new ArrayList<PCEPNotificationObject>() {
148                                         private static final long serialVersionUID = 1L;
149
150                                         {
151                                                 this.add(new PCEPNotificationObject((short) 2, (short) 3));
152                                         }
153                                 }));
154                         }
155                 };
156
157                 final PCEPNotificationMessage m = new PCEPNotificationMessage(notifications);
158                 final PCEPNotificationMessage m2 = new PCEPNotificationMessage(notifications);
159                 final PCEPNotificationMessage m3 = new PCEPNotificationMessage(new ArrayList<CompositeNotifyObject>() {
160                         private static final long serialVersionUID = 1L;
161
162                         {
163                                 this.add(new CompositeNotifyObject(new ArrayList<PCEPNotificationObject>() {
164                                         private static final long serialVersionUID = 1L;
165
166                                         {
167                                                 this.add(new PCEPNotificationObject((short) 2, (short) 5));
168                                         }
169                                 }));
170                         }
171                 });
172
173                 assertEquals(m, m2);
174                 assertEquals(m.toString(), m2.toString());
175                 assertEquals(m.hashCode(), m2.hashCode());
176                 assertEquals(m.getNotifications(), m2.getNotifications());
177                 assertFalse(m.equals(null));
178                 assertFalse(m.equals(m3));
179                 assertFalse(m.equals(new PCEPOpenMessage(new PCEPOpenObject(10, 10, 1))));
180                 assertTrue(m.equals(m));
181
182                 new PCEPNotificationMessage(null);
183         }
184
185         @Test(expected = IllegalArgumentException.class)
186         public void replyMessageTest() {
187                 final List<CompositeResponseObject> replies = new ArrayList<CompositeResponseObject>() {
188                         private static final long serialVersionUID = 1L;
189
190                         {
191                                 this.add(new CompositeResponseObject(new PCEPRequestParameterObject(true, true, true, true, true, false, false, false, (short) 1, 1, true,
192                                                 false)));
193                         }
194                 };
195
196                 final PCEPReplyMessage m = new PCEPReplyMessage(replies);
197                 final PCEPReplyMessage m2 = new PCEPReplyMessage(replies);
198                 final PCEPReplyMessage m3 = new PCEPReplyMessage(new ArrayList<CompositeResponseObject>() {
199                         private static final long serialVersionUID = 1L;
200
201                         {
202                                 this.add(new CompositeResponseObject(new PCEPRequestParameterObject(true, true, true, false, true, false, false, false, (short) 2, 1, false,
203                                                 false)));
204                         }
205                 });
206
207                 assertEquals(m, m2);
208                 assertEquals(m.toString(), m2.toString());
209                 assertEquals(m.hashCode(), m2.hashCode());
210                 assertEquals(m.getResponses(), m2.getResponses());
211                 assertFalse(m.equals(null));
212                 assertFalse(m.equals(m3));
213                 assertFalse(m.equals(new PCEPOpenMessage(new PCEPOpenObject(10, 10, 1))));
214                 assertTrue(m.equals(m));
215
216                 new PCEPReplyMessage(null);
217         }
218
219         @Test(expected = IllegalArgumentException.class)
220         public void reportMessageTest() {
221                 final List<CompositeStateReportObject> reports = new ArrayList<CompositeStateReportObject>() {
222                         private static final long serialVersionUID = 1L;
223
224                         {
225                                 this.add(new CompositeStateReportObject(new PCEPLspObject(1, true, true, true, true)));
226                         }
227                 };
228
229                 final PCEPReportMessage m = new PCEPReportMessage(reports);
230                 final PCEPReportMessage m2 = new PCEPReportMessage(reports);
231                 final PCEPReportMessage m3 = new PCEPReportMessage(new ArrayList<CompositeStateReportObject>() {
232                         private static final long serialVersionUID = 1L;
233
234                         {
235                                 this.add(new CompositeStateReportObject(new PCEPLspObject(5, false, true, true, true)));
236                         }
237                 });
238
239                 assertEquals(m, m2);
240                 assertEquals(m.toString(), m2.toString());
241                 assertEquals(m.hashCode(), m2.hashCode());
242                 assertEquals(m.getStateReports(), m2.getStateReports());
243                 assertFalse(m.equals(null));
244                 assertFalse(m.equals(m3));
245                 assertFalse(m.equals(new PCEPOpenMessage(new PCEPOpenObject(10, 10, 1))));
246                 assertTrue(m.equals(m));
247
248                 new PCEPReportMessage(null);
249         }
250
251         @Test(expected = IllegalArgumentException.class)
252         public void requestMessageTest() {
253                 final List<CompositeRequestObject> reports = new ArrayList<CompositeRequestObject>() {
254                         private static final long serialVersionUID = 1L;
255
256                         {
257                                 this.add(new CompositeRequestObject(
258                                                 new PCEPRequestParameterObject(true, true, true, true, true, false, false, false, (short) 5, 5, true, true),
259                                                 new PCEPEndPointsObject<IPv4Address>(
260                                                         new IPv4Address(new byte[] { (byte) 127, (byte) 0, (byte) 0, (byte) 1 }),
261                                                         new IPv4Address(new byte[] { (byte) 127, (byte) 0, (byte) 0, (byte) 1 }))));
262                         }
263                 };
264
265                 final PCEPRequestMessage m = new PCEPRequestMessage(reports);
266                 final PCEPRequestMessage m2 = new PCEPRequestMessage(reports);
267                 final PCEPRequestMessage m3 = new PCEPRequestMessage(new ArrayList<CompositeRequestObject>() {
268                         private static final long serialVersionUID = 1L;
269
270                         {
271                                 this.add(new CompositeRequestObject(
272                                                 new PCEPRequestParameterObject(true, true, true, false, true, false, false, false, (short) 5, 5, true, true),
273                                                 new PCEPEndPointsObject<IPv4Address>(
274                                                         new IPv4Address(new byte[] { (byte) 127, (byte) 0, (byte) 0, (byte) 2 }),
275                                                         new IPv4Address(new byte[] { (byte) 127, (byte) 0, (byte) 0, (byte) 1 }))));
276                         }
277                 });
278
279                 assertEquals(m, m2);
280                 assertEquals(m.toString(), m2.toString());
281                 assertEquals(m.hashCode(), m2.hashCode());
282                 assertEquals(m.getSvecObjects(), m2.getSvecObjects());
283                 assertEquals(m.getRequests(), m2.getRequests());
284                 assertFalse(m.equals(null));
285                 assertFalse(m.equals(m3));
286                 assertFalse(m.equals(new PCEPOpenMessage(new PCEPOpenObject(10, 10, 1))));
287                 assertTrue(m.equals(m));
288
289                 new PCEPRequestMessage(null);
290         }
291
292         @Test(expected = IllegalArgumentException.class)
293         public void updateRequestMessageTest() {
294                 final List<CompositeUpdateRequestObject> reports = new ArrayList<CompositeUpdateRequestObject>() {
295                         private static final long serialVersionUID = 1L;
296
297                         {
298                                 this.add(new CompositeUpdateRequestObject(new PCEPLspObject(1, true, true, true, true)));
299                         }
300                 };
301
302                 final PCEPUpdateRequestMessage m = new PCEPUpdateRequestMessage(reports);
303                 final PCEPUpdateRequestMessage m2 = new PCEPUpdateRequestMessage(reports);
304                 final PCEPUpdateRequestMessage m3 = new PCEPUpdateRequestMessage(new ArrayList<CompositeUpdateRequestObject>() {
305                         private static final long serialVersionUID = 1L;
306
307                         {
308                                 this.add(new CompositeUpdateRequestObject(new PCEPLspObject(5, true, true, true, true)));
309                         }
310                 });
311
312                 assertEquals(m, m2);
313                 assertEquals(m.toString(), m2.toString());
314                 assertEquals(m.hashCode(), m2.hashCode());
315                 assertEquals(m.getUpdateRequests(), m2.getUpdateRequests());
316                 assertFalse(m.equals(null));
317                 assertFalse(m.equals(m3));
318                 assertFalse(m.equals(new PCEPOpenMessage(new PCEPOpenObject(10, 10, 1))));
319                 assertTrue(m.equals(m));
320
321                 new PCEPUpdateRequestMessage(null);
322         }
323 }