|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.action.admin.indices.view; |
| 10 | + |
| 11 | +import org.opensearch.cluster.metadata.View; |
| 12 | +import org.opensearch.index.IndexNotFoundException; |
| 13 | +import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; |
| 14 | +import org.opensearch.test.OpenSearchIntegTestCase.Scope; |
| 15 | +import org.hamcrest.MatcherAssert; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; |
| 20 | +import static org.hamcrest.Matchers.contains; |
| 21 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 22 | +import static org.hamcrest.Matchers.hasSize; |
| 23 | +import static org.hamcrest.Matchers.is; |
| 24 | +import static org.hamcrest.Matchers.not; |
| 25 | + |
| 26 | +@ClusterScope(scope = Scope.TEST, numDataNodes = 2) |
| 27 | +public class ViewIT extends ViewTestBase { |
| 28 | + |
| 29 | + public void testCreateView() throws Exception { |
| 30 | + final String viewName = randomAlphaOfLength(8); |
| 31 | + final String indexPattern = randomAlphaOfLength(8); |
| 32 | + |
| 33 | + logger.info("Testing createView with valid parameters"); |
| 34 | + final View view = createView(viewName, indexPattern).getView(); |
| 35 | + MatcherAssert.assertThat(view.getName(), is(viewName)); |
| 36 | + MatcherAssert.assertThat(view.getTargets().size(), is(1)); |
| 37 | + MatcherAssert.assertThat(view.getTargets().first().getIndexPattern(), is(indexPattern)); |
| 38 | + |
| 39 | + logger.info("Testing createView with existing view name"); |
| 40 | + final Exception ex = assertThrows(ViewAlreadyExistsException.class, () -> createView(viewName, randomAlphaOfLength(8))); |
| 41 | + MatcherAssert.assertThat(ex.getMessage(), is("View [" + viewName + "] already exists")); |
| 42 | + } |
| 43 | + |
| 44 | + public void testCreateViewTargetsSet() throws Exception { |
| 45 | + final String viewName = randomAlphaOfLength(8); |
| 46 | + final String indexPattern = "a" + randomAlphaOfLength(8); |
| 47 | + final String indexPattern2 = "b" + randomAlphaOfLength(8); |
| 48 | + final List<String> targetPatterns = List.of(indexPattern2, indexPattern, indexPattern); |
| 49 | + |
| 50 | + logger.info("Testing createView with targets that will be reordered and deduplicated"); |
| 51 | + final View view = createView(viewName, targetPatterns).getView(); |
| 52 | + MatcherAssert.assertThat(view.getName(), is(viewName)); |
| 53 | + MatcherAssert.assertThat(view.getTargets().size(), is(2)); |
| 54 | + MatcherAssert.assertThat(view.getTargets().first().getIndexPattern(), is(indexPattern)); |
| 55 | + MatcherAssert.assertThat(view.getTargets().last().getIndexPattern(), is(indexPattern2)); |
| 56 | + } |
| 57 | + |
| 58 | + public void testGetView() throws Exception { |
| 59 | + final String viewName = randomAlphaOfLength(8); |
| 60 | + createView(viewName, randomAlphaOfLength(8)); |
| 61 | + |
| 62 | + final View view = getView(viewName).getView(); |
| 63 | + MatcherAssert.assertThat(view.getName(), is(viewName)); |
| 64 | + |
| 65 | + logger.info("Testing getView with non-existent view"); |
| 66 | + final String nonExistentView = "non-existent-" + randomAlphaOfLength(8); |
| 67 | + final Exception whenNeverExistedEx = assertThrows(ViewNotFoundException.class, () -> getView(nonExistentView)); |
| 68 | + MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist")); |
| 69 | + } |
| 70 | + |
| 71 | + public void testDeleteView() throws Exception { |
| 72 | + final String viewName = randomAlphaOfLength(8); |
| 73 | + createView(viewName, randomAlphaOfLength(8)); |
| 74 | + |
| 75 | + logger.info("Testing deleteView with existing view"); |
| 76 | + deleteView(viewName); |
| 77 | + final Exception whenDeletedEx = assertThrows(ViewNotFoundException.class, () -> getView(viewName)); |
| 78 | + MatcherAssert.assertThat(whenDeletedEx.getMessage(), is("View [" + viewName + "] does not exist")); |
| 79 | + |
| 80 | + logger.info("Testing deleteView with non-existent view"); |
| 81 | + final String nonExistentView = "non-existent-" + randomAlphaOfLength(8); |
| 82 | + final Exception whenNeverExistedEx = assertThrows(ViewNotFoundException.class, () -> deleteView(nonExistentView)); |
| 83 | + MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist")); |
| 84 | + } |
| 85 | + |
| 86 | + public void testUpdateView() throws Exception { |
| 87 | + final String viewName = randomAlphaOfLength(8); |
| 88 | + final String originalIndexPattern = randomAlphaOfLength(8); |
| 89 | + final View originalView = createView(viewName, originalIndexPattern).getView(); |
| 90 | + |
| 91 | + logger.info("Testing updateView with existing view"); |
| 92 | + final String newDescription = randomAlphaOfLength(20); |
| 93 | + final String newIndexPattern = "newPattern-" + originalIndexPattern; |
| 94 | + final View updatedView = updateView(viewName, newDescription, newIndexPattern).getView(); |
| 95 | + |
| 96 | + MatcherAssert.assertThat(updatedView, not(is(originalView))); |
| 97 | + MatcherAssert.assertThat(updatedView.getDescription(), is(newDescription)); |
| 98 | + MatcherAssert.assertThat(updatedView.getTargets(), hasSize(1)); |
| 99 | + MatcherAssert.assertThat(updatedView.getTargets().first().getIndexPattern(), is(newIndexPattern)); |
| 100 | + |
| 101 | + logger.info("Testing updateView with non-existent view"); |
| 102 | + final String nonExistentView = "non-existent-" + randomAlphaOfLength(8); |
| 103 | + final Exception whenNeverExistedEx = assertThrows(ViewNotFoundException.class, () -> updateView(nonExistentView, null, "index-*")); |
| 104 | + MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist")); |
| 105 | + } |
| 106 | + |
| 107 | + public void testListViewNames() throws Exception { |
| 108 | + logger.info("Testing listViewNames when no views have been created"); |
| 109 | + MatcherAssert.assertThat(listViewNames(), is(List.of())); |
| 110 | + |
| 111 | + final String view1 = "view1"; |
| 112 | + final String view2 = "view2"; |
| 113 | + createView(view1, "index-1-*"); |
| 114 | + createView(view2, "index-2-*"); |
| 115 | + |
| 116 | + logger.info("Testing listViewNames"); |
| 117 | + final List<String> views = listViewNames(); |
| 118 | + MatcherAssert.assertThat(views, containsInAnyOrder(view1, view2)); |
| 119 | + |
| 120 | + logger.info("Testing listViewNames after deleting a view"); |
| 121 | + deleteView(view1); |
| 122 | + final List<String> viewsAfterDeletion = listViewNames(); |
| 123 | + MatcherAssert.assertThat(viewsAfterDeletion, not(contains(view1))); |
| 124 | + MatcherAssert.assertThat(viewsAfterDeletion, contains(view2)); |
| 125 | + } |
| 126 | + |
| 127 | + public void testSearchOperations() throws Exception { |
| 128 | + final String indexInView1 = "index-1"; |
| 129 | + final String indexInView2 = "index-2"; |
| 130 | + final String indexNotInView = "another-index-1"; |
| 131 | + |
| 132 | + final int indexInView1DocCount = createIndexWithDocs(indexInView1); |
| 133 | + final int indexInView2DocCount = createIndexWithDocs(indexInView2); |
| 134 | + createIndexWithDocs(indexNotInView); |
| 135 | + |
| 136 | + logger.info("Testing view with no matches"); |
| 137 | + createView("no-matches", "this-pattern-will-match-nothing"); |
| 138 | + final Exception ex = assertThrows(IndexNotFoundException.class, () -> searchView("no-matches")); |
| 139 | + MatcherAssert.assertThat(ex.getMessage(), is("no such index [this-pattern-will-match-nothing]")); |
| 140 | + |
| 141 | + logger.info("Testing view with exact index match"); |
| 142 | + createView("only-index-1", "index-1"); |
| 143 | + assertHitCount(searchView("only-index-1"), indexInView1DocCount); |
| 144 | + |
| 145 | + logger.info("Testing view with wildcard matches"); |
| 146 | + createView("both-indices", "index-*"); |
| 147 | + assertHitCount(searchView("both-indices"), indexInView1DocCount + indexInView2DocCount); |
| 148 | + |
| 149 | + logger.info("Testing searchView with non-existent view"); |
| 150 | + final String nonExistentView = "non-existent-" + randomAlphaOfLength(8); |
| 151 | + final Exception whenNeverExistedEx = assertThrows(ViewNotFoundException.class, () -> searchView(nonExistentView)); |
| 152 | + MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist")); |
| 153 | + } |
| 154 | +} |
0 commit comments