Skip to content

Commit

Permalink
Added: empty content validation for featured items
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Jan 14, 2025
1 parent 5c61511 commit f4b6e0c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AbstractWriteDataverseFeaturedItemCommand(DataverseRequest request, Datav
}

protected void validateAndSetContent(DataverseFeaturedItem featuredItem, String content) throws InvalidCommandArgumentsException {
if (content == null) {
if (content == null || content.trim().isEmpty()) {
throw new InvalidCommandArgumentsException(
BundleUtil.getStringFromBundle("dataverse.create.featuredItem.error.contentShouldBeProvided"),
this
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/propertyFiles/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ dataverse.create.error.jsonparsetodataverse=Error parsing the POSTed json into a
dataverse.create.featuredItem.error.imageFileProcessing=Error processing featured item file: {0}
dataverse.create.featuredItem.error.fileSizeExceedsLimit=File exceeds the maximum size of {0}
dataverse.create.featuredItem.error.invalidFileType=Invalid image file type
dataverse.create.featuredItem.error.contentShouldBeProvided=Featured item 'content' property should be provided.
dataverse.create.featuredItem.error.contentShouldBeProvided=Featured item 'content' property should be provided and not empty.
dataverse.create.featuredItem.error.contentExceedsLengthLimit=Featured item content exceeds the maximum allowed length of {0} characters.
dataverse.update.featuredItems.error.missingInputParams=All input parameters (id, content, displayOrder, keepFile, fileNames) are required.
dataverse.update.featuredItems.error.inputListsSizeMismatch=All input lists (id, content, displayOrder, keepFile, fileNames) must have the same size.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import edu.harvard.iq.dataverse.util.BundleUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
Expand Down Expand Up @@ -122,16 +124,14 @@ void execute_invalidFileTypeProvided_throwsInvalidCommandArgumentsException() th
}

@Test
void execute_contentNotProvided_throwsInvalidCommandArgumentsException() {
testNewDataverseFeaturedItemDTO.setContent(null);
InputStream inputStreamMock = mock(InputStream.class);
testNewDataverseFeaturedItemDTO.setImageFileInputStream(inputStreamMock);
void execute_contentIsNull_throwsInvalidCommandArgumentsException() {
assertContentShouldBeProvidedInvalidCommandArgumentsException(null);
}

InvalidCommandArgumentsException exception = assertThrows(InvalidCommandArgumentsException.class, () -> sut.execute(contextStub));
assertEquals(
BundleUtil.getStringFromBundle("dataverse.create.featuredItem.error.contentShouldBeProvided"),
exception.getMessage()
);
@ParameterizedTest
@ValueSource(strings = {" ", ""})
void execute_contentIsEmpty_throwsInvalidCommandArgumentsException(String content) {
assertContentShouldBeProvidedInvalidCommandArgumentsException(content);
}

@Test
Expand All @@ -150,6 +150,18 @@ void execute_contentExceedsLimit_throwsInvalidCommandArgumentsException() {
);
}

private void assertContentShouldBeProvidedInvalidCommandArgumentsException(String content) {
testNewDataverseFeaturedItemDTO.setContent(content);
InputStream inputStreamMock = mock(InputStream.class);
testNewDataverseFeaturedItemDTO.setImageFileInputStream(inputStreamMock);

InvalidCommandArgumentsException exception = assertThrows(InvalidCommandArgumentsException.class, () -> sut.execute(contextStub));
assertEquals(
BundleUtil.getStringFromBundle("dataverse.create.featuredItem.error.contentShouldBeProvided"),
exception.getMessage()
);
}

private String createContentExceedingMaxLength() {
return "a".repeat(Math.max(0, DataverseFeaturedItem.MAX_FEATURED_ITEM_CONTENT_SIZE + 1));
}
Expand Down

0 comments on commit f4b6e0c

Please sign in to comment.