|
| 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 org.apache.cloudstack.api.command.test; |
| 18 | + |
| 19 | +import junit.framework.Assert; |
| 20 | +import junit.framework.TestCase; |
| 21 | + |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.mockito.Mockito; |
| 25 | + |
| 26 | +import java.util.LinkedList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import org.apache.cloudstack.api.ResponseGenerator; |
| 30 | +import org.apache.cloudstack.api.ServerApiException; |
| 31 | +import org.apache.cloudstack.api.ResponseObject.ResponseView; |
| 32 | +import org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd; |
| 33 | +import org.apache.cloudstack.api.response.UserVmResponse; |
| 34 | + |
| 35 | +import com.cloud.exception.ConcurrentOperationException; |
| 36 | +import com.cloud.exception.InsufficientCapacityException; |
| 37 | +import com.cloud.exception.ResourceAllocationException; |
| 38 | +import com.cloud.exception.ResourceUnavailableException; |
| 39 | +import com.cloud.uservm.UserVm; |
| 40 | +import com.cloud.vm.UserVmService; |
| 41 | + |
| 42 | +public class UpdateVmNicIpTest extends TestCase { |
| 43 | + |
| 44 | + private UpdateVmNicIpCmd updateVmNicIpCmd; |
| 45 | + private ResponseGenerator responseGenerator; |
| 46 | + |
| 47 | + @Override |
| 48 | + @Before |
| 49 | + public void setUp() { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testSuccess() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException { |
| 55 | + |
| 56 | + UserVmService userVmService = Mockito.mock(UserVmService.class); |
| 57 | + updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class); |
| 58 | + UserVm userVm = Mockito.mock(UserVm.class); |
| 59 | + |
| 60 | + Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(userVm); |
| 61 | + |
| 62 | + updateVmNicIpCmd._userVmService = userVmService; |
| 63 | + responseGenerator = Mockito.mock(ResponseGenerator.class); |
| 64 | + |
| 65 | + List<UserVmResponse> list = new LinkedList<UserVmResponse>(); |
| 66 | + UserVmResponse userVmResponse = Mockito.mock(UserVmResponse.class); |
| 67 | + list.add(userVmResponse); |
| 68 | + Mockito.when(responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm)).thenReturn(list); |
| 69 | + |
| 70 | + updateVmNicIpCmd._responseGenerator = responseGenerator; |
| 71 | + updateVmNicIpCmd.execute(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testFailure() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException { |
| 76 | + UserVmService userVmService = Mockito.mock(UserVmService.class); |
| 77 | + updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class); |
| 78 | + |
| 79 | + Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(null); |
| 80 | + |
| 81 | + updateVmNicIpCmd._userVmService = userVmService; |
| 82 | + |
| 83 | + updateVmNicIpCmd._responseGenerator = responseGenerator; |
| 84 | + try { |
| 85 | + updateVmNicIpCmd.execute(); |
| 86 | + } catch (ServerApiException exception) { |
| 87 | + Assert.assertEquals("Failed to update ip address on vm NIC. Refer to server logs for details.", exception.getDescription()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments