Skip to content

Commit 07bcfe0

Browse files
committed
Add test cases
Signed-off-by: Peter Nied <petern@amazon.com>
1 parent 292a322 commit 07bcfe0

File tree

11 files changed

+254
-49
lines changed

11 files changed

+254
-49
lines changed

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

+32-24
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
public class ViewIT extends ViewTestBase {
2828

2929
public void testCreateView() throws Exception {
30-
final String viewName = "test-view";
31-
final String indexPattern = "test-index-*";
30+
final String viewName = randomAlphaOfLength(8);
31+
final String indexPattern = randomAlphaOfLength(8);
3232

3333
logger.info("Testing createView with valid parameters");
3434
final View view = createView(viewName, indexPattern).getView();
@@ -37,58 +37,66 @@ public void testCreateView() throws Exception {
3737
MatcherAssert.assertThat(view.getTargets().get(0).getIndexPattern(), is(indexPattern));
3838

3939
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"));
40+
final Exception ex = assertThrows(ResourceNotFoundException.class, () -> createView(viewName, randomAlphaOfLength(8)));
41+
MatcherAssert.assertThat(ex.getMessage(), is("View [" + viewName + "] already exists"));
4242
}
4343

4444
public void testGetView() throws Exception {
45-
final String viewName = "existing-view";
45+
final String viewName = randomAlphaOfLength(8);
46+
createView(viewName, randomAlphaOfLength(8));
4647

47-
logger.info("Testing getView with existing view");
48-
createView(viewName, "index-*");
4948
final View view = getView(viewName).getView();
5049
MatcherAssert.assertThat(view.getName(), is(viewName));
5150

5251
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+
final String nonExistentView = "non-existent-" + randomAlphaOfLength(8);
53+
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> getView(nonExistentView));
54+
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist"));
5555
}
5656

5757
public void testDeleteView() throws Exception {
58-
final String viewName = "deleted-view";
59-
createView(viewName, "index-*");
58+
final String viewName = randomAlphaOfLength(8);
59+
createView(viewName, randomAlphaOfLength(8));
6060

6161
logger.info("Testing deleteView with existing view");
6262
deleteView(viewName);
6363
final Exception whenDeletedEx = assertThrows(ResourceNotFoundException.class, () -> getView(viewName));
64-
MatcherAssert.assertThat(whenDeletedEx.getMessage(), is("View [deleted-view] does not exist"));
64+
MatcherAssert.assertThat(whenDeletedEx.getMessage(), is("View [" + viewName + "] does not exist"));
6565

6666
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"));
67+
final String nonExistentView = "non-existent-" + randomAlphaOfLength(8);
68+
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> deleteView(nonExistentView));
69+
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist"));
6970
}
7071

7172
public void testUpdateView() throws Exception {
72-
final String viewName = "updatable-view";
73-
final View originalView = createView(viewName, "index-old-*").getView();
73+
final String viewName = randomAlphaOfLength(8);
74+
final String originalIndexPattern = randomAlphaOfLength(8);
75+
final View originalView = createView(viewName, originalIndexPattern).getView();
7476

7577
logger.info("Testing updateView with existing view");
76-
final View updatedView = updateView(viewName, "new description", "index-new-*").getView();
78+
final String newDescription = randomAlphaOfLength(20);
79+
final String newIndexPattern = "newPattern-" + originalIndexPattern;
80+
final View updatedView = updateView(viewName, newDescription, newIndexPattern).getView();
7781

7882
MatcherAssert.assertThat(updatedView, not(is(originalView)));
79-
MatcherAssert.assertThat(updatedView.getDescription(), is("new description"));
83+
MatcherAssert.assertThat(updatedView.getDescription(), is(newDescription));
8084
MatcherAssert.assertThat(updatedView.getTargets(), hasSize(1));
81-
MatcherAssert.assertThat(updatedView.getTargets().get(0).getIndexPattern(), is("index-new-*"));
85+
MatcherAssert.assertThat(updatedView.getTargets().get(0).getIndexPattern(), is(newIndexPattern));
8286

8387
logger.info("Testing updateView with non-existent view");
88+
final String nonExistentView = "non-existent-" + randomAlphaOfLength(8);
8489
final Exception whenNeverExistedEx = assertThrows(
8590
ResourceNotFoundException.class,
86-
() -> updateView("non-existent", null, "index-*")
91+
() -> updateView(nonExistentView, null, "index-*")
8792
);
88-
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist"));
93+
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist"));
8994
}
9095

9196
public void testListViewNames() throws Exception {
97+
logger.info("Testing listViewNames when no views have been created");
98+
MatcherAssert.assertThat(listViewNames(), is(List.of()));
99+
92100
final String view1 = "view1";
93101
final String view2 = "view2";
94102
createView(view1, "index-1-*");
@@ -128,8 +136,8 @@ public void testSearchOperations() throws Exception {
128136
assertHitCount(searchView("both-indices"), indexInView1DocCount + indexInView2DocCount);
129137

130138
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"));
133-
139+
final String nonExistentView = "non-existent-" + randomAlphaOfLength(8);
140+
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> searchView(nonExistentView));
141+
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist"));
134142
}
135143
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public String getName() {
6969
public boolean equals(Object o) {
7070
if (this == o) return true;
7171
if (o == null || getClass() != o.getClass()) return false;
72-
Request that = (Request) o;
72+
final Request that = (Request) o;
7373
return name.equals(that.name);
7474
}
7575

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public String getName() {
7272
public boolean equals(Object o) {
7373
if (this == o) return true;
7474
if (o == null || getClass() != o.getClass()) return false;
75-
Request that = (Request) o;
75+
final Request that = (Request) o;
7676
return name.equals(that.name);
7777
}
7878

@@ -131,6 +131,19 @@ public View getView() {
131131
return view;
132132
}
133133

134+
@Override
135+
public boolean equals(Object o) {
136+
if (this == o) return true;
137+
if (o == null || getClass() != o.getClass()) return false;
138+
final Response that = (Response) o;
139+
return getView().equals(that.getView());
140+
}
141+
142+
@Override
143+
public int hashCode() {
144+
return Objects.hash(getView());
145+
}
146+
134147
@Override
135148
public void writeTo(final StreamOutput out) throws IOException {
136149
this.view.writeTo(out);

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

+27
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.IOException;
2828
import java.util.List;
29+
import java.util.Objects;
2930

3031
/** Action to list a view names */
3132
@ExperimentalApi
@@ -45,6 +46,19 @@ public Request() {}
4546

4647
public Request(final StreamInput in) {}
4748

49+
@Override
50+
public boolean equals(Object o) {
51+
if (this == o) return true;
52+
if (o == null || getClass() != o.getClass()) return false;
53+
final Request that = (Request) o;
54+
return true;
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return 1;
60+
}
61+
4862
@Override
4963
public ActionRequestValidationException validate() {
5064
return null;
@@ -69,6 +83,19 @@ public List<String> getViewNames() {
6983
return views;
7084
}
7185

86+
@Override
87+
public boolean equals(Object o) {
88+
if (this == o) return true;
89+
if (o == null || getClass() != o.getClass()) return false;
90+
final Request that = (Request) o;
91+
return views.equals(that.views);
92+
}
93+
94+
@Override
95+
public int hashCode() {
96+
return Objects.hash(views);
97+
}
98+
7299
@Override
73100
public void writeTo(final StreamOutput out) throws IOException {
74101
out.writeStringCollection(views);

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
import org.opensearch.action.ActionRequestValidationException;
1212
import org.opensearch.core.common.io.stream.Writeable;
1313
import org.opensearch.test.AbstractWireSerializingTestCase;
14+
import org.hamcrest.MatcherAssert;
1415

1516
import java.util.List;
1617

1718
import static org.hamcrest.Matchers.contains;
19+
import static org.hamcrest.Matchers.nullValue;
1820

19-
public class CreateViewTests extends AbstractWireSerializingTestCase<CreateViewAction.Request> {
21+
public class CreateViewRequestTests extends AbstractWireSerializingTestCase<CreateViewAction.Request> {
2022

2123
@Override
2224
protected Writeable.Reader<CreateViewAction.Request> instanceReader() {
@@ -39,14 +41,14 @@ public void testValidateRequest() {
3941
List.of(new CreateViewAction.Request.Target("my-indices-*"))
4042
);
4143

42-
assertNull(request.validate());
44+
MatcherAssert.assertThat(request.validate(), nullValue());
4345
}
4446

4547
public void testValidateRequestWithoutName() {
4648
final CreateViewAction.Request request = new CreateViewAction.Request("", null, null);
47-
ActionRequestValidationException e = request.validate();
49+
final ActionRequestValidationException e = request.validate();
4850

49-
assertThat(e.validationErrors(), contains("name cannot be empty or null", "targets cannot be empty"));
51+
MatcherAssert.assertThat(e.validationErrors(), contains("name cannot be empty or null", "targets cannot be empty"));
5052
}
5153

5254
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.ActionRequestValidationException;
12+
import org.opensearch.core.common.io.stream.Writeable;
13+
import org.opensearch.test.AbstractWireSerializingTestCase;
14+
import org.hamcrest.MatcherAssert;
15+
16+
import static org.hamcrest.Matchers.contains;
17+
import static org.hamcrest.Matchers.nullValue;
18+
19+
public class DeleteViewRequestTests extends AbstractWireSerializingTestCase<DeleteViewAction.Request> {
20+
21+
@Override
22+
protected Writeable.Reader<DeleteViewAction.Request> instanceReader() {
23+
return DeleteViewAction.Request::new;
24+
}
25+
26+
@Override
27+
protected DeleteViewAction.Request createTestInstance() {
28+
return new DeleteViewAction.Request(randomAlphaOfLength(8));
29+
}
30+
31+
public void testValidateRequest() {
32+
final DeleteViewAction.Request request = new DeleteViewAction.Request("my-view");
33+
34+
MatcherAssert.assertThat(request.validate(), nullValue());
35+
}
36+
37+
public void testValidateRequestWithoutName() {
38+
final DeleteViewAction.Request request = new DeleteViewAction.Request("");
39+
final ActionRequestValidationException e = request.validate();
40+
41+
MatcherAssert.assertThat(e.validationErrors(), contains("name cannot be empty or null"));
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.core.common.io.stream.Writeable;
13+
import org.opensearch.test.AbstractWireSerializingTestCase;
14+
15+
public class GetViewResponseTests extends AbstractWireSerializingTestCase<GetViewAction.Response> {
16+
17+
@Override
18+
protected Writeable.Reader<GetViewAction.Response> instanceReader() {
19+
return GetViewAction.Response::new;
20+
}
21+
22+
@Override
23+
protected GetViewAction.Response createTestInstance() {
24+
return new GetViewAction.Response(
25+
new View(
26+
randomAlphaOfLength(8),
27+
randomAlphaOfLength(8),
28+
randomLong(),
29+
randomLong(),
30+
randomList(5, () -> new View.Target(randomAlphaOfLength(8)))
31+
)
32+
);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.core.common.io.stream.Writeable;
12+
import org.opensearch.test.AbstractWireSerializingTestCase;
13+
import org.hamcrest.MatcherAssert;
14+
15+
import static org.hamcrest.Matchers.nullValue;
16+
17+
public class ListViewNamesRequestTests extends AbstractWireSerializingTestCase<ListViewNamesAction.Request> {
18+
19+
@Override
20+
protected Writeable.Reader<ListViewNamesAction.Request> instanceReader() {
21+
return ListViewNamesAction.Request::new;
22+
}
23+
24+
@Override
25+
protected ListViewNamesAction.Request createTestInstance() {
26+
return new ListViewNamesAction.Request();
27+
}
28+
29+
public void testValidateRequest() {
30+
final ListViewNamesAction.Request request = new ListViewNamesAction.Request();
31+
32+
MatcherAssert.assertThat(request.validate(), nullValue());
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.core.common.io.stream.Writeable;
12+
import org.opensearch.test.AbstractWireSerializingTestCase;
13+
14+
public class ListViewNamesResponseTests extends AbstractWireSerializingTestCase<ListViewNamesAction.Response> {
15+
16+
@Override
17+
protected Writeable.Reader<ListViewNamesAction.Response> instanceReader() {
18+
return ListViewNamesAction.Response::new;
19+
}
20+
21+
@Override
22+
protected ListViewNamesAction.Response createTestInstance() {
23+
return new ListViewNamesAction.Response(randomList(5, () -> randomAlphaOfLength(8)));
24+
}
25+
}

0 commit comments

Comments
 (0)