List of usage examples for org.apache.commons.httpclient HttpStatus SC_CREATED
int SC_CREATED
To view the source code for org.apache.commons.httpclient HttpStatus SC_CREATED.
Click Source Link
From source file:org.xwiki.test.rest.PageResourceTest.java
@Test public void testPUTNonExistingPage() throws Exception { final List<String> SPACE_NAME = Arrays.asList("Test"); final String PAGE_NAME = String.format("Test-%d", System.currentTimeMillis()); final String CONTENT = String.format("Content %d", System.currentTimeMillis()); final String TITLE = String.format("Title %d", System.currentTimeMillis()); final String PARENT = "Main.WebHome"; Page page = objectFactory.createPage(); page.setContent(CONTENT);//w w w . j av a 2s. c om page.setTitle(TITLE); page.setParent(PARENT); PutMethod putMethod = executePutXml( buildURI(PageResource.class, getWiki(), SPACE_NAME, PAGE_NAME).toString(), page, TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword()); Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode()); Page modifiedPage = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); Assert.assertEquals(CONTENT, modifiedPage.getContent()); Assert.assertEquals(TITLE, modifiedPage.getTitle()); Assert.assertEquals(PARENT, modifiedPage.getParent()); }
From source file:org.xwiki.test.rest.PageResourceTest.java
@Test public void testPUTGETTranslation() throws Exception { createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TRANSLATIONS_PAGE_NAME, "Translations"); // PUT/*from ww w. j av a 2 s .com*/ String[] languages = Locale.getISOLanguages(); final String languageId = languages[random.nextInt(languages.length)]; Page page = objectFactory.createPage(); page.setContent(languageId); PutMethod putMethod = executePutXml( buildURI(PageTranslationResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TRANSLATIONS_PAGE_NAME, languageId).toString(), page, TestUtils.ADMIN_CREDENTIALS.getUserName(), TestUtils.ADMIN_CREDENTIALS.getPassword()); Assert.assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode()); // GET GetMethod getMethod = executeGet(buildURI(PageTranslationResource.class, getWiki(), TestConstants.TEST_SPACE_NAME, TestConstants.TRANSLATIONS_PAGE_NAME, languageId).toString()); Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); Page modifiedPage = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); // Some of the language codes returned by Locale#getISOLanguages() are deprecated and Locale's constructors map // the new codes to the old ones which means the language code we have submitted can be different than the // actual language code used when the Locale object is created on the server side. Let's go through the Locale // constructor to be safe. String expectedLanguage = LocaleUtils.toLocale(languageId).getLanguage(); Assert.assertEquals(expectedLanguage, modifiedPage.getLanguage()); Assert.assertTrue(modifiedPage.getTranslations().getTranslations().size() > 0); for (Translation translation : modifiedPage.getTranslations().getTranslations()) { getMethod = executeGet(getFirstLinkByRelation(translation, Relations.PAGE).getHref()); Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); modifiedPage = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); Assert.assertEquals(modifiedPage.getLanguage(), translation.getLanguage()); checkLinks(translation); } }
From source file:org.xwiki.test.rest.PageTranslationResourceTest.java
@Test public void testPUTGETDELETETranslation() throws Exception { Page newPage = this.objectFactory.createPage(); newPage.setTitle("fr titre"); newPage.setContent("fr contenue"); newPage.setLanguage(Locale.FRENCH.toString()); assertFalse(this.testUtils.rest().exists(this.referenceDefault)); String uri = buildURI(PageTranslationResource.class, getWiki(), this.spaces, this.pageName, Locale.FRENCH) .toString();/*from www .j av a2 s. c o m*/ // PUT PutMethod putMethod = executePutXml(uri, newPage, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword()); assertEquals(getHttpMethodInfo(putMethod), HttpStatus.SC_CREATED, putMethod.getStatusCode()); Page modifiedPage = (Page) this.unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); assertEquals("fr titre", modifiedPage.getTitle()); assertEquals("fr contenue", modifiedPage.getContent()); assertEquals(Locale.FRENCH.toString(), modifiedPage.getLanguage()); assertTrue(this.testUtils.rest().exists(this.referenceFR)); assertFalse(this.testUtils.rest().exists(this.referenceDefault)); // GET GetMethod getMethod = executeGet(uri); assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode()); modifiedPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); assertEquals("fr titre", modifiedPage.getTitle()); assertEquals("fr contenue", modifiedPage.getContent()); assertEquals(Locale.FRENCH.toString(), modifiedPage.getLanguage()); // DELETE DeleteMethod deleteMethod = executeDelete(uri, TestUtils.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_ADMIN_CREDENTIALS.getPassword()); assertEquals(getHttpMethodInfo(deleteMethod), HttpStatus.SC_NO_CONTENT, deleteMethod.getStatusCode()); assertFalse(this.testUtils.rest().exists(this.referenceDefault)); assertFalse(this.testUtils.rest().exists(this.referenceFR)); }
From source file:org.xwiki.wiki.test.ui.WikiManagerRestTest.java
@Test public void testCreateWiki() throws Exception { String WIKI_ID = "foo"; Wiki wiki = objectFactory.createWiki(); wiki.setId(WIKI_ID);/*from ww w .j av a 2 s .co m*/ PostMethod postMethod = executePost(getFullUri(WikiManagerREST.class), "superadmin", "pass", wiki); Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode()); wiki = (Wiki) unmarshaller.unmarshal(postMethod.getResponseBodyAsStream()); Assert.assertEquals(WIKI_ID, wiki.getId()); GetMethod getMethod = executeGet(getFullUri(WikisResource.class)); Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode()); Wikis wikis = (Wikis) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); boolean found = false; for (Wiki w : wikis.getWikis()) { if (WIKI_ID.equals(w.getId())) { found = true; break; } } Assert.assertTrue(found); }
From source file:org.xwiki.wiki.test.ui.WikiManagerRestTest.java
@Ignore("This test doesn't seem to work correctly with HSQLDB but it actually works if run against MySQL.") @Test/*from w w w .j a v a 2 s . c o m*/ public void testMultiwikiSearch() throws Exception { String WIKI1_ID = "w1"; String WIKI2_ID = "w2"; String PAGE_SPACE = "Main"; String PAGE_NAME = "Test"; String PAGE1_STRING = "foo"; String PAGE2_STRING = "bar"; Wiki wiki = objectFactory.createWiki(); wiki.setId(WIKI1_ID); PostMethod postMethod = executePost(getFullUri(WikiManagerREST.class), "superadmin", "pass", wiki); Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode()); wiki = objectFactory.createWiki(); wiki.setId(WIKI2_ID); postMethod = executePost(getFullUri(WikiManagerREST.class), "superadmin", "pass", wiki); Assert.assertEquals(HttpStatus.SC_CREATED, postMethod.getStatusCode()); /* Store the page */ Page page1 = objectFactory.createPage(); page1.setTitle(PAGE1_STRING); page1.setContent(PAGE1_STRING); PutMethod putMethod = executePut( getUriBuilder(PageResource.class).build(WIKI1_ID, PAGE_SPACE, PAGE_NAME).toString(), "superadmin", "pass", page1); Assert.assertEquals(HttpStatus.SC_CREATED, putMethod.getStatusCode()); page1 = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); /* Retrieve the page to check that it exists */ GetMethod getMethod = executeGet( getUriBuilder(PageResource.class).build(WIKI1_ID, PAGE_SPACE, PAGE_NAME).toString()); Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode()); Page page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); Assert.assertEquals(WIKI1_ID, page.getWiki()); Assert.assertEquals(PAGE_SPACE, page.getSpace()); Assert.assertEquals(PAGE_NAME, page.getName()); Assert.assertEquals(PAGE1_STRING, page.getTitle()); Assert.assertEquals(PAGE1_STRING, page.getContent()); Assert.assertEquals(page1.getCreated(), page.getCreated()); Assert.assertEquals(page1.getModified(), page.getModified()); /* Store the page */ Page page2 = objectFactory.createPage(); page2.setTitle(PAGE2_STRING); page2.setContent(PAGE2_STRING); putMethod = executePut(getUriBuilder(PageResource.class).build(WIKI2_ID, PAGE_SPACE, PAGE_NAME).toString(), "superadmin", "pass", page2); Assert.assertEquals(HttpStatus.SC_CREATED, putMethod.getStatusCode()); page2 = (Page) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream()); /* Retrieve the page to check that it exists */ getMethod = executeGet(getUriBuilder(PageResource.class).build(WIKI2_ID, PAGE_SPACE, PAGE_NAME).toString()); Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode()); page = (Page) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); Assert.assertEquals(WIKI2_ID, page.getWiki()); Assert.assertEquals(PAGE_SPACE, page.getSpace()); Assert.assertEquals(PAGE_NAME, page.getName()); Assert.assertEquals(PAGE2_STRING, page.getTitle()); Assert.assertEquals(PAGE2_STRING, page.getContent()); Assert.assertEquals(page2.getCreated(), page.getCreated()); Assert.assertEquals(page2.getModified(), page.getModified()); /* Wait a bit that the Lucene Indexer indexes the pages. */ Thread.sleep(5000); getMethod = executeGet(URIUtil.encodeQuery( String.format("%s?q=\"%s\"&wikis=w1,w2", getFullUri(WikisSearchQueryResource.class), PAGE_NAME))); Assert.assertEquals(HttpStatus.SC_OK, getMethod.getStatusCode()); SearchResults searchResults = (SearchResults) unmarshaller.unmarshal(getMethod.getResponseBodyAsStream()); Assert.assertEquals(2, searchResults.getSearchResults().size()); for (SearchResult searchResult : searchResults.getSearchResults()) { Page pageToBeCheckedAgainst = null; if (searchResult.getWiki().equals(WIKI1_ID)) { pageToBeCheckedAgainst = page1; } else { pageToBeCheckedAgainst = page2; } Assert.assertEquals(pageToBeCheckedAgainst.getWiki(), searchResult.getWiki()); Assert.assertEquals(pageToBeCheckedAgainst.getTitle(), searchResult.getTitle()); Assert.assertEquals(pageToBeCheckedAgainst.getAuthor(), searchResult.getAuthor()); Assert.assertEquals(pageToBeCheckedAgainst.getModified(), searchResult.getModified()); Assert.assertEquals(pageToBeCheckedAgainst.getVersion(), searchResult.getVersion()); } }
From source file:org.zenoss.zep.rest.EventsResourceIT.java
@Test public void testUpdateEventSummary() throws ZepException, IOException { List<String> uuids = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { uuids.add(createSummaryNew(EventSummaryDaoImplIT.createUniqueEvent()).getUuid()); }/* w ww.j av a 2s. c om*/ // Update first 50 EventStatus status = EventStatus.STATUS_ACKNOWLEDGED; String ackUuid = UUID.randomUUID().toString(); String ackName = "testuser123"; EventQuery.Builder queryBuilder = EventQuery.newBuilder(); queryBuilder.setEventFilter(EventFilter.newBuilder().addAllUuid(uuids).build()); EventQuery query = queryBuilder.build(); RestResponse restResponse = client.postProtobuf(EVENTS_URI + "/search", query); assertEquals(HttpStatus.SC_CREATED, restResponse.getResponseCode()); String location = restResponse.getHeaders().get("location").get(0); String query_uuid = location.substring(location.lastIndexOf('/') + 1); final EventSummaryUpdate updateFields = EventSummaryUpdate.newBuilder().setCurrentUserUuid(ackUuid) .setCurrentUserName(ackName).setStatus(status).build(); EventSummaryUpdateRequest.Builder reqBuilder = EventSummaryUpdateRequest.newBuilder(); reqBuilder.setLimit(50); reqBuilder.setUpdateFields(updateFields); EventSummaryUpdateRequest req = reqBuilder.build(); restResponse.getResponse().getEntity().consumeContent(); restResponse = client.putProtobuf(location, req); EventSummaryUpdateResponse response = (EventSummaryUpdateResponse) restResponse.getMessage(); assertEquals(EventSummaryUpdateRequest.newBuilder(req).setOffset(50).setEventQueryUuid(query_uuid).build(), response.getNextRequest()); assertEquals(uuids.size(), response.getTotal()); assertEquals(50, response.getUpdated()); // Repeat request for last 50 restResponse = client.putProtobuf(location, response.getNextRequest()); EventSummaryUpdateResponse newResponse = (EventSummaryUpdateResponse) restResponse.getMessage(); assertFalse(newResponse.hasNextRequest()); assertEquals(50, response.getUpdated()); assertEquals(uuids.size(), response.getTotal()); assertEquals(HttpStatus.SC_NO_CONTENT, client.delete(location).getResponseCode()); assertEquals(HttpStatus.SC_NOT_FOUND, client.getProtobuf(location).getResponseCode()); // Verify updates hit the database List<EventSummary> summaries = summaryDao.findByUuids(uuids); assertEquals(uuids.size(), summaries.size()); for (EventSummary summary : summaries) { assertEquals(status, summary.getStatus()); assertEquals(ackUuid, summary.getCurrentUserUuid()); assertEquals(ackName, summary.getCurrentUserName()); } }
From source file:org.zenoss.zep.rest.EventsResourceIT.java
@Test public void testUpdateEventSummaryExclusions() throws ZepException, IOException { long firstSeen = System.currentTimeMillis(); TimestampRange firstSeenRange = TimestampRange.newBuilder().setStartTime(firstSeen).setEndTime(firstSeen) .build();/* w w w . j a v a2s . c o m*/ Set<String> uuids = new HashSet<String>(); Map<String, EventSummary> excludedUuids = new HashMap<String, EventSummary>(); for (int i = 0; i < 5; i++) { Event event = Event.newBuilder(EventSummaryDaoImplIT.createUniqueEvent()).setCreatedTime(firstSeen) .build(); EventSummary summary = createSummaryNew(event); if ((i % 2) == 0) { uuids.add(summary.getUuid()); } else { excludedUuids.put(summary.getUuid(), summary); } } EventQuery.Builder queryBuilder = EventQuery.newBuilder(); queryBuilder.setEventFilter(EventFilter.newBuilder().addFirstSeen(firstSeenRange).build()); queryBuilder.setExclusionFilter(EventFilter.newBuilder().addAllUuid(excludedUuids.keySet()).build()); EventQuery query = queryBuilder.build(); RestResponse restResponse = client.postProtobuf(EVENTS_URI + "/search", query); assertEquals(HttpStatus.SC_CREATED, restResponse.getResponseCode()); String location = restResponse.getHeaders().get("location").get(0); restResponse.getResponse().getEntity().consumeContent(); // Update first 10 EventStatus status = EventStatus.STATUS_ACKNOWLEDGED; String ackUuid = UUID.randomUUID().toString(); String ackName = "testuser123"; final EventSummaryUpdate updateFields = EventSummaryUpdate.newBuilder().setCurrentUserUuid(ackUuid) .setCurrentUserName(ackName).setStatus(status).build(); EventSummaryUpdateRequest.Builder reqBuilder = EventSummaryUpdateRequest.newBuilder(); reqBuilder.setLimit(10); reqBuilder.setUpdateFields(updateFields); EventSummaryUpdateRequest req = reqBuilder.build(); EventSummaryUpdateResponse response = (EventSummaryUpdateResponse) client.putProtobuf(location, req) .getMessage(); assertFalse(response.hasNextRequest()); assertEquals(uuids.size(), response.getUpdated()); assertEquals(uuids.size(), response.getTotal()); assertEquals(HttpStatus.SC_NO_CONTENT, client.delete(location).getResponseCode()); assertEquals(HttpStatus.SC_NOT_FOUND, client.getProtobuf(location).getResponseCode()); // Verify updates hit the database List<String> allUuids = new ArrayList<String>(); allUuids.addAll(uuids); allUuids.addAll(excludedUuids.keySet()); List<EventSummary> summaries = summaryDao.findByUuids(allUuids); assertEquals(allUuids.size(), summaries.size()); for (EventSummary summary : summaries) { if (uuids.contains(summary.getUuid())) { assertEquals(status, summary.getStatus()); assertEquals(ackUuid, summary.getCurrentUserUuid()); assertEquals(ackName, summary.getCurrentUserName()); } else { // Excluded UUIDs shouldn't have changed assertEquals(excludedUuids.get(summary.getUuid()), summary); } } }
From source file:uk.co.firstzero.webdav.Push.java
/** * Creates a directory on the webdav server * @param path The directory path to be created * @param fileName The filename to be uploaded *//* www . j av a 2s . c om*/ private void createDirectory(String path, String fileName) { try { //Remove the filename at the end String directoryPath = path.split(fileName)[0].trim(); //Build the upload URL String uploadUrl = url; String[] directories = directoryPath.split(File.separator); //If a directory needs to be created if (directoryPath.length() > 0) { //Recursively create the directory structure for (String directoryName : directories) { uploadUrl = uploadUrl + "/" + directoryName; MkColMethod mkdir = new MkColMethod(uploadUrl); int status = httpClient.executeMethod(mkdir); if (status == HttpStatus.SC_METHOD_NOT_ALLOWED) { // Directory exists. Do Nothing logger.trace("Directory exists"); } else if (status != HttpStatus.SC_CREATED) { logger.error("ERR " + " " + status + " " + uploadUrl); } else { logger.debug("Directory " + uploadUrl + " created"); } } } } catch (java.lang.ArrayIndexOutOfBoundsException e) { //Ignore as there is no directory to be created } catch (Exception e) { logger.error("ERR creating " + path); logger.error(e); } }