-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remote inference: add unit test for create connector request/response (…
…#1067) * remote inference: add unit test for create connector request/response Signed-off-by: Yaliang Wu <ylwu@amazon.com> * fix failed UT Signed-off-by: Yaliang Wu <ylwu@amazon.com> * fix failed UT Signed-off-by: Yaliang Wu <ylwu@amazon.com> --------- Signed-off-by: Yaliang Wu <ylwu@amazon.com> (cherry picked from commit 1602aea)
- Loading branch information
1 parent
f1f0e1e
commit d543904
Showing
7 changed files
with
144 additions
and
11 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
72 changes: 72 additions & 0 deletions
72
.../test/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorRequestTest.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,72 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
public class MLCreateConnectorRequestTest { | ||
|
||
@Rule | ||
public ExpectedException exceptionRule = ExpectedException.none(); | ||
|
||
@Test | ||
public void validate_nullInput() { | ||
MLCreateConnectorRequest request = new MLCreateConnectorRequest((MLCreateConnectorInput)null); | ||
ActionRequestValidationException exception = request.validate(); | ||
Assert.assertTrue(exception.getMessage().contains("ML Connector input can't be null")); | ||
} | ||
|
||
@Test | ||
public void readFromStream() throws IOException { | ||
MLCreateConnectorInput input = MLCreateConnectorInput.builder() | ||
.name("test_connector") | ||
.protocol("http") | ||
.version("1") | ||
.description("test") | ||
.build(); | ||
MLCreateConnectorRequest request = new MLCreateConnectorRequest(input); | ||
BytesStreamOutput output = new BytesStreamOutput(); | ||
request.writeTo(output); | ||
MLCreateConnectorRequest request2 = new MLCreateConnectorRequest(output.bytes().streamInput()); | ||
Assert.assertEquals("test_connector", request2.getMlCreateConnectorInput().getName()); | ||
Assert.assertEquals("http", request2.getMlCreateConnectorInput().getProtocol()); | ||
Assert.assertEquals("1", request2.getMlCreateConnectorInput().getVersion()); | ||
Assert.assertEquals("test", request2.getMlCreateConnectorInput().getDescription()); | ||
} | ||
|
||
@Test | ||
public void fromActionRequest() { | ||
MLCreateConnectorInput input = MLCreateConnectorInput.builder() | ||
.name("test_connector") | ||
.protocol("http") | ||
.version("1") | ||
.description("test") | ||
.build(); | ||
ActionRequest request = new MLCreateConnectorRequest(input); | ||
MLCreateConnectorRequest request2 = MLCreateConnectorRequest.fromActionRequest(request); | ||
Assert.assertEquals("test_connector", request2.getMlCreateConnectorInput().getName()); | ||
Assert.assertEquals("http", request2.getMlCreateConnectorInput().getProtocol()); | ||
Assert.assertEquals("1", request2.getMlCreateConnectorInput().getVersion()); | ||
Assert.assertEquals("test", request2.getMlCreateConnectorInput().getDescription()); | ||
} | ||
|
||
@Test | ||
public void fromActionRequest_Exception() { | ||
exceptionRule.expect(UncheckedIOException.class); | ||
exceptionRule.expectMessage("Failed to parse ActionRequest into MLCreateConnectorRequest"); | ||
ActionRequest request = new MLConnectorGetRequest("test_id", true); | ||
MLCreateConnectorRequest.fromActionRequest(request); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...test/java/org/opensearch/ml/common/transport/connector/MLCreateConnectorResponseTest.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,38 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.connector; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.opensearch.common.io.stream.BytesStreamOutput; | ||
import org.opensearch.common.xcontent.XContentType; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.ml.common.TestHelper; | ||
|
||
import java.io.IOException; | ||
|
||
public class MLCreateConnectorResponseTest { | ||
|
||
@Test | ||
public void toXContent() throws IOException { | ||
MLCreateConnectorResponse response = new MLCreateConnectorResponse("test_id"); | ||
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); | ||
response.toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
String content = TestHelper.xContentBuilderToString(builder); | ||
Assert.assertEquals("{\"connector_id\":\"test_id\"}", content); | ||
} | ||
|
||
@Test | ||
public void readFromStream() throws IOException { | ||
MLCreateConnectorResponse response = new MLCreateConnectorResponse("test_id"); | ||
BytesStreamOutput output = new BytesStreamOutput(); | ||
response.writeTo(output); | ||
|
||
MLCreateConnectorResponse response2 = new MLCreateConnectorResponse(output.bytes().streamInput()); | ||
Assert.assertEquals("test_id", response2.getConnectorId()); | ||
} | ||
} |
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
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
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
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