Refactoring of ImageService tests.

This commit is contained in:
Jesse Brault 2024-07-23 08:53:19 -05:00
parent 3e4db86457
commit d98101b8a4

View File

@ -61,16 +61,6 @@ public class S3ImageServiceTests {
} }
} }
private Image createHal9000(User owner, InputStream data) throws ImageException, IOException {
return this.imageService.create(
owner,
"HAL9000.svg",
data,
"image/svg+xml",
27881L
);
}
private Image createHal9000(User owner) throws ImageException, IOException { private Image createHal9000(User owner) throws ImageException, IOException {
try (final InputStream hal9000 = getHal9000()) { try (final InputStream hal9000 = getHal9000()) {
return this.imageService.create( return this.imageService.create(
@ -88,111 +78,83 @@ public class S3ImageServiceTests {
@Test @Test
@DirtiesContext @DirtiesContext
public void simpleCreate() { public void simpleCreate() throws ImageException, IOException {
try (final InputStream hal9000 = getHal9000()) { final User owner = this.createTestUser("imageOwner");
final User owner = this.createTestUser("imageOwner"); final Image image = this.createHal9000(owner);
final Image image = this.createHal9000(owner, hal9000); assertThat(image.getOwner(), isUser(owner));
assertThat(image.getOwner(), isUser(owner)); assertThat(image.getCreated(), is(notNullValue()));
assertThat(image.getCreated(), is(notNullValue())); assertThat(image.getModified(), is(nullValue()));
assertThat(image.getModified(), is(nullValue())); assertThat(image.getUserFilename(), is("HAL9000.svg"));
assertThat(image.getUserFilename(), is("HAL9000.svg")); assertThat(image.getMimeType(), is("image/svg+xml"));
assertThat(image.getMimeType(), is("image/svg+xml")); assertThat(image.getAlt(), is(nullValue()));
assertThat(image.getAlt(), is(nullValue())); assertThat(image.getCaption(), is(nullValue()));
assertThat(image.getCaption(), is(nullValue())); assertThat(image.getInternalUrl(), is(notNullValue()));
assertThat(image.getInternalUrl(), is(notNullValue())); assertThat(image.isPublic(), is(false));
assertThat(image.isPublic(), is(false)); assertThat(image.getViewers(), is(empty()));
assertThat(image.getViewers(), is(empty())); }
} catch (IOException | ImageException e) {
throw new RuntimeException(e); @Test
@DirtiesContext
public void loadImageWithOwner() throws ImageException, IOException {
final User owner = this.createTestUser("imageOwner");
final Image image = this.createHal9000(owner);
try (final InputStream stored = this.imageService.getImageContentById(image.getId(), owner)) {
final byte[] storedBytes = stored.readAllBytes();
assertThat(storedBytes.length, is(27881));
}
}
@Test
public void loadPublicImage() throws ImageException, IOException {
final User owner = this.createTestUser("imageOwner");
Image image = this.createHal9000(owner);
image = this.imageService.setPublic(image, owner, true);
try (final InputStream stored = this.imageService.getImageContentById(image.getId())) {
final byte[] storedBytes = stored.readAllBytes();
assertThat(storedBytes.length, is(27881));
} }
} }
@Test @Test
@DirtiesContext @DirtiesContext
public void loadImageWithOwner() { public void loadImageWithViewer() throws ImageException, IOException {
try (final InputStream hal9000 = getHal9000()) { final User owner = this.createTestUser("imageOwner");
final User owner = this.createTestUser("imageOwner"); final User viewer = this.createTestUser("imageViewer");
final Image image = this.createHal9000(owner, hal9000); Image image = this.createHal9000(owner);
try (final InputStream stored = this.imageService.getImageContentById(image.getId(), owner)) { image = this.imageService.addViewer(image, owner, viewer);
final byte[] storedBytes = stored.readAllBytes(); try (final InputStream stored = this.imageService.getImageContentById(image.getId(), viewer)) {
assertThat(storedBytes.length, is(27881)); final byte[] storedBytes = stored.readAllBytes();
} assertThat(storedBytes.length, is(27881));
} catch (IOException | ImageException e) {
throw new RuntimeException(e);
}
}
@Test
public void loadPublicImage() {
try (final InputStream hal9000 = getHal9000()) {
final User owner = this.createTestUser("imageOwner");
Image image = this.createHal9000(owner, hal9000);
image = this.imageService.setPublic(image, owner, true);
try (final InputStream stored = this.imageService.getImageContentById(image.getId())) {
final byte[] storedBytes = stored.readAllBytes();
assertThat(storedBytes.length, is(27881));
}
} catch (IOException | ImageException e) {
throw new RuntimeException(e);
} }
} }
@Test @Test
@DirtiesContext @DirtiesContext
public void loadImageWithViewer() { public void getImagesOwnedBy() throws ImageException, IOException {
try (final InputStream hal9000 = getHal9000()) { final User owner = this.createTestUser("imageOwner");
final User owner = this.createTestUser("imageOwner"); final User otherOwner = this.createTestUser("otherImageOwner");
final User viewer = this.createTestUser("imageViewer"); final Image image0 = this.createHal9000(owner);
Image image = this.createHal9000(owner, hal9000); final Image image1 = this.createHal9000(owner);
image = this.imageService.addViewer(image, owner, viewer); final Image image2 = this.createHal9000(otherOwner);
try (final InputStream stored = this.imageService.getImageContentById(image.getId(), viewer)) {
final byte[] storedBytes = stored.readAllBytes(); final List<Image> ownedImages = this.imageService.getImagesOwnedBy(owner);
assertThat(storedBytes.length, is(27881)); assertThat(ownedImages.size(), is(2));
} assertThat(ownedImages, containsImages(image0, image1));
} catch (IOException | ImageException e) {
throw new RuntimeException(e); final List<Image> otherOwnedImages = this.imageService.getImagesOwnedBy(otherOwner);
} assertThat(otherOwnedImages.size(), is(1));
assertThat(otherOwnedImages, containsImages(image2));
} }
@Test @Test
@DirtiesContext @DirtiesContext
public void getImagesOwnedBy() { public void updateOwner() throws ImageException, IOException {
try ( final User oldOwner = this.createTestUser("oldImageOwner");
final InputStream hal9000_0 = getHal9000(); final User newOwner = this.createTestUser("newImageOwner");
final InputStream hal9000_1 = getHal9000(); Image image = this.createHal9000(oldOwner);
final InputStream hal9000_2 = getHal9000(); assertThat(image.getOwner(), isUser(oldOwner));
) { image = this.imageService.updateOwner(image, oldOwner, newOwner);
final User owner = this.createTestUser("imageOwner"); assertThat(image.getOwner(), isUser(newOwner));
final User otherOwner = this.createTestUser("otherImageOwner");
final Image image0 = this.createHal9000(owner, hal9000_0);
final Image image1 = this.createHal9000(owner, hal9000_1);
final Image image2 = this.createHal9000(otherOwner, hal9000_2);
final List<Image> ownedImages = this.imageService.getImagesOwnedBy(owner);
assertThat(ownedImages.size(), is(2));
assertThat(ownedImages, containsImages(image0, image1));
final List<Image> otherOwnedImages = this.imageService.getImagesOwnedBy(otherOwner);
assertThat(otherOwnedImages.size(), is(1));
assertThat(otherOwnedImages, containsImages(image2));
} catch (IOException | ImageException e) {
throw new RuntimeException(e);
}
}
@Test
@DirtiesContext
public void updateOwner() {
try (final InputStream hal9000 = getHal9000()) {
final User oldOwner = this.createTestUser("oldImageOwner");
final User newOwner = this.createTestUser("newImageOwner");
Image image = this.createHal9000(oldOwner, hal9000);
assertThat(image.getOwner(), isUser(oldOwner));
image = this.imageService.updateOwner(image, oldOwner, newOwner);
assertThat(image.getOwner(), isUser(newOwner));
} catch (ImageException | IOException e) {
throw new RuntimeException(e);
}
} }
@Test @Test