13
13
import java .io .IOException ;
14
14
import java .util .Arrays ;
15
15
import java .util .Collections ;
16
+ import java .util .Map ;
16
17
17
18
import static org .mockito .Mockito .mock ;
18
19
import static org .mockito .Mockito .times ;
19
20
import static org .mockito .Mockito .verify ;
20
21
import static org .mockito .Mockito .when ;
22
+ import static org .opensearch .neuralsearch .util .TestUtils .xContentBuilderToMap ;
21
23
22
24
public class TimestampedEventStatSnapshotTests extends OpenSearchTestCase {
23
25
private static final EventStatName STAT_NAME = EventStatName .TEXT_EMBEDDING_PROCESSOR_EXECUTIONS ;
@@ -79,7 +81,7 @@ public void test_aggregateEventStatSnapshotsReturnsNull() {
79
81
80
82
public void test_aggregateEventStatDataThrowsException () {
81
83
TimestampedEventStatSnapshot snapshot1 = new TimestampedEventStatSnapshot (STAT_NAME , 100L , 50L , 10L );
82
- TimestampedEventStatSnapshot snapshot2 = new TimestampedEventStatSnapshot (EventStatName . NEURAL_QUERY_COUNT , 200L , 100L , 5L );
84
+ TimestampedEventStatSnapshot snapshot2 = new TimestampedEventStatSnapshot (null , 200L , 100L , 5L );
83
85
84
86
assertThrows (
85
87
IllegalArgumentException .class ,
@@ -89,20 +91,15 @@ public void test_aggregateEventStatDataThrowsException() {
89
91
90
92
public void test_toXContent () throws IOException {
91
93
XContentBuilder builder = JsonXContent .contentBuilder ();
92
-
93
- // Create a real snapshot with known values
94
94
TimestampedEventStatSnapshot snapshot = new TimestampedEventStatSnapshot (STAT_NAME , 100L , 50L , 10L );
95
95
96
- // Convert to XContent
97
96
snapshot .toXContent (builder , null );
98
97
99
- // Convert to string for assertion
100
- String json = builder .toString ();
98
+ Map <String , Object > responseMap = xContentBuilderToMap (builder );
101
99
102
- // Verify the JSON structure contains all expected fields with correct values
103
- assertTrue (json .contains ("\" value\" :100" ));
104
- assertTrue (json .contains ("\" trailing_interval_value\" :50" ));
105
- assertTrue (json .contains ("\" minutes_since_last_event\" :10" ));
106
- assertTrue (json .contains ("\" stat_type\" " ));
100
+ assertEquals (100 , responseMap .get ("value" ));
101
+ assertEquals (50 , responseMap .get ("trailing_interval_value" ));
102
+ assertEquals (10 , responseMap .get ("minutes_since_last_event" ));
103
+ assertEquals (STAT_NAME .getStatType ().getName (), responseMap .get ("stat_type" ));
107
104
}
108
105
}
0 commit comments