|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.vertexai.gemini; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.net.MalformedURLException; |
| 21 | +import java.net.URI; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | + |
| 29 | +import org.springframework.core.io.PathResource; |
| 30 | +import org.springframework.util.MimeType; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.springframework.ai.vertexai.gemini.MimeTypeDetector.GEMINI_MIME_TYPES; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author YunKui Lu |
| 37 | + */ |
| 38 | +class MimeTypeDetectorTests { |
| 39 | + |
| 40 | + private static Stream<Arguments> provideMimeTypes() { |
| 41 | + return GEMINI_MIME_TYPES.entrySet().stream().map(entry -> Arguments.of(entry.getKey(), entry.getValue())); |
| 42 | + } |
| 43 | + |
| 44 | + @ParameterizedTest |
| 45 | + @MethodSource("provideMimeTypes") |
| 46 | + void getMimeTypeByURLPath(String extension, MimeType expectedMimeType) throws MalformedURLException { |
| 47 | + String path = "https://testhost/test." + extension; |
| 48 | + MimeType mimeType = MimeTypeDetector.getMimeType(URI.create(path).toURL()); |
| 49 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 50 | + } |
| 51 | + |
| 52 | + @ParameterizedTest |
| 53 | + @MethodSource("provideMimeTypes") |
| 54 | + void getMimeTypeByURI(String extension, MimeType expectedMimeType) { |
| 55 | + String path = "https://testhost/test." + extension; |
| 56 | + MimeType mimeType = MimeTypeDetector.getMimeType(URI.create(path)); |
| 57 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @MethodSource("provideMimeTypes") |
| 62 | + void getMimeTypeByFile(String extension, MimeType expectedMimeType) { |
| 63 | + String path = "test." + extension; |
| 64 | + MimeType mimeType = MimeTypeDetector.getMimeType(new File(path)); |
| 65 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("provideMimeTypes") |
| 70 | + void getMimeTypeByPath(String extension, MimeType expectedMimeType) { |
| 71 | + String path = "test." + extension; |
| 72 | + MimeType mimeType = MimeTypeDetector.getMimeType(Path.of(path)); |
| 73 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 74 | + } |
| 75 | + |
| 76 | + @ParameterizedTest |
| 77 | + @MethodSource("provideMimeTypes") |
| 78 | + void getMimeTypeByResource(String extension, MimeType expectedMimeType) { |
| 79 | + String path = "test." + extension; |
| 80 | + MimeType mimeType = MimeTypeDetector.getMimeType(new PathResource(path)); |
| 81 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 82 | + } |
| 83 | + |
| 84 | + @ParameterizedTest |
| 85 | + @MethodSource("provideMimeTypes") |
| 86 | + void getMimeTypeByString(String extension, MimeType expectedMimeType) { |
| 87 | + String path = "test." + extension; |
| 88 | + MimeType mimeType = MimeTypeDetector.getMimeType(path); |
| 89 | + assertThat(mimeType).isEqualTo(expectedMimeType); |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments