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,10 +78,9 @@ 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, hal9000); final Image image = this.createHal9000(owner);
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()));
@ -102,71 +91,51 @@ public class S3ImageServiceTests {
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 @Test
@DirtiesContext @DirtiesContext
public void loadImageWithOwner() { public void loadImageWithOwner() 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, hal9000); final Image image = this.createHal9000(owner);
try (final InputStream stored = this.imageService.getImageContentById(image.getId(), owner)) { try (final InputStream stored = this.imageService.getImageContentById(image.getId(), owner)) {
final byte[] storedBytes = stored.readAllBytes(); final byte[] storedBytes = stored.readAllBytes();
assertThat(storedBytes.length, is(27881)); assertThat(storedBytes.length, is(27881));
} }
} catch (IOException | ImageException e) {
throw new RuntimeException(e);
}
} }
@Test @Test
public void loadPublicImage() { public void loadPublicImage() throws ImageException, IOException {
try (final InputStream hal9000 = getHal9000()) {
final User owner = this.createTestUser("imageOwner"); final User owner = this.createTestUser("imageOwner");
Image image = this.createHal9000(owner, hal9000); Image image = this.createHal9000(owner);
image = this.imageService.setPublic(image, owner, true); image = this.imageService.setPublic(image, owner, true);
try (final InputStream stored = this.imageService.getImageContentById(image.getId())) { try (final InputStream stored = this.imageService.getImageContentById(image.getId())) {
final byte[] storedBytes = stored.readAllBytes(); final byte[] storedBytes = stored.readAllBytes();
assertThat(storedBytes.length, is(27881)); assertThat(storedBytes.length, is(27881));
} }
} catch (IOException | ImageException e) {
throw new RuntimeException(e);
}
} }
@Test @Test
@DirtiesContext @DirtiesContext
public void loadImageWithViewer() { 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 User viewer = this.createTestUser("imageViewer");
Image image = this.createHal9000(owner, hal9000); Image image = this.createHal9000(owner);
image = this.imageService.addViewer(image, owner, viewer); image = this.imageService.addViewer(image, owner, viewer);
try (final InputStream stored = this.imageService.getImageContentById(image.getId(), viewer)) { try (final InputStream stored = this.imageService.getImageContentById(image.getId(), viewer)) {
final byte[] storedBytes = stored.readAllBytes(); final byte[] storedBytes = stored.readAllBytes();
assertThat(storedBytes.length, is(27881)); assertThat(storedBytes.length, is(27881));
} }
} catch (IOException | ImageException e) {
throw new RuntimeException(e);
}
} }
@Test @Test
@DirtiesContext @DirtiesContext
public void getImagesOwnedBy() { public void getImagesOwnedBy() throws ImageException, IOException {
try (
final InputStream hal9000_0 = getHal9000();
final InputStream hal9000_1 = getHal9000();
final InputStream hal9000_2 = getHal9000();
) {
final User owner = this.createTestUser("imageOwner"); final User owner = this.createTestUser("imageOwner");
final User otherOwner = this.createTestUser("otherImageOwner"); final User otherOwner = this.createTestUser("otherImageOwner");
final Image image0 = this.createHal9000(owner, hal9000_0); final Image image0 = this.createHal9000(owner);
final Image image1 = this.createHal9000(owner, hal9000_1); final Image image1 = this.createHal9000(owner);
final Image image2 = this.createHal9000(otherOwner, hal9000_2); final Image image2 = this.createHal9000(otherOwner);
final List<Image> ownedImages = this.imageService.getImagesOwnedBy(owner); final List<Image> ownedImages = this.imageService.getImagesOwnedBy(owner);
assertThat(ownedImages.size(), is(2)); assertThat(ownedImages.size(), is(2));
@ -175,24 +144,17 @@ public class S3ImageServiceTests {
final List<Image> otherOwnedImages = this.imageService.getImagesOwnedBy(otherOwner); final List<Image> otherOwnedImages = this.imageService.getImagesOwnedBy(otherOwner);
assertThat(otherOwnedImages.size(), is(1)); assertThat(otherOwnedImages.size(), is(1));
assertThat(otherOwnedImages, containsImages(image2)); assertThat(otherOwnedImages, containsImages(image2));
} catch (IOException | ImageException e) {
throw new RuntimeException(e);
}
} }
@Test @Test
@DirtiesContext @DirtiesContext
public void updateOwner() { public void updateOwner() throws ImageException, IOException {
try (final InputStream hal9000 = getHal9000()) {
final User oldOwner = this.createTestUser("oldImageOwner"); final User oldOwner = this.createTestUser("oldImageOwner");
final User newOwner = this.createTestUser("newImageOwner"); final User newOwner = this.createTestUser("newImageOwner");
Image image = this.createHal9000(oldOwner, hal9000); Image image = this.createHal9000(oldOwner);
assertThat(image.getOwner(), isUser(oldOwner)); assertThat(image.getOwner(), isUser(oldOwner));
image = this.imageService.updateOwner(image, oldOwner, newOwner); image = this.imageService.updateOwner(image, oldOwner, newOwner);
assertThat(image.getOwner(), isUser(newOwner)); assertThat(image.getOwner(), isUser(newOwner));
} catch (ImageException | IOException e) {
throw new RuntimeException(e);
}
} }
@Test @Test