Skip to content

Commit b19342f

Browse files
Add unit tests for OpenShiftAiChatCompletionServiceSettings
1 parent b98d8d6 commit b19342f

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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.openshiftai.completion;
9+
10+
import org.elasticsearch.TransportVersion;
11+
import org.elasticsearch.common.Strings;
12+
import org.elasticsearch.common.ValidationException;
13+
import org.elasticsearch.common.io.stream.Writeable;
14+
import org.elasticsearch.common.xcontent.XContentHelper;
15+
import org.elasticsearch.xcontent.XContentBuilder;
16+
import org.elasticsearch.xcontent.XContentFactory;
17+
import org.elasticsearch.xcontent.XContentType;
18+
import org.elasticsearch.xpack.core.ml.AbstractBWCWireSerializationTestCase;
19+
import org.elasticsearch.xpack.inference.services.ConfigurationParseContext;
20+
import org.elasticsearch.xpack.inference.services.ServiceFields;
21+
import org.elasticsearch.xpack.inference.services.ServiceUtils;
22+
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
23+
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettingsTests;
24+
25+
import java.io.IOException;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import static org.hamcrest.Matchers.containsString;
30+
import static org.hamcrest.Matchers.is;
31+
32+
public class OpenShiftAiChatCompletionServiceSettingsTests extends AbstractBWCWireSerializationTestCase<
33+
OpenShiftAiChatCompletionServiceSettings> {
34+
35+
public static final String MODEL_ID = "some model";
36+
public static final String CORRECT_URL = "https://www.elastic.co";
37+
public static final int RATE_LIMIT = 2;
38+
39+
public void testFromMap_AllFields_Success() {
40+
var serviceSettings = OpenShiftAiChatCompletionServiceSettings.fromMap(
41+
new HashMap<>(
42+
Map.of(
43+
ServiceFields.MODEL_ID,
44+
MODEL_ID,
45+
ServiceFields.URL,
46+
CORRECT_URL,
47+
RateLimitSettings.FIELD_NAME,
48+
new HashMap<>(Map.of(RateLimitSettings.REQUESTS_PER_MINUTE_FIELD, RATE_LIMIT))
49+
)
50+
),
51+
ConfigurationParseContext.PERSISTENT
52+
);
53+
54+
assertThat(
55+
serviceSettings,
56+
is(new OpenShiftAiChatCompletionServiceSettings(MODEL_ID, CORRECT_URL, new RateLimitSettings(RATE_LIMIT)))
57+
);
58+
}
59+
60+
public void testFromMap_MissingModelId_Success() {
61+
var serviceSettings = OpenShiftAiChatCompletionServiceSettings.fromMap(
62+
new HashMap<>(
63+
Map.of(
64+
ServiceFields.URL,
65+
CORRECT_URL,
66+
RateLimitSettings.FIELD_NAME,
67+
new HashMap<>(Map.of(RateLimitSettings.REQUESTS_PER_MINUTE_FIELD, RATE_LIMIT))
68+
)
69+
),
70+
ConfigurationParseContext.PERSISTENT
71+
);
72+
73+
assertThat(serviceSettings, is(new OpenShiftAiChatCompletionServiceSettings(null, CORRECT_URL, new RateLimitSettings(RATE_LIMIT))));
74+
}
75+
76+
public void testFromMap_MissingUrl_ThrowsException() {
77+
var thrownException = expectThrows(
78+
ValidationException.class,
79+
() -> OpenShiftAiChatCompletionServiceSettings.fromMap(
80+
new HashMap<>(
81+
Map.of(
82+
ServiceFields.MODEL_ID,
83+
MODEL_ID,
84+
RateLimitSettings.FIELD_NAME,
85+
new HashMap<>(Map.of(RateLimitSettings.REQUESTS_PER_MINUTE_FIELD, RATE_LIMIT))
86+
)
87+
),
88+
ConfigurationParseContext.PERSISTENT
89+
)
90+
);
91+
92+
assertThat(
93+
thrownException.getMessage(),
94+
containsString("Validation Failed: 1: [service_settings] does not contain the required setting [url];")
95+
);
96+
}
97+
98+
public void testFromMap_MissingRateLimit_Success() {
99+
var serviceSettings = OpenShiftAiChatCompletionServiceSettings.fromMap(
100+
new HashMap<>(Map.of(ServiceFields.MODEL_ID, MODEL_ID, ServiceFields.URL, CORRECT_URL)),
101+
ConfigurationParseContext.PERSISTENT
102+
);
103+
104+
assertThat(serviceSettings, is(new OpenShiftAiChatCompletionServiceSettings(MODEL_ID, CORRECT_URL, null)));
105+
}
106+
107+
public void testToXContent_WritesAllValues() throws IOException {
108+
var serviceSettings = OpenShiftAiChatCompletionServiceSettings.fromMap(
109+
new HashMap<>(
110+
Map.of(
111+
ServiceFields.MODEL_ID,
112+
MODEL_ID,
113+
ServiceFields.URL,
114+
CORRECT_URL,
115+
RateLimitSettings.FIELD_NAME,
116+
new HashMap<>(Map.of(RateLimitSettings.REQUESTS_PER_MINUTE_FIELD, RATE_LIMIT))
117+
)
118+
),
119+
ConfigurationParseContext.PERSISTENT
120+
);
121+
122+
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
123+
serviceSettings.toXContent(builder, null);
124+
String xContentResult = Strings.toString(builder);
125+
var expected = XContentHelper.stripWhitespace("""
126+
{
127+
"model_id": "some model",
128+
"url": "https://www.elastic.co",
129+
"rate_limit": {
130+
"requests_per_minute": 2
131+
}
132+
}
133+
""");
134+
135+
assertThat(xContentResult, is(expected));
136+
}
137+
138+
public void testToXContent_DoesNotWriteOptionalValues_DefaultRateLimit() throws IOException {
139+
var serviceSettings = OpenShiftAiChatCompletionServiceSettings.fromMap(
140+
new HashMap<>(Map.of(ServiceFields.URL, CORRECT_URL)),
141+
ConfigurationParseContext.PERSISTENT
142+
);
143+
144+
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
145+
serviceSettings.toXContent(builder, null);
146+
String xContentResult = Strings.toString(builder);
147+
var expected = XContentHelper.stripWhitespace("""
148+
{
149+
"url": "https://www.elastic.co",
150+
"rate_limit": {
151+
"requests_per_minute": 3000
152+
}
153+
}
154+
""");
155+
assertThat(xContentResult, is(expected));
156+
}
157+
158+
@Override
159+
protected Writeable.Reader<OpenShiftAiChatCompletionServiceSettings> instanceReader() {
160+
return OpenShiftAiChatCompletionServiceSettings::new;
161+
}
162+
163+
@Override
164+
protected OpenShiftAiChatCompletionServiceSettings createTestInstance() {
165+
return createRandom();
166+
}
167+
168+
@Override
169+
protected OpenShiftAiChatCompletionServiceSettings mutateInstance(OpenShiftAiChatCompletionServiceSettings instance)
170+
throws IOException {
171+
return randomValueOtherThan(instance, OpenShiftAiChatCompletionServiceSettingsTests::createRandom);
172+
}
173+
174+
@Override
175+
protected OpenShiftAiChatCompletionServiceSettings mutateInstanceForVersion(
176+
OpenShiftAiChatCompletionServiceSettings instance,
177+
TransportVersion version
178+
) {
179+
return instance;
180+
}
181+
182+
private static OpenShiftAiChatCompletionServiceSettings createRandom() {
183+
var modelId = randomAlphaOfLengthOrNull(8);
184+
var url = randomAlphaOfLength(15);
185+
return new OpenShiftAiChatCompletionServiceSettings(modelId, ServiceUtils.createUri(url), RateLimitSettingsTests.createRandom());
186+
}
187+
188+
public static Map<String, Object> getServiceSettingsMap(String model, String url) {
189+
var map = new HashMap<String, Object>();
190+
191+
map.put(ServiceFields.MODEL_ID, model);
192+
map.put(ServiceFields.URL, url);
193+
194+
return map;
195+
}
196+
197+
}

0 commit comments

Comments
 (0)