Unit test additions (and some minor command class changes to support testing)

Signed-off-by: Chip Childers <chip.childers@gmail.com>
This commit is contained in:
Meghna Kale 2012-11-20 12:21:54 -05:00 committed by Chip Childers
parent 66ca4323b4
commit b7c74ee78b
7 changed files with 402 additions and 8 deletions

View File

@ -40,5 +40,6 @@
<build>
<defaultGoal>install</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
</build>
</project>

View File

@ -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();
}

View File

@ -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();
}
}
}

View File

@ -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());
}
}

View File

@ -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 "customer@abc.com";
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());
}
}*/
}

View File

@ -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());
}
}
}

View File

@ -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>