Search in sources :

Example 1 with Translation

use of org.xwiki.rest.model.jaxb.Translation in project xwiki-platform by xwiki.

the class PageResourceTest method testPUTGETTranslation.

@Test
public void testPUTGETTranslation() throws Exception {
    createPageIfDoesntExist(TestConstants.TEST_SPACE_NAME, TestConstants.TRANSLATIONS_PAGE_NAME, "Translations");
    // PUT
    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.SUPER_ADMIN_CREDENTIALS.getUserName(), TestUtils.SUPER_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) this.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) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
        Assert.assertEquals(modifiedPage.getLanguage(), translation.getLanguage());
        checkLinks(translation);
    }
}
Also used : Translation(org.xwiki.rest.model.jaxb.Translation) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Page(org.xwiki.rest.model.jaxb.Page) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 2 with Translation

use of org.xwiki.rest.model.jaxb.Translation in project xwiki-platform by xwiki.

the class PageResourceTest method testPageHistory.

@Test
public void testPageHistory() throws Exception {
    GetMethod getMethod = executeGet(buildURI(PageResource.class, getWiki(), this.spaces, this.pageName).toString());
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    Page originalPage = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    Assert.assertEquals(this.spaces.get(0), originalPage.getSpace());
    String pageHistoryUri = buildURI(PageHistoryResource.class, getWiki(), this.spaces, originalPage.getName()).toString();
    getMethod = executeGet(pageHistoryUri);
    Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
    History history = (History) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
    HistorySummary firstVersion = null;
    for (HistorySummary historySummary : history.getHistorySummaries()) {
        if ("1.1".equals(historySummary.getVersion())) {
            firstVersion = historySummary;
        }
        getMethod = executeGet(getFirstLinkByRelation(historySummary, Relations.PAGE).getHref());
        Assert.assertEquals(getHttpMethodInfo(getMethod), HttpStatus.SC_OK, getMethod.getStatusCode());
        Page page = (Page) this.unmarshaller.unmarshal(getMethod.getResponseBodyAsStream());
        checkLinks(page);
        for (Translation translation : page.getTranslations().getTranslations()) {
            checkLinks(translation);
        }
    }
    Assert.assertNotNull(firstVersion);
    Assert.assertEquals("Test page", firstVersion.getComment());
}
Also used : Translation(org.xwiki.rest.model.jaxb.Translation) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HistorySummary(org.xwiki.rest.model.jaxb.HistorySummary) Page(org.xwiki.rest.model.jaxb.Page) PageHistoryResource(org.xwiki.rest.resources.pages.PageHistoryResource) History(org.xwiki.rest.model.jaxb.History) AbstractHttpTest(org.xwiki.test.rest.framework.AbstractHttpTest) Test(org.junit.Test)

Example 3 with Translation

use of org.xwiki.rest.model.jaxb.Translation in project xwiki-platform by xwiki.

the class ModelFactory method toRestTranslations.

public Translations toRestTranslations(URI baseUri, Document doc) throws XWikiException {
    Translations translations = this.objectFactory.createTranslations();
    List<String> languages = doc.getTranslationList();
    List<String> spaces = Utils.getSpacesFromSpaceId(doc.getSpace());
    if (!languages.isEmpty()) {
        if (!doc.getDefaultLanguage().equals("")) {
            translations.setDefault(doc.getDefaultLanguage());
            Translation translation = this.objectFactory.createTranslation();
            translation.setLanguage(doc.getDefaultLanguage());
            /* Add the default page with the default translation explicitely */
            String pageTranslationUri = Utils.createURI(baseUri, PageResource.class, doc.getWiki(), spaces, doc.getName()).toString();
            Link pageTranslationLink = this.objectFactory.createLink();
            pageTranslationLink.setHref(pageTranslationUri);
            pageTranslationLink.setRel(Relations.PAGE);
            translation.getLinks().add(pageTranslationLink);
            String historyUri = Utils.createURI(baseUri, PageHistoryResource.class, doc.getWiki(), spaces, doc.getName()).toString();
            Link historyLink = this.objectFactory.createLink();
            historyLink.setHref(historyUri);
            historyLink.setRel(Relations.HISTORY);
            translation.getLinks().add(historyLink);
            translations.getTranslations().add(translation);
        }
    }
    for (String language : languages) {
        Translation translation = this.objectFactory.createTranslation();
        translation.setLanguage(language);
        String pageTranslationUri = Utils.createURI(baseUri, PageTranslationResource.class, doc.getWiki(), spaces, doc.getName(), language).toString();
        Link pageTranslationLink = this.objectFactory.createLink();
        pageTranslationLink.setHref(pageTranslationUri);
        pageTranslationLink.setRel(Relations.PAGE);
        translation.getLinks().add(pageTranslationLink);
        String historyUri = Utils.createURI(baseUri, PageTranslationHistoryResource.class, doc.getWiki(), spaces, doc.getName(), language).toString();
        Link historyLink = this.objectFactory.createLink();
        historyLink.setHref(historyUri);
        historyLink.setRel(Relations.HISTORY);
        translation.getLinks().add(historyLink);
        translations.getTranslations().add(translation);
    }
    return translations;
}
Also used : PageResource(org.xwiki.rest.resources.pages.PageResource) Translation(org.xwiki.rest.model.jaxb.Translation) PageTranslationResource(org.xwiki.rest.resources.pages.PageTranslationResource) PageTranslationHistoryResource(org.xwiki.rest.resources.pages.PageTranslationHistoryResource) PageHistoryResource(org.xwiki.rest.resources.pages.PageHistoryResource) Translations(org.xwiki.rest.model.jaxb.Translations) Link(org.xwiki.rest.model.jaxb.Link)

Aggregations

Translation (org.xwiki.rest.model.jaxb.Translation)3 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 Test (org.junit.Test)2 Page (org.xwiki.rest.model.jaxb.Page)2 PageHistoryResource (org.xwiki.rest.resources.pages.PageHistoryResource)2 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)2 PutMethod (org.apache.commons.httpclient.methods.PutMethod)1 History (org.xwiki.rest.model.jaxb.History)1 HistorySummary (org.xwiki.rest.model.jaxb.HistorySummary)1 Link (org.xwiki.rest.model.jaxb.Link)1 Translations (org.xwiki.rest.model.jaxb.Translations)1 PageResource (org.xwiki.rest.resources.pages.PageResource)1 PageTranslationHistoryResource (org.xwiki.rest.resources.pages.PageTranslationHistoryResource)1 PageTranslationResource (org.xwiki.rest.resources.pages.PageTranslationResource)1