Skip to content

Commit b7c74ee

Browse files
Meghna Kalechipchilders
authored andcommitted
Unit test additions (and some minor command class changes to support testing)
Signed-off-by: Chip Childers <chip.childers@gmail.com>
1 parent 66ca432 commit b7c74ee

7 files changed

Lines changed: 402 additions & 8 deletions

File tree

‎api/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@
4040
<build>
4141
<defaultGoal>install</defaultGoal>
4242
<sourceDirectory>src</sourceDirectory>
43+
<testSourceDirectory>test</testSourceDirectory>
4344
</build>
4445
</project>

‎api/src/com/cloud/api/commands/ActivateProjectCmd.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ public String getCommandName() {
6161

6262
@Override
6363
public long getEntityOwnerId() {
64-
Project project= _projectService.getProject(id);
64+
Project project= _projectService.getProject(getId());
6565
//verify input parameters
6666
if (project == null) {
67-
throw new InvalidParameterValueException("Unable to find project by id " + id);
67+
throw new InvalidParameterValueException("Unable to find project by id " + getId());
6868
}
6969

70-
return _projectService.getProjectOwner(id).getId();
70+
return _projectService.getProjectOwner(getId()).getId();
7171
}
7272

7373

‎api/src/com/cloud/api/commands/AddAccountToProjectCmd.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ public void execute(){
9898

9999
@Override
100100
public long getEntityOwnerId() {
101-
Project project= _projectService.getProject(projectId);
101+
Project project= _projectService.getProject(getProjectId());
102102
//verify input parameters
103103
if (project == null) {
104104
InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project with specified id");
105-
ex.addProxyObject(project, projectId, "projectId");
105+
ex.addProxyObject(project, getProjectId(), "projectId");
106106
throw ex;
107107
}
108108

109-
return _projectService.getProjectOwner(projectId).getId();
109+
return _projectService.getProjectOwner(getProjectId()).getId();
110110
}
111111

112112
@Override
@@ -117,9 +117,9 @@ public String getEventType() {
117117
@Override
118118
public String getEventDescription() {
119119
if (accountName != null) {
120-
return "Adding account " + accountName + " to project: " + projectId;
120+
return "Adding account " + getAccountName() + " to project: " + getProjectId();
121121
} else {
122-
return "Sending invitation to email " + email + " to join project: " + projectId;
122+
return "Sending invitation to email " + email + " to join project: " + getProjectId();
123123
}
124124
}
125125
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api.commands;
18+
19+
import junit.framework.Assert;
20+
import junit.framework.TestCase;
21+
22+
import org.junit.Before;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.rules.ExpectedException;
26+
import org.mockito.Mockito;
27+
28+
import com.cloud.exception.InvalidParameterValueException;
29+
import com.cloud.projects.Project;
30+
import com.cloud.projects.ProjectService;
31+
import com.cloud.user.Account;
32+
33+
public class ActivateProjectCmdTest extends TestCase {
34+
35+
private ActivateProjectCmd activateProjectCmd;
36+
37+
@Rule
38+
public ExpectedException expectedException = ExpectedException.none();
39+
40+
@Before
41+
public void setUp() {
42+
activateProjectCmd = new ActivateProjectCmd(){
43+
44+
@Override
45+
public Long getId() {
46+
return 2L;
47+
}
48+
};
49+
}
50+
51+
@Test
52+
public void testGetEntityOwnerIdForNullProject() {
53+
ProjectService projectService = Mockito.mock(ProjectService.class);
54+
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(null);
55+
activateProjectCmd._projectService = projectService;
56+
57+
try {
58+
activateProjectCmd.getEntityOwnerId();
59+
} catch(InvalidParameterValueException exception) {
60+
Assert.assertEquals("Unable to find project by id 2", exception.getLocalizedMessage());
61+
}
62+
}
63+
64+
@Test
65+
public void testGetEntityOwnerIdForProject() {
66+
Project project = Mockito.mock(Project.class);
67+
Mockito.when(project.getId()).thenReturn(2L);
68+
ProjectService projectService = Mockito.mock(ProjectService.class);
69+
Account account = Mockito.mock(Account.class);
70+
Mockito.when(account.getId()).thenReturn(2L);
71+
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(project);
72+
73+
Mockito.when(projectService.getProjectOwner(Mockito.anyLong())).thenReturn(account);
74+
activateProjectCmd._projectService = projectService;
75+
76+
Assert.assertEquals(2L, activateProjectCmd.getEntityOwnerId());
77+
78+
}
79+
80+
81+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.api.commands;
18+
19+
import junit.framework.Assert;
20+
import junit.framework.TestCase;
21+
22+
import org.junit.Before;
23+
import org.junit.Rule;
24+
import org.junit.Test;
25+
import org.junit.rules.ExpectedException;
26+
import org.mockito.Mockito;
27+
28+
import com.cloud.exception.InvalidParameterValueException;
29+
import com.cloud.projects.Project;
30+
import com.cloud.projects.ProjectService;
31+
import com.cloud.user.Account;
32+
33+
public class AddAccountToProjectCmdTest extends TestCase {
34+
35+
36+
private AddAccountToProjectCmd addAccountToProjectCmd;
37+
38+
@Rule
39+
public ExpectedException expectedException = ExpectedException.none();
40+
41+
@Before
42+
public void setUp() {
43+
addAccountToProjectCmd = new AddAccountToProjectCmd(){
44+
45+
@Override
46+
public Long getProjectId() {
47+
return 2L;
48+
}
49+
50+
@Override
51+
public String getAccountName() {
52+
53+
// to run the test testGetEventDescriptionForAccount set the accountName
54+
// return "accountName";
55+
// to run the test the testGetEventDescriptionForNullAccount return accountname as null
56+
return null;
57+
}
58+
59+
@Override
60+
public String getEmail() {
61+
// return "customer@abc.com";
62+
return null;
63+
}
64+
65+
};
66+
}
67+
68+
69+
/****
70+
* Condition not handled in the code
71+
*
72+
*****/
73+
74+
/*@Test
75+
public void testGetEntityOwnerIdForNullProject() {
76+
77+
ProjectService projectService = Mockito.mock(ProjectService.class);
78+
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(null);
79+
addAccountToProjectCmd._projectService = projectService;
80+
81+
try {
82+
addAccountToProjectCmd.getEntityOwnerId();
83+
} catch(InvalidParameterValueException exception) {
84+
Assert.assertEquals("Unable to find project by id 2", exception.getLocalizedMessage());
85+
}
86+
87+
}*/
88+
89+
90+
@Test
91+
public void testGetEntityOwnerIdForProject() {
92+
93+
Project project = Mockito.mock(Project.class);
94+
Mockito.when(project.getId()).thenReturn(2L);
95+
96+
ProjectService projectService = Mockito.mock(ProjectService.class);
97+
Account account = Mockito.mock(Account.class);
98+
99+
Mockito.when(account.getId()).thenReturn(2L);
100+
Mockito.when(projectService.getProject(Mockito.anyLong())).thenReturn(
101+
project);
102+
103+
Mockito.when(projectService.getProjectOwner(Mockito.anyLong()))
104+
.thenReturn(account);
105+
addAccountToProjectCmd._projectService = projectService;
106+
107+
Assert.assertEquals(2L, addAccountToProjectCmd.getEntityOwnerId());
108+
109+
}
110+
111+
/**
112+
* To run the test uncomment the return statement for getAccountName() in setup() and return null
113+
*
114+
* **/
115+
116+
117+
/*@Test
118+
public void testGetEventDescriptionForNullAccount() {
119+
120+
String result = addAccountToProjectCmd.getEventDescription();
121+
String expected = "Sending invitation to email null to join project: 2";
122+
Assert.assertEquals(expected, result);
123+
124+
}*/
125+
126+
/***
127+
*
128+
*
129+
*
130+
* ***/
131+
132+
133+
134+
/*@Test
135+
public void testGetEventDescriptionForAccount() {
136+
137+
String result = addAccountToProjectCmd.getEventDescription();
138+
String expected = "Adding account accountName to project: 2";
139+
Assert.assertEquals(expected, result);
140+
141+
}*/
142+
143+
@Test
144+
public void testExecuteForNullAccountNameEmail() {
145+
146+
try {
147+
addAccountToProjectCmd.execute();
148+
} catch(InvalidParameterValueException exception) {
149+
Assert.assertEquals("Either accountName or email is required", exception.getLocalizedMessage());
150+
}
151+
152+
}
153+
154+
155+
/*@Test
156+
public void testExecuteForAccountNameEmail() {
157+
158+
try {
159+
160+
ComponentLocator c = Mockito.mock(ComponentLocator.class);
161+
UserContext userContext = Mockito.mock(UserContext.class);
162+
163+
// Mockito.when(userContext.current()).thenReturn(userContext);
164+
165+
166+
addAccountToProjectCmd.execute();
167+
} catch(InvalidParameterValueException exception) {
168+
Assert.assertEquals("Either accountName or email is required", exception.getLocalizedMessage());
169+
}
170+
171+
}*/
172+
173+
174+
175+
}

0 commit comments

Comments
 (0)