|
| 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.framework.extensions.api; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertFalse; |
| 22 | +import static org.junit.Assert.assertNull; |
| 23 | +import static org.junit.Assert.assertTrue; |
| 24 | +import static org.mockito.Mockito.any; |
| 25 | +import static org.mockito.Mockito.doNothing; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.spy; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.mockito.Mockito.when; |
| 30 | + |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +import org.apache.cloudstack.acl.RoleType; |
| 38 | +import org.apache.cloudstack.api.ApiCommandResourceType; |
| 39 | +import org.apache.cloudstack.api.response.ExtensionCustomActionResponse; |
| 40 | +import org.apache.cloudstack.extension.ExtensionCustomAction; |
| 41 | +import org.apache.cloudstack.framework.extensions.manager.ExtensionsManager; |
| 42 | +import org.apache.commons.collections.MapUtils; |
| 43 | +import org.junit.Before; |
| 44 | +import org.junit.Test; |
| 45 | +import org.junit.runner.RunWith; |
| 46 | +import org.mockito.junit.MockitoJUnitRunner; |
| 47 | +import org.springframework.test.util.ReflectionTestUtils; |
| 48 | + |
| 49 | +import com.cloud.user.Account; |
| 50 | + |
| 51 | +@RunWith(MockitoJUnitRunner.class) |
| 52 | +public class AddCustomActionCmdTest { |
| 53 | + |
| 54 | + private AddCustomActionCmd cmd; |
| 55 | + private ExtensionsManager extensionsManager; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() throws Exception { |
| 59 | + cmd = new AddCustomActionCmd(); |
| 60 | + extensionsManager = mock(ExtensionsManager.class); |
| 61 | + cmd.extensionsManager = extensionsManager; |
| 62 | + } |
| 63 | + |
| 64 | + private void setField(String fieldName, Object value) { |
| 65 | + ReflectionTestUtils.setField(cmd, fieldName, value); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testGetters() { |
| 70 | + Long extensionId = 42L; |
| 71 | + String name = "actionName"; |
| 72 | + String description = "desc"; |
| 73 | + String resourceType = "VM"; |
| 74 | + List<String> allowedRoleTypes = Arrays.asList(RoleType.Admin.name(), RoleType.User.name()); |
| 75 | + Map<String, Object> parameters = new HashMap<>(); |
| 76 | + parameters.put("name", "param1"); |
| 77 | + String successMessage = "Success!"; |
| 78 | + String errorMessage = "Error!"; |
| 79 | + Integer timeout = 10; |
| 80 | + Boolean enabled = true; |
| 81 | + Map<String, Map<String, String>> details = new HashMap<>(); |
| 82 | + Map<String, String> inner = new HashMap<>(); |
| 83 | + inner.put("vendor", "acme"); |
| 84 | + details.put("details", inner); |
| 85 | + setField("details", details); |
| 86 | + |
| 87 | + setField("extensionId", extensionId); |
| 88 | + setField("name", name); |
| 89 | + setField("description", description); |
| 90 | + setField("resourceType", resourceType); |
| 91 | + setField("allowedRoleTypes", allowedRoleTypes); |
| 92 | + setField("parameters", parameters); |
| 93 | + setField("successMessage", successMessage); |
| 94 | + setField("errorMessage", errorMessage); |
| 95 | + setField("timeout", timeout); |
| 96 | + setField("enabled", enabled); |
| 97 | + setField("details", details); |
| 98 | + |
| 99 | + assertEquals(extensionId, cmd.getExtensionId()); |
| 100 | + assertEquals(name, cmd.getName()); |
| 101 | + assertEquals(description, cmd.getDescription()); |
| 102 | + assertEquals(resourceType, cmd.getResourceType()); |
| 103 | + assertEquals(allowedRoleTypes, cmd.getAllowedRoleTypes()); |
| 104 | + assertEquals(parameters, cmd.getParametersMap()); |
| 105 | + assertEquals(successMessage, cmd.getSuccessMessage()); |
| 106 | + assertEquals(errorMessage, cmd.getErrorMessage()); |
| 107 | + assertEquals(timeout, cmd.getTimeout()); |
| 108 | + assertTrue(cmd.isEnabled()); |
| 109 | + assertTrue(MapUtils.isNotEmpty(cmd.getDetails())); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testIsEnabledReturnsFalseWhenNull() { |
| 114 | + setField("enabled", null); |
| 115 | + assertFalse(cmd.isEnabled()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testIsEnabledReturnsFalseWhenFalse() { |
| 120 | + setField("enabled", Boolean.FALSE); |
| 121 | + assertFalse(cmd.isEnabled()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testIsEnabledReturnsTrueWhenTrue() { |
| 126 | + setField("enabled", Boolean.TRUE); |
| 127 | + assertTrue(cmd.isEnabled()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testGetAllowedRoleTypesReturnsNullWhenUnset() { |
| 132 | + setField("allowedRoleTypes", null); |
| 133 | + assertNull(cmd.getAllowedRoleTypes()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testGetAllowedRoleTypesReturnsEmptyList() { |
| 138 | + setField("allowedRoleTypes", Collections.emptyList()); |
| 139 | + assertEquals(0, cmd.getAllowedRoleTypes().size()); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testGetParametersMapReturnsNullWhenUnset() { |
| 144 | + setField("parameters", null); |
| 145 | + assertNull(cmd.getParametersMap()); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testGetParametersMapReturnsMap() { |
| 150 | + Map<String, Object> parameters = new HashMap<>(); |
| 151 | + parameters.put("foo", "bar"); |
| 152 | + setField("parameters", parameters); |
| 153 | + assertEquals(parameters, cmd.getParametersMap()); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testGetDetailsReturnsNullWhenUnset() { |
| 158 | + setField("details", null); |
| 159 | + assertTrue(MapUtils.isEmpty(cmd.getDetails())); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testGetDetailsReturnsMap() { |
| 164 | + Map<String, Map<String, String>> details = new HashMap<>(); |
| 165 | + Map<String, String> inner = new HashMap<>(); |
| 166 | + inner.put("key", "value"); |
| 167 | + details.put("details", inner); |
| 168 | + setField("details", details); |
| 169 | + assertTrue(MapUtils.isNotEmpty(cmd.getDetails())); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void testGetDescriptionReturnsNullWhenUnset() { |
| 174 | + setField("description", null); |
| 175 | + assertNull(cmd.getDescription()); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testGetSuccessMessageReturnsNullWhenUnset() { |
| 180 | + setField("successMessage", null); |
| 181 | + assertNull(cmd.getSuccessMessage()); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testGetErrorMessageReturnsNullWhenUnset() { |
| 186 | + setField("errorMessage", null); |
| 187 | + assertNull(cmd.getErrorMessage()); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testGetTimeoutReturnsNullWhenUnset() { |
| 192 | + setField("timeout", null); |
| 193 | + assertNull(cmd.getTimeout()); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testExecuteCallsExtensionsManagerAndSetsResponse() { |
| 198 | + ExtensionCustomAction extensionCustomAction = mock(ExtensionCustomAction.class); |
| 199 | + ExtensionCustomActionResponse response = mock(ExtensionCustomActionResponse.class); |
| 200 | + |
| 201 | + when(extensionsManager.addCustomAction(any(AddCustomActionCmd.class))).thenReturn(extensionCustomAction); |
| 202 | + when(extensionsManager.createCustomActionResponse(extensionCustomAction)).thenReturn(response); |
| 203 | + |
| 204 | + AddCustomActionCmd spyCmd = spy(cmd); |
| 205 | + doNothing().when(spyCmd).setResponseObject(any()); |
| 206 | + |
| 207 | + spyCmd.execute(); |
| 208 | + |
| 209 | + verify(extensionsManager).addCustomAction(spyCmd); |
| 210 | + verify(extensionsManager).createCustomActionResponse(extensionCustomAction); |
| 211 | + verify(response).setResponseName(spyCmd.getCommandName()); |
| 212 | + verify(spyCmd).setResponseObject(response); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void testGetEntityOwnerIdReturnsSystemAccount() { |
| 217 | + assertEquals(Account.ACCOUNT_ID_SYSTEM, cmd.getEntityOwnerId()); |
| 218 | + } |
| 219 | + |
| 220 | + @Test |
| 221 | + public void testGetApiResourceTypeReturnsExtensionCustomAction() { |
| 222 | + assertEquals(ApiCommandResourceType.ExtensionCustomAction, cmd.getApiResourceType()); |
| 223 | + } |
| 224 | +} |
0 commit comments