@@ -174,3 +174,133 @@ def test_handler_missing_parameters(mock_get_client, lambda_context):
174174
175175 with pytest .raises (ValueError , match = "Missing required parameters" ):
176176 handler (event , lambda_context )
177+
178+
179+ @patch ("boto3.Session" )
180+ def test_get_opensearch_client_aoss (mock_session ):
181+ """Test OpenSearch client creation for AOSS"""
182+ mock_session .return_value .get_credentials .return_value = Mock ()
183+
184+ from app import get_opensearch_client
185+
186+ client = get_opensearch_client ("test.aoss.amazonaws.com" )
187+
188+ assert client is not None
189+
190+
191+ @patch ("boto3.Session" )
192+ def test_get_opensearch_client_es (mock_session ):
193+ """Test OpenSearch client creation for ES"""
194+ mock_session .return_value .get_credentials .return_value = Mock ()
195+
196+ from app import get_opensearch_client
197+
198+ client = get_opensearch_client ("test.es.amazonaws.com" )
199+
200+ assert client is not None
201+
202+
203+ @patch ("time.sleep" )
204+ @patch ("time.time" )
205+ def test_wait_for_index_aoss_timeout (mock_time , mock_sleep ):
206+ """Test index wait timeout"""
207+ mock_client = Mock ()
208+ mock_client .indices .exists .return_value = False
209+ mock_time .side_effect = [0 , 400 ] # Start time, then timeout
210+
211+ from app import wait_for_index_aoss
212+
213+ result = wait_for_index_aoss (mock_client , "test-index" , timeout = 300 , poll_interval = 5 )
214+
215+ assert result is False
216+
217+
218+ @patch ("time.sleep" )
219+ @patch ("time.time" )
220+ def test_wait_for_index_aoss_exception (mock_time , mock_sleep ):
221+ """Test index wait with exception"""
222+ mock_client = Mock ()
223+ mock_client .indices .exists .side_effect = Exception ("Connection error" )
224+ mock_time .side_effect = [0 , 400 ] # Start time, then timeout
225+
226+ from app import wait_for_index_aoss
227+
228+ result = wait_for_index_aoss (mock_client , "test-index" , timeout = 300 , poll_interval = 5 )
229+
230+ assert result is False
231+
232+
233+ @patch ("app.wait_for_index_aoss" )
234+ @patch ("app.get_opensearch_client" )
235+ def test_create_and_wait_for_index_wait_fails (mock_get_client , mock_wait , mock_opensearch_client ):
236+ """Test create index when wait fails"""
237+ mock_get_client .return_value = mock_opensearch_client
238+ mock_wait .return_value = False # Wait fails
239+
240+ from app import create_and_wait_for_index
241+
242+ with pytest .raises (RuntimeError , match = "failed to appear in time" ):
243+ create_and_wait_for_index (mock_opensearch_client , "test-index" )
244+
245+
246+ @patch ("app.get_opensearch_client" )
247+ def test_handler_delete_index_not_exists (mock_get_client , lambda_context ):
248+ """Test handler for Delete request when index doesn't exist"""
249+ mock_client = Mock ()
250+ mock_client .indices .exists .return_value = False
251+ mock_get_client .return_value = mock_client
252+
253+ from app import handler
254+
255+ event = {
256+ "RequestType" : "Delete" ,
257+ "Endpoint" : "test-endpoint" ,
258+ "IndexName" : "test-index" ,
259+ }
260+
261+ result = handler (event , lambda_context )
262+
263+ mock_client .indices .delete .assert_not_called ()
264+ assert result ["Status" ] == "SUCCESS"
265+
266+
267+ @patch ("app.get_opensearch_client" )
268+ def test_handler_delete_error (mock_get_client , lambda_context ):
269+ """Test handler for Delete request with error"""
270+ mock_client = Mock ()
271+ mock_client .indices .exists .return_value = True
272+ mock_client .indices .delete .side_effect = Exception ("Delete error" )
273+ mock_get_client .return_value = mock_client
274+
275+ from app import handler
276+
277+ event = {
278+ "RequestType" : "Delete" ,
279+ "Endpoint" : "test-endpoint" ,
280+ "IndexName" : "test-index" ,
281+ }
282+
283+ result = handler (event , lambda_context )
284+
285+ assert result ["Status" ] == "SUCCESS" # Should still succeed
286+
287+
288+ @patch ("json.loads" )
289+ @patch ("app.get_opensearch_client" )
290+ @patch ("app.create_and_wait_for_index" )
291+ def test_handler_with_payload (mock_create_wait , mock_get_client , mock_json_loads , lambda_context ):
292+ """Test handler with Payload in event"""
293+ mock_json_loads .return_value = {
294+ "RequestType" : "Create" ,
295+ "Endpoint" : "test-endpoint" ,
296+ "IndexName" : "test-index" ,
297+ }
298+
299+ from app import handler
300+
301+ event = {"Payload" : '{"RequestType": "Create", "Endpoint": "test-endpoint", "IndexName": "test-index"}' }
302+
303+ result = handler (event , lambda_context )
304+
305+ mock_json_loads .assert_called_once ()
306+ assert result ["Status" ] == "SUCCESS"
0 commit comments