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);
}
}
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());
}
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;
}
Aggregations