Search in sources :

Example 11 with MessageVO

use of org.olat.modules.fo.restapi.MessageVO in project OpenOLAT by OpenOLAT.

the class ForumTest method testNewThread.

@Test
public void testNewThread() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = getForumUriBuilder().path("threads").queryParam("authorKey", id1.getKey()).queryParam("title", "New thread").queryParam("body", "A very interesting thread").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO thread = conn.parse(response, MessageVO.class);
    assertNotNull(thread);
    assertNotNull(thread.getKey());
    assertEquals(thread.getForumKey(), forum.getKey());
    assertEquals(thread.getAuthorKey(), id1.getKey());
    // really saved?
    boolean saved = false;
    ForumManager fm = ForumManager.getInstance();
    List<Message> allMessages = fm.getMessagesByForum(forum);
    for (Message message : allMessages) {
        if (message.getKey().equals(thread.getKey())) {
            saved = true;
        }
    }
    assertTrue(saved);
    conn.shutdown();
}
Also used : Message(org.olat.modules.fo.Message) ForumManager(org.olat.modules.fo.manager.ForumManager) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) MessageVO(org.olat.modules.fo.restapi.MessageVO) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 12 with MessageVO

use of org.olat.modules.fo.restapi.MessageVO in project OpenOLAT by OpenOLAT.

the class ForumTest method testNewMessage.

@Test
public void testNewMessage() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).queryParam("authorKey", id1.getKey()).queryParam("title", "New message").queryParam("body", "A very interesting response in Thread-1").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO message = conn.parse(response, MessageVO.class);
    assertNotNull(message);
    assertNotNull(message.getKey());
    assertEquals(message.getForumKey(), forum.getKey());
    assertEquals(message.getAuthorKey(), id1.getKey());
    assertEquals(message.getParentKey(), m1.getKey());
    // really saved?
    boolean saved = false;
    ForumManager fm = ForumManager.getInstance();
    List<Message> allMessages = fm.getMessagesByForum(forum);
    for (Message msg : allMessages) {
        if (msg.getKey().equals(message.getKey())) {
            saved = true;
        }
    }
    assertTrue(saved);
    conn.shutdown();
}
Also used : Message(org.olat.modules.fo.Message) ForumManager(org.olat.modules.fo.manager.ForumManager) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) MessageVO(org.olat.modules.fo.restapi.MessageVO) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 13 with MessageVO

use of org.olat.modules.fo.restapi.MessageVO in project OpenOLAT by OpenOLAT.

the class ForumTest method testUploadAttachmentAndRename.

@Test
public void testUploadAttachmentAndRename() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).queryParam("authorKey", id1.getKey()).queryParam("title", "New message with attachment ").queryParam("body", "A very interesting response in Thread-1 with an attachment").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO message = conn.parse(response, MessageVO.class);
    assertNotNull(message);
    // attachment
    URL portraitUrl = CoursesElementsTest.class.getResource("portrait.jpg");
    assertNotNull(portraitUrl);
    File portrait = new File(portraitUrl.toURI());
    // upload portrait
    URI attachUri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build();
    HttpPost attachMethod = conn.createPost(attachUri, MediaType.APPLICATION_JSON);
    conn.addMultipart(attachMethod, "portrait.jpg", portrait);
    HttpResponse attachCode = conn.execute(attachMethod);
    assertEquals(200, attachCode.getStatusLine().getStatusCode());
    EntityUtils.consume(attachCode.getEntity());
    // upload portrait a second time
    URI attach2Uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build();
    HttpPost attach2Method = conn.createPost(attach2Uri, MediaType.APPLICATION_JSON);
    conn.addMultipart(attach2Method, "portrait.jpg", portrait);
    HttpResponse attach2Code = conn.execute(attach2Method);
    assertEquals(200, attach2Code.getStatusLine().getStatusCode());
    EntityUtils.consume(attach2Code.getEntity());
    // load the attachments
    URI loadUri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).path("attachments").build();
    HttpGet loadMethod = conn.createGet(loadUri, MediaType.APPLICATION_JSON, true);
    HttpResponse loadResponse = conn.execute(loadMethod);
    assertEquals(200, loadResponse.getStatusLine().getStatusCode());
    InputStream loadBody = loadResponse.getEntity().getContent();
    List<FileVO> files = parseFileArray(loadBody);
    assertNotNull(files);
    assertEquals(2, files.size());
    conn.shutdown();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) FileVO(org.olat.restapi.support.vo.FileVO) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) MessageVO(org.olat.modules.fo.restapi.MessageVO) File(java.io.File) HttpPut(org.apache.http.client.methods.HttpPut) URL(java.net.URL) Test(org.junit.Test)

Example 14 with MessageVO

use of org.olat.modules.fo.restapi.MessageVO in project OpenOLAT by OpenOLAT.

the class ForumTest method testUpload64Attachment.

@Test
public void testUpload64Attachment() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).queryParam("authorKey", id1.getKey()).queryParam("title", "New message with attachment ").queryParam("body", "A very interesting response in Thread-1 with an attachment").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO message = conn.parse(response, MessageVO.class);
    assertNotNull(message);
    // attachment
    InputStream portraitStream = CoursesElementsTest.class.getResourceAsStream("portrait.jpg");
    assertNotNull(portraitStream);
    // upload portrait
    URI attachUri = getForumUriBuilder().path("posts").path(message.getKey().toString()).path("attachments").build();
    byte[] portraitBytes = IOUtils.toByteArray(portraitStream);
    byte[] portrait64 = Base64.encodeBase64(portraitBytes, true);
    HttpPost attachMethod = conn.createPost(attachUri, MediaType.APPLICATION_JSON);
    attachMethod.addHeader("Content-Type", MediaType.APPLICATION_FORM_URLENCODED);
    conn.addEntity(attachMethod, new BasicNameValuePair("file", new String(portrait64)), new BasicNameValuePair("filename", "portrait64.jpg"));
    HttpResponse attachCode = conn.execute(attachMethod);
    assertEquals(200, attachCode.getStatusLine().getStatusCode());
    // check if the file exists
    ForumManager fm = ForumManager.getInstance();
    VFSContainer container = fm.getMessageContainer(message.getForumKey(), message.getKey());
    VFSItem uploadedFile = container.resolve("portrait64.jpg");
    assertNotNull(uploadedFile);
    assertTrue(uploadedFile instanceof VFSLeaf);
    // check if the image is still an image
    VFSLeaf uploadedImage = (VFSLeaf) uploadedFile;
    InputStream uploadedStream = uploadedImage.getInputStream();
    BufferedImage image = ImageIO.read(uploadedStream);
    FileUtils.closeSafely(uploadedStream);
    assertNotNull(image);
    conn.shutdown();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpResponse(org.apache.http.HttpResponse) VFSItem(org.olat.core.util.vfs.VFSItem) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) BufferedImage(java.awt.image.BufferedImage) ForumManager(org.olat.modules.fo.manager.ForumManager) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) MessageVO(org.olat.modules.fo.restapi.MessageVO) Test(org.junit.Test)

Example 15 with MessageVO

use of org.olat.modules.fo.restapi.MessageVO in project OpenOLAT by OpenOLAT.

the class ForumTest method testUploadAttachment.

@Test
public void testUploadAttachment() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id1.getName(), "A6B7C8"));
    URI uri = getForumUriBuilder().path("posts").path(m1.getKey().toString()).queryParam("authorKey", id1.getKey()).queryParam("title", "New message with attachment ").queryParam("body", "A very interesting response in Thread-1 with an attachment").build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    MessageVO message = conn.parse(response, MessageVO.class);
    assertNotNull(message);
    // attachment
    URL portraitUrl = CoursesElementsTest.class.getResource("portrait.jpg");
    assertNotNull(portraitUrl);
    File portrait = new File(portraitUrl.toURI());
    // upload portrait
    URI attachUri = getForumUriBuilder().path("posts").path(message.getKey().toString()).path("attachments").build();
    HttpPost attachMethod = conn.createPost(attachUri, MediaType.APPLICATION_JSON);
    conn.addMultipart(attachMethod, "portrait.jpg", portrait);
    HttpResponse attachResponse = conn.execute(attachMethod);
    assertEquals(200, attachResponse.getStatusLine().getStatusCode());
    // check if the file exists
    ForumManager fm = ForumManager.getInstance();
    VFSContainer container = fm.getMessageContainer(message.getForumKey(), message.getKey());
    VFSItem uploadedFile = container.resolve("portrait.jpg");
    assertNotNull(uploadedFile);
    assertTrue(uploadedFile instanceof VFSLeaf);
    // check if the image is still an image
    VFSLeaf uploadedImage = (VFSLeaf) uploadedFile;
    InputStream uploadedStream = uploadedImage.getInputStream();
    BufferedImage image = ImageIO.read(uploadedStream);
    FileUtils.closeSafely(uploadedStream);
    assertNotNull(image);
    conn.shutdown();
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) InputStream(java.io.InputStream) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpResponse(org.apache.http.HttpResponse) VFSItem(org.olat.core.util.vfs.VFSItem) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) URL(java.net.URL) BufferedImage(java.awt.image.BufferedImage) ForumManager(org.olat.modules.fo.manager.ForumManager) MessageVO(org.olat.modules.fo.restapi.MessageVO) File(java.io.File) Test(org.junit.Test)

Aggregations

URI (java.net.URI)22 HttpResponse (org.apache.http.HttpResponse)22 Test (org.junit.Test)22 MessageVO (org.olat.modules.fo.restapi.MessageVO)22 InputStream (java.io.InputStream)18 HttpPut (org.apache.http.client.methods.HttpPut)14 HttpGet (org.apache.http.client.methods.HttpGet)12 ForumManager (org.olat.modules.fo.manager.ForumManager)12 BufferedImage (java.awt.image.BufferedImage)8 VFSContainer (org.olat.core.util.vfs.VFSContainer)8 VFSItem (org.olat.core.util.vfs.VFSItem)8 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)8 HttpPost (org.apache.http.client.methods.HttpPost)6 File (java.io.File)4 URL (java.net.URL)4 Message (org.olat.modules.fo.Message)4 File64VO (org.olat.restapi.support.vo.File64VO)4 FileVO (org.olat.restapi.support.vo.FileVO)4 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 ReplyVO (org.olat.modules.fo.restapi.ReplyVO)2