Skip to content

Commit 87130a1

Browse files
committed
added code to create test user accounts
1 parent d9397b6 commit 87130a1

2 files changed

Lines changed: 84 additions & 29 deletions

File tree

CloudCoderLoadTester/src/org/cloudcoder/app/loadtester/Client.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import org.cloudcoder.app.client.rpc.GetCoursesAndProblemsService;
99
import org.cloudcoder.app.client.rpc.LoginService;
1010
import org.cloudcoder.app.client.rpc.SubmitService;
11+
import org.cloudcoder.app.client.rpc.UserService;
1112
import org.cloudcoder.app.shared.model.Change;
1213
import org.cloudcoder.app.shared.model.CloudCoderAuthenticationException;
1314
import org.cloudcoder.app.shared.model.Course;
1415
import org.cloudcoder.app.shared.model.CourseAndCourseRegistration;
16+
import org.cloudcoder.app.shared.model.CourseRegistrationType;
17+
import org.cloudcoder.app.shared.model.EditedUser;
1518
import org.cloudcoder.app.shared.model.Problem;
1619
import org.cloudcoder.app.shared.model.QuizEndedException;
1720
import org.cloudcoder.app.shared.model.SubmissionException;
@@ -31,31 +34,10 @@
3134
* @see https://code.google.com/p/gwt-syncproxy/
3235
*/
3336
public class Client {
34-
// private String protocol;
35-
// private String host;
36-
// private int port;
37-
// private String contextPath;
3837
private HostConfig hostConfig;
3938
private HashMap<Class<?>, Object> serviceMap;
4039
private User user;
4140
private CookieManager cookieManager;
42-
43-
// /**
44-
// * Constructor.
45-
// *
46-
// * @param protocol protocol for CloudCoder webapp (e.g., "http" or "https")
47-
// * @param host hostname of CloudCoder webapp
48-
// * @param port port of CloudCoder webapp
49-
// * @param contextPath context path of CloudCoder webapp
50-
// */
51-
// public Client(String protocol, String host, int port, String contextPath) {
52-
// this.protocol = protocol;
53-
// this.host = host;
54-
// this.port = port;
55-
// this.contextPath = contextPath;
56-
// this.serviceMap = new HashMap<Class<?>, Object>();
57-
// this.cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
58-
// }
5941

6042
/**
6143
* Constructor.
@@ -207,4 +189,25 @@ public SubmissionResult submitCode(int problemId, String code, long pollInterval
207189
Thread.sleep(pollIntervalMs);
208190
}
209191
}
192+
193+
/**
194+
* Add user to given couse, creating if necessary.
195+
*
196+
* @param user the user
197+
* @param course the course
198+
* @param regType the user's registration type
199+
* @param section the course section
200+
* @throws CloudCoderAuthenticationException
201+
*/
202+
public void createUser(User user, Course course, CourseRegistrationType regType, int section) throws CloudCoderAuthenticationException {
203+
UserService userSvc = getService(UserService.class);
204+
205+
EditedUser editedUser = new EditedUser();
206+
editedUser.setUser(user);
207+
editedUser.setPassword(user.getPasswordHash());
208+
editedUser.setRegistrationType(regType);
209+
editedUser.setSection(section);
210+
211+
userSvc.addUserToCourse(editedUser, course.getId());
212+
}
210213
}
Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,68 @@
11
package org.cloudcoder.app.loadtester;
22

3-
import java.io.IOException;
4-
import java.util.Properties;
3+
import java.util.Scanner;
54

6-
import org.cloudcoder.app.server.persist.JDBCDatabaseConfig;
7-
import org.cloudcoder.app.server.persist.util.DBUtil;
5+
import org.cloudcoder.app.shared.model.CloudCoderAuthenticationException;
6+
import org.cloudcoder.app.shared.model.CourseAndCourseRegistration;
7+
import org.cloudcoder.app.shared.model.CourseRegistrationType;
8+
import org.cloudcoder.app.shared.model.User;
89

910
public class PrepareDatabaseForLoadTesting {
10-
public static void main(String[] args) throws IOException {
11-
final Properties config = DBUtil.getConfigProperties();
12-
JDBCDatabaseConfig.createFromProperties(config);
11+
public static void main(String[] args) throws Exception {
12+
Scanner keyboard = new Scanner(System.in);
13+
createTestUserAccounts(keyboard);
14+
}
15+
16+
private static void createTestUserAccounts(Scanner keyboard)
17+
throws CloudCoderAuthenticationException {
18+
HostConfig hostConfig = HostConfigDatabase.forName("default");
19+
20+
Client client = new Client(hostConfig);
21+
22+
// Must be logged in using an instructor account
23+
System.out.println("Instructor username: ");
24+
String userName = keyboard.nextLine();
25+
System.out.println("Instructor password: ");
26+
String password = keyboard.nextLine();
27+
28+
client.login(userName, password);
29+
30+
System.out.println("Course name: ");
31+
String courseName = keyboard.nextLine();
32+
33+
// Find the course and make sure user is really an instructor
34+
CourseAndCourseRegistration theCCR = null;
35+
36+
CourseAndCourseRegistration[] courses = client.getRegisteredCourses();
37+
for (CourseAndCourseRegistration ccr : courses) {
38+
if (ccr.getCourse().getName().equals(courseName)) {
39+
theCCR = ccr;
40+
break;
41+
}
42+
}
43+
44+
if (theCCR == null) {
45+
System.out.println("Could not find course " + courseName);
46+
System.exit(1);
47+
}
48+
if (!theCCR.getCourseRegistration().getRegistrationType().isInstructor()) {
49+
System.out.println("User is not an instructor");
50+
System.exit(1);
51+
}
1352

14-
//Database.getInstance().
53+
for (int n = 1; n <= 10; n++) {
54+
User user = new User();
55+
String testUserName = "user" + n;
56+
user.setUsername(testUserName);
57+
user.setFirstname("Test");
58+
user.setLastname("User");
59+
user.setPasswordHash(testUserName); // set to plaintext when adding/registering a user
60+
user.setEmail(testUserName + "@unseen.edu");
61+
user.setConsent("");
62+
user.setWebsite("http://student.unseen.edu/~" + testUserName);
63+
64+
System.out.println("Adding user " + testUserName);
65+
client.createUser(user, theCCR.getCourse(), CourseRegistrationType.STUDENT, 101);
66+
}
1567
}
1668
}

0 commit comments

Comments
 (0)