diff --git a/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/view/ViewIT.java b/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/view/ViewIT.java index 9630c484d27db..aa0eca75d1a7c 100644 --- a/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/view/ViewIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/action/admin/indices/view/ViewIT.java @@ -27,8 +27,8 @@ public class ViewIT extends ViewTestBase { public void testCreateView() throws Exception { - final String viewName = "test-view"; - final String indexPattern = "test-index-*"; + final String viewName = randomAlphaOfLength(8); + final String indexPattern = randomAlphaOfLength(8); logger.info("Testing createView with valid parameters"); final View view = createView(viewName, indexPattern).getView(); @@ -37,58 +37,66 @@ public void testCreateView() throws Exception { MatcherAssert.assertThat(view.getTargets().get(0).getIndexPattern(), is(indexPattern)); logger.info("Testing createView with existing view name"); - final Exception ex = assertThrows(ResourceNotFoundException.class, () -> createView(viewName, "new-pattern")); - MatcherAssert.assertThat(ex.getMessage(), is("View [test-view] already exists")); + final Exception ex = assertThrows(ResourceNotFoundException.class, () -> createView(viewName, randomAlphaOfLength(8))); + MatcherAssert.assertThat(ex.getMessage(), is("View [" + viewName + "] already exists")); } public void testGetView() throws Exception { - final String viewName = "existing-view"; + final String viewName = randomAlphaOfLength(8); + createView(viewName, randomAlphaOfLength(8)); - logger.info("Testing getView with existing view"); - createView(viewName, "index-*"); final View view = getView(viewName).getView(); MatcherAssert.assertThat(view.getName(), is(viewName)); logger.info("Testing getView with non-existent view"); - final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> getView("non-existent")); - MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist")); + final String nonExistentView = "non-existent-" + randomAlphaOfLength(8); + final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> getView(nonExistentView)); + MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist")); } public void testDeleteView() throws Exception { - final String viewName = "deleted-view"; - createView(viewName, "index-*"); + final String viewName = randomAlphaOfLength(8); + createView(viewName, randomAlphaOfLength(8)); logger.info("Testing deleteView with existing view"); deleteView(viewName); final Exception whenDeletedEx = assertThrows(ResourceNotFoundException.class, () -> getView(viewName)); - MatcherAssert.assertThat(whenDeletedEx.getMessage(), is("View [deleted-view] does not exist")); + MatcherAssert.assertThat(whenDeletedEx.getMessage(), is("View [" + viewName + "] does not exist")); logger.info("Testing deleteView with non-existent view"); - final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> deleteView("non-existent")); - MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist")); + final String nonExistentView = "non-existent-" + randomAlphaOfLength(8); + final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> deleteView(nonExistentView)); + MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist")); } public void testUpdateView() throws Exception { - final String viewName = "updatable-view"; - final View originalView = createView(viewName, "index-old-*").getView(); + final String viewName = randomAlphaOfLength(8); + final String originalIndexPattern = randomAlphaOfLength(8); + final View originalView = createView(viewName, originalIndexPattern).getView(); logger.info("Testing updateView with existing view"); - final View updatedView = updateView(viewName, "new description", "index-new-*").getView(); + final String newDescription = randomAlphaOfLength(20); + final String newIndexPattern = "newPattern-" + originalIndexPattern; + final View updatedView = updateView(viewName, newDescription, newIndexPattern).getView(); MatcherAssert.assertThat(updatedView, not(is(originalView))); - MatcherAssert.assertThat(updatedView.getDescription(), is("new description")); + MatcherAssert.assertThat(updatedView.getDescription(), is(newDescription)); MatcherAssert.assertThat(updatedView.getTargets(), hasSize(1)); - MatcherAssert.assertThat(updatedView.getTargets().get(0).getIndexPattern(), is("index-new-*")); + MatcherAssert.assertThat(updatedView.getTargets().get(0).getIndexPattern(), is(newIndexPattern)); logger.info("Testing updateView with non-existent view"); + final String nonExistentView = "non-existent-" + randomAlphaOfLength(8); final Exception whenNeverExistedEx = assertThrows( ResourceNotFoundException.class, - () -> updateView("non-existent", null, "index-*") + () -> updateView(nonExistentView, null, "index-*") ); - MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist")); + MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist")); } public void testListViewNames() throws Exception { + logger.info("Testing listViewNames when no views have been created"); + MatcherAssert.assertThat(listViewNames(), is(List.of())); + final String view1 = "view1"; final String view2 = "view2"; createView(view1, "index-1-*"); @@ -128,8 +136,8 @@ public void testSearchOperations() throws Exception { assertHitCount(searchView("both-indices"), indexInView1DocCount + indexInView2DocCount); logger.info("Testing searchView with non-existent view"); - final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> searchView("non-existent")); - MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist")); - + final String nonExistentView = "non-existent-" + randomAlphaOfLength(8); + final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> searchView(nonExistentView)); + MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist")); } } diff --git a/server/src/main/java/org/opensearch/action/admin/indices/view/DeleteViewAction.java b/server/src/main/java/org/opensearch/action/admin/indices/view/DeleteViewAction.java index 9b0adebcfaee4..abb3c3f4db5f6 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/view/DeleteViewAction.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/view/DeleteViewAction.java @@ -69,7 +69,7 @@ public String getName() { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Request that = (Request) o; + final Request that = (Request) o; return name.equals(that.name); } diff --git a/server/src/main/java/org/opensearch/action/admin/indices/view/GetViewAction.java b/server/src/main/java/org/opensearch/action/admin/indices/view/GetViewAction.java index 330646837b9e2..762eea965c8c1 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/view/GetViewAction.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/view/GetViewAction.java @@ -72,7 +72,7 @@ public String getName() { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Request that = (Request) o; + final Request that = (Request) o; return name.equals(that.name); } @@ -131,6 +131,19 @@ public View getView() { return view; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Response that = (Response) o; + return getView().equals(that.getView()); + } + + @Override + public int hashCode() { + return Objects.hash(getView()); + } + @Override public void writeTo(final StreamOutput out) throws IOException { this.view.writeTo(out); diff --git a/server/src/main/java/org/opensearch/action/admin/indices/view/ListViewNamesAction.java b/server/src/main/java/org/opensearch/action/admin/indices/view/ListViewNamesAction.java index b2c9ac1fe857c..8341d102a10ea 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/view/ListViewNamesAction.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/view/ListViewNamesAction.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.util.List; +import java.util.Objects; /** Action to list a view names */ @ExperimentalApi @@ -45,6 +46,19 @@ public Request() {} public Request(final StreamInput in) {} + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Request that = (Request) o; + return true; + } + + @Override + public int hashCode() { + return 1; + } + @Override public ActionRequestValidationException validate() { return null; @@ -69,6 +83,19 @@ public List getViewNames() { return views; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Request that = (Request) o; + return views.equals(that.views); + } + + @Override + public int hashCode() { + return Objects.hash(views); + } + @Override public void writeTo(final StreamOutput out) throws IOException { out.writeStringCollection(views); diff --git a/server/src/test/java/org/opensearch/action/admin/indices/view/CreateViewTests.java b/server/src/test/java/org/opensearch/action/admin/indices/view/CreateViewRequestTests.java similarity index 75% rename from server/src/test/java/org/opensearch/action/admin/indices/view/CreateViewTests.java rename to server/src/test/java/org/opensearch/action/admin/indices/view/CreateViewRequestTests.java index 7b4d29d7fb839..edb9f398debbe 100644 --- a/server/src/test/java/org/opensearch/action/admin/indices/view/CreateViewTests.java +++ b/server/src/test/java/org/opensearch/action/admin/indices/view/CreateViewRequestTests.java @@ -11,12 +11,14 @@ import org.opensearch.action.ActionRequestValidationException; import org.opensearch.core.common.io.stream.Writeable; import org.opensearch.test.AbstractWireSerializingTestCase; +import org.hamcrest.MatcherAssert; import java.util.List; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.nullValue; -public class CreateViewTests extends AbstractWireSerializingTestCase { +public class CreateViewRequestTests extends AbstractWireSerializingTestCase { @Override protected Writeable.Reader instanceReader() { @@ -39,14 +41,14 @@ public void testValidateRequest() { List.of(new CreateViewAction.Request.Target("my-indices-*")) ); - assertNull(request.validate()); + MatcherAssert.assertThat(request.validate(), nullValue()); } public void testValidateRequestWithoutName() { final CreateViewAction.Request request = new CreateViewAction.Request("", null, null); - ActionRequestValidationException e = request.validate(); + final ActionRequestValidationException e = request.validate(); - assertThat(e.validationErrors(), contains("name cannot be empty or null", "targets cannot be empty")); + MatcherAssert.assertThat(e.validationErrors(), contains("name cannot be empty or null", "targets cannot be empty")); } } diff --git a/server/src/test/java/org/opensearch/action/admin/indices/view/DeleteViewRequestTests.java b/server/src/test/java/org/opensearch/action/admin/indices/view/DeleteViewRequestTests.java new file mode 100644 index 0000000000000..29305e3dfb92f --- /dev/null +++ b/server/src/test/java/org/opensearch/action/admin/indices/view/DeleteViewRequestTests.java @@ -0,0 +1,44 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.action.admin.indices.view; + +import org.opensearch.action.ActionRequestValidationException; +import org.opensearch.core.common.io.stream.Writeable; +import org.opensearch.test.AbstractWireSerializingTestCase; +import org.hamcrest.MatcherAssert; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.nullValue; + +public class DeleteViewRequestTests extends AbstractWireSerializingTestCase { + + @Override + protected Writeable.Reader instanceReader() { + return DeleteViewAction.Request::new; + } + + @Override + protected DeleteViewAction.Request createTestInstance() { + return new DeleteViewAction.Request(randomAlphaOfLength(8)); + } + + public void testValidateRequest() { + final DeleteViewAction.Request request = new DeleteViewAction.Request("my-view"); + + MatcherAssert.assertThat(request.validate(), nullValue()); + } + + public void testValidateRequestWithoutName() { + final DeleteViewAction.Request request = new DeleteViewAction.Request(""); + final ActionRequestValidationException e = request.validate(); + + MatcherAssert.assertThat(e.validationErrors(), contains("name cannot be empty or null")); + } + +} diff --git a/server/src/test/java/org/opensearch/action/admin/indices/view/GetViewResponseTests.java b/server/src/test/java/org/opensearch/action/admin/indices/view/GetViewResponseTests.java new file mode 100644 index 0000000000000..4dd075597a804 --- /dev/null +++ b/server/src/test/java/org/opensearch/action/admin/indices/view/GetViewResponseTests.java @@ -0,0 +1,34 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.action.admin.indices.view; + +import org.opensearch.cluster.metadata.View; +import org.opensearch.core.common.io.stream.Writeable; +import org.opensearch.test.AbstractWireSerializingTestCase; + +public class GetViewResponseTests extends AbstractWireSerializingTestCase { + + @Override + protected Writeable.Reader instanceReader() { + return GetViewAction.Response::new; + } + + @Override + protected GetViewAction.Response createTestInstance() { + return new GetViewAction.Response( + new View( + randomAlphaOfLength(8), + randomAlphaOfLength(8), + randomLong(), + randomLong(), + randomList(5, () -> new View.Target(randomAlphaOfLength(8))) + ) + ); + } +} diff --git a/server/src/test/java/org/opensearch/action/admin/indices/view/ListViewNamesRequestTests.java b/server/src/test/java/org/opensearch/action/admin/indices/view/ListViewNamesRequestTests.java new file mode 100644 index 0000000000000..80a2827d158bb --- /dev/null +++ b/server/src/test/java/org/opensearch/action/admin/indices/view/ListViewNamesRequestTests.java @@ -0,0 +1,35 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.action.admin.indices.view; + +import org.opensearch.core.common.io.stream.Writeable; +import org.opensearch.test.AbstractWireSerializingTestCase; +import org.hamcrest.MatcherAssert; + +import static org.hamcrest.Matchers.nullValue; + +public class ListViewNamesRequestTests extends AbstractWireSerializingTestCase { + + @Override + protected Writeable.Reader instanceReader() { + return ListViewNamesAction.Request::new; + } + + @Override + protected ListViewNamesAction.Request createTestInstance() { + return new ListViewNamesAction.Request(); + } + + public void testValidateRequest() { + final ListViewNamesAction.Request request = new ListViewNamesAction.Request(); + + MatcherAssert.assertThat(request.validate(), nullValue()); + } + +} diff --git a/server/src/test/java/org/opensearch/action/admin/indices/view/ListViewNamesResponseTests.java b/server/src/test/java/org/opensearch/action/admin/indices/view/ListViewNamesResponseTests.java new file mode 100644 index 0000000000000..ee8409fe3c805 --- /dev/null +++ b/server/src/test/java/org/opensearch/action/admin/indices/view/ListViewNamesResponseTests.java @@ -0,0 +1,25 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.action.admin.indices.view; + +import org.opensearch.core.common.io.stream.Writeable; +import org.opensearch.test.AbstractWireSerializingTestCase; + +public class ListViewNamesResponseTests extends AbstractWireSerializingTestCase { + + @Override + protected Writeable.Reader instanceReader() { + return ListViewNamesAction.Response::new; + } + + @Override + protected ListViewNamesAction.Response createTestInstance() { + return new ListViewNamesAction.Response(randomList(5, () -> randomAlphaOfLength(8))); + } +} diff --git a/server/src/test/java/org/opensearch/action/admin/indices/view/SearchViewTests.java b/server/src/test/java/org/opensearch/action/admin/indices/view/SearchViewRequestTests.java similarity index 73% rename from server/src/test/java/org/opensearch/action/admin/indices/view/SearchViewTests.java rename to server/src/test/java/org/opensearch/action/admin/indices/view/SearchViewRequestTests.java index e07104cbb6ad6..c6d2fcc19563e 100644 --- a/server/src/test/java/org/opensearch/action/admin/indices/view/SearchViewTests.java +++ b/server/src/test/java/org/opensearch/action/admin/indices/view/SearchViewRequestTests.java @@ -12,13 +12,15 @@ import org.opensearch.action.search.SearchRequest; import org.opensearch.core.common.io.stream.Writeable; import org.opensearch.test.AbstractWireSerializingTestCase; +import org.hamcrest.MatcherAssert; import java.io.IOException; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; -public class SearchViewTests extends AbstractWireSerializingTestCase { +public class SearchViewRequestTests extends AbstractWireSerializingTestCase { @Override protected Writeable.Reader instanceReader() { @@ -36,15 +38,15 @@ protected SearchViewAction.Request createTestInstance() { public void testValidateRequest() throws IOException { final SearchViewAction.Request request = SearchViewAction.createRequestWith("my-view", new SearchRequest()); - assertNull(request.validate()); + MatcherAssert.assertThat(request.validate(), nullValue()); } public void testValidateRequestWithoutName() { final SearchViewAction.Request request = new SearchViewAction.Request((String) null); - ActionRequestValidationException e = request.validate(); - assertNotNull(e); - assertThat(e.validationErrors().size(), equalTo(1)); - assertThat(e.validationErrors().get(0), containsString("View is required")); + final ActionRequestValidationException e = request.validate(); + + MatcherAssert.assertThat(e.validationErrors().size(), equalTo(1)); + MatcherAssert.assertThat(e.validationErrors().get(0), containsString("View is required")); } } diff --git a/server/src/test/java/org/opensearch/action/admin/indices/view/ViewServiceTest.java b/server/src/test/java/org/opensearch/action/admin/indices/view/ViewServiceTests.java similarity index 81% rename from server/src/test/java/org/opensearch/action/admin/indices/view/ViewServiceTest.java rename to server/src/test/java/org/opensearch/action/admin/indices/view/ViewServiceTests.java index 93fa70b2981ac..45b48f5f9dec5 100644 --- a/server/src/test/java/org/opensearch/action/admin/indices/view/ViewServiceTest.java +++ b/server/src/test/java/org/opensearch/action/admin/indices/view/ViewServiceTests.java @@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.function.LongSupplier; +import static org.opensearch.test.OpenSearchTestCase.randomAlphaOfLength; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThrows; import static org.mockito.Mockito.any; @@ -40,10 +41,16 @@ import static org.mockito.Mockito.when; @SuppressWarnings("unchecked") -public class ViewServiceTest { +public class ViewServiceTests { - private final View.Target typicalTarget = new View.Target("my-index-*"); - private final View typicalView = new View("actualView", "actual description", -1L, -1L, List.of(typicalTarget)); + private final View.Target typicalTarget = new View.Target(randomAlphaOfLength(8)); + private final View typicalView = new View( + "view-" + randomAlphaOfLength(8), + "description " + randomAlphaOfLength(20), + -1L, + -1L, + List.of(typicalTarget) + ); private ClusterService clusterService; private NodeClient nodeClient; @@ -65,8 +72,16 @@ public void after() { verifyNoMoreInteractions(timeProvider, clusterService, nodeClient); } + private CreateViewAction.Request createTypicalViewRequest() { + return new CreateViewAction.Request( + randomAlphaOfLength(8), + randomAlphaOfLength(20), + List.of(new CreateViewAction.Request.Target(randomAlphaOfLength(8))) + ); + } + public void createView() { - final var request = new CreateViewAction.Request("a", "b", List.of(new CreateViewAction.Request.Target("my-index-*"))); + final var request = createTypicalViewRequest(); final var listener = mock(ActionListener.class); setGetViewOrThrowExceptionToReturnTypicalView(); @@ -77,7 +92,7 @@ public void createView() { } public void updateView() { - final var request = new CreateViewAction.Request("a", "b", List.of(new CreateViewAction.Request.Target("my-index-*"))); + final var request = createTypicalViewRequest(); final var listener = mock(ActionListener.class); setGetViewOrThrowExceptionToReturnTypicalView(); @@ -88,7 +103,7 @@ public void updateView() { } public void updateView_doesNotExist() { - final var request = new CreateViewAction.Request("a", "b", List.of(new CreateViewAction.Request.Target("my-index-*"))); + final var request = createTypicalViewRequest(); final var listener = mock(ActionListener.class); doThrow(new ResourceNotFoundException("abc")).when(viewService).getViewOrThrowException(anyString()); @@ -97,7 +112,7 @@ public void updateView_doesNotExist() { } public void deleteView() { - final var request = new DeleteViewAction.Request("viewName"); + final var request = new DeleteViewAction.Request(randomAlphaOfLength(8)); final var listener = mock(ActionListener.class); setGetViewOrThrowExceptionToReturnTypicalView(); @@ -107,7 +122,7 @@ public void deleteView() { } public void deleteView_doesNotExist() { - final var request = new DeleteViewAction.Request("viewName"); + final var request = new DeleteViewAction.Request(randomAlphaOfLength(8)); final var listener = mock(ActionListener.class); doThrow(new ResourceNotFoundException("abc")).when(viewService).getViewOrThrowException(anyString()); @@ -117,7 +132,7 @@ public void deleteView_doesNotExist() { } public void getView() { - final var request = new GetViewAction.Request("viewName"); + final var request = new GetViewAction.Request(randomAlphaOfLength(8)); final var listener = mock(ActionListener.class); setGetViewOrThrowExceptionToReturnTypicalView(); @@ -127,7 +142,7 @@ public void getView() { } public void getView_doesNotExist() { - final var request = new GetViewAction.Request("viewName"); + final var request = new GetViewAction.Request(randomAlphaOfLength(8)); final var listener = mock(ActionListener.class); doThrow(new ResourceNotFoundException("abc")).when(viewService).getViewOrThrowException(anyString()); @@ -137,7 +152,7 @@ public void getView_doesNotExist() { } public void listViewNames() { - final var clusterState = new ClusterState.Builder(new ClusterName("MyCluster")).metadata( + final var clusterState = new ClusterState.Builder(new ClusterName(randomAlphaOfLength(8))).metadata( new Metadata.Builder().views(Map.of(typicalView.getName(), typicalView)).build() ).build(); final var listener = mock(ActionListener.class); @@ -150,7 +165,7 @@ public void listViewNames() { } public void listViewNames_noViews() { - final var clusterState = new ClusterState.Builder(new ClusterName("MyCluster")).build(); + final var clusterState = new ClusterState.Builder(new ClusterName(randomAlphaOfLength(8))).build(); final var listener = mock(ActionListener.class); when(clusterService.state()).thenReturn(clusterState); @@ -161,7 +176,7 @@ public void listViewNames_noViews() { } public void searchView() { - final var request = spy(new SearchViewAction.Request("view")); + final var request = spy(new SearchViewAction.Request(randomAlphaOfLength(8))); final var listener = mock(ActionListener.class); setGetViewOrThrowExceptionToReturnTypicalView();