use of org.olat.restapi.support.vo.CourseVO in project OpenOLAT by OpenOLAT.
the class CoursesTest method testImportCourse.
@Test
public void testImportCourse() throws IOException, URISyntaxException {
URL cpUrl = CoursesTest.class.getResource("Course_with_blog.zip");
assertNotNull(cpUrl);
File cp = new File(cpUrl.toURI());
assertTrue(conn.login("administrator", "openolat"));
URI request = UriBuilder.fromUri(getContextURI()).path("repo/courses").build();
HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON);
String softKey = UUID.randomUUID().toString().replace("-", "").substring(0, 30);
HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()).addTextBody("filename", "Very_small_course.zip").addTextBody("foldername", "New folder 1 2 3").addTextBody("resourcename", "Very small course").addTextBody("displayname", "Very small course").addTextBody("access", "3").addTextBody("softkey", softKey).build();
method.setEntity(entity);
HttpResponse response = conn.execute(method);
assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
CourseVO vo = conn.parse(response, CourseVO.class);
assertNotNull(vo);
assertNotNull(vo.getRepoEntryKey());
assertNotNull(vo.getKey());
Long repoKey = vo.getRepoEntryKey();
RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(repoKey);
assertNotNull(re);
assertNotNull(re.getOlatResource());
assertEquals("Very small course", re.getDisplayname());
assertEquals(softKey, re.getSoftkey());
}
use of org.olat.restapi.support.vo.CourseVO in project OpenOLAT by OpenOLAT.
the class CoursesTest method testImportCourse_owner.
@Test
public void testImportCourse_owner() throws IOException, URISyntaxException {
URL cpUrl = CoursesTest.class.getResource("Course_with_blog.zip");
File cp = new File(cpUrl.toURI());
String username = "ownerImportCourse";
Identity owner = JunitTestHelper.createAndPersistIdentityAsUser(username);
assertTrue(conn.login("administrator", "openolat"));
URI request = UriBuilder.fromUri(getContextURI()).path("repo/courses").queryParam("ownerUsername", owner.getName()).build();
HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON);
String softKey = UUID.randomUUID().toString().replace("-", "").substring(0, 30);
HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody("file", cp, ContentType.APPLICATION_OCTET_STREAM, cp.getName()).addTextBody("filename", "Very_small_course.zip").addTextBody("foldername", "New folder 1 2 3").addTextBody("resourcename", "Very small course").addTextBody("displayname", "Very small course").addTextBody("access", "3").addTextBody("softkey", softKey).build();
method.setEntity(entity);
HttpResponse response = conn.execute(method);
assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
CourseVO vo = conn.parse(response, CourseVO.class);
Long repoKey = vo.getRepoEntryKey();
RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(repoKey);
assertTrue(repositoryEntryRelationDao.hasRole(owner, re, GroupRoles.owner.name()));
}
use of org.olat.restapi.support.vo.CourseVO in project OpenOLAT by OpenOLAT.
the class UserCoursesTest method testMyCourses.
@Test
public void testMyCourses() throws IOException, URISyntaxException {
// prepare a course with a participant
Identity user = JunitTestHelper.createAndPersistIdentityAsUser("My-course-" + UUID.randomUUID().toString());
ICourse course = CoursesWebService.createEmptyCourse(user, "My course 1", "My course", null);
RepositoryEntry courseRe = repositoryManager.lookupRepositoryEntry(course, true);
repositoryManager.setAccess(courseRe, RepositoryEntry.ACC_OWNERS, true);
repositoryService.addRole(user, courseRe, GroupRoles.participant.name());
dbInstance.commitAndCloseSession();
RestConnection conn = new RestConnection();
Assert.assertTrue(conn.login(user.getName(), JunitTestHelper.PWD));
// without paging
URI request = UriBuilder.fromUri(getContextURI()).path("/users").path(user.getKey().toString()).path("/courses/my").build();
HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
InputStream body = response.getEntity().getContent();
List<CourseVO> courses = parseCourseArray(body);
Assert.assertNotNull(courses);
Assert.assertEquals(1, courses.size());
// with paging
URI pagedRequest = UriBuilder.fromUri(getContextURI()).path("/users").path(user.getKey().toString()).path("/courses/my").queryParam("start", "0").queryParam("limit", "10").build();
HttpGet pagedMethod = conn.createGet(pagedRequest, MediaType.APPLICATION_JSON + ";pagingspec=1.0", true);
HttpResponse pagedResponse = conn.execute(pagedMethod);
Assert.assertEquals(200, pagedResponse.getStatusLine().getStatusCode());
InputStream pagedBody = pagedResponse.getEntity().getContent();
CourseVOes pagedCourses = conn.parse(pagedBody, CourseVOes.class);
Assert.assertNotNull(pagedCourses);
Assert.assertEquals(1, pagedCourses.getTotalCount());
Assert.assertNotNull(pagedCourses.getCourses());
Assert.assertEquals(1, pagedCourses.getCourses().length);
conn.shutdown();
}
use of org.olat.restapi.support.vo.CourseVO in project OpenOLAT by OpenOLAT.
the class RepositoryRestClient method deployCourse.
public CourseVO deployCourse(File archive, String resourcename, String displayname) throws URISyntaxException, IOException {
RestConnection conn = new RestConnection(deploymentUrl);
assertTrue(conn.login(username, password));
URI request = UriBuilder.fromUri(deploymentUrl.toURI()).path("restapi").path("repo/courses").build();
HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON);
String softKey = UUID.randomUUID().toString();
HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody("file", archive, ContentType.APPLICATION_OCTET_STREAM, archive.getName()).addTextBody("filename", archive.getName()).addTextBody("resourcename", resourcename).addTextBody("displayname", displayname).addTextBody("access", "3").addTextBody("softkey", softKey).build();
method.setEntity(entity);
HttpResponse response = conn.execute(method);
assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
CourseVO vo = conn.parse(response, CourseVO.class);
assertNotNull(vo);
assertNotNull(vo.getRepoEntryKey());
assertNotNull(vo.getKey());
return vo;
}
use of org.olat.restapi.support.vo.CourseVO in project openolat by klemens.
the class CoursesWebService method toCourseVo.
public static CourseVO[] toCourseVo(List<RepositoryEntry> repoEntries) {
List<CourseVO> voList = new ArrayList<CourseVO>();
int count = 0;
for (RepositoryEntry repoEntry : repoEntries) {
try {
ICourse course = loadCourse(repoEntry.getOlatResource().getResourceableId());
voList.add(ObjectFactory.get(repoEntry, course));
if (count % 33 == 0) {
DBFactory.getInstance().commitAndCloseSession();
}
} catch (Exception e) {
log.error("Cannot load the course with this repository entry: " + repoEntry, e);
}
}
CourseVO[] vos = new CourseVO[voList.size()];
voList.toArray(vos);
return vos;
}
Aggregations