Skip to content

Commit

Permalink
#25666 limit of the sublist should be limit+offset (#25685)
Browse files Browse the repository at this point in the history
* #25666 limit of the sublist should be limit+offset

* #25666 sum offset and limit, new test
  • Loading branch information
erickgonzalez committed Aug 7, 2023
1 parent a07a969 commit b1b1271
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,86 @@ public void testPullRelatedFieldShouldRespectDefaultOrder()
}
}


/**
* Method to test: {@link ContentUtils#pullRelatedField(Relationship, String, String, int, int, String, User, String, boolean, long, Boolean)}
* Given Scenario: Pulling related content when the offset is greater than the limit
* ExpectedResult: Should return results, if they're available.
*
* In this test 5 items are related, and we're passing offset = 3 and limit = 1, so 1 item should be returned.
* @throws DotDataException
* @throws DotSecurityException
*/
@Test
public void testPullRelatedField_withOffsetGreaterThanLimit()
throws DotDataException, DotSecurityException {

final long languageId = languageAPI.getDefaultLanguage().getId();
final ContentType news = getNewsLikeContentType("News");
final ContentType comments = getCommentsLikeContentType("Comments");
relateContentTypes(news, comments);

final ContentType newsContentType = contentTypeAPI.find("News");
final ContentType commentsContentType = contentTypeAPI.find("Comments");

Contentlet newsContentlet = null;
Contentlet commentsContentlet;
final List<Contentlet> relatedComments = new ArrayList<>();

try {
//creates parent contentlet
ContentletDataGen dataGen = new ContentletDataGen(newsContentType.id());

//English version
newsContentlet = dataGen.languageId(languageId)
.setProperty("title", "News Test")
.setProperty("urlTitle", "news-test").setProperty("byline", "news-test")
.setProperty("sysPublishDate", new Date()).setProperty("story", "news-test")
.next();

//creates child contentlet
dataGen = new ContentletDataGen(commentsContentType.id());

for (int i=1; i<5 ;i++){
commentsContentlet = dataGen
.languageId(languageId)
.setProperty("title", "Comment for News " + i)
.setProperty("email", "testing@dotcms.com")
.setProperty("comment", "Comment for News " + i)
.setPolicy(IndexPolicy.FORCE).nextPersisted();
relatedComments.add(commentsContentlet);

}

//Saving relationship
final Relationship relationship = relationshipAPI.byTypeValue("News-Comments");

newsContentlet.setIndexPolicy(IndexPolicy.FORCE);

newsContentlet = contentletAPI.checkin(newsContentlet,
map(relationship, relatedComments),
null, user, false);

//Pull related content from comment child
List<Contentlet> result = ContentUtils.
pullRelatedField(relationship, newsContentlet.getIdentifier(), "+languageId:1",
1, 3, "", user, null, false, languageId, false);

assertNotNull(result);
assertEquals(1,result.size());

} finally {
if (null != newsContentlet && UtilMethods.isSet(newsContentlet.getInode())) {
ContentletDataGen.remove(newsContentlet);
}

for (final Contentlet contentlet: relatedComments){
ContentletDataGen.remove(contentlet);
}
}
}


/**
* Method to test: {@link ContentUtils#addRelationships(Contentlet, User, PageMode, long, int, HttpServletRequest, HttpServletResponse)}
* Given Scenario: Creates a content parent with a children many to many relationship, create a few instances of the child type and related to the parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public static List<Contentlet> pullRelated(String relationshipName, String conte
* Logic used for `pullRelated` and `pullRelatedField` methods
*/
public static List<Contentlet> getPullResults(final Relationship relationship,
String contentletIdentifier, final String condition, final int limit, final int offset,
String contentletIdentifier, final String condition, final int limit, int offset,
String sort, final User user, final String tmDate, final boolean pullParents,
final long language, final Boolean live) {

Expand Down Expand Up @@ -642,8 +642,9 @@ public static List<Contentlet> getPullResults(final Relationship relationship,

final List<Contentlet> filteredList = relatedContent.stream().filter(c->results.contains(c.getIdentifier()))
.collect(Collectors.toList());

return filteredList.subList(offset>=0?offset:0, (limit > 0 && limit <= filteredList.size())? limit: filteredList.size());

offset = offset >=0 ? offset : 0;
return filteredList.subList(offset, (limit <= 0 || limit >= filteredList.size() ) ? filteredList.size() : offset+limit);
}

//pulling parents
Expand Down

0 comments on commit b1b1271

Please sign in to comment.