|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.server; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.Mockito; |
| 23 | +import org.mockito.Spy; |
| 24 | +import org.mockito.runners.MockitoJUnitRunner; |
| 25 | + |
| 26 | +import org.apache.cloudstack.api.command.user.ssh.RegisterSSHKeyPairCmd; |
| 27 | + |
| 28 | +import com.cloud.exception.InvalidParameterValueException; |
| 29 | +import com.cloud.user.Account; |
| 30 | +import com.cloud.user.SSHKeyPairVO; |
| 31 | +import com.cloud.user.dao.SSHKeyPairDao; |
| 32 | + |
| 33 | +@RunWith(MockitoJUnitRunner.class) |
| 34 | +public class ManagementServerImplTest { |
| 35 | + |
| 36 | + @Mock |
| 37 | + RegisterSSHKeyPairCmd regCmd; |
| 38 | + @Mock |
| 39 | + SSHKeyPairVO existingPair; |
| 40 | + @Mock |
| 41 | + Account account; |
| 42 | + @Mock |
| 43 | + SSHKeyPairDao sshKeyPairDao; |
| 44 | + ManagementServerImpl ms = new ManagementServerImpl(); |
| 45 | + @Spy |
| 46 | + ManagementServerImpl spy; |
| 47 | + |
| 48 | + @Test(expected = InvalidParameterValueException.class) |
| 49 | + public void testExistingPairRegistration() { |
| 50 | + String accountName = "account"; |
| 51 | + String publicKeyString = "very public"; |
| 52 | + // setup owner with domainid |
| 53 | + Mockito.doReturn(account).when(spy).getCaller(); |
| 54 | + Mockito.doReturn(account).when(spy).getOwner(regCmd); |
| 55 | + // mock _sshKeyPairDao.findByName to return null |
| 56 | + Mockito.doNothing().when(spy).checkForKeyByName(regCmd, account); |
| 57 | + // mock _sshKeyPairDao.findByPublicKey to return a known object |
| 58 | + Mockito.doReturn(accountName).when(regCmd).getAccountName(); |
| 59 | + Mockito.doReturn(publicKeyString).when(regCmd).getPublicKey(); |
| 60 | + Mockito.doReturn("name").when(regCmd).getName(); |
| 61 | + spy._sshKeyPairDao = sshKeyPairDao; |
| 62 | + Mockito.doReturn(1L).when(account).getAccountId(); |
| 63 | + Mockito.doReturn(1L).when(account).getDomainId(); |
| 64 | + Mockito.doReturn(existingPair).when(sshKeyPairDao).findByPublicKey(1L, 1L, publicKeyString); |
| 65 | + spy.registerSSHKeyPair(regCmd); |
| 66 | + } |
| 67 | +} |
0 commit comments