Java Code Examples for org.apache.commons.httpclient.methods.PutMethod#releaseConnection()

The following examples show how to use org.apache.commons.httpclient.methods.PutMethod#releaseConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BigSwitchBcfApi.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
protected <T> String executeUpdateObject(final T newObject, final String uri,
        final Map<String, String> parameters) throws BigSwitchBcfApiException,
IllegalArgumentException{
    checkInvariants();

    PutMethod pm = (PutMethod)createMethod("put", uri, _port);

    setHttpHeader(pm);

    try {
        pm.setRequestEntity(new StringRequestEntity(gson.toJson(newObject), CONTENT_JSON, null));
    } catch (UnsupportedEncodingException e) {
        throw new BigSwitchBcfApiException("Failed to encode json request body", e);
    }

    executeMethod(pm);

    String hash = checkResponse(pm, "BigSwitch HTTP update failed: ");

    pm.releaseConnection();

    return hash;
}
 
Example 2
Source File: NotebookRestApiTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void testRenameNote() throws IOException {
  LOG.info("Running testRenameNote");
  Note note = null;
  try {
    String oldName = "old_name";
    note = TestUtils.getInstance(Notebook.class).createNote(oldName, anonymous);
    assertEquals(note.getName(), oldName);
    String noteId = note.getId();

    final String newName = "testName";
    String jsonRequest = "{\"name\": " + newName + "}";

    PutMethod put = httpPut("/notebook/" + noteId + "/rename/", jsonRequest);
    assertThat("test testRenameNote:", put, isAllowed());
    put.releaseConnection();

    assertEquals(note.getName(), newName);
  } finally {
    // cleanup
    if (null != note) {
      TestUtils.getInstance(Notebook.class).removeNote(note, anonymous);
    }
  }
}
 
Example 3
Source File: CoursesContactElementITCase.java    From olat with Apache License 2.0 6 votes vote down vote up
@Test
public void testBareBoneConfig() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    // create an contact node
    final URI newContactUri = getElementsUri(course1).path("contact").queryParam("parentNodeId", rootNodeId).queryParam("position", "0")
            .queryParam("shortTitle", "Contact-0").queryParam("longTitle", "Contact-long-0").queryParam("objectives", "Contact-objectives-0").build();
    final PutMethod method = createPut(newContactUri, MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();

    assertEquals(200, code);
    final CourseNodeVO contactNode = parse(body, CourseNodeVO.class);
    assertNotNull(contactNode);
    assertNotNull(contactNode.getId());
    assertEquals(contactNode.getShortTitle(), "Contact-0");
    assertEquals(contactNode.getLongTitle(), "Contact-long-0");
    assertEquals(contactNode.getLearningObjectives(), "Contact-objectives-0");
    assertEquals(contactNode.getParentId(), rootNodeId);
}
 
Example 4
Source File: CoursesContactElementITCase.java    From olat with Apache License 2.0 6 votes vote down vote up
@Test
public void testBareBoneConfig() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    // create an contact node
    final URI newContactUri = getElementsUri(course1).path("contact").queryParam("parentNodeId", rootNodeId).queryParam("position", "0")
            .queryParam("shortTitle", "Contact-0").queryParam("longTitle", "Contact-long-0").queryParam("objectives", "Contact-objectives-0").build();
    final PutMethod method = createPut(newContactUri, MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();

    assertEquals(200, code);
    final CourseNodeVO contactNode = parse(body, CourseNodeVO.class);
    assertNotNull(contactNode);
    assertNotNull(contactNode.getId());
    assertEquals(contactNode.getShortTitle(), "Contact-0");
    assertEquals(contactNode.getLongTitle(), "Contact-long-0");
    assertEquals(contactNode.getLearningObjectives(), "Contact-objectives-0");
    assertEquals(contactNode.getParentId(), rootNodeId);
}
 
Example 5
Source File: UserMgmtITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Test machine format for gender and date
 */
@Test
public void testCreateUser2() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final UserVO vo = new UserVO();
    final String username = UUID.randomUUID().toString();
    vo.setLogin(username);
    vo.setFirstName("John");
    vo.setLastName("Smith");
    vo.setEmail(username + "@frentix.com");
    vo.putProperty("telOffice", "39847592");
    vo.putProperty("telPrivate", "39847592");
    vo.putProperty("telMobile", "39847592");
    vo.putProperty("gender", "female");// male or female
    vo.putProperty("birthDay", "20091212");

    final String stringuifiedAuth = stringuified(vo);
    final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true);
    final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8");
    method.setRequestEntity(entity);
    method.addRequestHeader("Accept-Language", "en");

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final UserVO savedVo = parse(body, UserVO.class);

    final Identity savedIdent = securityManager.findIdentityByName(username);

    assertNotNull(savedVo);
    assertNotNull(savedIdent);
    assertEquals(savedVo.getKey(), savedIdent.getKey());
    assertEquals(savedVo.getLogin(), savedIdent.getName());
    assertEquals("Female", userService.getUserProperty(savedIdent.getUser(), UserConstants.GENDER, Locale.ENGLISH));
    assertEquals("39847592", userService.getUserProperty(savedIdent.getUser(), UserConstants.TELPRIVATE, Locale.ENGLISH));
    assertEquals("12/12/09", userService.getUserProperty(savedIdent.getUser(), UserConstants.BIRTHDAY, Locale.ENGLISH));
}
 
Example 6
Source File: CatalogITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutCatalogEntryQuery() throws IOException {
    final RepositoryEntry re = createRepository("put-cat-entry-query", 6458439l);

    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build();
    final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true);
    method.setQueryString(new NameValuePair[] { new NameValuePair("name", "Sub-entry-2"), new NameValuePair("description", "Sub-entry-description-2"),
            new NameValuePair("type", String.valueOf(CatalogEntry.TYPE_NODE)), new NameValuePair("repoEntryKey", re.getKey().toString()) });

    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final CatalogEntryVO vo = parse(body, CatalogEntryVO.class);
    assertNotNull(vo);

    final List<CatalogEntry> children = catalogService.getChildrenOf(entry1);
    CatalogEntry ce = null;
    for (final CatalogEntry child : children) {
        if (vo.getKey().equals(child.getKey())) {
            ce = child;
            break;
        }
    }

    assertNotNull(ce);
    assertNotNull(ce.getRepositoryEntry());
    assertEquals(re.getKey(), ce.getRepositoryEntry().getKey());
}
 
Example 7
Source File: CatalogITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutCatalogEntryJson() throws IOException {
    final RepositoryEntry re = createRepository("put-cat-entry-json", 6458438l);

    final HttpClient c = loginWithCookie("administrator", "olat");

    final CatalogEntryVO subEntry = new CatalogEntryVO();
    subEntry.setName("Sub-entry-1");
    subEntry.setDescription("Sub-entry-description-1");
    subEntry.setType(CatalogEntry.TYPE_NODE);
    subEntry.setRepositoryEntryKey(re.getKey());
    final String entity = stringuified(subEntry);

    final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build();
    final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.APPLICATION_JSON);
    final RequestEntity requestEntity = new StringRequestEntity(entity, MediaType.APPLICATION_JSON, "UTF-8");
    method.setRequestEntity(requestEntity);

    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final CatalogEntryVO vo = parse(body, CatalogEntryVO.class);
    assertNotNull(vo);

    final List<CatalogEntry> children = catalogService.getChildrenOf(entry1);
    CatalogEntry ce = null;
    for (final CatalogEntry child : children) {
        if (vo.getKey().equals(child.getKey())) {
            ce = child;
            break;
        }
    }

    assertNotNull(ce);
    assertNotNull(ce.getRepositoryEntry());
    assertEquals(re.getKey(), ce.getRepositoryEntry().getKey());
}
 
Example 8
Source File: CatalogITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutCatalogEntryQuery() throws IOException {
    final RepositoryEntry re = createRepository("put-cat-entry-query", 6458439l);

    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build();
    final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true);
    method.setQueryString(new NameValuePair[] { new NameValuePair("name", "Sub-entry-2"), new NameValuePair("description", "Sub-entry-description-2"),
            new NameValuePair("type", String.valueOf(CatalogEntry.TYPE_NODE)), new NameValuePair("repoEntryKey", re.getKey().toString()) });

    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final CatalogEntryVO vo = parse(body, CatalogEntryVO.class);
    assertNotNull(vo);

    final List<CatalogEntry> children = catalogService.getChildrenOf(entry1);
    CatalogEntry ce = null;
    for (final CatalogEntry child : children) {
        if (vo.getKey().equals(child.getKey())) {
            ce = child;
            break;
        }
    }

    assertNotNull(ce);
    assertNotNull(ce.getRepositoryEntry());
    assertEquals(re.getKey(), ce.getRepositoryEntry().getKey());
}
 
Example 9
Source File: CatalogITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutCategoryQuery() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build();
    final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true);
    method.setQueryString(new NameValuePair[] { new NameValuePair("name", "Sub-entry-2"), new NameValuePair("description", "Sub-entry-description-2"),
            new NameValuePair("type", String.valueOf(CatalogEntry.TYPE_NODE)) });

    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final CatalogEntryVO vo = parse(body, CatalogEntryVO.class);
    assertNotNull(vo);

    final List<CatalogEntry> children = catalogService.getChildrenOf(entry1);
    boolean saved = false;
    for (final CatalogEntry child : children) {
        if (vo.getKey().equals(child.getKey())) {
            saved = true;
            break;
        }
    }

    assertTrue(saved);
}
 
Example 10
Source File: RepositoryEntriesITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testImportTest() throws HttpException, IOException, URISyntaxException {
    final URL cpUrl = RepositoryEntriesITCase.class.getResource("qti-demo.zip");
    assertNotNull(cpUrl);
    final File cp = new File(cpUrl.toURI());

    final HttpClient c = loginWithCookie("administrator", "olat");
    final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "qti-demo.zip"), new StringPart("resourcename", "QTI demo"),
            new StringPart("displayname", "QTI demo") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);

    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class);
    assertNotNull(vo);

    final Long key = vo.getKey();
    final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOwnerGroup());
    assertNotNull(re.getOlatResource());
    assertEquals("QTI demo", re.getDisplayname());
    log.info(re.getOlatResource().getResourceableTypeName());
}
 
Example 11
Source File: UserMgmtITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateUser() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final UserVO vo = new UserVO();
    final String username = UUID.randomUUID().toString();
    vo.setLogin(username);
    vo.setFirstName("John");
    vo.setLastName("Smith");
    vo.setEmail(username + "@frentix.com");
    vo.putProperty("telOffice", "39847592");
    vo.putProperty("telPrivate", "39847592");
    vo.putProperty("telMobile", "39847592");
    vo.putProperty("gender", "Female");// male or female
    vo.putProperty("birthDay", "12/12/2009");

    final String stringuifiedAuth = stringuified(vo);
    final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true);
    final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8");
    method.setRequestEntity(entity);
    method.addRequestHeader("Accept-Language", "en");

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final UserVO savedVo = parse(body, UserVO.class);

    final Identity savedIdent = securityManager.findIdentityByName(username);

    assertNotNull(savedVo);
    assertNotNull(savedIdent);
    assertEquals(savedVo.getKey(), savedIdent.getKey());
    assertEquals(savedVo.getLogin(), savedIdent.getName());
    assertEquals("Female", userService.getUserProperty(savedIdent.getUser(), UserConstants.GENDER, Locale.ENGLISH));
    assertEquals("39847592", userService.getUserProperty(savedIdent.getUser(), UserConstants.TELPRIVATE, Locale.ENGLISH));
    assertEquals("12/12/09", userService.getUserProperty(savedIdent.getUser(), UserConstants.BIRTHDAY, Locale.ENGLISH));
}
 
Example 12
Source File: CatalogITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutCategoryQuery() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build();
    final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true);
    method.setQueryString(new NameValuePair[] { new NameValuePair("name", "Sub-entry-2"), new NameValuePair("description", "Sub-entry-description-2"),
            new NameValuePair("type", String.valueOf(CatalogEntry.TYPE_NODE)) });

    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final CatalogEntryVO vo = parse(body, CatalogEntryVO.class);
    assertNotNull(vo);

    final List<CatalogEntry> children = catalogService.getChildrenOf(entry1);
    boolean saved = false;
    for (final CatalogEntry child : children) {
        if (vo.getKey().equals(child.getKey())) {
            saved = true;
            break;
        }
    }

    assertTrue(saved);
}
 
Example 13
Source File: ClusterEventTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenameNoteEvent() throws IOException {
  Note note = null;
  try {
    String oldName = "old_name";
    note = TestUtils.getInstance(Notebook.class).createNote(oldName, anonymous);
    assertEquals(note.getName(), oldName);
    String noteId = note.getId();

    final String newName = "testName";
    String jsonRequest = "{\"name\": " + newName + "}";

    PutMethod put = httpPut("/notebook/" + noteId + "/rename/", jsonRequest);
    assertThat("test testRenameNote:", put, isAllowed());
    put.releaseConnection();

    assertEquals(note.getName(), newName);

    // wait cluster sync event
    Thread.sleep(1000);
    checkClusterNoteEventListener();
  } catch (InterruptedException e) {
    LOGGER.error(e.getMessage(), e);
  } finally {
    // cleanup
    if (null != note) {
      TestUtils.getInstance(Notebook.class).removeNote(note, anonymous);
    }
  }
}
 
Example 14
Source File: RepositoryEntriesITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testImportQuestionnaire() throws HttpException, IOException, URISyntaxException {
    final URL cpUrl = RepositoryEntriesITCase.class.getResource("questionnaire-demo.zip");
    assertNotNull(cpUrl);
    final File cp = new File(cpUrl.toURI());

    final HttpClient c = loginWithCookie("administrator", "olat");
    final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "questionnaire-demo.zip"), new StringPart("resourcename", "Questionnaire demo"),
            new StringPart("displayname", "Questionnaire demo") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);

    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class);
    assertNotNull(vo);

    final Long key = vo.getKey();
    final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOwnerGroup());
    assertNotNull(re.getOlatResource());
    assertEquals("Questionnaire demo", re.getDisplayname());
    log.info(re.getOlatResource().getResourceableTypeName());
}
 
Example 15
Source File: UserMgmtITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Test machine format for gender and date
 */
@Test
public void testCreateUser2() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final UserVO vo = new UserVO();
    final String username = UUID.randomUUID().toString();
    vo.setLogin(username);
    vo.setFirstName("John");
    vo.setLastName("Smith");
    vo.setEmail(username + "@frentix.com");
    vo.putProperty("telOffice", "39847592");
    vo.putProperty("telPrivate", "39847592");
    vo.putProperty("telMobile", "39847592");
    vo.putProperty("gender", "female");// male or female
    vo.putProperty("birthDay", "20091212");

    final String stringuifiedAuth = stringuified(vo);
    final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true);
    final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8");
    method.setRequestEntity(entity);
    method.addRequestHeader("Accept-Language", "en");

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final UserVO savedVo = parse(body, UserVO.class);

    final Identity savedIdent = securityManager.findIdentityByName(username);

    assertNotNull(savedVo);
    assertNotNull(savedIdent);
    assertEquals(savedVo.getKey(), savedIdent.getKey());
    assertEquals(savedVo.getLogin(), savedIdent.getName());
    assertEquals("Female", userService.getUserProperty(savedIdent.getUser(), UserConstants.GENDER, Locale.ENGLISH));
    assertEquals("39847592", userService.getUserProperty(savedIdent.getUser(), UserConstants.TELPRIVATE, Locale.ENGLISH));
    assertEquals("12/12/09", userService.getUserProperty(savedIdent.getUser(), UserConstants.BIRTHDAY, Locale.ENGLISH));
}
 
Example 16
Source File: RepositoryEntriesITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testImportWiki() throws HttpException, IOException, URISyntaxException {
    final URL cpUrl = RepositoryEntriesITCase.class.getResource("wiki-demo.zip");
    assertNotNull(cpUrl);
    final File cp = new File(cpUrl.toURI());

    final HttpClient c = loginWithCookie("administrator", "olat");
    final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true);
    method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA);
    final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "wiki-demo.zip"), new StringPart("resourcename", "Wiki demo"),
            new StringPart("displayname", "Wiki demo") };
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

    final int code = c.executeMethod(method);
    assertTrue(code == 200 || code == 201);

    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class);
    assertNotNull(vo);

    final Long key = vo.getKey();
    final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key);
    assertNotNull(re);
    assertNotNull(re.getOwnerGroup());
    assertNotNull(re.getOlatResource());
    assertEquals("Wiki demo", re.getDisplayname());
    log.info(re.getOlatResource().getResourceableTypeName());
}
 
Example 17
Source File: NotebookRestApiTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateParagraphConfig() throws IOException {
  LOG.info("Running testUpdateParagraphConfig");
  Note note = null;
  try {
    note = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
    String noteId = note.getId();
    Paragraph p = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
    assertNull(p.getConfig().get("colWidth"));
    String paragraphId = p.getId();
    String jsonRequest = "{\"colWidth\": 6.0}";

    PutMethod put = httpPut("/notebook/" + noteId + "/paragraph/" + paragraphId + "/config",
            jsonRequest);
    assertThat("test testUpdateParagraphConfig:", put, isAllowed());

    Map<String, Object> resp = gson.fromJson(put.getResponseBodyAsString(),
            new TypeToken<Map<String, Object>>() {}.getType());
    Map<String, Object> respBody = (Map<String, Object>) resp.get("body");
    Map<String, Object> config = (Map<String, Object>) respBody.get("config");
    put.releaseConnection();

    assertEquals(config.get("colWidth"), 6.0);
    note = TestUtils.getInstance(Notebook.class).getNote(noteId);
    assertEquals(note.getParagraph(paragraphId).getConfig().get("colWidth"), 6.0);
  } finally {
    // cleanup
    if (null != note) {
      TestUtils.getInstance(Notebook.class).removeNote(note, anonymous);
    }
  }
}
 
Example 18
Source File: ZeppelinRestApiTest.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdateParagraph() throws IOException {
  Note note = null;
  try {
    note = TestUtils.getInstance(Notebook.class).createNote("note1_testUpdateParagraph", anonymous);

    String jsonRequest = "{\"title\": \"title1\", \"text\": \"text1\"}";
    PostMethod post = httpPost("/notebook/" + note.getId() + "/paragraph", jsonRequest);
    Map<String, Object> resp = gson.fromJson(post.getResponseBodyAsString(),
            new TypeToken<Map<String, Object>>() {}.getType());
    post.releaseConnection();

    String newParagraphId = (String) resp.get("body");
    Paragraph newParagraph = TestUtils.getInstance(Notebook.class).getNote(note.getId())
            .getParagraph(newParagraphId);

    assertEquals("title1", newParagraph.getTitle());
    assertEquals("text1", newParagraph.getText());

    String updateRequest = "{\"text\": \"updated text\"}";
    PutMethod put = httpPut("/notebook/" + note.getId() + "/paragraph/" + newParagraphId,
            updateRequest);
    assertThat("Test update method:", put, isAllowed());
    put.releaseConnection();

    Paragraph updatedParagraph = TestUtils.getInstance(Notebook.class).getNote(note.getId())
            .getParagraph(newParagraphId);

    assertEquals("title1", updatedParagraph.getTitle());
    assertEquals("updated text", updatedParagraph.getText());

    String updateBothRequest = "{\"title\": \"updated title\", \"text\" : \"updated text 2\" }";
    PutMethod updatePut = httpPut("/notebook/" + note.getId() + "/paragraph/" + newParagraphId,
            updateBothRequest);
    updatePut.releaseConnection();

    Paragraph updatedBothParagraph = TestUtils.getInstance(Notebook.class).getNote(note.getId())
            .getParagraph(newParagraphId);

    assertEquals("updated title", updatedBothParagraph.getTitle());
    assertEquals("updated text 2", updatedBothParagraph.getText());
  } finally {
    //cleanup
    if (null != note) {
      TestUtils.getInstance(Notebook.class).removeNote(note, anonymous);
    }
  }
}
 
Example 19
Source File: NotebookRestApiTest.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Test
public void testClearAllParagraphOutput() throws IOException {
  LOG.info("Running testClearAllParagraphOutput");
  Note note = null;
  try {
    // Create note and set result explicitly
    note = TestUtils.getInstance(Notebook.class).createNote("note1", anonymous);
    Paragraph p1 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
    InterpreterResult result = new InterpreterResult(InterpreterResult.Code.SUCCESS,
            InterpreterResult.Type.TEXT, "result");
    p1.setResult(result);

    Paragraph p2 = note.addNewParagraph(AuthenticationInfo.ANONYMOUS);
    p2.setReturn(result, new Throwable());

    // clear paragraph result
    PutMethod put = httpPut("/notebook/" + note.getId() + "/clear", "");
    LOG.info("test clear paragraph output response\n" + put.getResponseBodyAsString());
    assertThat(put, isAllowed());
    put.releaseConnection();

    // check if paragraph results are cleared
    GetMethod get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p1.getId());
    assertThat(get, isAllowed());
    Map<String, Object> resp1 = gson.fromJson(get.getResponseBodyAsString(),
            new TypeToken<Map<String, Object>>() {}.getType());
    Map<String, Object> resp1Body = (Map<String, Object>) resp1.get("body");
    assertNull(resp1Body.get("result"));

    get = httpGet("/notebook/" + note.getId() + "/paragraph/" + p2.getId());
    assertThat(get, isAllowed());
    Map<String, Object> resp2 = gson.fromJson(get.getResponseBodyAsString(),
            new TypeToken<Map<String, Object>>() {}.getType());
    Map<String, Object> resp2Body = (Map<String, Object>) resp2.get("body");
    assertNull(resp2Body.get("result"));
    get.releaseConnection();
  } finally {
    // cleanup
    if (null != note) {
      TestUtils.getInstance(Notebook.class).removeNote(note, anonymous);
    }
  }
}
 
Example 20
Source File: NotebookRepoRestApiTest.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
private void updateNotebookRepoWithNewSetting(String payload) throws IOException {
  PutMethod put = httpPut("/notebook-repositories", payload);
  int status = put.getStatusCode();
  put.releaseConnection();
  assertThat(status, is(200));
}