8
8
9
9
package org .opensearch .action .admin .indices .view ;
10
10
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 ;
16
13
import org .opensearch .test .OpenSearchIntegTestCase .ClusterScope ;
17
14
import org .opensearch .test .OpenSearchIntegTestCase .Scope ;
18
15
import org .hamcrest .MatcherAssert ;
21
18
22
19
import static org .opensearch .test .hamcrest .OpenSearchAssertions .assertHitCount ;
23
20
import static org .hamcrest .Matchers .contains ;
21
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
22
+ import static org .hamcrest .Matchers .hasSize ;
24
23
import static org .hamcrest .Matchers .is ;
24
+ import static org .hamcrest .Matchers .not ;
25
25
26
26
@ ClusterScope (scope = Scope .TEST , numDataNodes = 2 )
27
- public class ViewIT extends OpenSearchIntegTestCase {
27
+ public class ViewIT extends ViewTestBase {
28
28
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-*" ;
32
32
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 ));
37
38
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" )) ;
41
42
}
42
43
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" ));
52
55
}
53
56
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" ));
57
69
}
58
70
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" ));
61
89
}
62
90
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 ));
67
106
}
68
107
69
- public void testBasicOperations () throws Exception {
108
+ public void testSearchOperations () throws Exception {
70
109
final String indexInView1 = "index-1" ;
71
110
final String indexInView2 = "index-2" ;
72
111
final String indexNotInView = "another-index-1" ;
@@ -77,7 +116,7 @@ public void testBasicOperations() throws Exception {
77
116
78
117
logger .info ("Testing view with no matches" );
79
118
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" ));
81
120
MatcherAssert .assertThat (ex .getMessage (), is ("no such index [this-pattern-will-match-nothing]" ));
82
121
83
122
logger .info ("Testing view with exact index match" );
@@ -87,31 +126,10 @@ public void testBasicOperations() throws Exception {
87
126
logger .info ("Testing view with wildcard matches" );
88
127
createView ("both-indices" , "index-*" );
89
128
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" ));
110
129
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" ) );
114
133
115
- MatcherAssert .assertThat (viewNamesAfterUpdate , contains ("view2" ));
116
134
}
117
135
}
0 commit comments