Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bacffe4

Browse files
committedFeb 6, 2024
More tests
Signed-off-by: Peter Nied <petern@amazon.com>
1 parent b3e4cc6 commit bacffe4

File tree

3 files changed

+186
-7
lines changed

3 files changed

+186
-7
lines changed
 

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

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

11+
import org.hamcrest.MatcherAssert;
1112
import org.opensearch.action.search.SearchRequest;
1213
import org.opensearch.action.search.SearchResponse;
1314
import org.opensearch.index.IndexNotFoundException;
@@ -77,7 +78,7 @@ public void testBasicOperations() throws Exception {
7778
logger.info("Testing view with no matches");
7879
createView("no-matches", "this-pattern-will-match-nothing");
7980
final IndexNotFoundException ex = assertThrows(IndexNotFoundException.class, () -> searchView("no-matches"));
80-
assertThat(ex.getMessage(), is("no such index [this-pattern-will-match-nothing]"));
81+
MatcherAssert.assertThat(ex.getMessage(), is("no such index [this-pattern-will-match-nothing]"));
8182

8283
logger.info("Testing view with exact index match");
8384
createView("only-index-1", "index-1");
@@ -93,24 +94,24 @@ public void testListViewNames() throws Exception {
9394
createView("view1", "*");
9495
final List<String> viewNames1 = listViews();
9596

96-
assertThat(viewNames1, contains("view1"));
97+
MatcherAssert.assertThat(viewNames1, contains("view1"));
9798

9899
logger.info("Create a second view");
99100
createView("view2", "*");
100101
final List<String> viewNames2 = listViews();
101102

102-
assertThat(viewNames2, contains("view1", "view2"));
103+
MatcherAssert.assertThat(viewNames2, contains("view1", "view2"));
103104

104105
logger.info("Delete a view");
105106
deleteView("view1");
106107
final List<String> viewNamesAfterDelete = listViews();
107108

108-
assertThat(viewNamesAfterDelete, contains("view2"));
109+
MatcherAssert.assertThat(viewNamesAfterDelete, contains("view2"));
109110

110111
logger.info("Update a view");
111112
client().admin().indices().updateView(new CreateViewAction.Request("view2", "newDescription", List.of()));
112113
final List<String> viewNamesAfterUpdate = listViews();
113114

114-
assertThat(viewNamesAfterUpdate, contains("view2"));
115+
MatcherAssert.assertThat(viewNamesAfterUpdate, contains("view2"));
115116
}
116117
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void searchView(final SearchViewAction.Request request, final ActionListe
126126
client.executeLocally(SearchAction.INSTANCE, request, listener);
127127
}
128128

129-
private View getViewOrThrowException(final String viewName) {
129+
View getViewOrThrowException(final String viewName) {
130130
return Optional.ofNullable(clusterService)
131131
.map(ClusterService::state)
132132
.map(ClusterState::metadata)
@@ -135,7 +135,7 @@ private View getViewOrThrowException(final String viewName) {
135135
.orElseThrow(() -> new ResourceNotFoundException("no such view [" + viewName + "]"));
136136
}
137137

138-
private static enum Operation {
138+
private enum Operation {
139139
CreateView("create"),
140140
UpdateView("update");
141141

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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.junit.After;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.opensearch.ResourceNotFoundException;
15+
import org.opensearch.action.search.SearchAction;
16+
import org.opensearch.client.node.NodeClient;
17+
import org.opensearch.cluster.ClusterName;
18+
import org.opensearch.cluster.ClusterState;
19+
import org.opensearch.cluster.metadata.Metadata;
20+
import org.opensearch.cluster.metadata.View;
21+
import org.opensearch.cluster.service.ClusterService;
22+
import org.opensearch.core.action.ActionListener;
23+
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.concurrent.atomic.AtomicLong;
27+
import java.util.function.LongSupplier;
28+
29+
import static org.hamcrest.Matchers.equalTo;
30+
import static org.junit.Assert.*;
31+
import static org.mockito.Mockito.*;
32+
33+
34+
public class ViewServiceTest {
35+
36+
private final View.Target typicalTarget = new View.Target("my-index-*");
37+
private final View typicalView = new View("actualView", "actual description", -1L, -1L, List.of(typicalTarget));
38+
39+
private ClusterService clusterService;
40+
private NodeClient nodeClient;
41+
private AtomicLong currentTime = new AtomicLong(0);
42+
private LongSupplier timeProvider = currentTime::longValue;
43+
private ViewService viewService;
44+
45+
@Before
46+
public void before() {
47+
clusterService = mock(ClusterService.class);
48+
nodeClient = mock(NodeClient.class);
49+
timeProvider = mock(LongSupplier.class);
50+
doAnswer(invocation -> currentTime.get()).when(timeProvider).getAsLong();
51+
viewService = spy(new ViewService(clusterService, nodeClient, timeProvider));
52+
}
53+
54+
@After
55+
public void after() {
56+
verifyNoMoreInteractions(timeProvider, clusterService, nodeClient);
57+
}
58+
59+
@Test
60+
public void createView() {
61+
final var request = new CreateViewAction.Request("a", "b", List.of(new CreateViewAction.Request.Target("my-index-*")));
62+
final var listener = mock(ActionListener.class);
63+
setGetViewOrThrowExceptionToReturnTypicalView();
64+
65+
viewService.createView(request, listener);
66+
67+
verify(clusterService).submitStateUpdateTask(eq("create_view_task"), any());
68+
verify(timeProvider).getAsLong();
69+
}
70+
71+
@Test
72+
public void updateView() {
73+
final var request = new CreateViewAction.Request("a", "b", List.of(new CreateViewAction.Request.Target("my-index-*")));
74+
final var listener = mock(ActionListener.class);
75+
setGetViewOrThrowExceptionToReturnTypicalView();
76+
77+
viewService.updateView(request, listener);
78+
79+
verify(clusterService).submitStateUpdateTask(eq("update_view_task"), any());
80+
verify(timeProvider).getAsLong();
81+
}
82+
83+
@Test
84+
public void updateView_doesNotExist() {
85+
final var request = new CreateViewAction.Request("a", "b", List.of(new CreateViewAction.Request.Target("my-index-*")));
86+
final var listener = mock(ActionListener.class);
87+
doThrow(new ResourceNotFoundException("abc")).when(viewService).getViewOrThrowException(anyString());
88+
89+
final Exception ex = assertThrows(ResourceNotFoundException.class, () -> viewService.updateView(request, listener));
90+
assertThat(ex.getMessage(), equalTo("abc"));
91+
}
92+
93+
@Test
94+
public void deleteView() {
95+
final var request = new DeleteViewAction.Request("viewName");
96+
final var listener = mock(ActionListener.class);
97+
setGetViewOrThrowExceptionToReturnTypicalView();
98+
99+
viewService.deleteView(request, listener);
100+
101+
verify(clusterService).submitStateUpdateTask(eq("delete_view_task"), any());
102+
}
103+
104+
@Test
105+
public void deleteView_doesNotExist() {
106+
final var request = new DeleteViewAction.Request("viewName");
107+
final var listener = mock(ActionListener.class);
108+
doThrow(new ResourceNotFoundException("abc")).when(viewService).getViewOrThrowException(anyString());
109+
110+
final ResourceNotFoundException ex = assertThrows(ResourceNotFoundException.class, () -> viewService.deleteView(request, listener));
111+
112+
assertThat(ex.getMessage(), equalTo("abc"));
113+
}
114+
115+
@Test
116+
public void getView() {
117+
final var request = new GetViewAction.Request("viewName");
118+
final var listener = mock(ActionListener.class);
119+
setGetViewOrThrowExceptionToReturnTypicalView();
120+
121+
viewService.getView(request, listener);
122+
123+
verify(listener).onResponse(any());
124+
}
125+
126+
@Test
127+
public void getView_doesNotExist() {
128+
final var request = new GetViewAction.Request("viewName");
129+
final var listener = mock(ActionListener.class);
130+
doThrow(new ResourceNotFoundException("abc")).when(viewService).getViewOrThrowException(anyString());
131+
132+
final ResourceNotFoundException ex = assertThrows(ResourceNotFoundException.class, () -> viewService.getView(request, listener));
133+
134+
assertThat(ex.getMessage(), equalTo("abc"));
135+
}
136+
137+
@Test
138+
public void listViewNames() {
139+
final var clusterState = new ClusterState.Builder(new ClusterName("MyCluster"))
140+
.metadata(new Metadata.Builder().views(Map.of(typicalView.getName(), typicalView)).build())
141+
.build();
142+
final var listener = mock(ActionListener.class);
143+
when(clusterService.state()).thenReturn(clusterState);
144+
145+
viewService.listViewNames(listener);
146+
147+
verify(clusterService).state();
148+
verify(listener).onResponse(any());
149+
}
150+
151+
@Test
152+
public void listViewNames_noViews() {
153+
final var clusterState = new ClusterState.Builder(new ClusterName("MyCluster")).build();
154+
final var listener = mock(ActionListener.class);
155+
when(clusterService.state()).thenReturn(clusterState);
156+
157+
viewService.listViewNames(listener);
158+
159+
verify(clusterService).state();
160+
verify(listener).onResponse(any());
161+
}
162+
163+
@Test
164+
public void searchView() {
165+
final var request = spy(new SearchViewAction.Request("view"));
166+
final var listener = mock(ActionListener.class);
167+
setGetViewOrThrowExceptionToReturnTypicalView();
168+
169+
viewService.searchView(request, listener);
170+
171+
verify(nodeClient).executeLocally(eq(SearchAction.INSTANCE), any(), any(ActionListener.class));
172+
verify(request).indices(typicalTarget.getIndexPattern());
173+
}
174+
175+
private void setGetViewOrThrowExceptionToReturnTypicalView() {
176+
doAnswer(invocation -> typicalView).when(viewService).getViewOrThrowException(anyString());
177+
}
178+
}

0 commit comments

Comments
 (0)
Please sign in to comment.