Skip to content

Commit

Permalink
Increase unit test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: HenryL27 <[email protected]>
  • Loading branch information
HenryL27 committed Aug 17, 2023
1 parent 50c7518 commit d26fab1
Show file tree
Hide file tree
Showing 36 changed files with 763 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(prompt);
out.writeString(response);
out.writeString(agent);
out.writeString(metadata);
out.writeOptionalString(metadata);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import java.util.List;

import org.opensearch.action.ActionFuture;
import org.opensearch.action.ActionListener;
import org.opensearch.common.action.ActionFuture;
import org.opensearch.core.action.ActionListener;
import org.opensearch.ml.common.conversational.ConversationMeta;
import org.opensearch.ml.common.conversational.Interaction;
import org.opensearch.ml.common.conversational.Interaction.InteractionBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.io.IOException;

import org.opensearch.action.ActionResponse;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.opensearch.ml.conversational.action.memory.conversation;

import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public DeleteConversationRequest(StreamInput in) throws IOException {

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeString(conversationId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.io.IOException;

import org.opensearch.action.ActionResponse;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.opensearch.ml.conversational.action.memory.conversation;

import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.io.IOException;
import java.util.List;

import org.opensearch.action.ActionResponse;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.ml.common.conversational.ActionConstants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Rest Handler for list conversations
*/
public class GetConversationsRestAction extends BaseRestHandler {
private final static String LIST_CONVERSATION_NAME = "conversational_list_conversations";
private final static String LIST_CONVERSATION_NAME = "conversational_get_conversations";

@Override
public List<Route> routes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.util.List;

import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.opensearch.ml.conversational.action.memory.interaction;

import java.io.IOException;
import java.util.Map;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
Expand Down Expand Up @@ -71,9 +70,9 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(conversationId);
out.writeString(input);
out.writeString(response);
out.writeString(prompt);
out.writeString(agent);
out.writeString(attributes);
out.writeOptionalString(prompt);
out.writeOptionalString(agent);
out.writeOptionalString(attributes);
}

@Override
Expand All @@ -92,16 +91,12 @@ public ActionRequestValidationException validate() {
* @throws IOException if something goes wrong reading from request
*/
public static CreateInteractionRequest fromRestRequest(RestRequest request) throws IOException {
if(!request.hasContent()) {
throw new IOException("Put interaction request must have body");
}
Map<String, String> bodyMap = request.contentParser().mapStrings();
String cid = request.param(ActionConstants.CONVERSATION_ID_FIELD);
String inp = bodyMap.get(ActionConstants.INPUT_FIELD);
String prp = bodyMap.get(ActionConstants.PROMPT_FIELD);
String rsp = bodyMap.get(ActionConstants.AI_RESPONSE_FIELD);
String agt = bodyMap.get(ActionConstants.AI_AGENT_FIELD);
String att = bodyMap.get(ActionConstants.INTER_ATTRIBUTES_FIELD);
String inp = request.param(ActionConstants.INPUT_FIELD);
String prp = request.param(ActionConstants.PROMPT_FIELD);
String rsp = request.param(ActionConstants.AI_RESPONSE_FIELD);
String agt = request.param(ActionConstants.AI_AGENT_FIELD);
String att = request.param(ActionConstants.INTER_ATTRIBUTES_FIELD);
return new CreateInteractionRequest(cid, inp, prp, rsp, agt, att);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.io.IOException;

import org.opensearch.action.ActionResponse;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.opensearch.ml.conversational.action.memory.interaction;

import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.io.IOException;
import java.util.List;

import org.opensearch.action.ActionResponse;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.ml.common.conversational.ActionConstants;
Expand All @@ -40,6 +40,7 @@
public class GetInteractionsResponse extends ActionResponse implements ToXContentObject {
@Getter
private List<Interaction> interactions;
@Getter
private int nextToken;
private boolean hasMoreTokens;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.util.List;

import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.opensearch.OpenSearchSecurityException;
import org.opensearch.OpenSearchWrapperException;
import org.opensearch.ResourceAlreadyExistsException;
import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.DocWriteResponse.Result;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
import org.opensearch.action.admin.indices.create.CreateIndexResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.opensearch.OpenSearchSecurityException;
import org.opensearch.OpenSearchWrapperException;
import org.opensearch.ResourceAlreadyExistsException;
import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
import org.opensearch.action.admin.indices.create.CreateIndexResponse;
import org.opensearch.action.bulk.BulkRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.time.Instant;
import java.util.List;

import org.opensearch.action.ActionFuture;
import org.opensearch.action.ActionListener;
import org.opensearch.common.action.ActionFuture;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.StepListener;
import org.opensearch.action.support.PlainActionFuture;
import org.opensearch.client.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import org.junit.Before;
import org.opensearch.OpenSearchSecurityException;
import org.opensearch.action.ActionListener;
import org.opensearch.core.action.ActionListener;
import org.opensearch.action.LatchedActionListener;
import org.opensearch.action.StepListener;
import org.opensearch.client.Client;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 Aryn
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.ml.conversational.action.memory.conversation;

import org.opensearch.test.OpenSearchTestCase;

public class ActionTests extends OpenSearchTestCase {
public void testActions() {
assert(CreateConversationAction.INSTANCE instanceof CreateConversationAction);
assert(DeleteConversationAction.INSTANCE instanceof DeleteConversationAction);
assert(GetConversationsAction.INSTANCE instanceof GetConversationsAction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 Aryn
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.ml.conversational.action.memory.conversation;

import java.io.IOException;

import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.BytesStreamInput;
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.test.OpenSearchTestCase;

public class CreateConversationResponseTests extends OpenSearchTestCase {

public void testCreateConversationResponseStreaming() throws IOException {
CreateConversationResponse response = new CreateConversationResponse("test-id");
assert(response.getId().equals("test-id"));
BytesStreamOutput outbytes = new BytesStreamOutput();
StreamOutput osso = new OutputStreamStreamOutput(outbytes);
response.writeTo(osso);
StreamInput in = new BytesStreamInput(BytesReference.toBytes(outbytes.bytes()));
CreateConversationResponse newResp = new CreateConversationResponse(in);
assert(newResp.getId().equals("test-id"));
}

public void testToXContent() throws IOException {
CreateConversationResponse response = new CreateConversationResponse("createme");
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
String expected = "{\"conversationId\":\"createme\"}";
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
String result = BytesReference.bytes(builder).utf8ToString();
assert(result.equals(expected));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2023 Aryn
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensearch.ml.conversational.action.memory.conversation;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.List;
import java.util.Map;

import org.mockito.ArgumentCaptor;
import org.opensearch.client.node.NodeClient;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.ml.common.conversational.ActionConstants;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestRequest;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;

public class CreateConversationRestActionTests extends OpenSearchTestCase {
public void testBasics() {
CreateConversationRestAction action = new CreateConversationRestAction();
assert(action.getName().equals("conversational_create_conversation"));
List<Route> routes = action.routes();
assert(routes.size() == 1);
assert(routes.get(0).equals(new Route(RestRequest.Method.POST, ActionConstants.CREATE_CONVERSATION_PATH)));
}

public void testPrepareRequest() throws Exception {
CreateConversationRestAction action = new CreateConversationRestAction();
RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
.withParams(Map.of(ActionConstants.REQUEST_CONVERSATION_NAME_FIELD, "test-name"))
.build();

NodeClient client = mock(NodeClient.class);
RestChannel channel = mock(RestChannel.class);
action.handleRequest(request, channel, client);

ArgumentCaptor<CreateConversationRequest> argumentCaptor = ArgumentCaptor.forClass(CreateConversationRequest.class);
verify(client, times(1)).execute(eq(CreateConversationAction.INSTANCE), argumentCaptor.capture(), any());
assert(argumentCaptor.getValue().getName().equals("test-name"));
}
}
Loading

0 comments on commit d26fab1

Please sign in to comment.