|
| 1 | +package org.springframework.ai.vertexai.gemini; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URI; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.util.stream.Stream; |
| 8 | + |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.Arguments; |
| 11 | +import org.junit.jupiter.params.provider.MethodSource; |
| 12 | + |
| 13 | +import org.springframework.core.io.PathResource; |
| 14 | +import org.springframework.util.MimeType; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.springframework.ai.vertexai.gemini.MimeTypeDetector.GEMINI_MIME_TYPES; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author YunKui Lu |
| 21 | + */ |
| 22 | +class MimeTypeDetectorTests { |
| 23 | + |
| 24 | + private static Stream<Arguments> provideMimeTypes() { |
| 25 | + return GEMINI_MIME_TYPES.entrySet().stream().map(entry -> Arguments.of(entry.getKey(), entry.getValue())); |
| 26 | + } |
| 27 | + |
| 28 | + @ParameterizedTest |
| 29 | + @MethodSource("provideMimeTypes") |
| 30 | + void getMimeTypeByURLPath(String extension, MimeType expectedMimeType) throws MalformedURLException { |
| 31 | + String path = "https://testhost/test." + extension; |
| 32 | + MimeType mimeType = MimeTypeDetector.getMimeType(URI.create(path).toURL()); |
| 33 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 34 | + } |
| 35 | + |
| 36 | + @ParameterizedTest |
| 37 | + @MethodSource("provideMimeTypes") |
| 38 | + void getMimeTypeByURI(String extension, MimeType expectedMimeType) { |
| 39 | + String path = "https://testhost/test." + extension; |
| 40 | + MimeType mimeType = MimeTypeDetector.getMimeType(URI.create(path)); |
| 41 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 42 | + } |
| 43 | + |
| 44 | + @ParameterizedTest |
| 45 | + @MethodSource("provideMimeTypes") |
| 46 | + void getMimeTypeByFile(String extension, MimeType expectedMimeType) { |
| 47 | + String path = "test." + extension; |
| 48 | + MimeType mimeType = MimeTypeDetector.getMimeType(new File(path)); |
| 49 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 50 | + } |
| 51 | + |
| 52 | + @ParameterizedTest |
| 53 | + @MethodSource("provideMimeTypes") |
| 54 | + void getMimeTypeByPath(String extension, MimeType expectedMimeType) { |
| 55 | + String path = "test." + extension; |
| 56 | + MimeType mimeType = MimeTypeDetector.getMimeType(Path.of(path)); |
| 57 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @MethodSource("provideMimeTypes") |
| 62 | + void getMimeTypeByResource(String extension, MimeType expectedMimeType) { |
| 63 | + String path = "test." + extension; |
| 64 | + MimeType mimeType = MimeTypeDetector.getMimeType(new PathResource(path)); |
| 65 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("provideMimeTypes") |
| 70 | + void getMimeTypeByString(String extension, MimeType expectedMimeType) { |
| 71 | + String path = "test." + extension; |
| 72 | + MimeType mimeType = MimeTypeDetector.getMimeType(path); |
| 73 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments