Skip to content

Commit c61997f

Browse files
K0zkaDaan Hoogland
authored andcommitted
basic test for ApiDispatcher
Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent b7e00d3 commit c61997f

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

api/src/org/apache/cloudstack/context/CallContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
public class CallContext {
3939
private static final Logger s_logger = Logger.getLogger(CallContext.class);
40-
private static ThreadLocal<CallContext> s_currentContext = new ThreadLocal<CallContext>();
40+
private static final ThreadLocal<CallContext> s_currentContext = new ThreadLocal<CallContext>();
4141

4242
private String contextId;
4343
private Account account;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)