Add tests for supported image types.

This commit is contained in:
Jesse Brault 2026-02-19 17:43:39 -06:00
parent 537677abf7
commit 1d98d226a7
4 changed files with 64 additions and 0 deletions

View File

@ -7,14 +7,19 @@ import app.mealsmadeeasy.api.user.User;
import app.mealsmadeeasy.api.user.UserCreateException; import app.mealsmadeeasy.api.user.UserCreateException;
import app.mealsmadeeasy.api.user.UserService; import app.mealsmadeeasy.api.user.UserService;
import app.mealsmadeeasy.api.util.NoSuchEntityWithIdException; import app.mealsmadeeasy.api.util.NoSuchEntityWithIdException;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -37,6 +42,37 @@ public class S3ImageServiceTests {
return S3ImageServiceTests.class.getResourceAsStream("HAL9000.svg"); return S3ImageServiceTests.class.getResourceAsStream("HAL9000.svg");
} }
private record ImageResource(InputStream inputStream, long size) {}
private static ImageResource getAsResource(@Nullable InputStream inputStream) {
if (inputStream == null) {
throw new RuntimeException("Input stream is null");
}
try {
final byte[] bytes = inputStream.readAllBytes();
final long size = bytes.length;
return new ImageResource(new ByteArrayInputStream(bytes), size);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static ImageResource getWebpResource() {
return getAsResource(S3ImageServiceTests.class.getResourceAsStream("/KingArthurSourdough.webp"));
}
private static ImageResource getJpgResource() {
return getAsResource(S3ImageServiceTests.class.getResourceAsStream("/Obazda.jpg"));
}
private static ImageResource getPngResource() {
return getAsResource(S3ImageServiceTests.class.getResourceAsStream("/veganfood.png"));
}
private static ImageResource getSvgResource() {
return getAsResource(getHal9000InputStream());
}
@Autowired @Autowired
private UserService userService; private UserService userService;
@ -246,4 +282,32 @@ public class S3ImageServiceTests {
assertThrows(NoSuchEntityWithIdException.class, () -> this.imageService.getById(image.getId(), owner)); assertThrows(NoSuchEntityWithIdException.class, () -> this.imageService.getById(image.getId(), owner));
} }
@TestFactory
public List<DynamicTest> supportedImageTypesTests() {
return Map.of(
"jpeg", getJpgResource(),
"png", getPngResource(),
"webp", getWebpResource(),
"svg", getSvgResource()
)
.entrySet()
.stream()
.map(entry -> DynamicTest.dynamicTest(
String.format("%s_supported", entry.getKey()),
() -> {
final User owner = this.seedUser();
final Image image = this.imageService.create(
owner,
UUID.randomUUID() + "." + entry.getKey(),
entry.getValue().inputStream(),
entry.getValue().size(),
ImageCreateSpec.builder().build()
);
assertThat(image, is(notNullValue()));
}
)
)
.toList();
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB