From e1bd813efde36d5581b1bcfb1ab0baf8d7c6368a Mon Sep 17 00:00:00 2001 From: HenryL27 Date: Tue, 13 Feb 2024 13:55:13 -0800 Subject: [PATCH] move common feature disablement code to util class Signed-off-by: HenryL27 --- .../opensearch/ml/memory/MemoryTestUtil.java | 41 +++++++++++++++++++ ...reateConversationTransportActionTests.java | 6 +-- ...CreateInteractionTransportActionTests.java | 6 +-- ...eleteConversationTransportActionTests.java | 6 +-- .../GetConversationTransportActionTests.java | 6 +-- .../GetConversationsTransportActionTests.java | 6 +-- .../GetInteractionTransportActionTests.java | 6 +-- .../GetInteractionsTransportActionTests.java | 6 +-- ...archConversationsTransportActionTests.java | 6 +-- ...archInteractionsTransportActionsTests.java | 6 +-- 10 files changed, 59 insertions(+), 36 deletions(-) create mode 100644 memory/src/test/java/org/opensearch/ml/memory/MemoryTestUtil.java diff --git a/memory/src/test/java/org/opensearch/ml/memory/MemoryTestUtil.java b/memory/src/test/java/org/opensearch/ml/memory/MemoryTestUtil.java new file mode 100644 index 0000000000..96cdf8df3c --- /dev/null +++ b/memory/src/test/java/org/opensearch/ml/memory/MemoryTestUtil.java @@ -0,0 +1,41 @@ +/* + * Copyright 2024 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.memory; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Set; + +import org.opensearch.cluster.service.ClusterService; +import org.opensearch.common.settings.ClusterSettings; +import org.opensearch.common.settings.Settings; +import org.opensearch.ml.common.conversation.ConversationalIndexConstants; + +public class MemoryTestUtil { + + public static ClusterService clusterServiceWithMemoryFeatureDisabled() { + ClusterService mockClusterService = mock(ClusterService.class); + Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); + when(mockClusterService.getSettings()).thenReturn(settings); + when(mockClusterService.getClusterSettings()) + .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + return mockClusterService; + } + +} diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateConversationTransportActionTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateConversationTransportActionTests.java index b420c800b1..575e443461 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateConversationTransportActionTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateConversationTransportActionTests.java @@ -41,6 +41,7 @@ import org.opensearch.core.action.ActionListener; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -154,10 +155,7 @@ public void testDoExecuteFails_thenFail() { } public void testFeatureDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new CreateConversationTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener); diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateInteractionTransportActionTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateInteractionTransportActionTests.java index d4cf0a4566..182f4ed79e 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateInteractionTransportActionTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/CreateInteractionTransportActionTests.java @@ -45,6 +45,7 @@ import org.opensearch.core.index.shard.ShardId; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -227,10 +228,7 @@ public void testDoExecuteFails_thenFail() { } public void testFeatureDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new CreateInteractionTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener); diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/DeleteConversationTransportActionTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/DeleteConversationTransportActionTests.java index 00db9155b4..6fd3ecdecc 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/DeleteConversationTransportActionTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/DeleteConversationTransportActionTests.java @@ -40,6 +40,7 @@ import org.opensearch.core.action.ActionListener; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -138,10 +139,7 @@ public void testdoExecuteFails_thenFail() { } public void testFeatureDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new DeleteConversationTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener); diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetConversationTransportActionTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetConversationTransportActionTests.java index 16d24b0c71..bb88a5c1ed 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetConversationTransportActionTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetConversationTransportActionTests.java @@ -43,6 +43,7 @@ import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationMeta; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -138,10 +139,7 @@ public void testHandlerThrows_ThenFail() { } public void testFeatureDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new GetConversationTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener); diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetConversationsTransportActionTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetConversationsTransportActionTests.java index dfe84a954d..f0cd87f0e4 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetConversationsTransportActionTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetConversationsTransportActionTests.java @@ -44,6 +44,7 @@ import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationMeta; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -193,10 +194,7 @@ public void testdoExecuteFails_thenFail() { } public void testFeatureDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new GetConversationsTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener); diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetInteractionTransportActionTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetInteractionTransportActionTests.java index 4c4371536b..8f2878e9e7 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetInteractionTransportActionTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetInteractionTransportActionTests.java @@ -44,6 +44,7 @@ import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; import org.opensearch.ml.common.conversation.Interaction; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -147,10 +148,7 @@ public void testHandlerThrows_ThenFail() { } public void testFeatureDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new GetInteractionTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener); diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetInteractionsTransportActionTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetInteractionsTransportActionTests.java index 67c41aab5f..81f20277f4 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetInteractionsTransportActionTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/GetInteractionsTransportActionTests.java @@ -45,6 +45,7 @@ import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; import org.opensearch.ml.common.conversation.Interaction; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -185,10 +186,7 @@ public void testDoExecuteFails_thenFail() { } public void testFeatureDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new GetInteractionsTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener); diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/SearchConversationsTransportActionTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/SearchConversationsTransportActionTests.java index b1fcc0587f..2a43ed0176 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/SearchConversationsTransportActionTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/SearchConversationsTransportActionTests.java @@ -43,6 +43,7 @@ import org.opensearch.core.action.ActionListener; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -109,10 +110,7 @@ public void testEnabled_ThenSucceed() { } public void testDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new SearchConversationsTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener); diff --git a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/SearchInteractionsTransportActionsTests.java b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/SearchInteractionsTransportActionsTests.java index b19c575b0a..db6c2c20a3 100644 --- a/memory/src/test/java/org/opensearch/ml/memory/action/conversation/SearchInteractionsTransportActionsTests.java +++ b/memory/src/test/java/org/opensearch/ml/memory/action/conversation/SearchInteractionsTransportActionsTests.java @@ -42,6 +42,7 @@ import org.opensearch.core.action.ActionListener; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.ml.common.conversation.ConversationalIndexConstants; +import org.opensearch.ml.memory.MemoryTestUtil; import org.opensearch.ml.memory.index.OpenSearchConversationalMemoryHandler; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ThreadPool; @@ -109,10 +110,7 @@ public void testFeatureEnabled_ThenSucceed() { } public void testDisabled_ThenFail() { - Settings settings = Settings.builder().put(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED.getKey(), false).build(); - when(this.clusterService.getSettings()).thenReturn(settings); - when(this.clusterService.getClusterSettings()) - .thenReturn(new ClusterSettings(settings, Set.of(ConversationalIndexConstants.ML_COMMONS_MEMORY_FEATURE_ENABLED))); + clusterService = MemoryTestUtil.clusterServiceWithMemoryFeatureDisabled(); this.action = spy(new SearchInteractionsTransportAction(transportService, actionFilters, cmHandler, client, clusterService)); action.doExecute(null, request, actionListener);