Skip to content

Commit

Permalink
#24738 fixing sub folder dups + IT for rchive and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrizzio-dotCMS committed Jun 27, 2023
1 parent 94a0a71 commit 7f45de5
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,14 @@ public void Test_Parse_Language(){
}

/**
* Method to test : {@link WebAssetHelper#deleteAsset(AssetsRequestForm, User)}
* Method to test : {@link WebAssetHelper#archiveAsset(String, User)}
* Given Scenario: We submit a valid path to delete a file
* Expected Result: We get the asset content back as proof of success
* @throws DotDataException
* @throws DotSecurityException
*/
@Test
public void Test_Delete_File_Then_Delete_Folder() throws DotDataException, DotSecurityException {
public void Test_Archive_File() throws DotDataException, DotSecurityException {
final WebAssetHelper webAssetHelper = WebAssetHelper.newInstance();

Folder foo2 = new FolderDataGen().site(host).name("foo2").nextPersisted();
Expand All @@ -393,10 +393,9 @@ public void Test_Delete_File_Then_Delete_Folder() throws DotDataException, DotSe
String assetPath = String.format(ASSET_PATH_TEMPLATE, host.getHostname(), foo2.getName(),
bar2.getName(), testFile.getName());

final AssetsRequestForm deleteAssetForm = AssetsRequestForm.builder()
.assetPath(assetPath).language("en_US").live(false)
.build();
webAssetHelper.deleteAsset(deleteAssetForm, APILocator.systemUser());
webAssetHelper.archiveAsset(assetPath, APILocator.systemUser());

webAssetHelper.deleteAsset(assetPath, APILocator.systemUser());

final String folderPath = assetPath.replaceFirst(testFile.getName(), "");
webAssetHelper.deleteFolder(folderPath, APILocator.systemUser());
Expand All @@ -405,6 +404,34 @@ public void Test_Delete_File_Then_Delete_Folder() throws DotDataException, DotSe
}


/**
* Method to test : {@link WebAssetHelper#deleteAsset(String, User)}
* Given Scenario: We submit a valid path to delete a file
* Expected Result: We get the asset content back as proof of success
* @throws DotDataException
* @throws DotSecurityException
*/
@Test
public void Test_Delete_File_Then_Delete_Folder() throws DotDataException, DotSecurityException {
final WebAssetHelper webAssetHelper = WebAssetHelper.newInstance();

Folder foo3 = new FolderDataGen().site(host).name("foo3").nextPersisted();
Folder bar3 = new FolderDataGen().parent(foo3).name("bar3").nextPersisted();

new FileAssetDataGen(bar3, testFile).languageId(defaultLanguage.getId()).nextPersisted();

String assetPath = String.format(ASSET_PATH_TEMPLATE, host.getHostname(), foo3.getName(),
bar3.getName(), testFile.getName());


webAssetHelper.deleteAsset(assetPath, APILocator.systemUser());

final String folderPath = assetPath.replaceFirst(testFile.getName(), "");
webAssetHelper.deleteFolder(folderPath, APILocator.systemUser());

}


/**
* Method to test : {@link WebAssetHelper#getAssetInfo(String, User)}
* Given Scenario: First we create a file asset then we publish it making it be live and working at the same time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public WebAssetView getAssetInfo(final String path, final User user)
.build();
} else {
Logger.debug(this, String.format("Retrieving a folder by name: [%s] " , folder.getName()));
final Set<Treeable> folderContent = collectLiveAndWorkingContents(builder);
final List<Treeable> folderContent = browserAPI.getFolderContentList(builder.build());
//We're requesting a folder and all of its contents
final List<Folder> subFolders = folderContent.stream().filter(Folder.class::isInstance)
.map(f -> (Folder) f).collect(Collectors.toList());
Expand Down Expand Up @@ -542,22 +542,50 @@ Contentlet updateFileAsset(final File file, final Folder folder, final Language
}

/**
* Delete an asset
* @param form
* archive an asset
* @param assetPath
* @param user
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
public void deleteAsset(final AssetsRequestForm form, final User user)
public void archiveAsset(final String assetPath, final User user)
throws DotDataException, DotSecurityException {

final FileAsset fileAsset = getAsset(form, user);
if (!fileAsset.isArchived()) {
contentletAPI.archive(fileAsset, user, false);
final WebAssetView assetInfo = getAssetInfo(assetPath, user);
if(assetInfo instanceof AssetVersionsView){
final AssetVersionsView fileAssetView = (AssetVersionsView) assetInfo;
final String identifier = fileAssetView.versions().get(0).identifier();
final Contentlet fileAsset = contentletAPI.findContentletByIdentifierAnyLanguage(identifier) ;
if(!fileAsset.isArchived()){
contentletAPI.archive(fileAsset, user, false);
}
} else {
throw new IllegalArgumentException(String.format("The path [%s] can not be resolved as an asset", assetPath));
}
contentletAPI.delete(fileAsset, user, false);
}

/**
* Delete an asset
* @param assetPath
* @param user
* @return
* @throws DotDataException
* @throws DotSecurityException
*/
public void deleteAsset(final String assetPath, final User user)
throws DotDataException, DotSecurityException {
final WebAssetView assetInfo = getAssetInfo(assetPath, user);
if(assetInfo instanceof AssetVersionsView){
final AssetVersionsView fileAssetView = (AssetVersionsView) assetInfo;
final String identifier = fileAssetView.versions().get(0).identifier();
final Contentlet fileAsset = contentletAPI.findContentletByIdentifierAnyLanguage(identifier) ;
if(!fileAsset.isArchived()){
contentletAPI.archive(fileAsset, user, false);
}
contentletAPI.delete(fileAsset, user, false);
} else {
throw new IllegalArgumentException(String.format("The path [%s] can not be resolved as an asset", assetPath));
}
}

public void deleteFolder(final String path, final User user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public Response saveUpdateAsset(
}

/**
* Delete an asset by path language and version
* Delete an asset by path
* @param request
* @param response
* @param form
Expand All @@ -159,7 +159,42 @@ public Response saveUpdateAsset(
public Response deleteAsset(
@Context final HttpServletRequest request,
@Context final HttpServletResponse response,
AssetsRequestForm form
AssetInfoRequestForm form
) throws DotSecurityException, DotDataException, IOException {

final InitDataObject initDataObject = new WebResource.InitBuilder()
.requiredBackendUser(true)
.requiredFrontendUser(false)
.requestAndResponse(request, response)
.rejectWhenNoUser(true).init();

final User user = initDataObject.getUser();
helper.deleteAsset(form.assetPath(), user);
Logger.info(this,
String.format("User [%s] deleted asset for path [%s] ", user.getUserId(), form.assetPath()));
return Response.ok().build();
}


/**
* Delete an asset by path
* @param request
* @param response
* @param form
* @return
* @throws DotSecurityException
* @throws DotDataException
* @throws IOException
*/
@Path("/archive")
@DELETE
@JSONP
@NoCache
@Produces({MediaType.APPLICATION_JSON, "application/javascript"})
public Response archiveAsset(
@Context final HttpServletRequest request,
@Context final HttpServletResponse response,
AssetInfoRequestForm form
) throws DotSecurityException, DotDataException, IOException {

final InitDataObject initDataObject = new WebResource.InitBuilder()
Expand All @@ -169,11 +204,9 @@ public Response deleteAsset(
.rejectWhenNoUser(true).init();

final User user = initDataObject.getUser();
helper.deleteAsset(form, user);
helper.archiveAsset(form.assetPath(), user);
Logger.info(this,
String.format("User [%s] deleted asset for path [%s] with status live [%b] and lang [%s] ",
user.getUserId(), form.assetPath(), form.live(), form.language()
));
String.format("User [%s] deleted asset for path [%s] ", user.getUserId(), form.assetPath()));
return Response.ok().build();
}

Expand Down

0 comments on commit 7f45de5

Please sign in to comment.