4b653345f92ba3cf8aadea1831f1e9530cbc7720
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPSessionImplTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.pcep.impl;
10
11 import org.junit.After;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mockito;
16 import org.opendaylight.protocol.pcep.TerminationReason;
17 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.CloseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcerr;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Pcreq;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.PcreqBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
24
25 public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
26
27     private PCEPSessionImpl session;
28
29     @Before
30     public void setup() {
31         this.session = new PCEPSessionImpl(this.listener, 0, this.channel, this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen());
32         this.session.sessionUp();
33     }
34
35     @After
36     public void tearDown() {
37         this.session.tearDown();
38     }
39
40     @Test
41     public void testPcepSessionImpl() throws InterruptedException {
42         Assert.assertTrue(this.listener.up);
43
44         Assert.assertEquals(45, this.session.getDeadTimerValue().intValue());
45         Assert.assertEquals(15, this.session.getKeepAliveTimerValue().intValue());
46         this.session.handleMessage(this.kaMsg);
47         Assert.assertEquals(1, this.session.getReceivedMsgCount().intValue());
48
49         this.session.handleMessage(new PcreqBuilder().build());
50         Assert.assertEquals(2, this.session.getReceivedMsgCount().intValue());
51         Assert.assertEquals(1, this.listener.messages.size());
52         Assert.assertTrue(this.listener.messages.get(0) instanceof Pcreq);
53
54         this.session.handleMessage(new CloseBuilder().build());
55         Assert.assertEquals(3, this.session.getReceivedMsgCount().intValue());
56         Assert.assertEquals(1, this.listener.messages.size());
57         Assert.assertTrue(this.channel.isActive());
58         Mockito.verify(this.channel, Mockito.times(1)).close();
59     }
60
61     @Test
62     public void testAttemptSecondSession() {
63         this.session.handleMessage(this.openMsg);
64         Assert.assertEquals(1, this.session.getReceivedMsgCount().intValue());
65         Assert.assertEquals(1, this.msgsSend.size());
66         Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
67         final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
68         final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
69         Assert.assertEquals(PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
70     }
71
72     @Test
73     public void testCapabilityNotSupported() {
74         this.session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
75         Assert.assertEquals(2, this.msgsSend.size());
76         Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
77         final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
78         final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
79         Assert.assertEquals(PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
80         Assert.assertEquals(1, this.session.getUnknownMessagesTimes().size());
81         // exceeded max. unknown messages count - terminate session
82         Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
83         final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
84         Assert.assertEquals(TerminationReason.TooManyUnknownMsg, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
85         Mockito.verify(this.channel, Mockito.times(1)).close();
86     }
87
88     @Test
89     public void testEndoOfInput() {
90         Assert.assertTrue(this.listener.up);
91         this.session.endOfInput();
92         Assert.assertFalse(this.listener.up);
93     }
94
95     @Test
96     public void voidTestCloseSessionWithReason() {
97         this.session.close(TerminationReason.Unknown);
98         Assert.assertEquals(1, this.msgsSend.size());
99         Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
100         final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
101         Assert.assertEquals(TerminationReason.Unknown, TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason()));
102         Mockito.verify(this.channel, Mockito.times(1)).close();
103     }
104 }