1616// under the License.
1717package com .cloud .server ;
1818
19+ import com .cloud .user .SSHKeyPair ;
1920import org .junit .Test ;
2021import org .junit .runner .RunWith ;
2122import org .mockito .Mock ;
2223import org .mockito .Mockito ;
2324import org .mockito .Spy ;
2425import org .mockito .runners .MockitoJUnitRunner ;
2526
27+ import static org .mockito .Matchers .anyString ;
28+ import static org .mockito .Mockito .when ;
29+ import static org .mockito .Mockito .any ;
30+
2631import org .apache .cloudstack .api .command .user .ssh .RegisterSSHKeyPairCmd ;
2732
2833import com .cloud .exception .InvalidParameterValueException ;
@@ -35,33 +40,71 @@ public class ManagementServerImplTest {
3540
3641 @ Mock
3742 RegisterSSHKeyPairCmd regCmd ;
43+
3844 @ Mock
3945 SSHKeyPairVO existingPair ;
46+
4047 @ Mock
4148 Account account ;
49+
4250 @ Mock
4351 SSHKeyPairDao sshKeyPairDao ;
4452 ManagementServerImpl ms = new ManagementServerImpl ();
53+
54+ @ Mock
55+ SSHKeyPair sshKeyPair ;
56+
4557 @ Spy
4658 ManagementServerImpl spy ;
4759
4860 @ Test (expected = InvalidParameterValueException .class )
49- public void testExistingPairRegistration () {
61+ public void testDuplicateRegistraitons () {
5062 String accountName = "account" ;
51- String publicKeyString = "very public" ;
52- // setup owner with domainid
63+ String publicKeyString = "ssh-rsa very public" ;
64+ String publicKeyMaterial = spy .getPublicKeyFromKeyKeyMaterial (publicKeyString );
65+
5366 Mockito .doReturn (account ).when (spy ).getCaller ();
5467 Mockito .doReturn (account ).when (spy ).getOwner (regCmd );
55- // mock _sshKeyPairDao.findByName to return null
68+
5669 Mockito .doNothing ().when (spy ).checkForKeyByName (regCmd , account );
57- // mock _sshKeyPairDao.findByPublicKey to return a known object
5870 Mockito .doReturn (accountName ).when (regCmd ).getAccountName ();
71+
5972 Mockito .doReturn (publicKeyString ).when (regCmd ).getPublicKey ();
6073 Mockito .doReturn ("name" ).when (regCmd ).getName ();
74+
6175 spy ._sshKeyPairDao = sshKeyPairDao ;
6276 Mockito .doReturn (1L ).when (account ).getAccountId ();
6377 Mockito .doReturn (1L ).when (account ).getDomainId ();
64- Mockito .doReturn (existingPair ).when (sshKeyPairDao ).findByPublicKey (1L , 1L , publicKeyString );
78+ Mockito .doReturn (Mockito .mock (SSHKeyPairVO .class )).when (sshKeyPairDao ).persist (any (SSHKeyPairVO .class ));
79+
80+ when (sshKeyPairDao .findByName (1L , 1L , "name" )).thenReturn (null ).thenReturn (null );
81+ when (sshKeyPairDao .findByPublicKey (1L , 1L , publicKeyMaterial )).thenReturn (null ).thenReturn (existingPair );
82+
83+ spy .registerSSHKeyPair (regCmd );
84+ spy .registerSSHKeyPair (regCmd );
85+ }
86+ @ Test
87+ public void testSuccess (){
88+ String accountName = "account" ;
89+ String publicKeyString = "ssh-rsa very public" ;
90+ String publicKeyMaterial = spy .getPublicKeyFromKeyKeyMaterial (publicKeyString );
91+
92+ Mockito .doReturn (1L ).when (account ).getAccountId ();
93+ Mockito .doReturn (1L ).when (account ).getAccountId ();
94+ spy ._sshKeyPairDao = sshKeyPairDao ;
95+
96+
97+ //Mocking the DAO object functions - NO object found in DB
98+ Mockito .doReturn (Mockito .mock (SSHKeyPairVO .class )).when (sshKeyPairDao ).findByPublicKey (1L , 1L ,publicKeyMaterial );
99+ Mockito .doReturn (Mockito .mock (SSHKeyPairVO .class )).when (sshKeyPairDao ).findByName (1L , 1L , accountName );
100+ Mockito .doReturn (Mockito .mock (SSHKeyPairVO .class )).when (sshKeyPairDao ).persist (any (SSHKeyPairVO .class ));
101+
102+ //Mocking the User Params
103+ Mockito .doReturn (accountName ).when (regCmd ).getName ();
104+ Mockito .doReturn (publicKeyString ).when (regCmd ).getPublicKey ();
105+ Mockito .doReturn (account ).when (spy ).getOwner (regCmd );
106+
65107 spy .registerSSHKeyPair (regCmd );
108+ Mockito .verify (spy , Mockito .times (3 )).getPublicKeyFromKeyKeyMaterial (anyString ());
66109 }
67110}
0 commit comments