From bfa0404babb47531f2cac92673b4e3f44de9bde0 Mon Sep 17 00:00:00 2001 From: Alan Davis Date: Fri, 3 Jun 2016 13:59:39 +0000 Subject: [PATCH] Merged HEAD (5.2) to 5.2.N (5.2.1) 127528 jkaabimofrad: Merged API-STRIKES-BACK (5.2.0) to HEAD (5.2) 125406 jvonka: RA-779 / RA-780: Sites API - initial commit for create site [default/functional Share site ;-)] & delete site git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@127638 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- config/alfresco/public-rest-context.xml | 6 + source/java/org/alfresco/rest/api/Sites.java | 34 +-- .../rest/api/impl/FavouritesImpl.java | 34 +-- .../org/alfresco/rest/api/impl/SitesImpl.java | 203 ++++++++++++++++-- .../rest/api/model/FavouriteSite.java | 32 +-- .../org/alfresco/rest/api/model/Site.java | 171 +++++++++++++-- .../org/alfresco/rest/api/model/SiteImpl.java | 153 ------------- .../rest/api/sites/SiteEntityResource.java | 39 +++- .../jacksonextensions/TargetDeserializer.java | 3 +- .../rest/api/tests/AbstractBaseApiTest.java | 82 +++++-- .../alfresco/rest/api/tests/NodeApiTest.java | 140 ++++++------ .../rest/api/tests/RenditionsTest.java | 69 +++--- .../alfresco/rest/api/tests/TestSites.java | 101 ++++----- .../api/tests/client/PublicApiClient.java | 12 ++ .../rest/api/tests/client/data/SiteImpl.java | 72 +++++-- 15 files changed, 705 insertions(+), 446 deletions(-) delete mode 100644 source/java/org/alfresco/rest/api/model/SiteImpl.java diff --git a/config/alfresco/public-rest-context.xml b/config/alfresco/public-rest-context.xml index 3dfdafb932..f40fa2c2f6 100644 --- a/config/alfresco/public-rest-context.xml +++ b/config/alfresco/public-rest-context.xml @@ -650,6 +650,12 @@ + + + + + + diff --git a/source/java/org/alfresco/rest/api/Sites.java b/source/java/org/alfresco/rest/api/Sites.java index 09e1bd8015..9cc3bfee5a 100644 --- a/source/java/org/alfresco/rest/api/Sites.java +++ b/source/java/org/alfresco/rest/api/Sites.java @@ -1,4 +1,4 @@ -/* +/* * #%L * Alfresco Remote API * %% @@ -9,20 +9,20 @@ * the paid license agreement will prevail. Otherwise, the software is * provided under the following open source license terms: * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api; import org.alfresco.query.PagingResults; @@ -43,6 +43,8 @@ public interface Sites SiteInfo validateSite(NodeRef nodeRef); CollectionWithPagingInfo getSiteMembers(String siteShortName, Parameters parameters); Site getSite(String siteId); + void deleteSite(String siteId, Parameters parameters); + Site createSite(Site site); /** * people//sites/ @@ -67,4 +69,6 @@ public interface Sites String getSiteRole(String siteId); String getSiteRole(String siteId, String personId); + + String PARAM_PERMANENT = "permanent"; } diff --git a/source/java/org/alfresco/rest/api/impl/FavouritesImpl.java b/source/java/org/alfresco/rest/api/impl/FavouritesImpl.java index 9178b4a618..31681d45d0 100644 --- a/source/java/org/alfresco/rest/api/impl/FavouritesImpl.java +++ b/source/java/org/alfresco/rest/api/impl/FavouritesImpl.java @@ -1,4 +1,4 @@ -/* +/* * #%L * Alfresco Remote API * %% @@ -9,20 +9,20 @@ * the paid license agreement will prevail. Otherwise, the software is * provided under the following open source license terms: * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.impl; import java.util.AbstractList; @@ -47,7 +47,6 @@ import org.alfresco.rest.api.model.Favourite; import org.alfresco.rest.api.model.Folder; import org.alfresco.rest.api.model.FolderTarget; import org.alfresco.rest.api.model.Site; -import org.alfresco.rest.api.model.SiteImpl; import org.alfresco.rest.api.model.SiteTarget; import org.alfresco.rest.api.model.Target; import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; @@ -60,6 +59,7 @@ import org.alfresco.rest.framework.resource.parameters.where.QueryHelper.WalkerC import org.alfresco.service.cmr.favourites.FavouritesService; import org.alfresco.service.cmr.favourites.FavouritesService.Type; import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.site.SiteInfo; import org.alfresco.service.cmr.site.SiteService; import org.alfresco.util.Pair; @@ -126,7 +126,7 @@ public class FavouritesImpl implements Favourites { SiteInfo siteInfo = siteService.getSite(nodeRef); String role = sites.getSiteRole(siteInfo.getShortName()); - Site site = new SiteImpl(siteInfo, role); + Site site = new Site(siteInfo, role); target = new SiteTarget(site); } else @@ -189,8 +189,8 @@ public class FavouritesImpl implements Favourites else if(target instanceof SiteTarget) { SiteTarget siteTarget = (SiteTarget)target; - NodeRef guid = siteTarget.getSite().getGuid(); - SiteInfo siteInfo = sites.validateSite(guid); + String guid = siteTarget.getSite().getGuid(); + SiteInfo siteInfo = sites.validateSite(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, guid)); NodeRef siteNodeRef = siteInfo.getNodeRef(); String siteId = siteInfo.getShortName(); diff --git a/source/java/org/alfresco/rest/api/impl/SitesImpl.java b/source/java/org/alfresco/rest/api/impl/SitesImpl.java index 8bf8b92584..1f7e0f3424 100644 --- a/source/java/org/alfresco/rest/api/impl/SitesImpl.java +++ b/source/java/org/alfresco/rest/api/impl/SitesImpl.java @@ -1,4 +1,4 @@ -/* +/* * #%L * Alfresco Remote API * %% @@ -9,20 +9,20 @@ * the paid license agreement will prevail. Otherwise, the software is * provided under the following open source license terms: * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.impl; import java.io.Serializable; @@ -57,7 +57,6 @@ import org.alfresco.rest.api.model.FavouriteSite; import org.alfresco.rest.api.model.MemberOfSite; import org.alfresco.rest.api.model.Site; import org.alfresco.rest.api.model.SiteContainer; -import org.alfresco.rest.api.model.SiteImpl; import org.alfresco.rest.api.model.SiteMember; import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException; import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException; @@ -76,9 +75,18 @@ import org.alfresco.service.cmr.model.FileInfo; import org.alfresco.service.cmr.preference.PreferenceService; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.site.SiteInfo; import org.alfresco.service.cmr.site.SiteService; +import org.alfresco.service.cmr.site.SiteVisibility; +import org.alfresco.service.cmr.view.ImportPackageHandler; +import org.alfresco.service.cmr.view.ImporterBinding; +import org.alfresco.service.cmr.view.ImporterContentCache; +import org.alfresco.service.cmr.view.ImporterProgress; +import org.alfresco.service.cmr.view.ImporterService; +import org.alfresco.service.cmr.view.Location; import org.alfresco.service.namespace.QName; +import org.alfresco.util.ISO9075; import org.alfresco.util.Pair; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -87,6 +95,7 @@ import org.apache.commons.logging.LogFactory; * Centralises access to site services and maps between representations. * * @author steveglover + * @author janv * @since publicapi1.0 */ public class SitesImpl implements Sites @@ -102,6 +111,8 @@ public class SitesImpl implements Sites protected SiteService siteService; protected FavouritesService favouritesService; protected PreferenceService preferenceService; + protected ImporterService importerService; + protected SiteSurfConfig siteSurfConfig; public void setPreferenceService(PreferenceService preferenceService) { @@ -138,6 +149,16 @@ public class SitesImpl implements Sites this.siteService = siteService; } + public void setImporterService(ImporterService importerService) + { + this.importerService = importerService; + } + + public void setSiteSurfConfig(SiteSurfConfig siteSurfConfig) + { + this.siteSurfConfig = siteSurfConfig; + } + public SiteInfo validateSite(NodeRef guid) { SiteInfo siteInfo = null; @@ -241,7 +262,7 @@ public class SitesImpl implements Sites { role = getSiteRole(siteId); } - return new SiteImpl(siteInfo, role); + return new Site(siteInfo, role); } /** @@ -268,7 +289,7 @@ public class SitesImpl implements Sites String roleStr = siteService.getMembersRole(siteInfo.getShortName(), personId); if(roleStr != null) { - SiteImpl site = new SiteImpl(siteInfo, roleStr); + Site site = new Site(siteInfo, roleStr); siteMember = new MemberOfSite(site.getId(), siteInfo.getNodeRef(), roleStr); } else @@ -604,7 +625,7 @@ public class SitesImpl implements Sites List page = new AbstractList() { @Override - public SiteImpl get(int index) + public Site get(int index) { SiteInfo siteInfo = sites.get(index); @@ -613,7 +634,7 @@ public class SitesImpl implements Sites { role = siteService.getMembersRole(siteInfo.getShortName(), personId); } - return new SiteImpl(siteInfo, role); + return new Site(siteInfo, role); } @Override @@ -811,4 +832,146 @@ public class SitesImpl implements Sites return CollectionWithPagingInfo.asPaged(paging, favourites, favouriteSites.hasMoreItems(), favouriteSites.getTotalResultCount().getFirst()); } + + public void deleteSite(String siteId, Parameters parameters) + { + SiteInfo siteInfo = validateSite(siteId); + if(siteInfo == null) + { + // site does not exist + throw new EntityNotFoundException(siteId); + } + siteId = siteInfo.getShortName(); + + // default false (if not provided) + boolean permanentDelete = Boolean.valueOf(parameters.getParameter(PARAM_PERMANENT)); + + if (permanentDelete == true) + { + // Set as temporary to delete node instead of archiving. + nodeService.addAspect(siteInfo.getNodeRef(), ContentModel.ASPECT_TEMPORARY, null); + } + + siteService.deleteSite(siteId); + } + + // based on Share create site + private static final int SITE_MAXLEN_ID = 72; + private static final int SITE_MAXLEN_TITLE = 256; + private static final int SITE_MAXLEN_DESCRIPTION = 512; + + + /** + * Create default/preset (Share) site - with DocLib container/component + * + * @param site + * @return + */ + public Site createSite(Site site) + { + site = validateSite(site); + String siteId = site.getId(); + + siteService.createSite("sitePreset", siteId, site.getTitle(), site.getDescription(), site.getVisibility()); + importSite(siteId); + + // pre-create doclib + siteService.createContainer(siteId, SiteService.DOCUMENT_LIBRARY, ContentModel.TYPE_FOLDER, null); + + return getSite(siteId); + } + + private Site validateSite(Site site) + { + // site title - mandatory + String siteTitle = site.getTitle(); + if ((siteTitle == null) || siteTitle.isEmpty()) + { + throw new InvalidArgumentException("Site title is expected: "+siteTitle); + } + else if (siteTitle.length() > SITE_MAXLEN_TITLE) + { + throw new InvalidArgumentException("Site title exceeds max length of "+SITE_MAXLEN_TITLE+" characters"); + } + + SiteVisibility siteVisibility = site.getVisibility(); + if (siteVisibility == null) + { + throw new InvalidArgumentException("Site visibility is expected: "+siteTitle+" (eg. PUBLIC, PRIVATE, MODERATED)"); + } + + String siteId = site.getId(); + if (siteId == null) + { + siteId = siteTitle.trim(); + siteId = siteId.replace(" ","-"); + siteId = siteId.replaceAll("[^A-Za-z0-9\\-]",""); + } + else + { + if (! siteId.matches("[^A-Za-z0-9\\-]")) + { + throw new InvalidArgumentException("Invalid site id - should consist of alphanumeric/dash characters"); + } + } + + if (siteId.length() > SITE_MAXLEN_ID) + { + throw new InvalidArgumentException("Site id exceeds max length of "+SITE_MAXLEN_ID+ "characters"); + } + + site.setId(siteId); + + String siteDescription = site.getDescription(); + if ((siteDescription != null) && (siteDescription.length() > SITE_MAXLEN_DESCRIPTION)) + { + throw new InvalidArgumentException("Site description exceeds max length of "+SITE_MAXLEN_DESCRIPTION+" characters"); + } + + return site; + } + + private void importSite(final String siteId) + { + ImportPackageHandler acpHandler = new SiteImportPackageHandler(siteSurfConfig, siteId); + Location location = new Location(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE); + location.setPath("/app:company_home/st:sites/cm:" + ISO9075.encode(siteId)); + ImporterBinding binding = new ImporterBinding() + { + @Override + public String getValue(String key) + { + if (key.equals("siteId")) + { + return siteId; + } + return null; + } + + @Override + public UUID_BINDING getUUIDBinding() + { + return UUID_BINDING.CREATE_NEW; + } + + @Override + public QName[] getExcludedClasses() + { + return null; + } + + @Override + public boolean allowReferenceWithinTransaction() + { + return false; + } + + @Override + public ImporterContentCache getImportConentCache() + { + return null; + } + }; + importerService.importView(acpHandler, location, binding, (ImporterProgress)null); + } } diff --git a/source/java/org/alfresco/rest/api/model/FavouriteSite.java b/source/java/org/alfresco/rest/api/model/FavouriteSite.java index d3164acfc5..2a0d43c668 100644 --- a/source/java/org/alfresco/rest/api/model/FavouriteSite.java +++ b/source/java/org/alfresco/rest/api/model/FavouriteSite.java @@ -1,4 +1,4 @@ -/* +/* * #%L * Alfresco Remote API * %% @@ -9,20 +9,20 @@ * the paid license agreement will prevail. Otherwise, the software is * provided under the following open source license terms: * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.model; import org.alfresco.service.cmr.site.SiteInfo; @@ -35,7 +35,7 @@ import org.alfresco.service.cmr.site.SiteInfo; * @author steveglover * */ -public class FavouriteSite extends SiteImpl +public class FavouriteSite extends Site { public FavouriteSite() { diff --git a/source/java/org/alfresco/rest/api/model/Site.java b/source/java/org/alfresco/rest/api/model/Site.java index bcd05845bb..d475694a89 100644 --- a/source/java/org/alfresco/rest/api/model/Site.java +++ b/source/java/org/alfresco/rest/api/model/Site.java @@ -1,4 +1,4 @@ -/* +/* * #%L * Alfresco Remote API * %% @@ -9,23 +9,24 @@ * the paid license agreement will prevail. Otherwise, the software is * provided under the following open source license terms: * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.model; import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.site.SiteInfo; import org.alfresco.service.cmr.site.SiteVisibility; /** @@ -34,15 +35,139 @@ import org.alfresco.service.cmr.site.SiteVisibility; * @author steveglover * */ -public interface Site +public class Site implements Comparable { public static final String ROLE = "role"; - String getId(); - void setId(String id); - NodeRef getGuid(); - String getTitle(); - String getDescription(); - SiteVisibility getVisibility(); - String getRole(); + protected String id; // site id (aka short name) + protected String guid; // site nodeId + protected String title; + protected String description; + + public void setTitle(String title) + { + this.title = title; + } + + public void setDescription(String description) + { + this.description = description; + } + + public void setVisibility(SiteVisibility visibility) + { + this.visibility = visibility; + } + + public void setRole(String role) + { + this.role = role; + } + + protected SiteVisibility visibility; + protected String role; + + public Site() + { + } + + public Site(SiteInfo siteInfo, String role) + { + if(siteInfo == null) + { + throw new IllegalArgumentException("Must provide siteInfo"); + } + this.id = siteInfo.getShortName(); + this.guid = siteInfo.getNodeRef().getId(); + this.title = siteInfo.getTitle(); + this.description = siteInfo.getDescription(); + this.visibility = siteInfo.getVisibility(); + this.role = role; + } + + public String getId() + { + return id; + } + + public void setId(String id) + { + this.id = id; + } + + public String getGuid() + { + return guid; + } + + public void setGuid(String guid) + { + this.guid = guid; + } + + public String getTitle() + { + return title; + } + + public String getDescription() + { + return description; + } + + public SiteVisibility getVisibility() + { + return visibility; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + { + return true; + } + + if (obj == null) + { + return false; + } + + if (getClass() != obj.getClass()) + { + return false; + } + + Site other = (Site) obj; + return id.equals(other.id); + } + + @Override + public int compareTo(Site site) + { + return id.compareTo(site.getId()); + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public String toString() + { + return "Site [id=" + id + ", guid=" + guid + ", title=" + title + + ", description=" + description + ", visibility=" + visibility + + "]"; + } + + public String getRole() + { + return role; + } } \ No newline at end of file diff --git a/source/java/org/alfresco/rest/api/model/SiteImpl.java b/source/java/org/alfresco/rest/api/model/SiteImpl.java deleted file mode 100644 index 06136bfb24..0000000000 --- a/source/java/org/alfresco/rest/api/model/SiteImpl.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * #%L - * Alfresco Remote API - * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited - * %% - * This file is part of the Alfresco software. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ -package org.alfresco.rest.api.model; - -import org.alfresco.rest.framework.resource.UniqueId; -import org.alfresco.service.cmr.repository.NodeRef; -import org.alfresco.service.cmr.site.SiteInfo; -import org.alfresco.service.cmr.site.SiteVisibility; - -/** - * Represents a site. - * - * @author steveglover - * - */ -public class SiteImpl implements Site, Comparable -{ - protected String id; - protected NodeRef guid; - protected String title; - protected String description; - protected SiteVisibility visibility; - protected String role; - - public SiteImpl() - { - } - - public SiteImpl(SiteInfo siteInfo, String role) - { - if(siteInfo == null) - { - throw new IllegalArgumentException("Must provide siteInfo"); - } - this.id = siteInfo.getShortName(); - this.guid = siteInfo.getNodeRef(); - this.title = siteInfo.getTitle(); - this.description = siteInfo.getDescription(); - this.visibility = siteInfo.getVisibility(); - this.role = role; - } - - @UniqueId - public String getId() - { - return id; - } - - public void setId(String id) - { - this.id = id; - } - - public NodeRef getGuid() - { - return guid; - } - - public void setGuid(NodeRef guid) - { - this.guid = guid; - } - - public String getTitle() - { - return title; - } - - public String getDescription() - { - return description; - } - - public SiteVisibility getVisibility() - { - return visibility; - } - - @Override - public boolean equals(Object obj) - { - if (this == obj) - { - return true; - } - - if (obj == null) - { - return false; - } - - if (getClass() != obj.getClass()) - { - return false; - } - - SiteImpl other = (SiteImpl) obj; - return id.equals(other.id); - } - - @Override - public int compareTo(SiteImpl site) - { - return id.compareTo(site.getId()); - } - - @Override - public int hashCode() - { - final int prime = 31; - int result = 1; - result = prime * result - + ((id == null) ? 0 : id.hashCode()); - return result; - } - - @Override - public String toString() - { - return "Site [id=" + id + ", guid=" + guid + ", title=" + title - + ", description=" + description + ", visibility=" + visibility - + "]"; - } - - @Override - public String getRole() - { - return role; - } -} \ No newline at end of file diff --git a/source/java/org/alfresco/rest/api/sites/SiteEntityResource.java b/source/java/org/alfresco/rest/api/sites/SiteEntityResource.java index 03655b0280..74c929e172 100644 --- a/source/java/org/alfresco/rest/api/sites/SiteEntityResource.java +++ b/source/java/org/alfresco/rest/api/sites/SiteEntityResource.java @@ -28,6 +28,7 @@ package org.alfresco.rest.api.sites; import org.alfresco.rest.api.Sites; import org.alfresco.rest.api.model.Site; import org.alfresco.rest.framework.WebApiDescription; +import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException; import org.alfresco.rest.framework.resource.EntityResource; import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction; import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo; @@ -35,6 +36,9 @@ import org.alfresco.rest.framework.resource.parameters.Parameters; import org.alfresco.util.ParameterCheck; import org.springframework.beans.factory.InitializingBean; +import java.util.ArrayList; +import java.util.List; + /** * An implementation of an Entity Resource for a Site * @@ -42,7 +46,9 @@ import org.springframework.beans.factory.InitializingBean; * @author steveglover */ @EntityResource(name="sites", title = "Sites") -public class SiteEntityResource implements EntityResourceAction.Read, EntityResourceAction.ReadById, InitializingBean +public class SiteEntityResource implements EntityResourceAction.Read, + EntityResourceAction.ReadById, EntityResourceAction.Delete, + EntityResourceAction.Create, InitializingBean { private Sites sites; @@ -79,4 +85,35 @@ public class SiteEntityResource implements EntityResourceAction.Read, Enti return sites.getSite(siteId); } + /** + * Delete the given site. + * + * @param siteId String id of site. + */ + @Override + @WebApiDescription(title = "Delete Site", description="Delete the site. This will cascade delete") + public void delete(String siteId, Parameters parameters) + { + sites.deleteSite(siteId, parameters); + } + + /** + * + * @param entity + * @param parameters + * @return + */ + @Override + @WebApiDescription(title="Create site", description="Create the default/functional Share site") + public List create(List entity, Parameters parameters) + { + if (entity.size() != 1) + { + throw new InvalidArgumentException("Please specify one site entity only"); + } + + List result = new ArrayList<>(1); + result.add(sites.createSite(entity.get(0))); + return result; + } } diff --git a/source/java/org/alfresco/rest/framework/jacksonextensions/TargetDeserializer.java b/source/java/org/alfresco/rest/framework/jacksonextensions/TargetDeserializer.java index 467421df13..c2fae26638 100644 --- a/source/java/org/alfresco/rest/framework/jacksonextensions/TargetDeserializer.java +++ b/source/java/org/alfresco/rest/framework/jacksonextensions/TargetDeserializer.java @@ -32,7 +32,6 @@ import org.alfresco.rest.api.model.DocumentTarget; import org.alfresco.rest.api.model.Folder; import org.alfresco.rest.api.model.FolderTarget; import org.alfresco.rest.api.model.Site; -import org.alfresco.rest.api.model.SiteImpl; import org.alfresco.rest.api.model.SiteTarget; import org.alfresco.rest.api.model.Target; import org.alfresco.service.cmr.favourites.FavouritesService.Type; @@ -66,7 +65,7 @@ public class TargetDeserializer extends JsonDeserializer jp.nextToken(); try { - JavaType t = SimpleType.construct(SiteImpl.class); + JavaType t = SimpleType.construct(Site.class); BeanProperty p = new Std("", t, null, null); JsonDeserializer siteDeserializer = ctxt.getDeserializerProvider().findValueDeserializer(ctxt.getConfig(), t, p); diff --git a/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java b/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java index 6a61b3e60b..28a8fc4ea7 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java @@ -26,14 +26,20 @@ package org.alfresco.rest.api.tests; import static org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString; +import static org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsStringNonNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; + +import org.alfresco.model.ContentModel; import org.alfresco.repo.content.MimetypeMap; +import org.alfresco.repo.site.SiteInfoImpl; import org.alfresco.repo.tenant.TenantUtil; import org.alfresco.rest.api.Nodes; +import org.alfresco.rest.api.model.Site; import org.alfresco.rest.api.nodes.NodesEntityResource; +import org.alfresco.rest.api.sites.SiteEntityResource; import org.alfresco.rest.api.tests.RepoService.SiteInformation; import org.alfresco.rest.api.tests.RepoService.TestNetwork; import org.alfresco.rest.api.tests.RepoService.TestPerson; @@ -47,10 +53,9 @@ import org.alfresco.rest.api.tests.client.data.Document; import org.alfresco.rest.api.tests.client.data.Folder; import org.alfresco.rest.api.tests.client.data.Node; import org.alfresco.rest.api.tests.client.data.Rendition; -import org.alfresco.rest.api.tests.client.data.SiteRole; import org.alfresco.rest.api.tests.util.MultiPartBuilder; import org.alfresco.rest.api.tests.util.RestApiUtil; -import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.site.SiteInfo; import org.alfresco.service.cmr.site.SiteVisibility; import org.alfresco.util.TempFileProvider; import org.springframework.util.ResourceUtils; @@ -79,6 +84,10 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi private static final String URL_CHILDREN = "children"; private static final String URL_CONTENT = "content"; + protected static final String TYPE_CM_FOLDER = "cm:folder"; + protected static final String TYPE_CM_CONTENT = "cm:content"; + protected static final String TYPE_CM_OBJECT = "cm:cmobject"; + /** * The api scope. either public or private @@ -293,9 +302,11 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi return person.getId(); } + // @deprecated protected TestSite createSite(final TestNetwork testNetwork, TestPerson user, final SiteVisibility siteVisibility) { final String siteName = "RandomSite" + System.currentTimeMillis(); + final TestSite site = TenantUtil.runAsUserTenant(new TenantUtil.TenantRunAsWork() { @Override @@ -305,34 +316,59 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi return repoService.createSite(testNetwork, siteInfo); } }, user.getId(), testNetwork.getId()); - assertNotNull(site); return site; } - protected void inviteToSite(final TestSite testSite, final TestPerson invitee, final SiteRole siteRole) + protected Site createSite(String networkId, String userId, SiteVisibility siteVisibility) throws Exception { - TenantUtil.runAsTenant(new TenantUtil.TenantRunAsWork() - { - @Override - public Void doWork() throws Exception - { - testSite.inviteToSite(invitee.getId(), siteRole); - return null; - } - }, testSite.getNetworkId()); + String siteTitle = "RandomSite" + System.currentTimeMillis(); + return createSite(networkId, userId, null, siteTitle, siteVisibility); } - protected NodeRef getSiteDocLib(final TestSite testSite) + protected Site createSite(String networkId, String userId, String siteId, String siteTitle, SiteVisibility siteVisibility) throws Exception { - return TenantUtil.runAsTenant(new TenantUtil.TenantRunAsWork() - { - @Override - public NodeRef doWork() throws Exception - { - return testSite.getContainerNodeRef(("documentLibrary")); - } - }, testSite.getNetworkId()); + publicApiClient.setRequestContext(new RequestContext(networkId, userId)); + + Site site = new Site(); + site.setId(siteId); + site.setTitle(siteTitle); + site.setVisibility(siteVisibility); + + HttpResponse response = publicApiClient.post(getScope(), "sites", null, null, null, toJsonAsStringNonNull(site)); + return RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Site.class); + } + + protected HttpResponse deleteSite(String networkId, String userId, String siteId, int expectedStatus) throws Exception + { + publicApiClient.setRequestContext(new RequestContext(networkId, userId)); + + HttpResponse response = publicApiClient.delete(getScope(), "sites", siteId, null, null); + checkStatus(expectedStatus, response.getStatusCode()); + + return response; + } + + /** + * Helper: to get site container id (see also RepoService.getContainerNodeRef -> SiteService.getContainer) + * + * GET /nodes/-root?relativePath=/Sites/siteId/documentLibrary + * + * alternatively: + * + * GET /nodes/siteNodeId?relativePath=documentLibrary + */ + protected String getSiteContainerNodeId(String networkId, String runAsUserId, String siteId, String containerNameId) throws Exception + { + Map params = Collections.singletonMap(Nodes.PARAM_RELATIVE_PATH, "/Sites/"+siteId+"/"+containerNameId); + + publicApiClient.setRequestContext(new RequestContext(networkId, runAsUserId)); + + HttpResponse response = publicApiClient.get(NodesEntityResource.class, Nodes.PATH_ROOT, null, params); + checkStatus(200, response.getStatusCode()); + + Node node = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class); + return node.getId(); } protected void checkStatus(int expectedStatus, int actualStatus) @@ -374,7 +410,7 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi protected Folder createFolder(String runAsUserId, String parentId, String folderName, Map props) throws Exception { - return createNode(runAsUserId, parentId, folderName, "cm:folder", props, Folder.class); + return createNode(runAsUserId, parentId, folderName, TYPE_CM_FOLDER, props, Folder.class); } protected Node createNode(String runAsUserId, String parentId, String nodeName, String nodeType, Map props) throws Exception diff --git a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java index efe8b58034..3955cb6b61 100644 --- a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java @@ -42,10 +42,10 @@ import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.rest.api.Nodes; import org.alfresco.rest.api.model.NodeTarget; +import org.alfresco.rest.api.model.Site; import org.alfresco.rest.api.nodes.NodesEntityResource; import org.alfresco.rest.api.tests.RepoService.TestNetwork; import org.alfresco.rest.api.tests.RepoService.TestPerson; -import org.alfresco.rest.api.tests.RepoService.TestSite; import org.alfresco.rest.api.tests.client.HttpResponse; import org.alfresco.rest.api.tests.client.PublicApiClient; import org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging; @@ -58,6 +58,7 @@ import org.alfresco.rest.api.tests.client.data.Folder; import org.alfresco.rest.api.tests.client.data.Node; import org.alfresco.rest.api.tests.client.data.PathInfo; import org.alfresco.rest.api.tests.client.data.PathInfo.ElementInfo; +import org.alfresco.rest.api.tests.client.data.SiteMember; import org.alfresco.rest.api.tests.client.data.SiteRole; import org.alfresco.rest.api.tests.client.data.UserInfo; import org.alfresco.rest.api.tests.util.JacksonUtil; @@ -104,7 +105,7 @@ import java.util.UUID; * TODO * - improve test 'fwk' to enable api tests to be run against remote repo (rather than embedded jetty) * - requires replacement of non-remote calls with remote (preferably public) apis - * - eg. createUser (or any other usage of repoService), siteService (including getContainer), permissionService, node/archiveService + * - eg. createUser (or any other usage of repoService), permissionService, node/archiveService * * @author Jamal Kaabi-Mofrad * @author janv @@ -113,18 +114,23 @@ public class NodeApiTest extends AbstractBaseApiTest { private static final String PROP_OWNER = "cm:owner"; + TestNetwork networkOne; + /** * User one from network one */ private TestPerson userOneN1; + /** * User two from network one */ private TestPerson userTwoN1; + /** * Private site of user one from network one */ - private TestSite userOneN1Site; + private Site userOneN1Site; + private String user1; private String user2; private List users = new ArrayList<>(); @@ -148,7 +154,7 @@ public class NodeApiTest extends AbstractBaseApiTest jacksonUtil = new JacksonUtil(applicationContext.getBean("jsonHelper", JacksonHelper.class)); permissionService = applicationContext.getBean("permissionService", PermissionService.class); - // TODO replace with future V1 REST API for Trashcan + // TODO replace with V1 REST API for Trashcan nodeArchiveService = applicationContext.getBean("nodeArchiveService", NodeArchiveService.class); nodeService = applicationContext.getBean("nodeService", NodeService.class); @@ -163,16 +169,18 @@ public class NodeApiTest extends AbstractBaseApiTest users.add(user1); users.add(user2); - TestNetwork networkOne = getTestFixture().getRandomNetwork(); + networkOne = getTestFixture().getRandomNetwork(); userOneN1 = networkOne.createUser(); userTwoN1 = networkOne.createUser(); - userOneN1Site = createSite(networkOne, userOneN1, SiteVisibility.PRIVATE); + userOneN1Site = createSite(networkOne.getId(), userOneN1.getId(), SiteVisibility.PRIVATE); } @After public void tearDown() throws Exception { + deleteSite(networkOne.getId(), userOneN1.getId(), userOneN1Site.getId(), 204); + AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser(); for (final String user : users) { @@ -202,29 +210,28 @@ public class NodeApiTest extends AbstractBaseApiTest @Test public void testListDocLibChildren() throws Exception { - String userId = userOneN1.getId(); + String userOneId = userOneN1.getId(); + String userTwoId = userTwoN1.getId(); - AuthenticationUtil.setFullyAuthenticatedUser(userId); - NodeRef docLibNodeRef = userOneN1Site.getContainerNodeRef(("documentLibrary")); - String docLibNodeId = docLibNodeRef.getId(); + String docLibNodeId = getSiteContainerNodeId(networkOne.getId(), userOneId, userOneN1Site.getId(), "documentLibrary"); String folder1 = "folder" + System.currentTimeMillis() + "_1"; - createFolder(userId, docLibNodeId, folder1, null).getId(); + createFolder(userOneId, docLibNodeId, folder1, null).getId(); String folder2 = "folder" + System.currentTimeMillis() + "_2"; - createFolder(userId, docLibNodeId, folder2, null).getId(); + createFolder(userOneId, docLibNodeId, folder2, null).getId(); String content1 = "content" + System.currentTimeMillis() + "_1"; - createTextFile(userId, docLibNodeId, content1, "The quick brown fox jumps over the lazy dog 1.").getId(); + createTextFile(userOneId, docLibNodeId, content1, "The quick brown fox jumps over the lazy dog 1.").getId(); String content2 = "content" + System.currentTimeMillis() + "_2"; - createTextFile(userId, docLibNodeId, content2, "The quick brown fox jumps over the lazy dog 2.").getId(); + createTextFile(userOneId, docLibNodeId, content2, "The quick brown fox jumps over the lazy dog 2.").getId(); String forum1 = "forum" + System.currentTimeMillis() + "_1"; - createNode(userId, docLibNodeId, forum1, "fm:topic", null); + createNode(userOneId, docLibNodeId, forum1, "fm:topic", null); Paging paging = getPaging(0, 100); - HttpResponse response = getAll(getNodeChildrenUrl(docLibNodeId), userOneN1.getId(), paging, 200); + HttpResponse response = getAll(getNodeChildrenUrl(docLibNodeId), userOneId, paging, 200); List nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); assertEquals(4, nodes.size()); // forum is part of the default ignored types // Paging @@ -236,7 +243,7 @@ public class NodeApiTest extends AbstractBaseApiTest // Order by folders and modified date first Map orderBy = Collections.singletonMap("orderBy", "isFolder DESC,modifiedAt DESC"); - response = getAll(getNodeChildrenUrl(docLibNodeId), userOneN1.getId(), paging, orderBy, 200); + response = getAll(getNodeChildrenUrl(docLibNodeId), userOneId, paging, orderBy, 200); nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); assertEquals(4, nodes.size()); assertEquals(folder2, nodes.get(0).getName()); @@ -254,7 +261,7 @@ public class NodeApiTest extends AbstractBaseApiTest // Order by folders last and modified date first orderBy = Collections.singletonMap("orderBy", "isFolder ASC,modifiedAt DESC"); - response = getAll(getNodeChildrenUrl(docLibNodeId), userOneN1.getId(), paging, orderBy, 200); + response = getAll(getNodeChildrenUrl(docLibNodeId), userOneId, paging, orderBy, 200); nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); assertEquals(4, nodes.size()); assertEquals(content2, nodes.get(0).getName()); @@ -264,7 +271,7 @@ public class NodeApiTest extends AbstractBaseApiTest // Order by folders and modified date last orderBy = Collections.singletonMap("orderBy", "isFolder,modifiedAt"); - response = getAll(getNodeChildrenUrl(docLibNodeId), userOneN1.getId(), paging, orderBy, 200); + response = getAll(getNodeChildrenUrl(docLibNodeId), userOneId, paging, orderBy, 200); nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); assertEquals(4, nodes.size()); assertEquals(content1, nodes.get(0).getName()); @@ -276,7 +283,7 @@ public class NodeApiTest extends AbstractBaseApiTest orderBy = Collections.singletonMap("orderBy", "isFolder DESC,modifiedAt DESC"); // SkipCount=0,MaxItems=2 paging = getPaging(0, 2); - response = getAll(getNodeChildrenUrl(docLibNodeId), userOneN1.getId(), paging, orderBy, 200); + response = getAll(getNodeChildrenUrl(docLibNodeId), userOneId, paging, orderBy, 200); nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); assertEquals(2, nodes.size()); assertEquals(folder2, nodes.get(0).getName()); @@ -289,7 +296,7 @@ public class NodeApiTest extends AbstractBaseApiTest // SkipCount=2,MaxItems=4 paging = getPaging(2, 4); - response = getAll(getNodeChildrenUrl(docLibNodeId), userOneN1.getId(), paging, orderBy, 200); + response = getAll(getNodeChildrenUrl(docLibNodeId), userOneId, paging, orderBy, 200); nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); assertEquals(2, nodes.size()); assertEquals(content2, nodes.get(0).getName()); @@ -301,9 +308,9 @@ public class NodeApiTest extends AbstractBaseApiTest assertFalse(expectedPaging.getHasMoreItems().booleanValue()); // userTwoN1 tries to access userOneN1's docLib - AuthenticationUtil.setFullyAuthenticatedUser(userTwoN1.getId()); + AuthenticationUtil.setFullyAuthenticatedUser(userTwoId); paging = getPaging(0, Integer.MAX_VALUE); - getAll(getNodeChildrenUrl(docLibNodeId), userTwoN1.getId(), paging, 403); + getAll(getNodeChildrenUrl(docLibNodeId), userTwoId, paging, 403); } /** @@ -364,7 +371,7 @@ public class NodeApiTest extends AbstractBaseApiTest assertEquals(folder1, nodes.get(1).getName()); Document node = nodes.get(2); assertEquals(content1, node.getName()); - assertEquals("cm:content", node.getNodeType()); + assertEquals(TYPE_CM_CONTENT, node.getNodeType()); assertEquals(content1_Id, node.getId()); UserInfo createdByUser = node.getCreatedByUser(); assertEquals(user1, createdByUser.getId()); @@ -552,14 +559,15 @@ public class NodeApiTest extends AbstractBaseApiTest { String userId = userOneN1.getId(); - AuthenticationUtil.setFullyAuthenticatedUser(userId); - userOneN1Site.inviteToSite(userTwoN1.getEmail(), SiteRole.SiteConsumer); + publicApiClient.setRequestContext(new RequestContext(userOneN1.getId())); + PublicApiClient.Sites sitesProxy = publicApiClient.sites(); + sitesProxy.createSiteMember(userOneN1Site.getId(), new SiteMember(userTwoN1.getId(), SiteRole.SiteConsumer.toString())); - NodeRef docLibNodeRef = userOneN1Site.getContainerNodeRef(("documentLibrary")); + String docLibNodeId = getSiteContainerNodeId(networkOne.getId(), userOneN1.getId(), userOneN1Site.getId(), "documentLibrary"); // /Company Home/Sites/RandomSite/documentLibrary/folder_A String folderA = "folder" + System.currentTimeMillis() + "_A"; - String folderA_Id = createFolder(userId, docLibNodeRef.getId(), folderA).getId(); + String folderA_Id = createFolder(userId, docLibNodeId, folderA).getId(); // /Company Home/Sites/RandomSite/documentLibrary/folder_A/folder_B String folderB = "folder" + System.currentTimeMillis() + "_B"; @@ -577,6 +585,7 @@ public class NodeApiTest extends AbstractBaseApiTest // TODO refactor with remote permission api calls (use v0 until we have v1 ?) + AuthenticationUtil.setFullyAuthenticatedUser(userId); // Revoke folderB inherited permissions permissionService.setInheritParentPermissions(folderB_Ref, false); // Grant userTwoN1 permission for folderC @@ -597,7 +606,7 @@ public class NodeApiTest extends AbstractBaseApiTest assertEquals(7, pathElements.size()); assertEquals("Company Home", pathElements.get(0).getName()); assertEquals("Sites", pathElements.get(1).getName()); - assertEquals(userOneN1Site.getSiteId(), pathElements.get(2).getName()); + assertEquals(userOneN1Site.getId(), pathElements.get(2).getName()); assertEquals("documentLibrary", pathElements.get(3).getName()); assertEquals(folderA, pathElements.get(4).getName()); assertEquals(folderB, pathElements.get(5).getName()); @@ -673,7 +682,7 @@ public class NodeApiTest extends AbstractBaseApiTest d1.setId(content_Id); d1.setParentId(folderB_Id); d1.setName(contentName); - d1.setNodeType("cm:content"); + d1.setNodeType(TYPE_CM_CONTENT); ContentInfo ci = new ContentInfo(); ci.setMimeType("text/plain"); @@ -991,9 +1000,7 @@ public class NodeApiTest extends AbstractBaseApiTest final String fileName = "quick-1.txt"; final File file = getResourceFile(fileName); - AuthenticationUtil.setFullyAuthenticatedUser(userOneN1.getId()); - NodeRef docLibNodeRef = userOneN1Site.getContainerNodeRef(("documentLibrary")); - String docLibNodeId = docLibNodeRef.getId(); + String docLibNodeId = getSiteContainerNodeId(networkOne.getId(), userOneN1.getId(), userOneN1Site.getId(), "documentLibrary"); String folderA = "folder" + System.currentTimeMillis() + "_A"; String folderA_id = createFolder(userOneN1.getId(), docLibNodeId, folderA).getId(); @@ -1520,9 +1527,9 @@ public class NodeApiTest extends AbstractBaseApiTest @Test public void testCopySite() throws Exception { - TestNetwork networkOne = getTestFixture().getRandomNetwork(); - TestPerson cs1 = networkOne.createUser(); - TestSite tSite = createSite(networkOne, cs1, SiteVisibility.PRIVATE); + TestNetwork network = getTestFixture().getRandomNetwork(); + TestPerson cs1 = network.createUser(); + Site tSite = createSite(network.getId(), cs1.getId(), SiteVisibility.PRIVATE); // create folder Folder folderResp = createFolder(cs1.getId(), Nodes.PATH_MY, "siteCopytarget"); @@ -1532,13 +1539,12 @@ public class NodeApiTest extends AbstractBaseApiTest body.put("targetParentId", targetId); //test that you can't copy a site - post("nodes/"+tSite.getSiteInfo().getNodeRef().getId()+"/copy", cs1.getId(), toJsonAsStringNonNull(body), null, 422); + post("nodes/"+tSite.getGuid()+"/copy", cs1.getId(), toJsonAsStringNonNull(body), null, 422); - AuthenticationUtil.setFullyAuthenticatedUser(cs1.getId()); - NodeRef docLibNodeRef = tSite.getContainerNodeRef("documentLibrary"); + String docLibNodeId = getSiteContainerNodeId(network.getId(), cs1.getId(), tSite.getId(), "documentLibrary"); //test that you can't copy a site doclib - post("nodes/"+docLibNodeRef.getId()+"/copy", cs1.getId(), toJsonAsStringNonNull(body), null, 422); + post("nodes/"+docLibNodeId+"/copy", cs1.getId(), toJsonAsStringNonNull(body), null, 422); } @@ -1654,7 +1660,7 @@ public class NodeApiTest extends AbstractBaseApiTest Folder f1 = new Folder(); f1.setName("f1"); - f1.setNodeType("cm:folder"); + f1.setNodeType(TYPE_CM_FOLDER); f1.setIsFolder(true); f1.setParentId(myNodeId); @@ -1675,7 +1681,7 @@ public class NodeApiTest extends AbstractBaseApiTest Folder f2 = new Folder(); f2.setName("f2"); - f2.setNodeType("cm:folder"); + f2.setNodeType(TYPE_CM_FOLDER); f2.setProperties(props); f2.setIsFolder(true); @@ -1690,7 +1696,7 @@ public class NodeApiTest extends AbstractBaseApiTest // create another folder in a (partially existing) folder path Node n = new Node(); n.setName("fZ"); - n.setNodeType("cm:folder"); + n.setNodeType(TYPE_CM_FOLDER); n.setRelativePath("/f1/f2/f3/f4"); // create node @@ -1717,13 +1723,13 @@ public class NodeApiTest extends AbstractBaseApiTest // -ve test - name is mandatory Folder invalid = new Folder(); - invalid.setNodeType("cm:folder"); + invalid.setNodeType(TYPE_CM_FOLDER); post(postUrl, user1, toJsonAsStringNonNull(invalid), 400); // -ve test - invalid name invalid = new Folder(); invalid.setName("inv:alid"); - invalid.setNodeType("cm:folder"); + invalid.setNodeType(TYPE_CM_FOLDER); post(postUrl, user1, toJsonAsStringNonNull(invalid), 422); // -ve test - node type is mandatory @@ -1734,7 +1740,7 @@ public class NodeApiTest extends AbstractBaseApiTest // create empty file - used in -ve test below Document d1 = new Document(); d1.setName("d1.txt"); - d1.setNodeType("cm:content"); + d1.setNodeType(TYPE_CM_CONTENT); ContentInfo ci = new ContentInfo(); ci.setMimeType("text/plain"); d1.setContent(ci); @@ -1746,7 +1752,7 @@ public class NodeApiTest extends AbstractBaseApiTest // -ve test - invalid (eg. not a folder) parent id Folder f3 = new Folder(); f3.setName("f3"); - f3.setNodeType("cm:folder"); + f3.setNodeType(TYPE_CM_FOLDER); post(getNodeChildrenUrl(d1Id), user1, toJsonAsStringNonNull(f3), 400); // -ve test - it should not be possible to create a "system folder" @@ -1777,14 +1783,14 @@ public class NodeApiTest extends AbstractBaseApiTest // -ve test - invalid relative path n = new Node(); n.setName("fX"); - n.setNodeType("cm:folder"); + n.setNodeType(TYPE_CM_FOLDER); n.setRelativePath("/f1/inv:alid"); post(getNodeChildrenUrl(f2Id), user1, RestApiUtil.toJsonAsStringNonNull(n), 422); // -ve test - invalid relative path - points to existing node that is not a folder n = new Node(); n.setName("fY"); - n.setNodeType("cm:folder"); + n.setNodeType(TYPE_CM_FOLDER); n.setRelativePath("d1.txt"); post(getNodeChildrenUrl(myNodeId), user1, RestApiUtil.toJsonAsStringNonNull(n), 409); } @@ -1824,7 +1830,7 @@ public class NodeApiTest extends AbstractBaseApiTest { Node obj = new Node(); obj.setName("obj "+i+" "+timeNow); - obj.setNodeType("cm:cmobject"); + obj.setNodeType(TYPE_CM_OBJECT); // create node/object HttpResponse response = post(myChildrenUrl, user1, toJsonAsStringNonNull(obj), 201); @@ -1860,7 +1866,7 @@ public class NodeApiTest extends AbstractBaseApiTest // filtering, via where clause - folders Map params = new HashMap<>(); - params.put("where", "(nodeType='cm:folder')"); + params.put("where", "(nodeType='"+TYPE_CM_FOLDER+"')"); response = getAll(myChildrenUrl, user1, paging, params, 200); nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); @@ -1883,7 +1889,7 @@ public class NodeApiTest extends AbstractBaseApiTest // filtering, via where clause - files params = new HashMap<>(); - params.put("where", "(nodeType='cm:content')"); + params.put("where", "(nodeType='"+TYPE_CM_CONTENT+"')"); response = getAll(myChildrenUrl, user1, paging, params, 200); nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); @@ -1906,7 +1912,7 @@ public class NodeApiTest extends AbstractBaseApiTest // filtering, via where clause - non-folders / non-files params = new HashMap<>(); - params.put("where", "(nodeType='cm:cmobject')"); + params.put("where", "(nodeType='"+TYPE_CM_OBJECT+"')"); response = getAll(myChildrenUrl, user1, paging, params, 200); nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class); @@ -1986,7 +1992,7 @@ public class NodeApiTest extends AbstractBaseApiTest // create empty file d1 in f1 Document d1 = new Document(); d1.setName("d1.txt"); - d1.setNodeType("cm:content"); + d1.setNodeType(TYPE_CM_CONTENT); ContentInfo ci = new ContentInfo(); ci.setMimeType("text/plain"); d1.setContent(ci); @@ -2167,7 +2173,7 @@ public class NodeApiTest extends AbstractBaseApiTest Document d1 = new Document(); d1.setName("d1.txt"); - d1.setNodeType("cm:content"); + d1.setNodeType(TYPE_CM_CONTENT); ContentInfo ci = new ContentInfo(); ci.setMimeType("text/plain"); d1.setContent(ci); @@ -2197,7 +2203,7 @@ public class NodeApiTest extends AbstractBaseApiTest Document d2 = new Document(); d2.setName("d2.txt"); - d2.setNodeType("cm:content"); + d2.setNodeType(TYPE_CM_CONTENT); d2.setProperties(props); response = post(postUrl, user1, toJsonAsStringNonNull(d2), 201); @@ -2223,7 +2229,7 @@ public class NodeApiTest extends AbstractBaseApiTest // create another empty file in a (partially existing) folder path Node n = new Node(); n.setName("d3.txt"); - n.setNodeType("cm:content"); + n.setNodeType(TYPE_CM_CONTENT); n.setRelativePath("/f1/f2"); // create node @@ -2249,7 +2255,7 @@ public class NodeApiTest extends AbstractBaseApiTest // -ve test - name is mandatory Document invalid = new Document(); - invalid.setNodeType("cm:content"); + invalid.setNodeType(TYPE_CM_CONTENT); post(postUrl, user1, toJsonAsStringNonNull(invalid), 400); // -ve test - node type is mandatory @@ -2260,7 +2266,7 @@ public class NodeApiTest extends AbstractBaseApiTest // -ve test - invalid (eg. not a folder) parent id Document d3 = new Document(); d3.setName("d3.txt"); - d3.setNodeType("cm:content"); + d3.setNodeType(TYPE_CM_CONTENT); post(getNodeChildrenUrl(d1Id), user1, toJsonAsStringNonNull(d3), 400); // -ve test - unknown parent folder node id @@ -2314,7 +2320,7 @@ public class NodeApiTest extends AbstractBaseApiTest Folder f1 = new Folder(); f1.setName(folderName); - f1.setNodeType("cm:folder"); + f1.setNodeType(TYPE_CM_FOLDER); f1.setIsFolder(true); f1.setParentId(myNodeId); @@ -2329,7 +2335,7 @@ public class NodeApiTest extends AbstractBaseApiTest Document d1 = new Document(); d1.setName("d1.txt"); - d1.setNodeType("cm:content"); + d1.setNodeType(TYPE_CM_CONTENT); ContentInfo ci = new ContentInfo(); ci.setMimeType("text/plain"); d1.setContent(ci); @@ -2481,7 +2487,7 @@ public class NodeApiTest extends AbstractBaseApiTest // -ve test - generalise node type fUpdate = new Folder(); - fUpdate.setNodeType("cm:folder"); + fUpdate.setNodeType(TYPE_CM_FOLDER); put(URL_NODES, user1, fId, toJsonAsStringNonNull(fUpdate), null, 400); // -ve test - try to move to a different parent using PUT (note: should use new POST /nodes/{nodeId}/move operation instead) @@ -2622,7 +2628,7 @@ public class NodeApiTest extends AbstractBaseApiTest Folder f1 = new Folder(); f1.setName("F1"); - f1.setNodeType("cm:folder"); + f1.setNodeType(TYPE_CM_FOLDER); HttpResponse response = post(getNodeChildrenUrl(myNodeId), user1, toJsonAsStringNonNull(f1), 201); Folder folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class); @@ -2633,7 +2639,7 @@ public class NodeApiTest extends AbstractBaseApiTest Document doc = new Document(); final String docName = "testdoc"; doc.setName(docName); - doc.setNodeType("cm:content"); + doc.setNodeType(TYPE_CM_CONTENT); doc.setProperties(Collections.singletonMap("cm:title", (Object)"test title")); ContentInfo contentInfo = new ContentInfo(); contentInfo.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN); @@ -2673,7 +2679,7 @@ public class NodeApiTest extends AbstractBaseApiTest assertFalse(docResp.getIsFolder()); assertTrue(docResp.getIsFile()); assertNull(docResp.getIsLink()); - assertEquals("cm:content", docResp.getNodeType()); + assertEquals(TYPE_CM_CONTENT, docResp.getNodeType()); assertNotNull(docResp.getParentId()); assertEquals(f1_nodeId, docResp.getParentId()); assertNotNull(docResp.getProperties()); @@ -2750,7 +2756,7 @@ public class NodeApiTest extends AbstractBaseApiTest Document d1 = new Document(); d1.setName("d1.txt"); - d1.setNodeType("cm:content"); + d1.setNodeType(TYPE_CM_CONTENT); ContentInfo ci = new ContentInfo(); ci.setMimeType("text/plain"); d1.setContent(ci); @@ -3337,7 +3343,7 @@ public class NodeApiTest extends AbstractBaseApiTest // as userOneN1 ... String userId = userOneN1.getId(); AuthenticationUtil.setFullyAuthenticatedUser(userId); - String siteNodeId = userOneN1Site.getSiteInfo().getNodeRef().getId(); + String siteNodeId = userOneN1Site.getGuid(); AuthenticationUtil.clearCurrentSecurityContext(); response = getSingle(NodesEntityResource.class, userId, siteNodeId, params, 200); diff --git a/source/test-java/org/alfresco/rest/api/tests/RenditionsTest.java b/source/test-java/org/alfresco/rest/api/tests/RenditionsTest.java index efc6c7d000..fda635657a 100644 --- a/source/test-java/org/alfresco/rest/api/tests/RenditionsTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/RenditionsTest.java @@ -27,7 +27,6 @@ package org.alfresco.rest.api.tests; import static org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsString; -import static org.alfresco.rest.api.tests.util.RestApiUtil.toJsonAsStringNonNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; @@ -35,13 +34,10 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.google.common.collect.Ordering; -import org.alfresco.model.ContentModel; import org.alfresco.repo.content.MimetypeMap; -import org.alfresco.repo.security.authentication.AuthenticationUtil; -import org.alfresco.repo.tenant.TenantUtil; +import org.alfresco.rest.api.model.Site; import org.alfresco.rest.api.tests.RepoService.TestNetwork; import org.alfresco.rest.api.tests.RepoService.TestPerson; -import org.alfresco.rest.api.tests.RepoService.TestSite; import org.alfresco.rest.api.tests.client.HttpResponse; import org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedErrorResponse; import org.alfresco.rest.api.tests.client.PublicApiClient.ExpectedPaging; @@ -56,7 +52,6 @@ import org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest; import org.alfresco.rest.api.tests.util.RestApiUtil; import org.alfresco.service.cmr.site.SiteVisibility; import org.alfresco.service.cmr.thumbnail.ThumbnailService; -import org.alfresco.service.namespace.QName; import org.alfresco.util.TempFileProvider; import org.junit.After; import org.junit.Before; @@ -80,6 +75,11 @@ import java.util.UUID; */ public class RenditionsTest extends AbstractBaseApiTest { + /** + * Test network one + */ + TestNetwork networkOne; + /** * User one from network one */ @@ -88,23 +88,22 @@ public class RenditionsTest extends AbstractBaseApiTest /** * Private site of user one from network one */ - private TestSite userOneN1Site; + private Site userOneN1Site; @Before public void setup() throws Exception { - TestNetwork networkOne = repoService.createNetworkWithAlias("ping", true); + networkOne = repoService.createNetworkWithAlias("ping", true); networkOne.create(); - userOneN1 = networkOne.createUser(); - AuthenticationUtil.setFullyAuthenticatedUser(userOneN1.getId()); - userOneN1Site = createSite(networkOne, userOneN1, SiteVisibility.PRIVATE); + + userOneN1Site = createSite(networkOne.getId(), userOneN1.getId(), SiteVisibility.PRIVATE); } @After public void tearDown() throws Exception { - AuthenticationUtil.clearCurrentSecurityContext(); + deleteSite(networkOne.getId(), userOneN1.getId(), userOneN1Site.getId(), 204); } /** @@ -117,7 +116,7 @@ public class RenditionsTest extends AbstractBaseApiTest { // Create a folder within the site document's library String folderName = "folder" + System.currentTimeMillis(); - String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, ContentModel.TYPE_FOLDER, userOneN1.getId()); + String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, TYPE_CM_FOLDER, userOneN1.getId()); // Create multipart request String fileName = "quick.pdf"; @@ -233,9 +232,8 @@ public class RenditionsTest extends AbstractBaseApiTest getAll(getNodeRenditionsUrl(UUID.randomUUID().toString()), userOneN1.getId(), paging, params, 404); // Create a node without any content - String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", ContentModel.TYPE_CONTENT, userOneN1.getId()); - // The source node has no content - getAll(getNodeRenditionsUrl(emptyContentNodeId), userOneN1.getId(), paging, params, 400); + String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId()); + getAll(getNodeRenditionsUrl(emptyContentNodeId), userOneN1.getId(), paging, params, 200); // Invalid status value params.put("where", "(status='WRONG')"); @@ -256,7 +254,7 @@ public class RenditionsTest extends AbstractBaseApiTest { // Create a folder within the site document's library String folderName = "folder" + System.currentTimeMillis(); - String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, ContentModel.TYPE_FOLDER, userOneN1.getId()); + String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, TYPE_CM_FOLDER, userOneN1.getId()); // Create multipart request String fileName = "quick.pdf"; @@ -302,9 +300,8 @@ public class RenditionsTest extends AbstractBaseApiTest getSingle(getNodeRenditionsUrl(contentNodeId), userOneN1.getId(), ("renditionId" + System.currentTimeMillis()), 404); // Create a node without any content - String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", ContentModel.TYPE_CONTENT, userOneN1.getId()); - // The source node has no content - getSingle(getNodeRenditionsUrl(emptyContentNodeId), userOneN1.getId(), "doclib", 400); + String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId()); + getSingle(getNodeRenditionsUrl(emptyContentNodeId), userOneN1.getId(), "doclib", 200); // Create multipart request String jpgFileName = "quick.jpg"; @@ -339,7 +336,7 @@ public class RenditionsTest extends AbstractBaseApiTest { // Create a folder within the site document's library String folderName = "folder" + System.currentTimeMillis(); - String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, ContentModel.TYPE_FOLDER, userOneN1.getId()); + String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, TYPE_CM_FOLDER, userOneN1.getId()); // Create multipart request String fileName = "quick.pdf"; @@ -413,12 +410,12 @@ public class RenditionsTest extends AbstractBaseApiTest post(getNodeRenditionsUrl(contentNodeId), userOneN1.getId(), toJsonAsString(request), 400); // Create a node without any content - String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", ContentModel.TYPE_CONTENT, userOneN1.getId()); + String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId()); // The source node has no content request = new ArrayList<>(2); request.add(new Rendition().setId("doclib")); - post(getNodeRenditionsUrl(emptyContentNodeId), userOneN1.getId(), toJsonAsString(renditionRequest), 400); + post(getNodeRenditionsUrl(emptyContentNodeId), userOneN1.getId(), toJsonAsString(renditionRequest), 202); String content = "The quick brown fox jumps over the lazy dog."; file = TempFileProvider.createTempFile(new ByteArrayInputStream(content.getBytes()), getClass().getSimpleName(), ".bin"); @@ -472,7 +469,7 @@ public class RenditionsTest extends AbstractBaseApiTest // Create a folder within the site document's library String folderName = "folder" + System.currentTimeMillis(); - String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, ContentModel.TYPE_FOLDER, userId); + String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, TYPE_CM_FOLDER, userId); // Create multipart request - pdf file String renditionName = "doclib"; @@ -591,7 +588,7 @@ public class RenditionsTest extends AbstractBaseApiTest { // Create a folder within the site document's library String folderName = "folder" + System.currentTimeMillis(); - String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, ContentModel.TYPE_FOLDER, userOneN1.getId()); + String folder_Id = addToDocumentLibrary(userOneN1Site, folderName, TYPE_CM_FOLDER, userOneN1.getId()); // Create multipart request String fileName = "quick.pdf"; @@ -753,22 +750,18 @@ public class RenditionsTest extends AbstractBaseApiTest getSingle(getNodeRenditionsUrl(contentNodeId), userOneN1.getId(), ("renditionId" + System.currentTimeMillis() + "/content"), params, 404); // Create a node without any content - String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", ContentModel.TYPE_CONTENT, userOneN1.getId()); - // The source node has no content - getSingle(getNodeRenditionsUrl(emptyContentNodeId), userOneN1.getId(), "doclib/content", params, 400); - + String emptyContentNodeId = addToDocumentLibrary(userOneN1Site, "emptyDoc.txt", TYPE_CM_CONTENT, userOneN1.getId()); + getSingle(getNodeRenditionsUrl(emptyContentNodeId), userOneN1.getId(), "doclib/content", params, 200); } - private String addToDocumentLibrary(final TestSite testSite, final String name, final QName type, String user) + private String addToDocumentLibrary(Site testSite, String name, String nodeType, String userId) throws Exception { - return TenantUtil.runAsUserTenant(new TenantUtil.TenantRunAsWork() - { - @Override - public String doWork() throws Exception - { - return repoService.addToDocumentLibrary(testSite, name, type).getId(); - } - }, user, testSite.getNetworkId()); + // TODO refactor to consistently handle user/network (with option to switch network in cloud test scenarios) + // eg. set request context (rather than explicitly passing userId/networkId) ? + String networkId = repoService.tenantService.getUserDomain(userId); + + String parentId = getSiteContainerNodeId(networkId, userId, testSite.getId(), "documentLibrary"); + return createNode(userId, parentId, name, nodeType, null).getId(); } private Rendition getRendition(List renditions, String renditionName) diff --git a/source/test-java/org/alfresco/rest/api/tests/TestSites.java b/source/test-java/org/alfresco/rest/api/tests/TestSites.java index a93da97762..a39a80661c 100644 --- a/source/test-java/org/alfresco/rest/api/tests/TestSites.java +++ b/source/test-java/org/alfresco/rest/api/tests/TestSites.java @@ -1,4 +1,4 @@ -/* +/* * #%L * Alfresco Remote API * %% @@ -9,20 +9,20 @@ * the paid license agreement will prevail. Otherwise, the software is * provided under the following open source license terms: * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.tests; import static org.junit.Assert.assertEquals; @@ -42,7 +42,10 @@ import org.alfresco.rest.api.tests.client.PublicApiClient.Paging; import org.alfresco.rest.api.tests.client.PublicApiClient.Sites; import org.alfresco.rest.api.tests.client.PublicApiException; import org.alfresco.rest.api.tests.client.RequestContext; +import org.alfresco.rest.api.tests.client.data.Comment; import org.alfresco.rest.api.tests.client.data.Site; +import org.alfresco.rest.api.tests.client.data.SiteImpl; +import org.alfresco.rest.api.tests.client.data.SiteRole; import org.alfresco.service.cmr.site.SiteVisibility; import org.alfresco.util.GUID; import org.apache.commons.httpclient.HttpStatus; @@ -95,20 +98,7 @@ public class TestSites extends EnterpriseTestApi { assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); } - - - // Test Case cloud-1963 - // invalid methods - try - { - sitesProxy.create("sites", null, null, null, null, "Unable to POST to sites"); - fail(); - } - catch(PublicApiException e) - { - assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); - } - + try { sitesProxy.create("sites", "site", null, null, null, "Unable to POST to a site"); @@ -118,26 +108,6 @@ public class TestSites extends EnterpriseTestApi { assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); } - -// try -// { -// sitesProxy.remove("sites", null, null, null, "Unable to DELETE sites"); -// fail(); -// } -// catch(PublicApiException e) -// { -// assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); -// } -// -// try -// { -// sitesProxy.remove("sites", "site", null, null, "Unable to DELETE sites"); -// fail(); -// } -// catch(PublicApiException e) -// { -// assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); -// } try { @@ -148,15 +118,16 @@ public class TestSites extends EnterpriseTestApi { assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); } - + + // -ve - try to delete unknown site try { - sitesProxy.remove("sites", "site", null, null, "Unable to DELETE sites"); + sitesProxy.remove("sites", "dummy", null, null, "Unable to DELETE site - not found"); fail(); } catch(PublicApiException e) { - assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode()); + assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); } // invalid site @@ -216,10 +187,40 @@ public class TestSites extends EnterpriseTestApi ListResponse resp = sitesProxy.getSites(createParams(paging, null)); checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()), paging.getExpectedPaging(), resp); } + + { + String siteTitle = "my site 123"; + + Site site = new SiteImpl("my site 123", SiteVisibility.PRIVATE.toString()); + publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId)); + Site ret = sitesProxy.createSite(site); + + String siteId = siteTitle.replace(' ', '-'); + Site siteExp = new SiteImpl(null, siteId, ret.getGuid(), siteTitle, null, SiteVisibility.PRIVATE.toString(), null, SiteRole.SiteManager); + siteExp.expected(ret); + + publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId)); + ret = sitesProxy.getSite(siteId); + siteExp.expected(ret); + + sitesProxy.removeSite(siteId); + + try + { + publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId)); + sitesProxy.getSite(siteId); + fail(""); + } + catch(PublicApiException e) + { + assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode()); + } + } // Test Case cloud-1478 // Test Case cloud-1479 // user invited to network and user invited to site // user invited to network and user not invited to site + } } diff --git a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java index 056b797c1f..66d04203c6 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java @@ -63,6 +63,7 @@ import org.alfresco.rest.api.tests.client.data.SiteImpl; import org.alfresco.rest.api.tests.client.data.SiteMember; import org.alfresco.rest.api.tests.client.data.SiteMembershipRequest; import org.alfresco.rest.api.tests.client.data.Tag; +import org.alfresco.service.cmr.site.SiteVisibility; import org.apache.chemistry.opencmis.client.api.CmisObject; import org.apache.chemistry.opencmis.client.api.Document; import org.apache.chemistry.opencmis.client.api.FileableCmisObject; @@ -821,6 +822,17 @@ public class PublicApiClient return SiteImpl.parseSite((JSONObject)response.getJsonResponse().get("entry")); } + public Site createSite(Site site) throws PublicApiException + { + HttpResponse response = create("sites", null, null, null, site.toJSON().toString(), "Failed to create site"); + return SiteImpl.parseSite((JSONObject)response.getJsonResponse().get("entry")); + } + + public void removeSite(String siteId) throws PublicApiException + { + remove("sites", siteId, null, null, "Failed to remove site"); + } + public ListResponse getSiteContainers(String siteId, Map params) throws PublicApiException { HttpResponse response = getAll("sites", siteId, "containers", null, params, "Failed to get site containers"); diff --git a/source/test-java/org/alfresco/rest/api/tests/client/data/SiteImpl.java b/source/test-java/org/alfresco/rest/api/tests/client/data/SiteImpl.java index 3ad6f202e5..444a1bc749 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/data/SiteImpl.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/data/SiteImpl.java @@ -1,4 +1,4 @@ -/* +/* * #%L * Alfresco Remote API * %% @@ -9,20 +9,20 @@ * the paid license agreement will prevail. Otherwise, the software is * provided under the following open source license terms: * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.tests.client.data; import static org.junit.Assert.assertNotNull; @@ -59,14 +59,30 @@ public class SiteImpl implements Serializable, Site, Comparable, Expec public SiteImpl() { } - - public SiteImpl(String networkId, String siteId, String guid) - { - if(siteId == null) + + public SiteImpl(String title, String visibility) + { + if (title == null) { throw new java.lang.IllegalArgumentException(); } - if(guid == null) + + if (visibility == null) + { + throw new java.lang.IllegalArgumentException(); + } + this.title = title; + this.visibility = visibility; + } + + public SiteImpl(String networkId, String siteId, String guid) + { + if (siteId == null) + { + throw new java.lang.IllegalArgumentException(); + } + + if (guid == null) { throw new java.lang.IllegalArgumentException(); } @@ -249,7 +265,7 @@ public class SiteImpl implements Serializable, Site, Comparable, Expec @Override public String toString() { - return "SiteImpl [created=" + created + ", networkId=" + networkId + return "Site [created=" + created + ", networkId=" + networkId + ", siteId=" + siteId + ", guid=" + guid + ", title=" + title + ", description=" + description + ", role=" + role + ", visibility=" + visibility + ", type=" + type + "]"; @@ -259,8 +275,22 @@ public class SiteImpl implements Serializable, Site, Comparable, Expec public JSONObject toJSON() { JSONObject siteJson = new JSONObject(); - siteJson.put("id", getSiteId()); - siteJson.put("guid", getGuid()); + if (getSiteId() != null) + { + siteJson.put("id", getSiteId()); + } + if (getGuid() != null) + { + siteJson.put("guid", getGuid()); + } + if (getTitle() != null) + { + siteJson.put("title", getTitle()); + } + if (getVisibility() != null) + { + siteJson.put("visibility", getVisibility()); + } return siteJson; }