Skip to content

Commit

Permalink
fix(FlakyTest) Tags Test fails sporadically in our merge Queue Refs: #…
Browse files Browse the repository at this point in the history
…29861 (#29911)

### Proposed Changes
* Folder clean-up operation was breaking tests sporadically
* a few improvements on the Test-itself 
* Changes to make Tag's cache return an immutable list
  • Loading branch information
fabrizzio-dotCMS authored Sep 6, 2024
1 parent c2a4a83 commit 90d78fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ public void run() {
new TrashUtils().moveFileToTrash(folder, "h22");
}
} catch (IOException e) {
throw new DotStateException("unable to delete folder:" + folder, e);
//Prevent Flaky tests from failing
Logger.warn(this.getClass(), "unable to delete folder:" + folder, e);
}

}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public List<Tag> getFilteredTags(String tagName, String hostFilter, boolean glob
} catch ( Exception e ) {
Logger.warn(Tag.class, "getFilteredTags failed: " + e, e);
}
return new java.util.ArrayList<>();
return List.of();
}

@Override
Expand Down Expand Up @@ -493,7 +493,7 @@ public List<TagInode> getTagInodesByTagId ( String tagId ) throws DotDataExcepti
tagInodeCache.putForTagId(tagId, tagInodes);
}

return tagInodes;
return List.copyOf(tagInodes);
}

@Override
Expand Down Expand Up @@ -636,7 +636,7 @@ public List<Tag> getTagsByInode ( String inode ) throws DotDataException {
tagCache.putForInode(inode, tags);
}

return tags;
return List.copyOf(tags);
}

@Override
Expand All @@ -658,7 +658,7 @@ public List<Tag> getTagsByInodeAndFieldVarName(String inode, String fieldVarName
}
}

return tags;
return List.copyOf(tags);
}

@Override
Expand Down Expand Up @@ -691,7 +691,7 @@ public Set<String> getTopTagsBySiteId(final String siteId) throws DotDataExcepti
.addParam(siteId)
.loadObjectResults();

return results.stream().map(row -> row.get(TAG_COLUMN_TAGNAME).toString()).collect(Collectors.toSet());
return results.stream().map(row -> row.get(TAG_COLUMN_TAGNAME).toString()).collect(Collectors.toUnmodifiableSet());
}

/**
Expand Down Expand Up @@ -724,7 +724,7 @@ private List<Tag> convertForTagsFilteringDuplicated(List<Map<String, Object>> sq
}
}

return new ArrayList<>(tagsMap.values());
return List.copyOf(tagsMap.values());
}

/**
Expand All @@ -744,7 +744,7 @@ private List<Tag> convertForTags ( List<Map<String, Object>> sqlResults ) {
}
}

return tags;
return List.copyOf(tags);
}

/**
Expand All @@ -764,7 +764,7 @@ private List<TagInode> convertForTagInodes ( List<Map<String, Object>> sqlResult
}
}

return tagInodes;
return List.copyOf(tagInodes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ public void getTagsForUserByUserId() throws Exception{

List<Tag> tags = tagAPI.getTagsForUserByUserId(testUser.getUserId());
assertNotNull(tags);
assertTrue(tags.size() > 0);
assertFalse(tags.isEmpty());

String tagName = "testapi"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Tag createdTag = tagAPI.saveTag(tagName, testUser.getUserId(), defaultHostId, false);
tagAPI.addUserTagInode(createdTag, testUser.getUserId());

tags = tagAPI.getTagsForUserByUserId(testUser.getUserId());
assertNotNull(tags);
assertTrue(tags.size() > 0);
assertFalse(tags.isEmpty());
assertTrue(tags.contains(createdTag));
}

Expand Down Expand Up @@ -189,7 +189,7 @@ public void getFilteredTags() throws Exception{
List<Tag> tags = tagAPI.getFilteredTags (tagName, defaultHost.getIdentifier(), true, "tagname", 0, 10 );
assertTrue(tags.size() > 1);
for(Tag tag : tags){
assertTrue(tag.getTagName().indexOf(tagName) != -1);
assertTrue(tag.getTagName().contains(tagName));
}
}

Expand All @@ -203,7 +203,7 @@ public void getTagAndCreate() throws Exception{
Tag newTag = tagAPI.getTagAndCreate ( tagName, testUser.getUserId(), defaultHostId );
Tag tag = tagAPI.getTagByNameAndHost(tagName, defaultHostId);
assertNotNull(tag);
assertTrue(tag.equals(newTag));
assertEquals(tag, newTag);
}

/**
Expand All @@ -219,7 +219,7 @@ public void getTagByTagId() throws Exception{
Tag tag = tagAPI.getTagByTagId(tagId);

assertNotNull(tag);
assertTrue(tag.getTagId().equals(tagId));
assertEquals(tag.getTagId(), tagId);
}

/**
Expand Down Expand Up @@ -267,7 +267,7 @@ public void addTag() throws Exception {

Tag tag = tagAPI.getTagByNameAndHost(tagName, hostAPI.findSystemHost().getIdentifier());
assertNotNull(tag);
assertTrue(tag.getTagName().equals(tagName));
assertEquals(tag.getTagName(), tagName);
}

/**
Expand All @@ -285,16 +285,16 @@ public void updateTag () throws Exception{
tagAPI.updateTag(newTag.getTagId(), tagName2 );

Tag tag = tagAPI.getTagByTagId(newTag.getTagId());
assertTrue(tag.getTagName().equals(tagName2));
assertEquals(tag.getTagName(), tagName2);

//testing update tag second implementation ( String tagId, String tagName, boolean updateTagReference, String hostId )
String tagName3 ="testapi9"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Host host = hostAPI.findSystemHost();
tagAPI.updateTag(newTag.getTagId(), tagName3, true, host.getIdentifier() );

tag = tagAPI.getTagByTagId(newTag.getTagId());
assertTrue(tag.getTagName().equals(tagName3));
assertTrue(tag.getHostId().equals(host.getIdentifier()));
assertEquals(tag.getTagName(), tagName3);
assertEquals(tag.getHostId(), host.getIdentifier());
}

/**
Expand Down Expand Up @@ -346,7 +346,7 @@ public void addTagInode() throws Exception {

Contentlet contentAsset=new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name="testtagapi"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand Down Expand Up @@ -386,7 +386,7 @@ public void getTagInodesByInode() throws Exception{

Contentlet contentAsset=new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name="testtagapi15"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand Down Expand Up @@ -425,7 +425,7 @@ public void getTagInodesByInode() throws Exception{
public void getTagInodesByTagId() throws Exception{
Contentlet contentAsset=new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name="testtagapi16"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand Down Expand Up @@ -465,7 +465,7 @@ public void getTagInode() throws Exception {

Contentlet contentAsset=new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name="testtagapi17"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand All @@ -487,8 +487,8 @@ public void getTagInode() throws Exception {
assertNotNull(tagInode2);
assertTrue(UtilMethods.isSet(tagInode.getTagId()));
assertTrue(UtilMethods.isSet(tagInode2.getTagId()));
assertTrue(tagInode.getTagId().equals(tagInode2.getTagId()));
assertTrue(tagInode.getInode().equals(tagInode2.getInode()));
assertEquals(tagInode.getTagId(), tagInode2.getTagId());
assertEquals(tagInode.getInode(), tagInode2.getInode());

conAPI.destroy(contentAsset, testUser, false);
}
Expand All @@ -501,7 +501,7 @@ public void getTagInode() throws Exception {
public void deleteTagInode() throws Exception {
Contentlet contentAsset=new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name="testtagapi18"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand Down Expand Up @@ -558,7 +558,7 @@ public void removeTagRelationAndTagWhenPossible() throws Exception {

Contentlet contentAsset=new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name="testtagapi21"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand Down Expand Up @@ -599,14 +599,14 @@ public void getSuggestedTag() throws Exception{
int tagSize = tags.size();
assertTrue(tagSize > 1);
for(Tag tag : tags){
assertTrue(tag.getTagName().indexOf(tagName) != -1);
assertTrue(tag.getTagName().contains(tagName));
}

tagName="testing";
tags = tagAPI.getSuggestedTag (tagName, defaultHostId);
assertTrue(tags.size() >= 1);
assertFalse(tags.isEmpty());
for(Tag tag : tags){
assertTrue(tag.getTagName().indexOf(tagName) != -1);
assertTrue(tag.getTagName().contains(tagName));
}

assertTrue(tags.size() < tagSize);
Expand All @@ -624,8 +624,8 @@ public void updateTagReferences() throws Exception{
//Creates a new Host
Contentlet host = new Contentlet();
Structure st = structureAPI.findByVarName("Host", systemUser);
host.setStructureInode(st.getInode());
String hostName = "testtagapiHost_" + System.currentTimeMillis();
host.setContentTypeId(st.getInode());
String hostName = "testtagapiHost" + System.currentTimeMillis() + ".com";
host.setProperty(Host.HOST_NAME_KEY, hostName);
host.setLanguageId(langAPI.getDefaultLanguage().getId());
host.setIndexPolicy(IndexPolicy.FORCE);
Expand All @@ -645,15 +645,15 @@ public void updateTagReferences() throws Exception{

//Gets the default Host Tags
List<Tag> tags = tagAPI.getTagsByHostId(defaultHostId);
int defaultHostInitialNumberOfTags = tags.size();
assertNotNull(tags);
int defaultHostInitialNumberOfTags = tags.size();
assertTrue(defaultHostInitialNumberOfTags > 0);

//Gets the new Host Tags
tags = tagAPI.getTagsByHostId(newHost.getIdentifier());
int newHostinitialNumberOfTags = tags.size();
assertNotNull(tags);
assertTrue(newHostinitialNumberOfTags == 0);
int newHostinitialNumberOfTags = tags.size();
assertEquals(0, newHostinitialNumberOfTags);

//Move the Tags to the new Host
tagAPI.updateTagReferences(defaultHostId, defaultHostId, newHost.getIdentifier());
Expand All @@ -673,21 +673,21 @@ public void updateTagReferences() throws Exception{
tagCache.clearCache();

defaultHostTagsAfterUpdate = tagAPI.getTagsByHostId(defaultHostId);
assertTrue(defaultHostTagsAfterUpdate.size() == defaultHostInitialNumberOfTags);
assertEquals(defaultHostTagsAfterUpdate.size(), defaultHostInitialNumberOfTags);

/*here the amount is not 0 because is entering in the condition
* if((hostIdentifier.equals(newTagStorageId) && hostTagList.size() == 0) && !newTagStorageId.equals(Host.SYSTEM_HOST)) {
* saveTag(tag.getTagName(), "", hostIdentifier);
*/
newHostTagsAfterUpdate = tagAPI.getTagsByHostId(newHost.getIdentifier());
assertTrue(newHostTagsAfterUpdate.size() == defaultHostInitialNumberOfTags);
assertEquals(newHostTagsAfterUpdate.size(), defaultHostInitialNumberOfTags);
} finally {

if (null != newHost) {
//delete host
hostAPI.archive(newHost,systemUser,false);
hostAPI.delete(newHost,systemUser,false);
assertTrue(tagAPI.getTagsByHostId(newHost.getIdentifier()).size()==0);
assertEquals(0, tagAPI.getTagsByHostId(newHost.getIdentifier()).size());
}

}
Expand All @@ -713,7 +713,7 @@ public void testGetTagsByHostId_returnAllTagsOfAHost() throws Exception{
public void getTagsByInode() throws Exception{
Contentlet contentAsset=new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name="testtagapi22"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand Down Expand Up @@ -751,7 +751,7 @@ public void getTagsInText() throws Exception{
String text="test1, test 2, test number three,another\n testing\t to'do\r now";
List<Tag> tags = tagAPI.getTagsInText (text, testUser.getUserId(), defaultHostId);
assertNotNull(tags);
assertTrue(tags.size()==7);
assertEquals(7, tags.size());
}

/**
Expand All @@ -762,7 +762,7 @@ public void getTagsInText() throws Exception{
public void validatePersonaTags() throws Exception {

Contentlet persona = new Contentlet();
persona.setStructureInode(PersonaAPI.DEFAULT_PERSONAS_STRUCTURE_INODE);
persona.setContentTypeId(PersonaAPI.DEFAULT_PERSONAS_STRUCTURE_INODE);
persona.setHost(defaultHostId);
persona.setLanguageId(langAPI.getDefaultLanguage().getId());
String name="testtagapipersona1"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand Down Expand Up @@ -885,7 +885,7 @@ public void validateTagCache() throws Exception {
*/
Contentlet contentAsset=new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name="testtagapi27"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
Expand All @@ -898,7 +898,7 @@ public void validateTagCache() throws Exception {
APILocator.getContentletAPI().publish(contentAsset, testUser, false);

tagName ="testapi27"+UtilMethods.dateToHTMLDate(new Date(),"MMddyyyyHHmmss");
tag = tagAPI.saveTag(tagName, testUser.getUserId(), defaultHostId);
tagAPI.saveTag(tagName, testUser.getUserId(), defaultHostId);
tagAPI.addContentletTagInode(tagName, contentAsset.getInode(), defaultHostId, WIKI_TAG_VARNAME);

//Verify the cache -> THE SAVE SHOULD ADD NOTHING TO CACHE, JUST THE LOAD
Expand Down Expand Up @@ -991,12 +991,12 @@ public void findTopTags_should_be_not_null_not_empty_and_contains_one_popular_ta
final String tagvalue1 = "mytesttag1";

final Tag tag1 = Try.of(()->APILocator.getTagAPI().saveTag(tagvalue1, testUser.getUserId(), defaultHostId)).getOrNull();
IntStream.range(0, 100).forEach($ -> {
IntStream.range(0, 100).forEach(r -> {

try {
final Contentlet contentAsset = new Contentlet();
ContentType contentType = TestDataUtils.getWikiLikeContentType();
contentAsset.setStructureInode(contentType.id());
contentAsset.setContentTypeId(contentType.id());
contentAsset.setHost(defaultHostId);
contentAsset.setProperty(WIKI_SYSPUBLISHDATE_VARNAME, new Date());
String name = "testtagapi" + UtilMethods.dateToHTMLDate(new Date(), "MMddyyyyHHmmss");
Expand Down

0 comments on commit 90d78fc

Please sign in to comment.