Improve callhome-protocol unit tests
[netconf.git] / netconf / callhome-protocol / src / test / java / org / opendaylight / netconf / callhome / protocol / CallHomeAuthorizationTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.netconf.callhome.protocol;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.anyString;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20
21 import java.security.KeyPair;
22 import java.security.PrivateKey;
23 import java.security.PublicKey;
24 import org.junit.Test;
25 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
26 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
27
28 public class CallHomeAuthorizationTest {
29     @Test
30     public void anAuthorizationOfRejectedIsNotAllowed() {
31         // given
32         CallHomeAuthorization auth = CallHomeAuthorization.rejected();
33         // expect
34         assertFalse(auth.isServerAllowed());
35     }
36
37     @Test
38     public void anAuthorizationOfRejectedCannotBeAppliedToASession() {
39         // given
40         CallHomeAuthorization auth = CallHomeAuthorization.rejected();
41         // when
42         final var ex = assertThrows(IllegalStateException.class, () -> auth.applyTo(mock(ClientSession.class)));
43         assertEquals("Server is not allowed.", ex.getMessage());
44     }
45
46     @Test
47     public void anAuthorizationOfAcceptanceIsAllowed() {
48         // given
49         String session = "some-session";
50         String user = "some-user-name";
51         ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
52         doNothing().when(mockSession).setUsername(user);
53
54         // and
55         CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user).build();
56         // when
57         auth.applyTo(mockSession);
58         // then
59         assertTrue(auth.isServerAllowed());
60     }
61
62     @Test
63     public void anAuthorizationOfAcceptanceCanBeAppliedToASession() {
64         // given
65         String session = "some-session";
66         String user = "some-user-name";
67         String pwd = "pwd1";
68         KeyPair pair = new KeyPair(mock(PublicKey.class), mock(PrivateKey.class));
69         ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
70         doNothing().when(mockSession).setUsername(user);
71         doNothing().when(mockSession).addPasswordIdentity(pwd);
72         doNothing().when(mockSession).addPublicKeyIdentity(pair);
73         // and
74         CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user)
75                 .addPassword(pwd)
76                 .addClientKeys(pair)
77                 .build();
78         // when
79         auth.applyTo(mockSession);
80         // then
81         verify(mockSession, times(1)).addPasswordIdentity(anyString());
82         verify(mockSession, times(1)).addPublicKeyIdentity(any(KeyPair.class));
83     }
84 }