-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add seder to transportPPLQueryResponse * add UT for seder * format code * Fix failure ITs --------- (cherry picked from commit 91e5b28) Signed-off-by: zane-neo <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
0f5b5ef
commit 636d323
Showing
2 changed files
with
105 additions
and
0 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
82 changes: 82 additions & 0 deletions
82
plugin/src/test/java/org/opensearch/sql/plugin/transport/TransportPPLQueryResponseTest.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,82 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.sql.plugin.transport; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.opensearch.core.action.ActionResponse; | ||
|
||
public class TransportPPLQueryResponseTest { | ||
|
||
@Rule public ExpectedException exceptionRule = ExpectedException.none(); | ||
|
||
@Test | ||
public void testFromActionResponseSameClassloader() { | ||
TransportPPLQueryResponse response1 = new TransportPPLQueryResponse("mock result"); | ||
TransportPPLQueryResponse response2 = TransportPPLQueryResponse.fromActionResponse(response1); | ||
assertEquals(response1.getResult(), response2.getResult()); | ||
} | ||
|
||
@Test | ||
public void testFromActionResponseDifferentClassLoader() | ||
throws ClassNotFoundException, | ||
InstantiationException, | ||
IllegalAccessException, | ||
NoSuchMethodException, | ||
InvocationTargetException, | ||
URISyntaxException { | ||
ClassLoader loader = TransportPPLQueryResponseTest.class.getClassLoader(); | ||
URI resourceURI = | ||
loader | ||
.getResource("org/opensearch/sql/plugin/transport/TransportPPLQueryResponse.class") | ||
.toURI(); | ||
Path classFilePath = Paths.get(resourceURI); | ||
CustomClassLoader classLoader1 = new CustomClassLoader(classFilePath); | ||
CustomClassLoader classLoader2 = new CustomClassLoader(classFilePath); | ||
|
||
Class<?> class1 = | ||
classLoader1.findClass("org.opensearch.sql.plugin.transport.TransportPPLQueryResponse"); | ||
Class<?> class2 = | ||
classLoader2.findClass("org.opensearch.sql.plugin.transport.TransportPPLQueryResponse"); | ||
|
||
assertFalse(class1.isAssignableFrom(class2)); | ||
String result = "mock result"; | ||
TransportPPLQueryResponse response2 = | ||
TransportPPLQueryResponse.fromActionResponse( | ||
(ActionResponse) class1.getDeclaredConstructor(String.class).newInstance(result)); | ||
assertEquals(result, response2.getResult()); | ||
} | ||
} | ||
|
||
class CustomClassLoader extends ClassLoader { | ||
|
||
private final Path classFilePath; | ||
|
||
public CustomClassLoader(Path classFilePath) { | ||
this.classFilePath = classFilePath; | ||
} | ||
|
||
@Override | ||
protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
try { | ||
byte[] classBytes = Files.readAllBytes(classFilePath); | ||
return defineClass(name, classBytes, 0, classBytes.length); | ||
} catch (IOException e) { | ||
throw new ClassNotFoundException("Failed to load class: " + name, e); | ||
} | ||
} | ||
} |