Further migration of test code from legacy setters
[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.ArgumentMatchers;
16 import org.mockito.Mockito;
17 import org.opendaylight.protocol.pcep.PCEPSession;
18 import org.opendaylight.protocol.pcep.TerminationReason;
19 import org.opendaylight.protocol.pcep.impl.spi.Util;
20 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcerr;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.Pcreq;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev181109.PcreqBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.error.messages.grouping.ErrorMessages;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.LocalPref;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.Messages;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.PeerPref;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.CloseMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObject;
30
31 public class PCEPSessionImplTest extends AbstractPCEPSessionTest {
32
33     private PCEPSessionImpl session;
34
35     @Before
36     public void setup() {
37         this.session = new PCEPSessionImpl(this.listener, 0, this.channel, this.openMsg.getOpenMessage().getOpen(),
38             this.openMsg.getOpenMessage().getOpen());
39         this.session.sessionUp();
40     }
41
42     @After
43     public void tearDown() {
44         this.session.close();
45     }
46
47     @Test
48     public void testPcepSessionImpl() {
49         Assert.assertTrue(this.listener.up);
50
51         this.session.handleMessage(this.kaMsg);
52         Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
53
54         this.session.handleMessage(new PcreqBuilder().build());
55         Assert.assertEquals(2, this.session.getMessages().getReceivedMsgCount().intValue());
56         Assert.assertEquals(1, this.listener.messages.size());
57         Assert.assertTrue(this.listener.messages.get(0) instanceof Pcreq);
58         Assert.assertEquals(2, this.session.getMessages().getReceivedMsgCount().intValue());
59
60         this.session.handleMessage(this.closeMsg);
61         Assert.assertEquals(3, this.session.getMessages().getReceivedMsgCount().intValue());
62         Assert.assertEquals(1, this.listener.messages.size());
63         Assert.assertTrue(this.channel.isActive());
64         Mockito.verify(this.channel, Mockito.times(1)).close();
65     }
66
67     @Test
68     public void testAttemptSecondSession() {
69         this.session.handleMessage(this.openMsg);
70         Assert.assertEquals(1, this.session.getMessages().getReceivedMsgCount().intValue());
71         Assert.assertEquals(1, this.msgsSend.size());
72         Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
73         final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
74         final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
75         Assert.assertEquals(
76             PCEPErrors.ATTEMPT_2ND_SESSION, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
77     }
78
79     @Test
80     public void testClosedByNode() {
81         this.session.handleMessage(this.closeMsg);
82         Mockito.verify(this.channel).close();
83     }
84
85     @Test
86     public void testCapabilityNotSupported() {
87         this.session.handleMalformedMessage(PCEPErrors.CAPABILITY_NOT_SUPPORTED);
88         Assert.assertEquals(2, this.msgsSend.size());
89         Assert.assertTrue(this.msgsSend.get(0) instanceof Pcerr);
90         final Pcerr pcErr = (Pcerr) this.msgsSend.get(0);
91         final ErrorObject errorObj = pcErr.getPcerrMessage().getErrors().get(0).getErrorObject();
92         Assert.assertEquals(
93             PCEPErrors.CAPABILITY_NOT_SUPPORTED, PCEPErrors.forValue(errorObj.getType(), errorObj.getValue()));
94         Assert.assertEquals(1, this.session.getMessages().getUnknownMsgReceived().intValue());
95         // exceeded max. unknown messages count - terminate session
96         Assert.assertTrue(this.msgsSend.get(1) instanceof CloseMessage);
97         final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(1);
98         Assert.assertEquals(TerminationReason.TOO_MANY_UNKNOWN_MSGS,
99             TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason().toJava()));
100         Mockito.verify(this.channel, Mockito.times(1)).close();
101     }
102
103     @Test
104     public void testEndoOfInput() {
105         Assert.assertTrue(this.listener.up);
106         this.session.endOfInput();
107         Assert.assertFalse(this.listener.up);
108     }
109
110     @Test
111     public void testCloseSessionWithReason() {
112         this.session.close(TerminationReason.UNKNOWN);
113         Assert.assertEquals(1, this.msgsSend.size());
114         Assert.assertTrue(this.msgsSend.get(0) instanceof CloseMessage);
115         final CloseMessage closeMsg = (CloseMessage) this.msgsSend.get(0);
116         Assert.assertEquals(TerminationReason.UNKNOWN,
117             TerminationReason.forValue(closeMsg.getCCloseMessage().getCClose().getReason().toJava()));
118         Mockito.verify(this.channel, Mockito.times(1)).close();
119     }
120
121     @Test
122     public void testSessionStatistics() {
123         this.session.handleMessage(Util.createErrorMessage(PCEPErrors.LSP_RSVP_ERROR, null));
124         Assert.assertEquals(this.ipAddress, this.session.getPeerPref().getIpAddress());
125         final PeerPref peerPref = this.session.getPeerPref();
126         Assert.assertEquals(this.ipAddress, peerPref.getIpAddress());
127         Assert.assertEquals(DEADTIMER, peerPref.getDeadtimer());
128         Assert.assertEquals(KEEP_ALIVE, peerPref.getKeepalive());
129         Assert.assertEquals(0, peerPref.getSessionId().intValue());
130         final LocalPref localPref = this.session.getLocalPref();
131         Assert.assertEquals(this.ipAddress, localPref.getIpAddress());
132         Assert.assertEquals(DEADTIMER, localPref.getDeadtimer());
133         Assert.assertEquals(KEEP_ALIVE, localPref.getKeepalive());
134         Assert.assertEquals(0, localPref.getSessionId().intValue());
135         final Messages msgs = this.session.getMessages();
136         Assert.assertEquals(1, msgs.getReceivedMsgCount().longValue());
137         Assert.assertEquals(0, msgs.getSentMsgCount().longValue());
138         Assert.assertEquals(0, msgs.getUnknownMsgReceived().longValue());
139         final ErrorMessages errMsgs = msgs.getErrorMessages();
140         Assert.assertEquals(1, errMsgs.getReceivedErrorMsgCount().intValue());
141         Assert.assertEquals(0, errMsgs.getSentErrorMsgCount().intValue());
142         Assert.assertEquals(
143             PCEPErrors.LSP_RSVP_ERROR.getErrorType(), errMsgs.getLastReceivedError().getErrorType().shortValue());
144         Assert.assertEquals(
145             PCEPErrors.LSP_RSVP_ERROR.getErrorValue(), errMsgs.getLastReceivedError().getErrorValue().shortValue());
146
147         this.session.sendMessage(Util.createErrorMessage(PCEPErrors.UNKNOWN_PLSP_ID, null));
148         final Messages msgs2 = this.session.getMessages();
149         Assert.assertEquals(1, msgs2.getReceivedMsgCount().longValue());
150         Assert.assertEquals(1, msgs2.getSentMsgCount().longValue());
151         Assert.assertEquals(0, msgs2.getUnknownMsgReceived().longValue());
152         final ErrorMessages errMsgs2 = msgs2.getErrorMessages();
153         Assert.assertEquals(1, errMsgs2.getReceivedErrorMsgCount().intValue());
154         Assert.assertEquals(1, errMsgs2.getSentErrorMsgCount().intValue());
155         Assert.assertEquals(
156             PCEPErrors.UNKNOWN_PLSP_ID.getErrorType(), errMsgs2.getLastSentError().getErrorType().shortValue());
157         Assert.assertEquals(
158             PCEPErrors.UNKNOWN_PLSP_ID.getErrorValue(), errMsgs2.getLastSentError().getErrorValue().shortValue());
159     }
160
161     @Test
162     public void testExceptionCaught() {
163         Assert.assertFalse(this.session.isClosed());
164         Assert.assertTrue(this.listener.up);
165         this.session.exceptionCaught(null, new Throwable("PCEP exception."));
166         Assert.assertFalse(this.listener.up);
167         Assert.assertTrue(this.session.isClosed());
168     }
169
170     @Test
171     public void testSessionRecoveryOnException() {
172         this.listener = new SimpleExceptionSessionListener();
173         this.session = Mockito.spy(new PCEPSessionImpl(this.listener, 0, this.channel,
174                 this.openMsg.getOpenMessage().getOpen(), this.openMsg.getOpenMessage().getOpen()));
175         Mockito.verify(this.session, Mockito.never()).handleException(ArgumentMatchers.any());
176         Mockito.verify(this.session, Mockito.never()).sendMessage(ArgumentMatchers.any());
177         Mockito.verify(this.session, Mockito.never()).closeChannel();
178         try {
179             this.session.sessionUp();
180             Assert.fail();  // expect the exception to be populated
181         } catch (final RuntimeException ignored) {
182         }
183         Assert.assertFalse(this.listener.up);
184         Mockito.verify(this.session).handleException(ArgumentMatchers.any());
185         Mockito.verify(this.session).sendMessage(ArgumentMatchers.any(CloseMessage.class));
186         Mockito.verify(this.session).closeChannel();
187     }
188
189     private static class SimpleExceptionSessionListener extends SimpleSessionListener {
190         @Override
191         public synchronized void onSessionUp(final PCEPSession session) {
192             super.onSessionUp(session);
193             throw new RuntimeException("Mocked runtime exception.");
194         }
195     }
196 }