|
| 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 | + |
| 18 | +package org.apache.cloudstack.api.command.test; |
| 19 | + |
| 20 | +import junit.framework.TestCase; |
| 21 | +import org.apache.cloudstack.acl.Role; |
| 22 | +import org.apache.cloudstack.acl.RoleService; |
| 23 | +import org.apache.cloudstack.acl.RoleType; |
| 24 | +import org.apache.cloudstack.api.command.admin.acl.UpdateRoleCmd; |
| 25 | +import org.apache.cloudstack.api.response.RoleResponse; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | +import org.mockito.Mockito; |
| 29 | +import org.springframework.test.util.ReflectionTestUtils; |
| 30 | + |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | + |
| 34 | +public class UpdateRoleCmdTest extends TestCase{ |
| 35 | + |
| 36 | + private UpdateRoleCmd updateRoleCmd; |
| 37 | + private RoleService roleService; |
| 38 | + private Role role; |
| 39 | + |
| 40 | + @Override |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + roleService = Mockito.spy(RoleService.class); |
| 44 | + updateRoleCmd = new UpdateRoleCmd(); |
| 45 | + ReflectionTestUtils.setField(updateRoleCmd,"roleService",roleService); |
| 46 | + ReflectionTestUtils.setField(updateRoleCmd,"roleId",1L); |
| 47 | + ReflectionTestUtils.setField(updateRoleCmd,"roleName","user"); |
| 48 | + ReflectionTestUtils.setField(updateRoleCmd,"roleType", "User"); |
| 49 | + ReflectionTestUtils.setField(updateRoleCmd,"roleDescription","Description Initial"); |
| 50 | + role = Mockito.mock(Role.class); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testUpdateSuccess() { |
| 55 | + when(roleService.findRole(updateRoleCmd.getRoleId())).thenReturn(role); |
| 56 | + when(role.getId()).thenReturn(1L); |
| 57 | + when(role.getUuid()).thenReturn("12345-abcgdkajd"); |
| 58 | + when(role.getDescription()).thenReturn("Defualt user"); |
| 59 | + when(role.getName()).thenReturn("User"); |
| 60 | + when(role.getRoleType()).thenReturn(RoleType.User); |
| 61 | + when(roleService.updateRole(role,updateRoleCmd.getRoleName(),updateRoleCmd.getRoleType(),updateRoleCmd.getRoleDescription())).thenReturn(role); |
| 62 | + when(role.getId()).thenReturn(1L); |
| 63 | + when(role.getDescription()).thenReturn("Description Initial"); |
| 64 | + when(role.getName()).thenReturn("User"); |
| 65 | + updateRoleCmd.execute(); |
| 66 | + RoleResponse response = (RoleResponse) updateRoleCmd.getResponseObject(); |
| 67 | + assertEquals((String)ReflectionTestUtils.getField(response, "roleName"),role.getName()); |
| 68 | + assertEquals((String)ReflectionTestUtils.getField(response, "roleDescription"),role.getDescription()); |
| 69 | + } |
| 70 | +} |
0 commit comments