|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.services.elastic.completion; |
| 9 | + |
| 10 | +import org.elasticsearch.common.Strings; |
| 11 | +import org.elasticsearch.common.ValidationException; |
| 12 | +import org.elasticsearch.common.io.stream.Writeable; |
| 13 | +import org.elasticsearch.test.AbstractWireSerializingTestCase; |
| 14 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 15 | +import org.elasticsearch.xcontent.XContentFactory; |
| 16 | +import org.elasticsearch.xcontent.XContentType; |
| 17 | +import org.elasticsearch.xpack.inference.services.ConfigurationParseContext; |
| 18 | +import org.elasticsearch.xpack.inference.services.ServiceFields; |
| 19 | +import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings; |
| 20 | +import org.elasticsearch.xpack.inference.services.settings.RateLimitSettingsTests; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import static org.hamcrest.Matchers.containsString; |
| 27 | +import static org.hamcrest.Matchers.is; |
| 28 | + |
| 29 | +public class EISCompletionServiceSettingsTests extends AbstractWireSerializingTestCase<ElasticInferenceServiceCompletionServiceSettings> { |
| 30 | + |
| 31 | + @Override |
| 32 | + protected Writeable.Reader<ElasticInferenceServiceCompletionServiceSettings> instanceReader() { |
| 33 | + return ElasticInferenceServiceCompletionServiceSettings::new; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + protected ElasticInferenceServiceCompletionServiceSettings createTestInstance() { |
| 38 | + return createRandom(); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected ElasticInferenceServiceCompletionServiceSettings mutateInstance(ElasticInferenceServiceCompletionServiceSettings instance) |
| 43 | + throws IOException { |
| 44 | + return randomValueOtherThan(instance, EISCompletionServiceSettingsTests::createRandom); |
| 45 | + } |
| 46 | + |
| 47 | + public void testFromMap() { |
| 48 | + var modelId = "model_id"; |
| 49 | + |
| 50 | + var serviceSettings = ElasticInferenceServiceCompletionServiceSettings.fromMap( |
| 51 | + new HashMap<>(Map.of(ServiceFields.MODEL_ID, modelId)), |
| 52 | + ConfigurationParseContext.REQUEST |
| 53 | + ); |
| 54 | + |
| 55 | + assertThat(serviceSettings, is(new ElasticInferenceServiceCompletionServiceSettings(modelId, new RateLimitSettings(1000)))); |
| 56 | + } |
| 57 | + |
| 58 | + public void testFromMap_MissingModelId_ThrowsException() { |
| 59 | + ValidationException validationException = expectThrows( |
| 60 | + ValidationException.class, |
| 61 | + () -> ElasticInferenceServiceCompletionServiceSettings.fromMap(new HashMap<>(Map.of()), ConfigurationParseContext.REQUEST) |
| 62 | + ); |
| 63 | + |
| 64 | + assertThat(validationException.getMessage(), containsString("does not contain the required setting [model_id]")); |
| 65 | + } |
| 66 | + |
| 67 | + public void testToXContent_WritesAllFields() throws IOException { |
| 68 | + var modelId = "model_id"; |
| 69 | + var serviceSettings = new ElasticInferenceServiceCompletionServiceSettings(modelId, new RateLimitSettings(1000)); |
| 70 | + |
| 71 | + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); |
| 72 | + serviceSettings.toXContent(builder, null); |
| 73 | + String xContentResult = Strings.toString(builder); |
| 74 | + |
| 75 | + assertThat(xContentResult, is(Strings.format(""" |
| 76 | + {"model_id":"%s","rate_limit":{"requests_per_minute":1000}}""", modelId))); |
| 77 | + } |
| 78 | + |
| 79 | + public static ElasticInferenceServiceCompletionServiceSettings createRandom() { |
| 80 | + return new ElasticInferenceServiceCompletionServiceSettings(randomAlphaOfLength(4), RateLimitSettingsTests.createRandom()); |
| 81 | + } |
| 82 | +} |
0 commit comments