You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by mu...@apache.org on 2014/03/11 15:26:27 UTC
[05/50] [abbrv] Dispatcher corrections, refactoring and tests
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c211f0bb/server/test/com/cloud/api/ApiDispatcherTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/api/ApiDispatcherTest.java b/server/test/com/cloud/api/ApiDispatcherTest.java
deleted file mode 100644
index 7314a57..0000000
--- a/server/test/com/cloud/api/ApiDispatcherTest.java
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package com.cloud.api;
-
-import java.util.HashMap;
-
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
-
-import org.apache.cloudstack.api.BaseCmd;
-import org.apache.cloudstack.api.Parameter;
-import org.apache.cloudstack.api.ServerApiException;
-import org.apache.cloudstack.context.CallContext;
-
-import com.cloud.exception.ConcurrentOperationException;
-import com.cloud.exception.InsufficientCapacityException;
-import com.cloud.exception.NetworkRuleConflictException;
-import com.cloud.exception.ResourceAllocationException;
-import com.cloud.exception.ResourceUnavailableException;
-import com.cloud.user.Account;
-import com.cloud.user.AccountManager;
-import com.cloud.user.User;
-
-@RunWith(MockitoJUnitRunner.class)
-public class ApiDispatcherTest {
-
- @Mock
- AccountManager accountManager;
-
- public static class TestCmd extends BaseCmd {
-
- @Parameter(name = "strparam1")
- String strparam1;
-
- @Parameter(name = "intparam1", type = CommandType.INTEGER)
- int intparam1;
-
- @Parameter(name = "boolparam1", type = CommandType.BOOLEAN)
- boolean boolparam1;
-
- @Override
- public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException,
- ResourceAllocationException, NetworkRuleConflictException {
- // well documented nothing
- }
-
- @Override
- public String getCommandName() {
- return "test";
- }
-
- @Override
- public long getEntityOwnerId() {
- return 0;
- }
-
- }
-
- @Before
- public void setup() {
- CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class));
- new ApiDispatcher().init();
- ApiDispatcher.getInstance()._accountMgr = accountManager;
- }
-
- @After
- public void cleanup() {
- CallContext.unregister();
- }
-
- @Test
- public void processParameters() {
- HashMap<String, String> params = new HashMap<String, String>();
- params.put("strparam1", "foo");
- params.put("intparam1", "100");
- params.put("boolparam1", "true");
- TestCmd cmd = new TestCmd();
- //how lucky that field is not protected, this test would be impossible
- ApiDispatcher.processParameters(cmd, params);
- Assert.assertEquals("foo", cmd.strparam1);
- Assert.assertEquals(100, cmd.intparam1);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c211f0bb/server/test/com/cloud/api/dispatch/CommandCreationWorkerTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/api/dispatch/CommandCreationWorkerTest.java b/server/test/com/cloud/api/dispatch/CommandCreationWorkerTest.java
new file mode 100644
index 0000000..34d64fc
--- /dev/null
+++ b/server/test/com/cloud/api/dispatch/CommandCreationWorkerTest.java
@@ -0,0 +1,48 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.api.dispatch;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.apache.cloudstack.api.BaseAsyncCreateCmd;
+
+import com.cloud.exception.ResourceAllocationException;
+
+public class CommandCreationWorkerTest {
+
+ @Test
+ public void testHandle() throws ResourceAllocationException {
+ // Prepare
+ final BaseAsyncCreateCmd asyncCreateCmd = mock(BaseAsyncCreateCmd.class);
+ final Map<String, String> params = new HashMap<String, String>();
+
+ // Execute
+ final CommandCreationWorker creationWorker = new CommandCreationWorker();
+
+ creationWorker.handle(new DispatchTask(asyncCreateCmd, params));
+
+ // Assert
+ verify(asyncCreateCmd, times(1)).create();
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c211f0bb/server/test/com/cloud/api/dispatch/DispatchChainFactoryTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/api/dispatch/DispatchChainFactoryTest.java b/server/test/com/cloud/api/dispatch/DispatchChainFactoryTest.java
new file mode 100644
index 0000000..f54caae
--- /dev/null
+++ b/server/test/com/cloud/api/dispatch/DispatchChainFactoryTest.java
@@ -0,0 +1,55 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.api.dispatch;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+
+public class DispatchChainFactoryTest {
+
+ protected static final String STANDARD_CHAIN_ERROR = "Expecting worker of class %s at index %s of StandardChain";
+ protected static final String ASYNC_CHAIN_ERROR = "Expecting worker of class %s at index %s of StandardChain";
+
+ @Test
+ public void testAllChainCreation() {
+ // Prepare
+ final DispatchChainFactory dispatchChainFactory = new DispatchChainFactory();
+ dispatchChainFactory.paramGenericValidationWorker = new ParamGenericValidationWorker();
+ dispatchChainFactory.specificCmdValidationWorker = new SpecificCmdValidationWorker();
+ dispatchChainFactory.paramProcessWorker = new ParamProcessWorker();
+ dispatchChainFactory.commandCreationWorker = new CommandCreationWorker();
+ dispatchChainFactory.paramUnpackWorker = new ParamUnpackWorker();
+
+ final Class<?>[] standardClasses = {ParamUnpackWorker.class, ParamProcessWorker.class,
+ ParamGenericValidationWorker.class, SpecificCmdValidationWorker.class};
+ final Class<?>[] asyncClasses = {ParamUnpackWorker.class, ParamProcessWorker.class,
+ ParamGenericValidationWorker.class, SpecificCmdValidationWorker.class, CommandCreationWorker.class};
+
+ // Execute
+ dispatchChainFactory.setup();
+ final DispatchChain standardChain = dispatchChainFactory.getStandardDispatchChain();
+ final DispatchChain asyncChain = dispatchChainFactory.getAsyncCreationDispatchChain();
+ for (int i = 0; i < standardClasses.length; i++) {
+ assertEquals(String.format(STANDARD_CHAIN_ERROR, standardClasses[i], i),
+ standardClasses[i], standardChain.workers.get(i).getClass());
+ }
+ for (int i = 0; i < asyncClasses.length; i++) {
+ assertEquals(String.format(ASYNC_CHAIN_ERROR, asyncClasses[i], i),
+ asyncClasses[i], asyncChain.workers.get(i).getClass());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c211f0bb/server/test/com/cloud/api/dispatch/ParamGenericValidationWorkerTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/api/dispatch/ParamGenericValidationWorkerTest.java b/server/test/com/cloud/api/dispatch/ParamGenericValidationWorkerTest.java
new file mode 100644
index 0000000..867625d
--- /dev/null
+++ b/server/test/com/cloud/api/dispatch/ParamGenericValidationWorkerTest.java
@@ -0,0 +1,173 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.api.dispatch;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.cloudstack.acl.RoleType;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.BaseResponse;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.user.Account;
+
+import org.apache.log4j.Logger;
+
+public class ParamGenericValidationWorkerTest {
+
+ protected static final String FAKE_CMD_NAME = "fakecmdname";
+
+ protected static final String FAKE_CMD_ROLE_NAME = "fakecmdrolename";
+
+ protected String loggerOutput;
+
+ protected void driveTest(final BaseCmd cmd, final Map<String, String> params) {
+ final ParamGenericValidationWorker genValidationWorker = new ParamGenericValidationWorker();
+
+ // We create a mock logger to verify the result
+ ParamGenericValidationWorker.s_logger = new Logger(""){
+ @Override
+ public void warn(final Object msg){
+ loggerOutput = msg.toString();
+ }
+ };
+
+ // Execute
+ genValidationWorker.handle(new DispatchTask(cmd, params));
+ }
+
+ @Test
+ public void testHandle() throws ResourceAllocationException {
+ // Prepare
+ final BaseCmd cmd = new FakeCmd();
+ final Map<String, String> params = new HashMap<String, String>();
+ params.put(ApiConstants.COMMAND, "");
+ params.put("addedParam", "");
+
+ // Execute
+ driveTest(cmd, params);
+
+ // Assert
+ assertEquals("There should be no errors since there are no unknown parameters for this command class", null, loggerOutput);
+ }
+
+ @Test
+ public void testHandleWithUnknownParams() throws ResourceAllocationException {
+ // Prepare
+ final String unknownParamKey = "unknownParam";
+ final BaseCmd cmd = new FakeCmd();
+ final Map<String, String> params = new HashMap<String, String>();
+ params.put(ApiConstants.COMMAND, "");
+ params.put("addedParam", "");
+ params.put(unknownParamKey, "");
+
+ // Execute
+ driveTest(cmd, params);
+
+ // Assert
+ assertTrue("There should be error msg, since there is one unknown parameter", loggerOutput.contains(unknownParamKey));
+ assertTrue("There should be error msg containing the correct command name", loggerOutput.contains(FAKE_CMD_NAME));
+ }
+
+ @Test
+ public void testHandleWithoutAuthorization() throws ResourceAllocationException {
+ final short type = Account.ACCOUNT_TYPE_NORMAL;
+ driveAuthTest(type);
+
+ // Assert
+ assertTrue("There should be error msg, since there is one unauthorized parameter", loggerOutput.contains("paramWithRole"));
+ assertTrue("There should be error msg containing the correct command name", loggerOutput.contains(FAKE_CMD_ROLE_NAME));
+ }
+
+ @Test
+ public void testHandleWithAuthorization() throws ResourceAllocationException {
+ final short type = Account.ACCOUNT_TYPE_ADMIN;
+ driveAuthTest(type);
+
+ // Assert
+ assertEquals("There should be no errors since parameters have authorization", null, loggerOutput);
+ }
+
+ protected void driveAuthTest(final short type) {
+ // Prepare
+ final BaseCmd cmd = new FakeCmdWithRoleAdmin();
+ final Account account = mock(Account.class);
+ ((FakeCmdWithRoleAdmin)cmd).account = account;
+ when(account.getType()).thenReturn(type);
+ final Map<String, String> params = new HashMap<String, String>();
+ params.put(ApiConstants.COMMAND, "");
+ params.put("addedParam", "");
+ params.put("paramWithRole", "");
+
+ // Execute
+ driveTest(cmd, params);
+ }
+}
+
+
+@APICommand(name=ParamGenericValidationWorkerTest.FAKE_CMD_NAME, responseObject=BaseResponse.class)
+class FakeCmd extends BaseCmd {
+
+ @Parameter(name = "addedParam")
+ private String addedParam;
+
+ public Account account;
+
+ @Override
+ protected Account getCurrentContextAccount() {
+ return account;
+ }
+
+ //
+ // Dummy methods for mere correct compilation
+ //
+ @Override
+ public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException,
+ NetworkRuleConflictException {
+ }
+ @Override
+ public String getCommandName() {
+ return null;
+ }
+ @Override
+ public long getEntityOwnerId() {
+ return 0;
+ }
+}
+
+@APICommand(name=ParamGenericValidationWorkerTest.FAKE_CMD_ROLE_NAME, responseObject=BaseResponse.class)
+class FakeCmdWithRoleAdmin extends FakeCmd {
+
+ @Parameter(name = "paramWithRole", authorized = {RoleType.Admin})
+ private String paramWithRole;
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c211f0bb/server/test/com/cloud/api/dispatch/ParamProcessWorkerTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/api/dispatch/ParamProcessWorkerTest.java b/server/test/com/cloud/api/dispatch/ParamProcessWorkerTest.java
new file mode 100644
index 0000000..12051a6
--- /dev/null
+++ b/server/test/com/cloud/api/dispatch/ParamProcessWorkerTest.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.cloud.api.dispatch;
+
+import java.util.HashMap;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ServerApiException;
+import org.apache.cloudstack.context.CallContext;
+
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.NetworkRuleConflictException;
+import com.cloud.exception.ResourceAllocationException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.user.Account;
+import com.cloud.user.AccountManager;
+import com.cloud.user.User;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ParamProcessWorkerTest {
+
+ @Mock
+ protected AccountManager accountManager;
+
+ protected ParamProcessWorker paramProcessWorker;
+
+ public static class TestCmd extends BaseCmd {
+
+ @Parameter(name = "strparam1")
+ String strparam1;
+
+ @Parameter(name = "intparam1", type = CommandType.INTEGER)
+ int intparam1;
+
+ @Parameter(name = "boolparam1", type = CommandType.BOOLEAN)
+ boolean boolparam1;
+
+ @Override
+ public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException,
+ ResourceAllocationException, NetworkRuleConflictException {
+ // well documented nothing
+ }
+
+ @Override
+ public String getCommandName() {
+ return "test";
+ }
+
+ @Override
+ public long getEntityOwnerId() {
+ return 0;
+ }
+
+ }
+
+ @Before
+ public void setup() {
+ CallContext.register(Mockito.mock(User.class), Mockito.mock(Account.class));
+ paramProcessWorker = new ParamProcessWorker();
+ paramProcessWorker._accountMgr = accountManager;
+ }
+
+ @After
+ public void cleanup() {
+ CallContext.unregister();
+ }
+
+ @Test
+ public void processParameters() {
+ final HashMap<String, String> params = new HashMap<String, String>();
+ params.put("strparam1", "foo");
+ params.put("intparam1", "100");
+ params.put("boolparam1", "true");
+ final TestCmd cmd = new TestCmd();
+ paramProcessWorker.processParameters(cmd, params);
+ Assert.assertEquals("foo", cmd.strparam1);
+ Assert.assertEquals(100, cmd.intparam1);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/c211f0bb/server/test/com/cloud/api/dispatch/SpecificCmdValidationWorkerTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/api/dispatch/SpecificCmdValidationWorkerTest.java b/server/test/com/cloud/api/dispatch/SpecificCmdValidationWorkerTest.java
new file mode 100644
index 0000000..4ae7200
--- /dev/null
+++ b/server/test/com/cloud/api/dispatch/SpecificCmdValidationWorkerTest.java
@@ -0,0 +1,48 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.api.dispatch;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import org.apache.cloudstack.api.BaseCmd;
+
+import com.cloud.exception.ResourceAllocationException;
+
+public class SpecificCmdValidationWorkerTest {
+
+ @Test
+ public void testHandle() throws ResourceAllocationException {
+ // Prepare
+ final BaseCmd cmd = mock(BaseCmd.class);
+ final Map<String, String> params = new HashMap<String, String>();
+
+ // Execute
+ final SpecificCmdValidationWorker worker = new SpecificCmdValidationWorker();
+
+ worker.handle(new DispatchTask(cmd, params));
+
+ // Assert
+ verify(cmd, times(1)).validateSpecificParameters(params);
+ }
+}