Unit test additions (and some minor command class changes to support testing)
Signed-off-by: Chip Childers <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/b7c74ee7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/b7c74ee7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/b7c74ee7

Branch: refs/heads/master
Commit: b7c74ee78b257af6a29cd8bb40bcd7b89b794529
Parents: 66ca432
Author: Meghna Kale <[email protected]>
Authored: Tue Nov 20 12:21:54 2012 -0500
Committer: Chip Childers <[email protected]>
Committed: Tue Nov 20 12:21:54 2012 -0500

----------------------------------------------------------------------
 api/pom.xml                                        |    1 +
 .../com/cloud/api/commands/ActivateProjectCmd.java |    6 +-
 .../cloud/api/commands/AddAccountToProjectCmd.java |   10 +-
 .../cloud/api/commands/ActivateProjectCmdTest.java |   81 +++++++
 .../api/commands/AddAccountToProjectCmdTest.java   |  175 +++++++++++++++
 .../com/cloud/api/commands/AddClusterCmdTest.java  |  131 +++++++++++
 pom.xml                                            |    6 +
 7 files changed, 402 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b7c74ee7/api/pom.xml
----------------------------------------------------------------------
diff --git a/api/pom.xml b/api/pom.xml
index f25a6bc..7461c67 100644
--- a/api/pom.xml
+++ b/api/pom.xml
@@ -40,5 +40,6 @@
   <build>
     <defaultGoal>install</defaultGoal>
     <sourceDirectory>src</sourceDirectory>
+    <testSourceDirectory>test</testSourceDirectory>
   </build>
 </project>

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b7c74ee7/api/src/com/cloud/api/commands/ActivateProjectCmd.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/api/commands/ActivateProjectCmd.java 
b/api/src/com/cloud/api/commands/ActivateProjectCmd.java
index 20b5507..7247767 100644
--- a/api/src/com/cloud/api/commands/ActivateProjectCmd.java
+++ b/api/src/com/cloud/api/commands/ActivateProjectCmd.java
@@ -61,13 +61,13 @@ public class ActivateProjectCmd extends BaseAsyncCmd {
     
     @Override
     public long getEntityOwnerId() {
-        Project project= _projectService.getProject(id);
+        Project project= _projectService.getProject(getId());
         //verify input parameters
         if (project == null) {
-            throw new InvalidParameterValueException("Unable to find project 
by id " + id);
+            throw new InvalidParameterValueException("Unable to find project 
by id " + getId());
         } 
         
-        return _projectService.getProjectOwner(id).getId(); 
+        return _projectService.getProjectOwner(getId()).getId(); 
     }
  
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b7c74ee7/api/src/com/cloud/api/commands/AddAccountToProjectCmd.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/api/commands/AddAccountToProjectCmd.java 
b/api/src/com/cloud/api/commands/AddAccountToProjectCmd.java
index 5178aa3..73609fb 100644
--- a/api/src/com/cloud/api/commands/AddAccountToProjectCmd.java
+++ b/api/src/com/cloud/api/commands/AddAccountToProjectCmd.java
@@ -98,15 +98,15 @@ public class AddAccountToProjectCmd extends BaseAsyncCmd {
     
     @Override
     public long getEntityOwnerId() {
-        Project project= _projectService.getProject(projectId);
+        Project project= _projectService.getProject(getProjectId());
         //verify input parameters
         if (project == null) {
                InvalidParameterValueException ex = new 
InvalidParameterValueException("Unable to find project with specified id");
-               ex.addProxyObject(project, projectId, "projectId");            
+               ex.addProxyObject(project, getProjectId(), "projectId");        
    
             throw ex;
         } 
         
-        return _projectService.getProjectOwner(projectId).getId(); 
+        return _projectService.getProjectOwner(getProjectId()).getId(); 
     }
     
     @Override
@@ -117,9 +117,9 @@ public class AddAccountToProjectCmd extends BaseAsyncCmd {
     @Override
     public String getEventDescription() {
         if (accountName != null) {
-            return  "Adding account " + accountName + " to project: " + 
projectId;
+            return  "Adding account " + getAccountName() + " to project: " + 
getProjectId();
         } else {
-            return  "Sending invitation to email " + email + " to join 
project: " + projectId;
+            return  "Sending invitation to email " + email + " to join 
project: " + getProjectId();
         }  
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b7c74ee7/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java
----------------------------------------------------------------------
diff --git a/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java 
b/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java
new file mode 100644
index 0000000..1b06c5d
--- /dev/null
+++ b/api/test/com/cloud/api/commands/ActivateProjectCmdTest.java
@@ -0,0 +1,81 @@
+// 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.commands;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.projects.Project;
+import com.cloud.projects.ProjectService;
+import com.cloud.user.Account;
+
+public class ActivateProjectCmdTest extends TestCase {
+       
+       private ActivateProjectCmd activateProjectCmd;
+       
+       @Rule
+       public ExpectedException expectedException = ExpectedException.none();
+       
+       @Before
+       public void setUp() {
+               activateProjectCmd = new ActivateProjectCmd(){
+                       
+                       @Override
+                       public Long getId() {
+                               return 2L;
+                       }
+               };
+       }
+       
+       @Test
+       public void testGetEntityOwnerIdForNullProject() {
+               ProjectService projectService = 
Mockito.mock(ProjectService.class);
+               
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(null);
+               activateProjectCmd._projectService = projectService;
+               
+               try {
+                       activateProjectCmd.getEntityOwnerId();
+               } catch(InvalidParameterValueException exception) {
+                       Assert.assertEquals("Unable to find project by id 2", 
exception.getLocalizedMessage());
+               }
+       }
+       
+       @Test
+       public void testGetEntityOwnerIdForProject() {
+               Project project = Mockito.mock(Project.class);
+               Mockito.when(project.getId()).thenReturn(2L);
+               ProjectService projectService = 
Mockito.mock(ProjectService.class);
+               Account account = Mockito.mock(Account.class);
+               Mockito.when(account.getId()).thenReturn(2L);
+               
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(project);
+               
+               
Mockito.when(projectService.getProjectOwner(Mockito.anyLong())).thenReturn(account);
+               activateProjectCmd._projectService = projectService;
+               
+               Assert.assertEquals(2L, activateProjectCmd.getEntityOwnerId());
+               
+       }
+       
+       
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b7c74ee7/api/test/com/cloud/api/commands/AddAccountToProjectCmdTest.java
----------------------------------------------------------------------
diff --git a/api/test/com/cloud/api/commands/AddAccountToProjectCmdTest.java 
b/api/test/com/cloud/api/commands/AddAccountToProjectCmdTest.java
new file mode 100644
index 0000000..152a089
--- /dev/null
+++ b/api/test/com/cloud/api/commands/AddAccountToProjectCmdTest.java
@@ -0,0 +1,175 @@
+// 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.commands;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.projects.Project;
+import com.cloud.projects.ProjectService;
+import com.cloud.user.Account;
+
+public class AddAccountToProjectCmdTest extends TestCase {
+
+       
+       private AddAccountToProjectCmd addAccountToProjectCmd;
+       
+       @Rule
+       public ExpectedException expectedException = ExpectedException.none();
+       
+       @Before
+       public void setUp() {
+               addAccountToProjectCmd = new AddAccountToProjectCmd(){
+                       
+                       @Override
+                       public Long getProjectId() {
+                               return 2L;
+                       }
+                       
+                       @Override
+                       public String getAccountName() {
+                               
+       // to run the test testGetEventDescriptionForAccount set the 
accountName 
+//                                     return "accountName";
+       // to run the test the testGetEventDescriptionForNullAccount return 
accountname as null
+                     return null;
+                   }
+                       
+                       @Override
+                        public String getEmail() {
+//                   return "[email protected]";
+                               return null;
+                           }
+
+               };
+       }
+       
+       
+       /****
+        * Condition not handled in the code
+        * 
+       *****/
+       
+       /*@Test
+       public void testGetEntityOwnerIdForNullProject() {
+               
+               ProjectService projectService = 
Mockito.mock(ProjectService.class);
+               
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(null);
+               addAccountToProjectCmd._projectService = projectService;
+               
+               try {
+                       addAccountToProjectCmd.getEntityOwnerId();
+               } catch(InvalidParameterValueException exception) {
+                       Assert.assertEquals("Unable to find project by id 2", 
exception.getLocalizedMessage());
+               }
+               
+       }*/
+       
+       
+       @Test
+       public void testGetEntityOwnerIdForProject() {
+
+               Project project = Mockito.mock(Project.class);
+               Mockito.when(project.getId()).thenReturn(2L);
+               
+               ProjectService projectService = 
Mockito.mock(ProjectService.class);
+               Account account = Mockito.mock(Account.class);
+               
+               Mockito.when(account.getId()).thenReturn(2L);
+               
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(
+                               project);
+
+               Mockito.when(projectService.getProjectOwner(Mockito.anyLong()))
+                               .thenReturn(account);
+               addAccountToProjectCmd._projectService = projectService;
+
+               Assert.assertEquals(2L, 
addAccountToProjectCmd.getEntityOwnerId());
+
+       }       
+       
+       /**
+        * To run the test uncomment the return statement for getAccountName() 
in setup() and return null 
+        * 
+        * **/
+       
+       
+       /*@Test
+       public void testGetEventDescriptionForNullAccount() {
+               
+               String result = addAccountToProjectCmd.getEventDescription();
+               String expected = "Sending invitation to email null to join 
project: 2";
+               Assert.assertEquals(expected, result);
+               
+       }*/
+       
+       /***
+        * 
+        * 
+        * 
+        * ***/
+       
+       
+       
+       /*@Test
+       public void testGetEventDescriptionForAccount() {
+               
+               String result = addAccountToProjectCmd.getEventDescription();
+               String expected = "Adding account accountName to project: 2";
+               Assert.assertEquals(expected, result);
+               
+       }*/
+
+       @Test
+       public void testExecuteForNullAccountNameEmail() {
+               
+               try {
+                       addAccountToProjectCmd.execute();
+               } catch(InvalidParameterValueException exception) {
+                       Assert.assertEquals("Either accountName or email is 
required", exception.getLocalizedMessage());
+               }
+               
+       }
+
+       
+       /*@Test
+       public void testExecuteForAccountNameEmail() {
+               
+               try {
+                       
+                       ComponentLocator c = 
Mockito.mock(ComponentLocator.class);
+                       UserContext userContext = 
Mockito.mock(UserContext.class);
+                       
+//                     
Mockito.when(userContext.current()).thenReturn(userContext);
+                       
+                       
+                       addAccountToProjectCmd.execute();
+               } catch(InvalidParameterValueException exception) {
+                       Assert.assertEquals("Either accountName or email is 
required", exception.getLocalizedMessage());
+               }
+               
+       }*/
+
+       
+       
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b7c74ee7/api/test/com/cloud/api/commands/AddClusterCmdTest.java
----------------------------------------------------------------------
diff --git a/api/test/com/cloud/api/commands/AddClusterCmdTest.java 
b/api/test/com/cloud/api/commands/AddClusterCmdTest.java
new file mode 100644
index 0000000..6473531
--- /dev/null
+++ b/api/test/com/cloud/api/commands/AddClusterCmdTest.java
@@ -0,0 +1,131 @@
+// 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.commands;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+
+import com.cloud.api.ServerApiException;
+import com.cloud.exception.DiscoveryException;
+import com.cloud.exception.ResourceInUseException;
+import com.cloud.org.Cluster;
+import com.cloud.resource.ResourceService;
+
+public class AddClusterCmdTest extends TestCase {
+       
+       private AddClusterCmd addClusterCmd;
+       
+       @Rule
+       public ExpectedException expectedException = ExpectedException.none();
+
+       @Before
+       public void setUp() {
+               addClusterCmd = new AddClusterCmd(){
+               };
+       }
+
+       
+       
+       @Test
+       public void testExecuteForNullResult() {
+
+               ResourceService resourceService = 
Mockito.mock(ResourceService.class);
+               
+               try {
+                       
Mockito.when(resourceService.discoverCluster(addClusterCmd)).thenReturn(null);
+               } catch (ResourceInUseException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (IllegalArgumentException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (DiscoveryException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
+               
+               addClusterCmd._resourceService = resourceService;
+               
+               try { 
+                       addClusterCmd.execute();
+               } catch (ServerApiException exception) {
+                       Assert.assertEquals("Failed to add cluster", 
exception.getDescription());
+               }
+               
+       }
+       
+       
+       @Test
+       public void testExecuteForEmptyResult() {
+
+               ResourceService resourceService = 
Mockito.mock(ResourceService.class);
+               addClusterCmd._resourceService = resourceService;
+       
+               try {
+                   addClusterCmd.execute();
+               } catch (ServerApiException exception) {
+                       Assert.assertEquals("Failed to add cluster", 
exception.getDescription());
+               }
+               
+       }
+       
+       
+       
+       @Test
+       public void testExecuteForResult() {
+
+               ResourceService resourceService = 
Mockito.mock(ResourceService.class);
+               
+               List<? extends Cluster> result = new ArrayList<Cluster>();
+               Cluster cluster = Mockito.mock(Cluster.class);
+//             result.add(cluster);
+               
+               /*try {
+                       
Mockito.when(resourceService.discoverCluster(addClusterCmd)).thenReturn(Arrays.asList(result));
+               } catch (ResourceInUseException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (IllegalArgumentException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               } catch (DiscoveryException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }*/
+               
+               addClusterCmd._resourceService = resourceService;
+               
+               try { 
+                       addClusterCmd.execute();
+               } catch (ServerApiException exception) {
+                       Assert.assertEquals("Failed to add cluster", 
exception.getDescription());
+               }
+               
+       }
+       
+}
+
+ 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b7c74ee7/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 4a02aa9..0ce02ef 100644
--- a/pom.xml
+++ b/pom.xml
@@ -178,6 +178,12 @@
       <version>${cs.junit.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+       <groupId>org.mockito</groupId>
+       <artifactId>mockito-all</artifactId>
+       <version>1.9.5</version>
+        <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>

Reply via email to