|
| 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.model.mistralai.autoconfigure; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.ai.mistralai.ocr.MistralOcrApi; |
| 22 | +import org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfiguration; |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 24 | +import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration; |
| 25 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * Unit Tests for {@link MistralAiOcrProperties} interacting with |
| 31 | + * {@link MistralAiCommonProperties}. |
| 32 | + * |
| 33 | + * @author Alexandros Pappas |
| 34 | + * @since 1.0.0 |
| 35 | + */ |
| 36 | +class MistralAiOcrPropertiesTests { |
| 37 | + |
| 38 | + // Define common configurations to load in tests |
| 39 | + private final AutoConfigurations autoConfigurations = AutoConfigurations.of(SpringAiRetryAutoConfiguration.class, |
| 40 | + RestClientAutoConfiguration.class, MistralAiOcrAutoConfiguration.class); |
| 41 | + |
| 42 | + @Test |
| 43 | + void commonPropertiesAppliedToOcr() { |
| 44 | + new ApplicationContextRunner() |
| 45 | + .withPropertyValues("spring.ai.mistralai.base-url=COMMON_BASE_URL", |
| 46 | + "spring.ai.mistralai.api-key=COMMON_API_KEY", |
| 47 | + "spring.ai.mistralai.ocr.options.model=mistral-ocr-specific-model") |
| 48 | + .withConfiguration(this.autoConfigurations) |
| 49 | + .run(context -> { |
| 50 | + assertThat(context).hasSingleBean(MistralAiCommonProperties.class); |
| 51 | + assertThat(context).hasSingleBean(MistralAiOcrProperties.class); |
| 52 | + |
| 53 | + var commonProps = context.getBean(MistralAiCommonProperties.class); |
| 54 | + var ocrProps = context.getBean(MistralAiOcrProperties.class); |
| 55 | + |
| 56 | + assertThat(commonProps.getBaseUrl()).isEqualTo("COMMON_BASE_URL"); |
| 57 | + assertThat(commonProps.getApiKey()).isEqualTo("COMMON_API_KEY"); |
| 58 | + |
| 59 | + assertThat(ocrProps.getBaseUrl()).isEqualTo(MistralAiCommonProperties.DEFAULT_BASE_URL); |
| 60 | + assertThat(ocrProps.getApiKey()).isNull(); |
| 61 | + |
| 62 | + assertThat(ocrProps.getOptions()).isNotNull(); |
| 63 | + assertThat(ocrProps.getOptions().getModel()).isEqualTo("mistral-ocr-specific-model"); |
| 64 | + |
| 65 | + assertThat(context).hasSingleBean(MistralOcrApi.class); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void ocrSpecificPropertiesOverrideCommon() { |
| 71 | + new ApplicationContextRunner() |
| 72 | + .withPropertyValues("spring.ai.mistralai.base-url=COMMON_BASE_URL", |
| 73 | + "spring.ai.mistralai.api-key=COMMON_API_KEY", "spring.ai.mistralai.ocr.base-url=OCR_BASE_URL", |
| 74 | + "spring.ai.mistralai.ocr.api-key=OCR_API_KEY", |
| 75 | + "spring.ai.mistralai.ocr.options.model=mistral-ocr-default") |
| 76 | + .withConfiguration(this.autoConfigurations) |
| 77 | + .run(context -> { |
| 78 | + assertThat(context).hasSingleBean(MistralAiCommonProperties.class); |
| 79 | + assertThat(context).hasSingleBean(MistralAiOcrProperties.class); |
| 80 | + |
| 81 | + var commonProps = context.getBean(MistralAiCommonProperties.class); |
| 82 | + var ocrProps = context.getBean(MistralAiOcrProperties.class); |
| 83 | + |
| 84 | + assertThat(commonProps.getBaseUrl()).isEqualTo("COMMON_BASE_URL"); |
| 85 | + assertThat(commonProps.getApiKey()).isEqualTo("COMMON_API_KEY"); |
| 86 | + |
| 87 | + assertThat(ocrProps.getBaseUrl()).isEqualTo("OCR_BASE_URL"); |
| 88 | + assertThat(ocrProps.getApiKey()).isEqualTo("OCR_API_KEY"); |
| 89 | + |
| 90 | + assertThat(ocrProps.getOptions()).isNotNull(); |
| 91 | + assertThat(ocrProps.getOptions().getModel()).isEqualTo("mistral-ocr-default"); |
| 92 | + |
| 93 | + assertThat(context).hasSingleBean(MistralOcrApi.class); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void ocrOptionsBinding() { |
| 99 | + new ApplicationContextRunner().withPropertyValues("spring.ai.mistralai.api-key=API_KEY", |
| 100 | + "spring.ai.mistralai.ocr.options.model=custom-ocr-model", |
| 101 | + "spring.ai.mistralai.ocr.options.id=ocr-request-id-123", "spring.ai.mistralai.ocr.options.pages=0,1,5", |
| 102 | + "spring.ai.mistralai.ocr.options.includeImageBase64=true", |
| 103 | + "spring.ai.mistralai.ocr.options.imageLimit=25", "spring.ai.mistralai.ocr.options.imageMinSize=150") |
| 104 | + .withConfiguration(this.autoConfigurations) |
| 105 | + .run(context -> { |
| 106 | + assertThat(context).hasSingleBean(MistralAiOcrProperties.class); |
| 107 | + var ocrProps = context.getBean(MistralAiOcrProperties.class); |
| 108 | + var options = ocrProps.getOptions(); |
| 109 | + |
| 110 | + assertThat(options).isNotNull(); |
| 111 | + assertThat(options.getModel()).isEqualTo("custom-ocr-model"); |
| 112 | + assertThat(options.getId()).isEqualTo("ocr-request-id-123"); |
| 113 | + assertThat(options.getPages()).containsExactly(0, 1, 5); |
| 114 | + assertThat(options.getIncludeImageBase64()).isTrue(); |
| 115 | + assertThat(options.getImageLimit()).isEqualTo(25); |
| 116 | + assertThat(options.getImageMinSize()).isEqualTo(150); |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void ocrActivationViaModelProperty() { |
| 122 | + // Scenario 1: OCR explicitly disabled |
| 123 | + new ApplicationContextRunner().withConfiguration(this.autoConfigurations) |
| 124 | + .withPropertyValues("spring.ai.mistralai.api-key=API_KEY", "spring.ai.model.ocr=none") |
| 125 | + .run(context -> { |
| 126 | + assertThat(context.getBeansOfType(MistralAiOcrProperties.class)).isEmpty(); |
| 127 | + assertThat(context.getBeansOfType(MistralOcrApi.class)).isEmpty(); |
| 128 | + // Should not have common properties either if only OCR config was loaded |
| 129 | + // and then disabled |
| 130 | + assertThat(context.getBeansOfType(MistralAiCommonProperties.class)).isEmpty(); |
| 131 | + }); |
| 132 | + |
| 133 | + // Scenario 2: OCR explicitly enabled for 'mistral' |
| 134 | + new ApplicationContextRunner().withConfiguration(this.autoConfigurations) |
| 135 | + .withPropertyValues("spring.ai.mistralai.api-key=API_KEY", "spring.ai.model.ocr=mistral") |
| 136 | + .run(context -> { |
| 137 | + assertThat(context).hasSingleBean(MistralAiCommonProperties.class); // Enabled |
| 138 | + // by |
| 139 | + // MistralAiOcrAutoConfiguration |
| 140 | + assertThat(context).hasSingleBean(MistralAiOcrProperties.class); |
| 141 | + assertThat(context).hasSingleBean(MistralOcrApi.class); |
| 142 | + }); |
| 143 | + |
| 144 | + // Scenario 3: OCR implicitly enabled (default behavior when property is absent) |
| 145 | + new ApplicationContextRunner().withConfiguration(this.autoConfigurations) |
| 146 | + .withPropertyValues("spring.ai.mistralai.api-key=API_KEY") |
| 147 | + .run(context -> { |
| 148 | + assertThat(context).hasSingleBean(MistralAiCommonProperties.class); // Enabled |
| 149 | + // by |
| 150 | + // MistralAiOcrAutoConfiguration |
| 151 | + assertThat(context).hasSingleBean(MistralAiOcrProperties.class); |
| 152 | + assertThat(context).hasSingleBean(MistralOcrApi.class); |
| 153 | + }); |
| 154 | + |
| 155 | + // Scenario 4: OCR implicitly disabled when another provider is chosen |
| 156 | + new ApplicationContextRunner().withConfiguration(this.autoConfigurations) |
| 157 | + .withPropertyValues("spring.ai.mistralai.api-key=API_KEY", "spring.ai.model.ocr=some-other-provider") |
| 158 | + .run(context -> { |
| 159 | + assertThat(context.getBeansOfType(MistralAiOcrProperties.class)).isEmpty(); |
| 160 | + assertThat(context.getBeansOfType(MistralOcrApi.class)).isEmpty(); |
| 161 | + // Common properties might still be loaded if another Mistral AI config |
| 162 | + // (like Chat) was active, |
| 163 | + // but in this minimal test setup, they shouldn't be loaded if OCR is |
| 164 | + // disabled. |
| 165 | + assertThat(context.getBeansOfType(MistralAiCommonProperties.class)).isEmpty(); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments