Search in sources :

Example 51 with CourseVO

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());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CourseVO(org.olat.restapi.support.vo.CourseVO) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) File(java.io.File) URI(java.net.URI) URL(java.net.URL) Test(org.junit.Test)

Example 52 with CourseVO

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()));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CourseVO(org.olat.restapi.support.vo.CourseVO) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) File(java.io.File) URI(java.net.URI) URL(java.net.URL) Test(org.junit.Test)

Example 53 with CourseVO

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();
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) CourseVOes(org.olat.restapi.support.vo.CourseVOes) Identity(org.olat.core.id.Identity) URI(java.net.URI) Test(org.junit.Test)

Example 54 with CourseVO

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CourseVO(org.olat.restapi.support.vo.CourseVO) RestConnection(org.olat.restapi.RestConnection) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI)

Example 55 with CourseVO

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;
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) ArrayList(java.util.ArrayList) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

CourseVO (org.olat.restapi.support.vo.CourseVO)70 Test (org.junit.Test)48 URI (java.net.URI)46 HttpResponse (org.apache.http.HttpResponse)46 RepositoryEntry (org.olat.repository.RepositoryEntry)34 HttpGet (org.apache.http.client.methods.HttpGet)26 ICourse (org.olat.course.ICourse)26 Identity (org.olat.core.id.Identity)20 InputStream (java.io.InputStream)18 Produces (javax.ws.rs.Produces)16 HttpEntity (org.apache.http.HttpEntity)16 HttpPut (org.apache.http.client.methods.HttpPut)16 File (java.io.File)14 HttpPost (org.apache.http.client.methods.HttpPost)14 CourseVOes (org.olat.restapi.support.vo.CourseVOes)14 URL (java.net.URL)12 GET (javax.ws.rs.GET)8 Path (javax.ws.rs.Path)8 UserRequest (org.olat.core.gui.UserRequest)8 RepositoryManager (org.olat.repository.RepositoryManager)8