|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.transport.agent; |
| 7 | + |
| 8 | +import static org.junit.Assert.*; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.UncheckedIOException; |
| 12 | +import java.util.Collections; |
| 13 | + |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.opensearch.action.ActionRequest; |
| 17 | +import org.opensearch.action.ActionRequestValidationException; |
| 18 | +import org.opensearch.common.io.stream.BytesStreamOutput; |
| 19 | +import org.opensearch.core.common.io.stream.StreamOutput; |
| 20 | +import org.opensearch.ml.common.agent.MLAgent; |
| 21 | +import org.opensearch.ml.common.agent.MLToolSpec; |
| 22 | + |
| 23 | +public class MLAgentUpdateRequestTests { |
| 24 | + |
| 25 | + String agentId; |
| 26 | + MLAgent mlAgent; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() { |
| 30 | + agentId = "test_agent_id"; |
| 31 | + mlAgent = MLAgent |
| 32 | + .builder() |
| 33 | + .name("test_agent") |
| 34 | + .appType("test_app") |
| 35 | + .type("flow") |
| 36 | + .tools(Collections.singletonList(MLToolSpec.builder().type("ListIndexTool").build())) |
| 37 | + .build(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void constructor_Agent() { |
| 42 | + MLAgentUpdateRequest mlAgentUpdateRequest = new MLAgentUpdateRequest(agentId, mlAgent); |
| 43 | + assertEquals(agentId, mlAgentUpdateRequest.getAgentId()); |
| 44 | + assertEquals(mlAgent, mlAgentUpdateRequest.getMlAgent()); |
| 45 | + |
| 46 | + ActionRequestValidationException validationException = mlAgentUpdateRequest.validate(); |
| 47 | + assertNull(validationException); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void constructor_NullId() { |
| 52 | + MLAgentUpdateRequest mlAgentUpdateRequest = new MLAgentUpdateRequest(null, mlAgent); |
| 53 | + assertNull(mlAgentUpdateRequest.getAgentId()); |
| 54 | + |
| 55 | + ActionRequestValidationException validationException = mlAgentUpdateRequest.validate(); |
| 56 | + assertNotNull(validationException); |
| 57 | + assertTrue(validationException.toString().contains("Agent ID and ML Agent cannot be null")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void constructor_NullAgent() { |
| 62 | + MLAgentUpdateRequest mlAgentUpdateRequest = new MLAgentUpdateRequest(agentId, null); |
| 63 | + assertNull(mlAgentUpdateRequest.getMlAgent()); |
| 64 | + |
| 65 | + ActionRequestValidationException validationException = mlAgentUpdateRequest.validate(); |
| 66 | + assertNotNull(validationException); |
| 67 | + assertTrue(validationException.toString().contains("Agent ID and ML Agent cannot be null")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void writeTo_Success() throws IOException { |
| 72 | + MLAgentUpdateRequest mlAgentUpdateRequest = new MLAgentUpdateRequest(agentId, mlAgent); |
| 73 | + BytesStreamOutput bytesStreamOutput = new BytesStreamOutput(); |
| 74 | + mlAgentUpdateRequest.writeTo(bytesStreamOutput); |
| 75 | + MLAgentUpdateRequest parsedRequest = new MLAgentUpdateRequest(bytesStreamOutput.bytes().streamInput()); |
| 76 | + assertEquals(agentId, parsedRequest.getAgentId()); |
| 77 | + assertEquals(mlAgent, parsedRequest.getMlAgent()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void fromActionRequest_Success() { |
| 82 | + MLAgentUpdateRequest mlAgentUpdateRequest = new MLAgentUpdateRequest(agentId, mlAgent); |
| 83 | + ActionRequest actionRequest = new ActionRequest() { |
| 84 | + @Override |
| 85 | + public ActionRequestValidationException validate() { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void writeTo(StreamOutput out) throws IOException { |
| 91 | + mlAgentUpdateRequest.writeTo(out); |
| 92 | + } |
| 93 | + }; |
| 94 | + MLAgentUpdateRequest parsedRequest = MLAgentUpdateRequest.fromActionRequest(actionRequest); |
| 95 | + assertNotSame(mlAgentUpdateRequest, parsedRequest); |
| 96 | + assertEquals(mlAgentUpdateRequest.getAgentId(), parsedRequest.getAgentId()); |
| 97 | + assertEquals(mlAgentUpdateRequest.getMlAgent(), parsedRequest.getMlAgent()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void fromActionRequest_Success_MLAgentUpdateRequest() { |
| 102 | + MLAgentUpdateRequest mlAgentUpdateRequest = new MLAgentUpdateRequest(agentId, mlAgent); |
| 103 | + MLAgentUpdateRequest parsedRequest = MLAgentUpdateRequest.fromActionRequest(mlAgentUpdateRequest); |
| 104 | + assertSame(mlAgentUpdateRequest, parsedRequest); |
| 105 | + } |
| 106 | + |
| 107 | + @Test(expected = UncheckedIOException.class) |
| 108 | + public void fromActionRequest_IOException() { |
| 109 | + ActionRequest actionRequest = new ActionRequest() { |
| 110 | + @Override |
| 111 | + public ActionRequestValidationException validate() { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void writeTo(StreamOutput out) throws IOException { |
| 117 | + throw new IOException(); |
| 118 | + } |
| 119 | + }; |
| 120 | + MLAgentUpdateRequest.fromActionRequest(actionRequest); |
| 121 | + } |
| 122 | +} |
0 commit comments