Skip to content

Commit

Permalink
Integration tests and some unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied committed Feb 3, 2024
1 parent 2f615cf commit d45fcb9
Show file tree
Hide file tree
Showing 17 changed files with 765 additions and 409 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 static org.hamcrest.Matchers.is;
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount;

import java.util.List;

import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.test.BackgroundIndexer;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope;
import org.opensearch.test.OpenSearchIntegTestCase.Scope;
import org.opensearch.test.junit.annotations.TestLogging;

@ClusterScope(scope = Scope.TEST, numDataNodes = 2)
public class ViewIT extends OpenSearchIntegTestCase {

private int createIndexWithDocs(final String indexName) throws Exception {
createIndex(indexName);
ensureGreen(indexName);

final int numOfDocs = scaledRandomIntBetween(0, 200);
try (final BackgroundIndexer indexer = new BackgroundIndexer(indexName, "_doc", client(), numOfDocs)) {
waitForDocs(numOfDocs, indexer);
}

refresh(indexName);
assertHitCount(client().prepareSearch(indexName).setSize(0).get(), numOfDocs);
return numOfDocs;
}

private CreateViewAction.Response createView(final String name, final String indexPattern) throws Exception {
final CreateViewAction.Request request = new CreateViewAction.Request(name, null, List.of(new CreateViewAction.Request.Target(indexPattern)));
final CreateViewAction.Response response = client().admin().indices().createView(request).actionGet();
performRemoteStoreTestAction();
return response;
}

private SearchResponse searchView(final String viewName) throws Exception {
final SearchViewAction.Request request = SearchViewAction.createRequestWith(viewName, new SearchRequest());
final SearchResponse response = client().searchView(request).actionGet();
return response;
}

public void testBasicOperations() throws Exception {
final String indexInView1 = "index-1";
final String indexInView2 = "index-2";
final String indexNotInView = "another-index-1";

final int indexInView1DocCount = createIndexWithDocs(indexInView1);
final int indexInView2DocCount = createIndexWithDocs(indexInView2);
createIndexWithDocs(indexNotInView);

logger.info("Testing view with no matches");
createView("no-matches", "this-pattern-will-match-nothing");
final IndexNotFoundException ex = assertThrows(IndexNotFoundException.class, () -> searchView("no-matches"));
assertThat(ex.getMessage(), is("no such index [this-pattern-will-match-nothing]"));

logger.info("Testing view with exact index match");
createView("only-index-1", "index-1");
assertHitCount(searchView("only-index-1"), indexInView1DocCount);

logger.info("Testing view with wildcard matches");
createView("both-indices", "index-*");
assertHitCount(searchView("both-indices"), indexInView1DocCount + indexInView2DocCount);
}
}
7 changes: 7 additions & 0 deletions server/src/main/java/org/opensearch/action/ActionModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
import org.opensearch.action.admin.indices.validate.query.TransportValidateQueryAction;
import org.opensearch.action.admin.indices.validate.query.ValidateQueryAction;
import org.opensearch.action.admin.indices.view.CreateViewAction;
import org.opensearch.action.admin.indices.view.SearchViewAction;
import org.opensearch.action.bulk.BulkAction;
import org.opensearch.action.bulk.TransportBulkAction;
import org.opensearch.action.bulk.TransportShardBulkAction;
Expand Down Expand Up @@ -410,6 +411,7 @@
import org.opensearch.rest.action.admin.indices.RestUpgradeAction;
import org.opensearch.rest.action.admin.indices.RestUpgradeStatusAction;
import org.opensearch.rest.action.admin.indices.RestValidateQueryAction;
import org.opensearch.rest.action.admin.indices.RestViewAction;
import org.opensearch.rest.action.cat.AbstractCatAction;
import org.opensearch.rest.action.cat.RestAliasAction;
import org.opensearch.rest.action.cat.RestAllocationAction;
Expand Down Expand Up @@ -724,6 +726,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void reg

// Views:
actions.register(CreateViewAction.INSTANCE, CreateViewAction.TransportAction.class);
actions.register(SearchViewAction.INSTANCE, SearchViewAction.TransportAction.class);

// Persistent tasks:
actions.register(StartPersistentTaskAction.INSTANCE, StartPersistentTaskAction.TransportAction.class);
Expand Down Expand Up @@ -919,6 +922,10 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
registerHandler.accept(new RestResolveIndexAction());
registerHandler.accept(new RestDataStreamsStatsAction());

// View API
registerHandler.accept(new RestViewAction.CreateViewHandler());
registerHandler.accept(new RestViewAction.SearchViewHandler());

// CAT API
registerHandler.accept(new RestAllocationAction());
registerHandler.accept(new RestCatSegmentReplicationAction());
Expand Down
Loading

0 comments on commit d45fcb9

Please sign in to comment.