|
| 1 | +package com.cloud.api; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.apache.cloudstack.api.BaseCmd; |
| 7 | +import org.apache.cloudstack.api.Parameter; |
| 8 | +import org.apache.cloudstack.api.ServerApiException; |
| 9 | +import org.apache.cloudstack.context.CallContext; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.mockito.Mock; |
| 16 | +import org.mockito.Mockito; |
| 17 | +import org.mockito.runners.MockitoJUnitRunner; |
| 18 | + |
| 19 | +import com.cloud.exception.ConcurrentOperationException; |
| 20 | +import com.cloud.exception.InsufficientCapacityException; |
| 21 | +import com.cloud.exception.NetworkRuleConflictException; |
| 22 | +import com.cloud.exception.ResourceAllocationException; |
| 23 | +import com.cloud.exception.ResourceUnavailableException; |
| 24 | +import com.cloud.user.Account; |
| 25 | +import com.cloud.user.AccountManager; |
| 26 | +import com.cloud.user.User; |
| 27 | + |
| 28 | +@RunWith(MockitoJUnitRunner.class) |
| 29 | +public class ApiDispatcherTest { |
| 30 | + |
| 31 | + @Mock |
| 32 | + AccountManager accountManager; |
| 33 | + |
| 34 | + public static class TestCmd extends BaseCmd { |
| 35 | + |
| 36 | + @Parameter(name = "strparam1") |
| 37 | + String strparam1; |
| 38 | + |
| 39 | + @Parameter(name="intparam1", type=CommandType.INTEGER) |
| 40 | + int intparam1; |
| 41 | + |
| 42 | + @Parameter(name="boolparam1", type=CommandType.BOOLEAN) |
| 43 | + boolean boolparam1; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void execute() throws ResourceUnavailableException, |
| 47 | + InsufficientCapacityException, ServerApiException, |
| 48 | + ConcurrentOperationException, ResourceAllocationException, |
| 49 | + NetworkRuleConflictException { |
| 50 | + // well documented nothing |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String getCommandName() { |
| 55 | + return "test"; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public long getEntityOwnerId() { |
| 60 | + return 0; |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setup() { |
| 67 | + CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class)); |
| 68 | + new ApiDispatcher().init(); |
| 69 | + ApiDispatcher.getInstance()._accountMgr = accountManager; |
| 70 | + } |
| 71 | + |
| 72 | + @After |
| 73 | + public void cleanup() { |
| 74 | + CallContext.unregister(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void processParameters() { |
| 79 | + HashMap<String, String> params = new HashMap<String, String>(); |
| 80 | + params.put("strparam1", "foo"); |
| 81 | + params.put("intparam1", "100"); |
| 82 | + params.put("boolparam1", "true"); |
| 83 | + TestCmd cmd = new TestCmd(); |
| 84 | + //how lucky that field is not protected, this test would be impossible |
| 85 | + ApiDispatcher.processParameters(cmd, params); |
| 86 | + Assert.assertEquals("foo", cmd.strparam1); |
| 87 | + Assert.assertEquals(100, cmd.intparam1); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments