Skip to content

Commit 292a322

Browse files
committed
Round out integration tests
Signed-off-by: Peter Nied <petern@amazon.com>
1 parent 01655d0 commit 292a322

File tree

6 files changed

+163
-68
lines changed

6 files changed

+163
-68
lines changed

server/src/internalClusterTest/java/org/opensearch/action/admin/indices/view/ViewIT.java

+78-60
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88

99
package org.opensearch.action.admin.indices.view;
1010

11-
import org.opensearch.action.search.SearchRequest;
12-
import org.opensearch.action.search.SearchResponse;
13-
import org.opensearch.index.IndexNotFoundException;
14-
import org.opensearch.test.BackgroundIndexer;
15-
import org.opensearch.test.OpenSearchIntegTestCase;
11+
import org.opensearch.ResourceNotFoundException;
12+
import org.opensearch.cluster.metadata.View;
1613
import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope;
1714
import org.opensearch.test.OpenSearchIntegTestCase.Scope;
1815
import org.hamcrest.MatcherAssert;
@@ -21,52 +18,94 @@
2118

2219
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount;
2320
import static org.hamcrest.Matchers.contains;
21+
import static org.hamcrest.Matchers.containsInAnyOrder;
22+
import static org.hamcrest.Matchers.hasSize;
2423
import static org.hamcrest.Matchers.is;
24+
import static org.hamcrest.Matchers.not;
2525

2626
@ClusterScope(scope = Scope.TEST, numDataNodes = 2)
27-
public class ViewIT extends OpenSearchIntegTestCase {
27+
public class ViewIT extends ViewTestBase {
2828

29-
private int createIndexWithDocs(final String indexName) throws Exception {
30-
createIndex(indexName);
31-
ensureGreen(indexName);
29+
public void testCreateView() throws Exception {
30+
final String viewName = "test-view";
31+
final String indexPattern = "test-index-*";
3232

33-
final int numOfDocs = scaledRandomIntBetween(0, 200);
34-
try (final BackgroundIndexer indexer = new BackgroundIndexer(indexName, "_doc", client(), numOfDocs)) {
35-
waitForDocs(numOfDocs, indexer);
36-
}
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().get(0).getIndexPattern(), is(indexPattern));
3738

38-
refresh(indexName);
39-
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), numOfDocs);
40-
return numOfDocs;
39+
logger.info("Testing createView with existing view name");
40+
final Exception ex = assertThrows(ResourceNotFoundException.class, () -> createView(viewName, "new-pattern"));
41+
MatcherAssert.assertThat(ex.getMessage(), is("View [test-view] already exists"));
4142
}
4243

43-
private GetViewAction.Response createView(final String name, final String indexPattern) throws Exception {
44-
final CreateViewAction.Request request = new CreateViewAction.Request(
45-
name,
46-
null,
47-
List.of(new CreateViewAction.Request.Target(indexPattern))
48-
);
49-
final GetViewAction.Response response = client().admin().indices().createView(request).actionGet();
50-
performRemoteStoreTestAction();
51-
return response;
44+
public void testGetView() throws Exception {
45+
final String viewName = "existing-view";
46+
47+
logger.info("Testing getView with existing view");
48+
createView(viewName, "index-*");
49+
final View view = getView(viewName).getView();
50+
MatcherAssert.assertThat(view.getName(), is(viewName));
51+
52+
logger.info("Testing getView with non-existent view");
53+
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> getView("non-existent"));
54+
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist"));
5255
}
5356

54-
private void deleteView(final String name) {
55-
client().admin().indices().deleteView(new DeleteViewAction.Request(name)).actionGet();
56-
performRemoteStoreTestAction();
57+
public void testDeleteView() throws Exception {
58+
final String viewName = "deleted-view";
59+
createView(viewName, "index-*");
60+
61+
logger.info("Testing deleteView with existing view");
62+
deleteView(viewName);
63+
final Exception whenDeletedEx = assertThrows(ResourceNotFoundException.class, () -> getView(viewName));
64+
MatcherAssert.assertThat(whenDeletedEx.getMessage(), is("View [deleted-view] does not exist"));
65+
66+
logger.info("Testing deleteView with non-existent view");
67+
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> deleteView("non-existent"));
68+
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist"));
5769
}
5870

59-
private List<String> listViews() {
60-
return client().listViewNames(new ListViewNamesAction.Request()).actionGet().getViewNames();
71+
public void testUpdateView() throws Exception {
72+
final String viewName = "updatable-view";
73+
final View originalView = createView(viewName, "index-old-*").getView();
74+
75+
logger.info("Testing updateView with existing view");
76+
final View updatedView = updateView(viewName, "new description", "index-new-*").getView();
77+
78+
MatcherAssert.assertThat(updatedView, not(is(originalView)));
79+
MatcherAssert.assertThat(updatedView.getDescription(), is("new description"));
80+
MatcherAssert.assertThat(updatedView.getTargets(), hasSize(1));
81+
MatcherAssert.assertThat(updatedView.getTargets().get(0).getIndexPattern(), is("index-new-*"));
82+
83+
logger.info("Testing updateView with non-existent view");
84+
final Exception whenNeverExistedEx = assertThrows(
85+
ResourceNotFoundException.class,
86+
() -> updateView("non-existent", null, "index-*")
87+
);
88+
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist"));
6189
}
6290

63-
private SearchResponse searchView(final String viewName) throws Exception {
64-
final SearchViewAction.Request request = SearchViewAction.createRequestWith(viewName, new SearchRequest());
65-
final SearchResponse response = client().searchView(request).actionGet();
66-
return response;
91+
public void testListViewNames() throws Exception {
92+
final String view1 = "view1";
93+
final String view2 = "view2";
94+
createView(view1, "index-1-*");
95+
createView(view2, "index-2-*");
96+
97+
logger.info("Testing listViewNames");
98+
final List<String> views = listViewNames();
99+
MatcherAssert.assertThat(views, containsInAnyOrder(view1, view2));
100+
101+
logger.info("Testing listViewNames after deleting a view");
102+
deleteView(view1);
103+
final List<String> viewsAfterDeletion = listViewNames();
104+
MatcherAssert.assertThat(viewsAfterDeletion, not(contains(view1)));
105+
MatcherAssert.assertThat(viewsAfterDeletion, contains(view2));
67106
}
68107

69-
public void testBasicOperations() throws Exception {
108+
public void testSearchOperations() throws Exception {
70109
final String indexInView1 = "index-1";
71110
final String indexInView2 = "index-2";
72111
final String indexNotInView = "another-index-1";
@@ -77,7 +116,7 @@ public void testBasicOperations() throws Exception {
77116

78117
logger.info("Testing view with no matches");
79118
createView("no-matches", "this-pattern-will-match-nothing");
80-
final IndexNotFoundException ex = assertThrows(IndexNotFoundException.class, () -> searchView("no-matches"));
119+
final Exception ex = assertThrows(ResourceNotFoundException.class, () -> searchView("no-matches"));
81120
MatcherAssert.assertThat(ex.getMessage(), is("no such index [this-pattern-will-match-nothing]"));
82121

83122
logger.info("Testing view with exact index match");
@@ -87,31 +126,10 @@ public void testBasicOperations() throws Exception {
87126
logger.info("Testing view with wildcard matches");
88127
createView("both-indices", "index-*");
89128
assertHitCount(searchView("both-indices"), indexInView1DocCount + indexInView2DocCount);
90-
}
91-
92-
public void testListViewNames() throws Exception {
93-
logger.info("Create a single view");
94-
createView("view1", "*");
95-
final List<String> viewNames1 = listViews();
96-
97-
MatcherAssert.assertThat(viewNames1, contains("view1"));
98-
99-
logger.info("Create a second view");
100-
createView("view2", "*");
101-
final List<String> viewNames2 = listViews();
102-
103-
MatcherAssert.assertThat(viewNames2, contains("view1", "view2"));
104-
105-
logger.info("Delete a view");
106-
deleteView("view1");
107-
final List<String> viewNamesAfterDelete = listViews();
108-
109-
MatcherAssert.assertThat(viewNamesAfterDelete, contains("view2"));
110129

111-
logger.info("Update a view");
112-
client().admin().indices().updateView(new CreateViewAction.Request("view2", "newDescription", List.of()));
113-
final List<String> viewNamesAfterUpdate = listViews();
130+
logger.info("Testing searchView with non-existent view");
131+
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> searchView("non-existent"));
132+
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist"));
114133

115-
MatcherAssert.assertThat(viewNamesAfterUpdate, contains("view2"));
116134
}
117135
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.action.search.SearchRequest;
12+
import org.opensearch.action.search.SearchResponse;
13+
import org.opensearch.test.BackgroundIndexer;
14+
import org.opensearch.test.OpenSearchIntegTestCase;
15+
16+
import java.util.List;
17+
18+
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount;
19+
20+
public abstract class ViewTestBase extends OpenSearchIntegTestCase {
21+
22+
protected int createIndexWithDocs(final String indexName) throws Exception {
23+
createIndex(indexName);
24+
ensureGreen(indexName);
25+
26+
final int numOfDocs = scaledRandomIntBetween(0, 200);
27+
try (final BackgroundIndexer indexer = new BackgroundIndexer(indexName, "_doc", client(), numOfDocs)) {
28+
waitForDocs(numOfDocs, indexer);
29+
}
30+
31+
refresh(indexName);
32+
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), numOfDocs);
33+
return numOfDocs;
34+
}
35+
36+
protected GetViewAction.Response createView(final String name, final String indexPattern) throws Exception {
37+
final CreateViewAction.Request request = new CreateViewAction.Request(
38+
name,
39+
null,
40+
List.of(new CreateViewAction.Request.Target(indexPattern))
41+
);
42+
return client().admin().indices().createView(request).actionGet();
43+
}
44+
45+
protected GetViewAction.Response getView(final String name) {
46+
return client().admin().indices().getView(new GetViewAction.Request(name)).actionGet();
47+
48+
}
49+
50+
protected void deleteView(final String name) {
51+
client().admin().indices().deleteView(new DeleteViewAction.Request(name)).actionGet();
52+
performRemoteStoreTestAction();
53+
}
54+
55+
protected List<String> listViewNames() {
56+
return client().listViewNames(new ListViewNamesAction.Request()).actionGet().getViewNames();
57+
}
58+
59+
protected SearchResponse searchView(final String viewName) throws Exception {
60+
final SearchViewAction.Request request = SearchViewAction.createRequestWith(viewName, new SearchRequest());
61+
final SearchResponse response = client().searchView(request).actionGet();
62+
return response;
63+
}
64+
65+
protected GetViewAction.Response updateView(final String name, final String description, final String indexPattern) {
66+
final CreateViewAction.Request request = new CreateViewAction.Request(
67+
name,
68+
description,
69+
List.of(new CreateViewAction.Request.Target(indexPattern))
70+
);
71+
final GetViewAction.Response response = client().admin().indices().updateView(request).actionGet();
72+
return response;
73+
}
74+
}

server/src/main/java/org/opensearch/action/admin/indices/view/GetViewAction.java

+4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public Response(final StreamInput in) throws IOException {
127127
this.view = new View(in);
128128
}
129129

130+
public View getView() {
131+
return view;
132+
}
133+
130134
@Override
131135
public void writeTo(final StreamOutput out) throws IOException {
132136
this.view.writeTo(out);

server/src/main/java/org/opensearch/action/admin/indices/view/ViewService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ View getViewOrThrowException(final String viewName) {
132132
.map(ClusterState::metadata)
133133
.map(m -> m.views())
134134
.map(views -> views.get(viewName))
135-
.orElseThrow(() -> new ResourceNotFoundException("no such view [" + viewName + "]"));
135+
.orElseThrow(() -> new ResourceNotFoundException("View [" + viewName + "] does not exist"));
136136
}
137137

138138
private enum Operation {

server/src/main/java/org/opensearch/client/IndicesAdminClient.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -848,21 +848,21 @@ public interface IndicesAdminClient extends OpenSearchClient {
848848
/** Create a view */
849849
ActionFuture<GetViewAction.Response> createView(CreateViewAction.Request request);
850850

851-
/** Gets a view */
851+
/** Get the details of a view */
852852
void getView(GetViewAction.Request request, ActionListener<GetViewAction.Response> listener);
853853

854-
/** Gets a view */
854+
/** Get the details of a view */
855855
ActionFuture<GetViewAction.Response> getView(GetViewAction.Request request);
856856

857-
/** Create a view */
857+
/** Delete a view */
858858
void deleteView(DeleteViewAction.Request request, ActionListener<AcknowledgedResponse> listener);
859859

860-
/** Create a view */
860+
/** Delete a view */
861861
ActionFuture<AcknowledgedResponse> deleteView(DeleteViewAction.Request request);
862862

863-
/** Create a view */
863+
/** Update a view */
864864
void updateView(CreateViewAction.Request request, ActionListener<GetViewAction.Response> listener);
865865

866-
/** Create a view */
866+
/** Update a view */
867867
ActionFuture<GetViewAction.Response> updateView(CreateViewAction.Request request);
868868
}

server/src/test/java/org/opensearch/action/admin/indices/view/ViewServiceTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.hamcrest.MatcherAssert;
2121
import org.junit.After;
2222
import org.junit.Before;
23-
import org.junit.Test;
2423

2524
import java.util.List;
2625
import java.util.Map;

0 commit comments

Comments
 (0)