|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.transport.connector; |
| 7 | + |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.opensearch.action.ActionResponse; |
| 11 | +import org.opensearch.common.Strings; |
| 12 | +import org.opensearch.common.io.stream.BytesStreamOutput; |
| 13 | +import org.opensearch.common.io.stream.StreamOutput; |
| 14 | +import org.opensearch.common.xcontent.XContentFactory; |
| 15 | +import org.opensearch.common.xcontent.XContentType; |
| 16 | +import org.opensearch.core.xcontent.ToXContent; |
| 17 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 18 | +import org.opensearch.ml.common.connector.Connector; |
| 19 | +import org.opensearch.ml.common.connector.HttpConnectorTest; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.UncheckedIOException; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertNotEquals; |
| 26 | +import static org.junit.Assert.assertNotNull; |
| 27 | +import static org.junit.Assert.assertNotSame; |
| 28 | +import static org.junit.Assert.assertSame; |
| 29 | + |
| 30 | +public class MLConnectorGetResponseTests { |
| 31 | + Connector connector; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() { |
| 35 | + connector = HttpConnectorTest.createHttpConnector(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void writeTo_Success() throws IOException { |
| 40 | + BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(); |
| 41 | + MLConnectorGetResponse response = MLConnectorGetResponse.builder().mlConnector(connector).build(); |
| 42 | + response.writeTo(bytesStreamOutput); |
| 43 | + MLConnectorGetResponse parsedResponse = new MLConnectorGetResponse(bytesStreamOutput.bytes().streamInput()); |
| 44 | + assertNotEquals(response, parsedResponse); |
| 45 | + assertNotSame(response.mlConnector, parsedResponse.mlConnector); |
| 46 | + assertEquals(response.mlConnector, parsedResponse.mlConnector); |
| 47 | + assertEquals(response.mlConnector.getName(), parsedResponse.mlConnector.getName()); |
| 48 | + assertEquals(response.mlConnector.getAccess(), parsedResponse.mlConnector.getAccess()); |
| 49 | + assertEquals(response.mlConnector.getProtocol(), parsedResponse.mlConnector.getProtocol()); |
| 50 | + assertEquals(response.mlConnector.getDecryptedHeaders(), parsedResponse.mlConnector.getDecryptedHeaders()); |
| 51 | + assertEquals(response.mlConnector.getBackendRoles(), parsedResponse.mlConnector.getBackendRoles()); |
| 52 | + assertEquals(response.mlConnector.getActions(), parsedResponse.mlConnector.getActions()); |
| 53 | + assertEquals(response.mlConnector.getParameters(), parsedResponse.mlConnector.getParameters()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void toXContentTest() throws IOException { |
| 58 | + MLConnectorGetResponse mlConnectorGetResponse = MLConnectorGetResponse.builder().mlConnector(connector).build(); |
| 59 | + XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); |
| 60 | + mlConnectorGetResponse.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 61 | + assertNotNull(builder); |
| 62 | + String jsonStr = Strings.toString(builder); |
| 63 | + assertEquals("{\"name\":\"test_connector_name\"," + |
| 64 | + "\"version\":\"1\",\"description\":\"this is a test connector\",\"protocol\":\"http\"," + |
| 65 | + "\"parameters\":{\"input\":\"test input value\"},\"credential\":{\"key\":\"test_key_value\"}," + |
| 66 | + "\"actions\":[{\"action_type\":\"PREDICT\",\"method\":\"POST\",\"url\":\"https://test.com\"," + |
| 67 | + "\"headers\":{\"api_key\":\"${credential.key}\"}," + |
| 68 | + "\"request_body\":\"{\\\"input\\\": \\\"${parameters.input}\\\"}\"," + |
| 69 | + "\"pre_process_function\":\"connector.pre_process.openai.embedding\"," + |
| 70 | + "\"post_process_function\":\"connector.post_process.openai.embedding\"}]," + |
| 71 | + "\"backend_roles\":[\"role1\",\"role2\"]," + |
| 72 | + "\"access\":\"public\"}", jsonStr); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void fromActionResponseWithMLConnectorGetResponse_Success() { |
| 77 | + MLConnectorGetResponse mlConnectorGetResponse = MLConnectorGetResponse.builder().mlConnector(connector).build(); |
| 78 | + MLConnectorGetResponse mlConnectorGetResponseFromActionResponse = MLConnectorGetResponse.fromActionResponse(mlConnectorGetResponse); |
| 79 | + assertSame(mlConnectorGetResponse, mlConnectorGetResponseFromActionResponse); |
| 80 | + assertEquals(mlConnectorGetResponse.mlConnector, mlConnectorGetResponseFromActionResponse.mlConnector); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void fromActionResponse_Success() { |
| 85 | + MLConnectorGetResponse mlConnectorGetResponse = MLConnectorGetResponse.builder().mlConnector(connector).build(); |
| 86 | + ActionResponse actionResponse = new ActionResponse() { |
| 87 | + @Override |
| 88 | + public void writeTo(StreamOutput out) throws IOException { |
| 89 | + mlConnectorGetResponse.writeTo(out); |
| 90 | + } |
| 91 | + }; |
| 92 | + MLConnectorGetResponse mlConnectorGetResponseFromActionResponse = MLConnectorGetResponse.fromActionResponse(actionResponse); |
| 93 | + assertNotSame(mlConnectorGetResponse, mlConnectorGetResponseFromActionResponse); |
| 94 | + assertEquals(mlConnectorGetResponse.mlConnector, mlConnectorGetResponseFromActionResponse.mlConnector); |
| 95 | + } |
| 96 | + |
| 97 | + @Test(expected = UncheckedIOException.class) |
| 98 | + public void fromActionResponse_IOException() { |
| 99 | + ActionResponse actionResponse = new ActionResponse() { |
| 100 | + @Override |
| 101 | + public void writeTo(StreamOutput out) throws IOException { |
| 102 | + throw new IOException(); |
| 103 | + } |
| 104 | + }; |
| 105 | + MLConnectorGetResponse.fromActionResponse(actionResponse); |
| 106 | + } |
| 107 | +} |
0 commit comments