Search in sources :

Example 61 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project openolat by klemens.

the class CoursesTest method testGetCourses.

@Test
public void testGetCourses() throws IOException, URISyntaxException {
    assertTrue(conn.login("administrator", "openolat"));
    URI request = UriBuilder.fromUri(getContextURI()).path("/repo/courses").build();
    HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    List<CourseVO> courses = parseCourseArray(body);
    assertNotNull(courses);
    assertTrue(courses.size() >= 2);
    boolean vo1 = false;
    boolean vo2 = false;
    for (CourseVO course : courses) {
        Long repoEntryKey = course.getRepoEntryKey();
        if (repoEntryKey != null && re1.getKey().equals(repoEntryKey)) {
            vo1 = true;
            Assert.assertEquals("courses1", course.getTitle());
        }
        if (repoEntryKey != null && re2.getKey().equals(repoEntryKey)) {
            vo2 = true;
            Assert.assertEquals("courses2", course.getTitle());
        }
    }
    Assert.assertTrue(vo1);
    Assert.assertTrue(vo2);
}
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) URI(java.net.URI) Test(org.junit.Test)

Example 62 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project openolat by klemens.

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 63 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project openolat by klemens.

the class CoursesTest method testGetCourses_searchExternalID_withLifecycle.

@Test
public void testGetCourses_searchExternalID_withLifecycle() throws IOException, URISyntaxException {
    assertTrue(conn.login("administrator", "openolat"));
    URI request = UriBuilder.fromUri(getContextURI()).path("/repo/courses").queryParam("externalId", externalId3).build();
    HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    List<CourseVO> courses = parseCourseArray(body);
    assertNotNull("Course list cannot be null", courses);
    assertEquals(1, courses.size());
    CourseVO vo = courses.get(0);
    assertNotNull("Course cannot be null", vo);
    assertEquals(vo.getKey(), course3.getResourceableId());
    assertNotNull("Has a lifecycle", vo.getLifecycle());
    assertNotNull("Life cycle has a soft key", vo.getLifecycle().getSoftkey());
}
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) URI(java.net.URI) Test(org.junit.Test)

Example 64 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project openolat by klemens.

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 65 with CourseVO

use of org.olat.restapi.support.vo.CourseVO in project openolat by klemens.

the class CoursePublishTest method testGetCourse.

@Test
public void testGetCourse() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    // deploy a test course
    URL courseWithForumsUrl = CoursePublishTest.class.getResource("myCourseWS.zip");
    Assert.assertNotNull(courseWithForumsUrl);
    File courseWithForums = new File(courseWithForumsUrl.toURI());
    String softKey = UUID.randomUUID().toString().replace("-", "").substring(0, 30);
    RepositoryEntry re = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4);
    Assert.assertNotNull(re);
    ICourse course = CourseFactory.loadCourse(re.getOlatResource().getResourceableId());
    CourseNode rootNode = course.getRunStructure().getRootNode();
    Assert.assertEquals(2, rootNode.getChildCount());
    dbInstance.commitAndCloseSession();
    // get the course
    URI uri = conn.getContextURI().path("repo").path("courses").path(course.getResourceableId().toString()).build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    CourseVO courseVo = conn.parse(response, CourseVO.class);
    Assert.assertNotNull(courseVo);
    // update the root node
    URI rootUri = getElementsUri(courseVo).path("structure").path(courseVo.getEditorRootNodeId()).build();
    HttpPost updateMethod = conn.createPost(rootUri, MediaType.APPLICATION_JSON);
    HttpEntity entity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addTextBody("shortTitle", "Change it short").addTextBody("longTitle", "Change it long").build();
    updateMethod.setEntity(entity);
    HttpResponse updateRootResponse = conn.execute(updateMethod);
    int updateRootCode = updateRootResponse.getStatusLine().getStatusCode();
    assertTrue(updateRootCode == 200 || updateRootCode == 201);
    EntityUtils.consume(updateRootResponse.getEntity());
    // publish
    URI publishUri = getCoursesUri().path(courseVo.getKey().toString()).path("publish").build();
    HttpPost publishMethod = conn.createPost(publishUri, MediaType.APPLICATION_JSON);
    HttpResponse publishResponse = conn.execute(publishMethod);
    int publishCode = publishResponse.getStatusLine().getStatusCode();
    assertTrue(publishCode == 200 || publishCode == 201);
    EntityUtils.consume(publishResponse.getEntity());
    // reload the course
    ICourse reloadedCourse = CourseFactory.loadCourse(re.getOlatResource().getResourceableId());
    CourseNode reloadRootNode = reloadedCourse.getRunStructure().getRootNode();
    Assert.assertEquals(2, reloadRootNode.getChildCount());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) URI(java.net.URI) URL(java.net.URL) CourseVO(org.olat.restapi.support.vo.CourseVO) CourseNode(org.olat.course.nodes.CourseNode) File(java.io.File) Test(org.junit.Test)

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