Skip to content

Commit 7232aab

Browse files
committed
add more unit test
Signed-off-by: Yaliang Wu <[email protected]>
1 parent 8e3d99b commit 7232aab

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

common/src/test/java/org/opensearch/ml/common/output/model/ModelTensorTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package org.opensearch.ml.common.output.model;
77

88
import static org.junit.Assert.assertEquals;
9+
import static org.mockito.ArgumentMatchers.any;
10+
import static org.mockito.Mockito.doThrow;
11+
import static org.mockito.Mockito.spy;
912
import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS;
1013

1114
import java.io.IOException;
@@ -130,4 +133,15 @@ public void test_ToString() {
130133
"{\"name\":\"model_tensor\",\"data_type\":\"INT32\",\"shape\":[1,2,3],\"data\":[1,2,3],\"byte_buffer\":{\"array\":\"AAEAAQ==\",\"order\":\"BIG_ENDIAN\"},\"result\":\"test result\",\"dataAsMap\":{\"key1\":\"test value1\",\"key2\":\"test value2\"}}";
131134
assertEquals(expected, result);
132135
}
136+
137+
@Test
138+
public void test_ToString_ThrowsException() throws IOException {
139+
ModelTensor spyTensor = spy(modelTensor);
140+
doThrow(new IOException("Mock IOException")).when(spyTensor).toXContent(any(XContentBuilder.class), any());
141+
142+
exceptionRule.expect(IllegalArgumentException.class);
143+
exceptionRule.expectMessage("Can't convert ModelTensor to string");
144+
145+
spyTensor.toString();
146+
}
133147
}

common/src/test/java/org/opensearch/ml/common/output/model/ModelTensorsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import static org.junit.Assert.assertEquals;
99
import static org.junit.Assert.assertNotNull;
1010
import static org.junit.Assert.assertNull;
11+
import static org.mockito.ArgumentMatchers.any;
12+
import static org.mockito.Mockito.doThrow;
13+
import static org.mockito.Mockito.spy;
1114
import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS;
1215

1316
import java.io.IOException;
@@ -282,4 +285,15 @@ public void test_ToString() {
282285
"{\"output\":[{\"name\":\"model_tensor\",\"data_type\":\"INT32\",\"shape\":[1,2,3],\"data\":[1,2,3],\"byte_buffer\":{\"array\":\"AAEAAQ==\",\"order\":\"BIG_ENDIAN\"}}]}";
283286
assertEquals(expected, result);
284287
}
288+
289+
@Test
290+
public void test_ToString_ThrowsException() throws IOException {
291+
ModelTensors spyTensors = spy(modelTensors);
292+
doThrow(new IOException("Mock IOException")).when(spyTensors).toXContent(any(XContentBuilder.class), any());
293+
294+
exceptionRule.expect(IllegalArgumentException.class);
295+
exceptionRule.expectMessage("Can't convert ModelTensor to string");
296+
297+
spyTensors.toString();
298+
}
285299
}

0 commit comments

Comments
 (0)