forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
73 additions
and
2 deletions.
There are no files selected for viewing
75 changes: 73 additions & 2 deletions
75
integ-test/src/test/java/org/opensearch/sql/sql/DummyIT.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 |
---|---|---|
@@ -1,12 +1,83 @@ | ||
package org.opensearch.sql.sql; | ||
|
||
import lombok.SneakyThrows; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.opensearch.client.RestClient; | ||
import org.opensearch.sql.legacy.OpenSearchSQLRestTestCase; | ||
import org.opensearch.test.rest.OpenSearchRestTestCase; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DummyIT extends OpenSearchSQLRestTestCase { | ||
|
||
private RestClient mockClient; | ||
|
||
@SneakyThrows | ||
@Before | ||
public void initClient2() { | ||
System.out.println("initClient2"); | ||
|
||
mockClient = mockRestClientForPoc(); | ||
|
||
set(OpenSearchSQLRestTestCase.class.getDeclaredField("remoteClient"), mockClient); | ||
set(OpenSearchSQLRestTestCase.class.getDeclaredField("remoteAdminClient"), mockClient); | ||
set(OpenSearchRestTestCase.class.getDeclaredField("client"), mockClient); | ||
set(OpenSearchRestTestCase.class.getDeclaredField("adminClient"), mockClient); | ||
} | ||
|
||
@SneakyThrows | ||
private static void set(Field field, Object value) { | ||
field.setAccessible(true); | ||
field.set(null, value); | ||
} | ||
|
||
@Override | ||
protected boolean preserveClusterUponCompletion() { | ||
// not necessary for client injection, just used to avoid failure due to mock client | ||
return true; | ||
} | ||
|
||
@Test | ||
public void dummyTest() { | ||
Assert.assertEquals(true, true); | ||
public void clientTest() { | ||
Assert.assertSame(mockClient, client()); | ||
Assert.assertSame(mockClient, remoteClient()); | ||
Assert.assertSame(mockClient, adminClient()); | ||
Assert.assertSame(mockClient, remoteAdminClient()); | ||
} | ||
|
||
@SneakyThrows | ||
private static RestClient mockRestClientForPoc() { | ||
|
||
List<RestClient> initialClients = new ArrayList<>(); | ||
|
||
add(initialClients, client()); | ||
add(initialClients, remoteClient()); | ||
add(initialClients, adminClient()); | ||
add(initialClients, remoteAdminClient()); | ||
|
||
RestClient mockClient = Mockito.mock(RestClient.class); | ||
|
||
Mockito.doAnswer(args -> { | ||
|
||
for (RestClient restClient : initialClients) { | ||
restClient.close(); | ||
} | ||
|
||
return null; | ||
}).when(mockClient).close(); | ||
|
||
return mockClient; | ||
} | ||
|
||
private static void add(List<RestClient> clients, RestClient restClient) { | ||
if (restClient == null) { | ||
return; | ||
} | ||
clients.add(restClient); | ||
} | ||
} |