-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit test and type field for fetching the payload
Signed-off-by: Owais Kazi <[email protected]>
- Loading branch information
1 parent
4b6e3e3
commit e89914b
Showing
3 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/org/opensearch/flowframework/workflow/CreateIndex/CreateIndexStepTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.workflow.CreateIndex; | ||
|
||
import org.opensearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.opensearch.action.admin.indices.create.CreateIndexResponse; | ||
import org.opensearch.client.AdminClient; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.client.IndicesAdminClient; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.flowframework.workflow.WorkflowData; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.mockito.ArgumentCaptor; | ||
|
||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class CreateIndexStepTests extends OpenSearchTestCase { | ||
|
||
private WorkflowData inputData = WorkflowData.EMPTY; | ||
|
||
private Client client; | ||
|
||
private AdminClient adminClient; | ||
|
||
private IndicesAdminClient indicesAdminClient; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
inputData = new WorkflowData() { | ||
|
||
@Override | ||
public Map<String, Object> getContent() { | ||
// See CreateIndexRequest ParseFields for source of content keys needed | ||
return Map.of("index-name", "demo", "type", "knn"); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getParams() { | ||
// See RestCreateIndexAction for source of param keys needed | ||
return Map.of(); | ||
} | ||
|
||
}; | ||
|
||
client = mock(Client.class); | ||
adminClient = mock(AdminClient.class); | ||
indicesAdminClient = mock(IndicesAdminClient.class); | ||
|
||
when(adminClient.indices()).thenReturn(indicesAdminClient); | ||
when(client.admin()).thenReturn(adminClient); | ||
|
||
} | ||
|
||
public void testCreateIndexStep() throws ExecutionException, InterruptedException, IOException { | ||
|
||
CreateIndexStep createIndexStep = new CreateIndexStep(client); | ||
|
||
ArgumentCaptor<ActionListener> actionListenerCaptor = ArgumentCaptor.forClass(ActionListener.class); | ||
CompletableFuture<WorkflowData> future = createIndexStep.execute(List.of(inputData)); | ||
assertFalse(future.isDone()); | ||
verify(indicesAdminClient, times(1)).create(any(CreateIndexRequest.class), actionListenerCaptor.capture()); | ||
actionListenerCaptor.getValue().onResponse(new CreateIndexResponse(true, true, "demo")); | ||
|
||
assertTrue(future.isDone()); | ||
|
||
Map<String, Object> outputData = Map.of("index-name", "demo"); | ||
assertEquals(outputData, future.get().getContent()); | ||
|
||
} | ||
|
||
public void testCreateIndexStepFailure() throws ExecutionException, InterruptedException { | ||
|
||
CreateIndexStep createIndexStep = new CreateIndexStep(client); | ||
|
||
ArgumentCaptor<ActionListener> actionListenerCaptor = ArgumentCaptor.forClass(ActionListener.class); | ||
CompletableFuture<WorkflowData> future = createIndexStep.execute(List.of(inputData)); | ||
assertFalse(future.isDone()); | ||
verify(indicesAdminClient, times(1)).create(any(CreateIndexRequest.class), actionListenerCaptor.capture()); | ||
|
||
actionListenerCaptor.getValue().onFailure(new Exception()); | ||
|
||
assertTrue(future.isDone()); | ||
assertThrows(Exception.class, () -> future.get().getContent()); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters