Skip to content

Commit

Permalink
Fix missing lombok version compilation failure issue (opensearch-proj…
Browse files Browse the repository at this point in the history
…ect#1278)

* Fix missing lombok version compilation failure issue

Signed-off-by: zane-neo <[email protected]>
Signed-off-by: Bhavana Ramaram <[email protected]>
  • Loading branch information
zane-neo authored and rbhavna committed Nov 24, 2023
1 parent 1b67485 commit c837c8a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 9 deletions.
4 changes: 4 additions & 0 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ checkstyle {
toolVersion = '10.12.2'
}

lombok {
version = "1.18.28"
}

opensearchplugin {
name 'opensearch-ml'
description 'machine learning plugin for opensearch'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

package org.opensearch.ml.action.connector;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.common.CommonValue.ML_CONNECTOR_INDEX;
import static org.opensearch.ml.utils.MLNodeUtils.createXContentParserFromRegistry;
import static org.opensearch.ml.utils.RestActionUtils.getFetchSourceContext;

import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import lombok.extern.log4j.Log4j2;
import org.opensearch.OpenSearchStatusException;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.get.GetRequest;
Expand All @@ -34,9 +32,10 @@
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import lombok.extern.log4j.Log4j2;
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.ml.common.CommonValue.ML_CONNECTOR_INDEX;
import static org.opensearch.ml.utils.MLNodeUtils.createXContentParserFromRegistry;
import static org.opensearch.ml.utils.RestActionUtils.getFetchSourceContext;

@Log4j2
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class RestActionUtils {
public static final String PARAMETER_ALGORITHM = "algorithm";
public static final String PARAMETER_ASYNC = "async";
public static final String PARAMETER_RETURN_CONTENT = "return_content";
public static final String PARAMETER_MODEL_GROUP_NAME = "model_group_name";
public static final String PARAMETER_MODEL_ID = "model_id";
public static final String PARAMETER_TASK_ID = "task_id";
public static final String PARAMETER_CONNECTOR_ID = "connector_id";
Expand Down
105 changes: 105 additions & 0 deletions plugin/src/test/java/org/opensearch/ml/utils/RestActionUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

package org.opensearch.ml.utils;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_CONNECTOR_ACCESS_CONTROL_ENABLED;
import static org.opensearch.ml.utils.RestActionUtils.OPENSEARCH_DASHBOARDS_USER_AGENT;
import static org.opensearch.ml.utils.RestActionUtils.PARAMETER_ALGORITHM;
import static org.opensearch.ml.utils.RestActionUtils.PARAMETER_ASYNC;
Expand All @@ -15,17 +18,30 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.opensearch.client.Client;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.commons.ConfigConstants;
import org.opensearch.commons.authuser.User;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.ml.common.FunctionName;
import org.opensearch.ml.plugin.MachineLearningPlugin;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestRequest;
import org.opensearch.search.builder.SearchSourceBuilder;
import org.opensearch.search.fetch.subphase.FetchSourceContext;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;
import org.opensearch.threadpool.ThreadPool;

import com.google.common.collect.ImmutableMap;

Expand Down Expand Up @@ -125,4 +141,93 @@ public void testGetSourceContext_FromClient_WithExcludes() {
FetchSourceContext sourceContext = RestActionUtils.getSourceContext(request, testSearchSourceBuilder);
assertEquals(sourceContext.excludes().length, 2);
}

public void test_getSourceContext_fetchSource_null_dashboardUserAgent_null() {
FakeRestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withMethod(RestRequest.Method.POST)
.withPath(urlPath)
.withParams(param)
.build();
SearchSourceBuilder testSearchSourceBuilder = new SearchSourceBuilder();
FetchSourceContext sourceContext = RestActionUtils.getSourceContext(request, testSearchSourceBuilder);
assertNotNull(sourceContext);
}

public void test_getSourceContext_fetchSource_null_dashboardUserAgent_notNull() {
FakeRestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withMethod(RestRequest.Method.POST)
.withPath(urlPath)
.withParams(param)
.withHeaders(ImmutableMap.of("User-Agent", Arrays.asList("OpenSearch Dashboards")))
.build();
SearchSourceBuilder testSearchSourceBuilder = new SearchSourceBuilder();
FetchSourceContext sourceContext = RestActionUtils.getSourceContext(request, testSearchSourceBuilder);
assertNotNull(sourceContext);
}

public void test_getFetchSourceContext_return_modelContent() {
FetchSourceContext result = RestActionUtils.getFetchSourceContext(true);
assertNotNull(result);
}

public void test_getFetchSourceContext_not_return_modelContent() {
FetchSourceContext result = RestActionUtils.getFetchSourceContext(false);
assertNotNull(result);
}

public void test_getAllNodes() {
ClusterService clusterService = mock(ClusterService.class);
ClusterState clusterState = mock(ClusterState.class);
DiscoveryNodes discoveryNodes = mock(DiscoveryNodes.class);
final Map<String, DiscoveryNode> dataNodes = Map.of("dataNodeId", mock(DiscoveryNode.class));
when(discoveryNodes.getDataNodes()).thenReturn(dataNodes);
when(discoveryNodes.getSize()).thenReturn(1);
when(discoveryNodes.iterator()).thenReturn(dataNodes.values().iterator());
when(clusterState.nodes()).thenReturn(discoveryNodes);
when(clusterService.state()).thenReturn(clusterState);
String[] result = RestActionUtils.getAllNodes(clusterService);
assertNotNull(result);
assertEquals(1, result.length);
}

public void test_onFailure() {
fakeRestRequest = createRestRequest(ImmutableMap.<String, String>builder().put(PARAMETER_ALGORITHM, "").build());
RestActionUtils.onFailure(mock(RestChannel.class), RestStatus.CREATED, "error", new IllegalArgumentException("test"));
}

public void test_splitCommaSeparatedParam() {
FakeRestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withMethod(RestRequest.Method.POST)
.withPath(urlPath)
.withParams(param)
.withHeaders(ImmutableMap.of("User-Agent", Arrays.asList("OpenSearch Dashboards")))
.build();
Optional<String[]> result = RestActionUtils.splitCommaSeparatedParam(request, PARAMETER_ALGORITHM);
assertNotNull(result);
assertNotNull(result.get());
}

public void test_getStringParam() {
FakeRestRequest request = new FakeRestRequest.Builder(xContentRegistry())
.withMethod(RestRequest.Method.POST)
.withPath(urlPath)
.withParams(param)
.withHeaders(ImmutableMap.of("User-Agent", Arrays.asList("OpenSearch Dashboards")))
.build();
Optional<String> result = RestActionUtils.getStringParam(request, PARAMETER_ALGORITHM);
assertNotNull(result);
assertNotNull(result.get());
}

public void test_getUserContext() {
Client client = mock(Client.class);
Settings settings = Settings.builder().put(ML_COMMONS_CONNECTOR_ACCESS_CONTROL_ENABLED.getKey(), true).build();
ThreadContext threadContext = new ThreadContext(settings);
threadContext.putTransient(ConfigConstants.OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT, "myuser||myrole");
ThreadPool threadPool = mock(ThreadPool.class);
when(client.threadPool()).thenReturn(threadPool);
when(threadPool.getThreadContext()).thenReturn(threadContext);
User user = RestActionUtils.getUserContext(client);
assertNotNull(user);
}
}

0 comments on commit c837c8a

Please sign in to comment.