5
5
6
6
package org .opensearch .ml .common .connector ;
7
7
8
+ import static org .junit .Assert .assertEquals ;
9
+ import static org .junit .Assert .assertThrows ;
8
10
import static org .opensearch .ml .common .connector .ConnectorAction .ActionType .isValidActionInModelPrediction ;
9
11
10
12
import java .io .IOException ;
11
13
import java .util .Collections ;
12
14
import java .util .HashMap ;
13
15
import java .util .Map ;
14
16
15
- import org .junit .Assert ;
16
- import org .junit .Rule ;
17
17
import org .junit .Test ;
18
- import org .junit .rules .ExpectedException ;
19
18
import org .opensearch .common .io .stream .BytesStreamOutput ;
20
19
import org .opensearch .common .settings .Settings ;
21
20
import org .opensearch .common .xcontent .XContentType ;
27
26
import org .opensearch .search .SearchModule ;
28
27
29
28
public class ConnectorActionTest {
30
- @ Rule
31
- public ExpectedException exceptionRule = ExpectedException .none ();
29
+
30
+ // Shared test data for the class
31
+ private static final ConnectorAction .ActionType TEST_ACTION_TYPE = ConnectorAction .ActionType .PREDICT ;
32
+ private static final String TEST_METHOD_POST = "post" ;
33
+ private static final String TEST_METHOD_HTTP = "http" ;
34
+ private static final String TEST_REQUEST_BODY = "{\" input\" : \" ${parameters.input}\" }" ;
35
+ private static final String URL = "https://test.com" ;
32
36
33
37
@ Test
34
38
public void constructor_NullActionType () {
35
- exceptionRule . expect ( IllegalArgumentException . class );
36
- exceptionRule . expectMessage ( "action type can't null" );
37
- ConnectorAction . ActionType actionType = null ;
38
- String method = "post" ;
39
- String url = "https://test.com" ;
40
- new ConnectorAction ( actionType , method , url , null , null , null , null );
39
+ Throwable exception = assertThrows (
40
+ IllegalArgumentException . class ,
41
+ () -> new ConnectorAction ( null , TEST_METHOD_POST , URL , null , TEST_REQUEST_BODY , null , null )
42
+ ) ;
43
+ assertEquals ( "action type can't be null" , exception . getMessage ()) ;
44
+
41
45
}
42
46
43
47
@ Test
44
48
public void constructor_NullUrl () {
45
- exceptionRule .expect (IllegalArgumentException .class );
46
- exceptionRule .expectMessage ("url can't null" );
47
- ConnectorAction .ActionType actionType = ConnectorAction .ActionType .PREDICT ;
48
- String method = "post" ;
49
- String url = null ;
50
- new ConnectorAction (actionType , method , url , null , null , null , null );
49
+ Throwable exception = assertThrows (
50
+ IllegalArgumentException .class ,
51
+ () -> new ConnectorAction (TEST_ACTION_TYPE , TEST_METHOD_POST , null , null , TEST_REQUEST_BODY , null , null )
52
+ );
53
+ assertEquals ("url can't be null" , exception .getMessage ());
51
54
}
52
55
53
56
@ Test
54
57
public void constructor_NullMethod () {
55
- exceptionRule .expect (IllegalArgumentException .class );
56
- exceptionRule .expectMessage ("method can't null" );
57
- ConnectorAction .ActionType actionType = ConnectorAction .ActionType .PREDICT ;
58
- String method = null ;
59
- String url = "https://test.com" ;
60
- new ConnectorAction (actionType , method , url , null , null , null , null );
58
+ Throwable exception = assertThrows (
59
+ IllegalArgumentException .class ,
60
+ () -> new ConnectorAction (TEST_ACTION_TYPE , null , URL , null , TEST_REQUEST_BODY , null , null )
61
+ );
62
+ assertEquals ("method can't be null" , exception .getMessage ());
61
63
}
62
64
63
65
@ Test
64
66
public void writeTo_NullValue () throws IOException {
65
- ConnectorAction .ActionType actionType = ConnectorAction .ActionType .PREDICT ;
66
- String method = "http" ;
67
- String url = "https://test.com" ;
68
- ConnectorAction action = new ConnectorAction (actionType , method , url , null , null , null , null );
67
+ ConnectorAction action = new ConnectorAction (TEST_ACTION_TYPE , TEST_METHOD_HTTP , URL , null , TEST_REQUEST_BODY , null , null );
69
68
BytesStreamOutput output = new BytesStreamOutput ();
70
69
action .writeTo (output );
71
70
ConnectorAction action2 = new ConnectorAction (output .bytes ().streamInput ());
72
- Assert . assertEquals (action , action2 );
71
+ assertEquals (action , action2 );
73
72
}
74
73
75
74
@ Test
76
75
public void writeTo () throws IOException {
77
- ConnectorAction .ActionType actionType = ConnectorAction .ActionType .PREDICT ;
78
- String method = "http" ;
79
- String url = "https://test.com" ;
80
76
Map <String , String > headers = new HashMap <>();
81
77
headers .put ("key1" , "value1" );
82
- String requestBody = "{\" input\" : \" ${parameters.input}\" }" ;
83
78
String preProcessFunction = MLPreProcessFunction .TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT ;
84
79
String postProcessFunction = MLPostProcessFunction .OPENAI_EMBEDDING ;
85
80
86
81
ConnectorAction action = new ConnectorAction (
87
- actionType ,
88
- method ,
89
- url ,
82
+ TEST_ACTION_TYPE ,
83
+ TEST_METHOD_HTTP ,
84
+ URL ,
90
85
headers ,
91
- requestBody ,
86
+ TEST_REQUEST_BODY ,
92
87
preProcessFunction ,
93
88
postProcessFunction
94
89
);
95
90
BytesStreamOutput output = new BytesStreamOutput ();
96
91
action .writeTo (output );
97
92
ConnectorAction action2 = new ConnectorAction (output .bytes ().streamInput ());
98
- Assert . assertEquals (action , action2 );
93
+ assertEquals (action , action2 );
99
94
}
100
95
101
96
@ Test
102
97
public void toXContent_NullValue () throws IOException {
103
- ConnectorAction .ActionType actionType = ConnectorAction .ActionType .PREDICT ;
104
- String method = "http" ;
105
- String url = "https://test.com" ;
106
- ConnectorAction action = new ConnectorAction (actionType , method , url , null , null , null , null );
98
+ ConnectorAction action = new ConnectorAction (TEST_ACTION_TYPE , TEST_METHOD_HTTP , URL , null , TEST_REQUEST_BODY , null , null );
107
99
108
100
XContentBuilder builder = XContentBuilder .builder (XContentType .JSON .xContent ());
109
101
action .toXContent (builder , ToXContent .EMPTY_PARAMS );
110
102
String content = TestHelper .xContentBuilderToString (builder );
111
- Assert .assertEquals ("{\" action_type\" :\" PREDICT\" ,\" method\" :\" http\" ,\" url\" :\" https://test.com\" }" , content );
103
+ String expctedContent = """
104
+ {"action_type":"PREDICT","method":"http","url":"https://test.com",\
105
+ "request_body":"{\\ "input\\ ": \\ "${parameters.input}\\ "}"}\
106
+ """ ;
107
+ assertEquals (expctedContent , content );
112
108
}
113
109
114
110
@ Test
115
111
public void toXContent () throws IOException {
116
- ConnectorAction .ActionType actionType = ConnectorAction .ActionType .PREDICT ;
117
- String method = "http" ;
118
- String url = "https://test.com" ;
119
112
Map <String , String > headers = new HashMap <>();
120
113
headers .put ("key1" , "value1" );
121
- String requestBody = "{\" input\" : \" ${parameters.input}\" }" ;
122
114
String preProcessFunction = MLPreProcessFunction .TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT ;
123
115
String postProcessFunction = MLPostProcessFunction .OPENAI_EMBEDDING ;
124
116
125
117
ConnectorAction action = new ConnectorAction (
126
- actionType ,
127
- method ,
128
- url ,
118
+ TEST_ACTION_TYPE ,
119
+ TEST_METHOD_HTTP ,
120
+ URL ,
129
121
headers ,
130
- requestBody ,
122
+ TEST_REQUEST_BODY ,
131
123
preProcessFunction ,
132
124
postProcessFunction
133
125
);
134
126
135
127
XContentBuilder builder = XContentBuilder .builder (XContentType .JSON .xContent ());
136
128
action .toXContent (builder , ToXContent .EMPTY_PARAMS );
137
129
String content = TestHelper .xContentBuilderToString (builder );
138
- Assert
139
- .assertEquals (
140
- "{\" action_type\" :\" PREDICT\" ,\" method\" :\" http\" ,\" url\" :\" https://test.com\" ,"
141
- + "\" headers\" :{\" key1\" :\" value1\" },\" request_body\" :\" {\\ \" input\\ \" : \\ \" ${parameters.input}\\ \" }\" ,"
142
- + "\" pre_process_function\" :\" connector.pre_process.openai.embedding\" ,"
143
- + "\" post_process_function\" :\" connector.post_process.openai.embedding\" }" ,
144
- content
145
- );
130
+ String expctedContent = """
131
+ {"action_type":"PREDICT","method":"http","url":"https://test.com","headers":{"key1":"value1"},\
132
+ "request_body":"{\\ "input\\ ": \\ "${parameters.input}\\ "}",\
133
+ "pre_process_function":"connector.pre_process.openai.embedding",\
134
+ "post_process_function":"connector.post_process.openai.embedding"}\
135
+ """ ;
136
+ assertEquals (expctedContent , content );
146
137
}
147
138
148
139
@ Test
149
140
public void parse () throws IOException {
150
- String jsonStr = "{\" action_type\" :\" PREDICT\" ,\" method\" :\" http\" ,\" url\" :\" https://test.com\" ,"
151
- + "\" headers\" :{\" key1\" :\" value1\" },\" request_body\" :\" {\\ \" input\\ \" : \\ \" ${parameters.input}\\ \" }\" ,"
152
- + "\" pre_process_function\" :\" connector.pre_process.openai.embedding\" ,"
153
- + "\" post_process_function\" :\" connector.post_process.openai.embedding\" }" ;
141
+ String jsonStr = """
142
+ {"action_type":"PREDICT","method":"http","url":"https://test.com","headers":{"key1":"value1"},\
143
+ "request_body":"{\\ "input\\ ": \\ "${parameters.input}\\ "}",\
144
+ "pre_process_function":"connector.pre_process.openai.embedding",\
145
+ "post_process_function":"connector.post_process.openai.embedding"}"\
146
+ """ ;
154
147
XContentParser parser = XContentType .JSON
155
148
.xContent ()
156
149
.createParser (
@@ -160,24 +153,23 @@ public void parse() throws IOException {
160
153
);
161
154
parser .nextToken ();
162
155
ConnectorAction action = ConnectorAction .parse (parser );
163
- Assert . assertEquals ("http" , action .getMethod ());
164
- Assert . assertEquals (ConnectorAction .ActionType .PREDICT , action .getActionType ());
165
- Assert . assertEquals ("https://test.com" , action .getUrl ());
166
- Assert . assertEquals ("{ \" input \" : \" ${parameters.input} \" }" , action .getRequestBody ());
167
- Assert . assertEquals ("connector.pre_process.openai.embedding" , action .getPreProcessFunction ());
168
- Assert . assertEquals ("connector.post_process.openai.embedding" , action .getPostProcessFunction ());
156
+ assertEquals (TEST_METHOD_HTTP , action .getMethod ());
157
+ assertEquals (ConnectorAction .ActionType .PREDICT , action .getActionType ());
158
+ assertEquals (URL , action .getUrl ());
159
+ assertEquals (TEST_REQUEST_BODY , action .getRequestBody ());
160
+ assertEquals ("connector.pre_process.openai.embedding" , action .getPreProcessFunction ());
161
+ assertEquals ("connector.post_process.openai.embedding" , action .getPostProcessFunction ());
169
162
}
170
163
171
164
@ Test
172
165
public void test_wrongActionType () {
173
- exceptionRule .expect (IllegalArgumentException .class );
174
- exceptionRule .expectMessage ("Wrong Action Type" );
175
- ConnectorAction .ActionType .from ("badAction" );
166
+ Throwable exception = assertThrows (IllegalArgumentException .class , () -> { ConnectorAction .ActionType .from ("badAction" ); });
167
+ assertEquals ("Wrong Action Type of badAction" , exception .getMessage ());
176
168
}
177
169
178
170
@ Test
179
171
public void test_invalidActionInModelPrediction () {
180
172
ConnectorAction .ActionType actionType = ConnectorAction .ActionType .from ("execute" );
181
- Assert . assertEquals (isValidActionInModelPrediction (actionType ), false );
173
+ assertEquals (isValidActionInModelPrediction (actionType ), false );
182
174
}
183
175
}
0 commit comments