|
26 | 26 | import org.junit.jupiter.params.provider.Arguments; |
27 | 27 | import org.junit.jupiter.params.provider.MethodSource; |
28 | 28 |
|
29 | | -import org.springframework.ai.google.genai.MimeTypeDetector; |
| 29 | +import org.junit.jupiter.params.provider.ValueSource; |
30 | 30 | import org.springframework.core.io.PathResource; |
31 | 31 | import org.springframework.util.MimeType; |
32 | 32 |
|
33 | 33 | import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; |
| 35 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
34 | 36 | import static org.springframework.ai.google.genai.MimeTypeDetector.GEMINI_MIME_TYPES; |
35 | 37 |
|
36 | 38 | /** |
@@ -90,4 +92,49 @@ void getMimeTypeByString(String extension, MimeType expectedMimeType) { |
90 | 92 | assertThat(mimeType).isEqualTo(expectedMimeType); |
91 | 93 | } |
92 | 94 |
|
| 95 | + @ParameterizedTest |
| 96 | + @ValueSource(strings = { " ", "\t", "\n" }) |
| 97 | + void getMimeTypeByStringWithInvalidInputShouldThrowException(String invalidPath) { |
| 98 | + assertThatThrownBy(() -> MimeTypeDetector.getMimeType(invalidPath)).isInstanceOf(IllegalArgumentException.class) |
| 99 | + .hasMessageContaining("Unable to detect the MIME type"); |
| 100 | + } |
| 101 | + |
| 102 | + @ParameterizedTest |
| 103 | + @ValueSource(strings = { "JPG", "PNG", "GIF" }) |
| 104 | + void getMimeTypeByStringWithUppercaseExtensionsShouldWork(String uppercaseExt) { |
| 105 | + String upperFileName = "test." + uppercaseExt; |
| 106 | + String lowerFileName = "test." + uppercaseExt.toLowerCase(); |
| 107 | + |
| 108 | + // Should throw for uppercase (not in map) but work for lowercase |
| 109 | + assertThatThrownBy(() -> MimeTypeDetector.getMimeType(upperFileName)) |
| 110 | + .isInstanceOf(IllegalArgumentException.class); |
| 111 | + |
| 112 | + // Lowercase should work if it's a supported extension |
| 113 | + if (GEMINI_MIME_TYPES.containsKey(uppercaseExt.toLowerCase())) { |
| 114 | + assertThatCode(() -> MimeTypeDetector.getMimeType(lowerFileName)).doesNotThrowAnyException(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @ParameterizedTest |
| 119 | + @ValueSource(strings = { "test.jpg", "test.png", "test.gif" }) |
| 120 | + void getMimeTypeSupportedFileAcrossDifferentMethodsShouldBeConsistent(String fileName) { |
| 121 | + MimeType stringResult = MimeTypeDetector.getMimeType(fileName); |
| 122 | + MimeType fileResult = MimeTypeDetector.getMimeType(new File(fileName)); |
| 123 | + MimeType pathResult = MimeTypeDetector.getMimeType(Path.of(fileName)); |
| 124 | + |
| 125 | + // All methods should return the same result for supported extensions |
| 126 | + assertThat(stringResult).isEqualTo(fileResult); |
| 127 | + assertThat(stringResult).isEqualTo(pathResult); |
| 128 | + } |
| 129 | + |
| 130 | + @ParameterizedTest |
| 131 | + @ValueSource(strings = { "https://example.com/documents/file.pdf", "https://example.com/data/file.json", |
| 132 | + "https://example.com/files/document.txt" }) |
| 133 | + void getMimeTypeByURIWithUnsupportedExtensionsShouldThrowException(String url) { |
| 134 | + URI uri = URI.create(url); |
| 135 | + |
| 136 | + assertThatThrownBy(() -> MimeTypeDetector.getMimeType(uri)).isInstanceOf(IllegalArgumentException.class) |
| 137 | + .hasMessageContaining("Unable to detect the MIME type"); |
| 138 | + } |
| 139 | + |
93 | 140 | } |
0 commit comments