Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied committed Feb 9, 2024
1 parent 292a322 commit 07bcfe0
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
public class ViewIT extends ViewTestBase {

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

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

logger.info("Testing createView with existing view name");
final Exception ex = assertThrows(ResourceNotFoundException.class, () -> createView(viewName, "new-pattern"));
MatcherAssert.assertThat(ex.getMessage(), is("View [test-view] already exists"));
final Exception ex = assertThrows(ResourceNotFoundException.class, () -> createView(viewName, randomAlphaOfLength(8)));
MatcherAssert.assertThat(ex.getMessage(), is("View [" + viewName + "] already exists"));
}

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

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

logger.info("Testing getView with non-existent view");
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> getView("non-existent"));
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist"));
final String nonExistentView = "non-existent-" + randomAlphaOfLength(8);
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> getView(nonExistentView));
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist"));
}

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

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

logger.info("Testing deleteView with non-existent view");
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> deleteView("non-existent"));
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist"));
final String nonExistentView = "non-existent-" + randomAlphaOfLength(8);
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> deleteView(nonExistentView));
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist"));
}

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

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

MatcherAssert.assertThat(updatedView, not(is(originalView)));
MatcherAssert.assertThat(updatedView.getDescription(), is("new description"));
MatcherAssert.assertThat(updatedView.getDescription(), is(newDescription));
MatcherAssert.assertThat(updatedView.getTargets(), hasSize(1));
MatcherAssert.assertThat(updatedView.getTargets().get(0).getIndexPattern(), is("index-new-*"));
MatcherAssert.assertThat(updatedView.getTargets().get(0).getIndexPattern(), is(newIndexPattern));

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

public void testListViewNames() throws Exception {
logger.info("Testing listViewNames when no views have been created");
MatcherAssert.assertThat(listViewNames(), is(List.of()));

final String view1 = "view1";
final String view2 = "view2";
createView(view1, "index-1-*");
Expand Down Expand Up @@ -128,8 +136,8 @@ public void testSearchOperations() throws Exception {
assertHitCount(searchView("both-indices"), indexInView1DocCount + indexInView2DocCount);

logger.info("Testing searchView with non-existent view");
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> searchView("non-existent"));
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [non-existent] does not exist"));

final String nonExistentView = "non-existent-" + randomAlphaOfLength(8);
final Exception whenNeverExistedEx = assertThrows(ResourceNotFoundException.class, () -> searchView(nonExistentView));
MatcherAssert.assertThat(whenNeverExistedEx.getMessage(), is("View [" + nonExistentView + "] does not exist"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String getName() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Request that = (Request) o;
final Request that = (Request) o;
return name.equals(that.name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public String getName() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Request that = (Request) o;
final Request that = (Request) o;
return name.equals(that.name);
}

Expand Down Expand Up @@ -131,6 +131,19 @@ public View getView() {
return view;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Response that = (Response) o;
return getView().equals(that.getView());
}

@Override
public int hashCode() {
return Objects.hash(getView());
}

@Override
public void writeTo(final StreamOutput out) throws IOException {
this.view.writeTo(out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Objects;

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

public Request(final StreamInput in) {}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Request that = (Request) o;
return true;
}

@Override
public int hashCode() {
return 1;
}

@Override
public ActionRequestValidationException validate() {
return null;
Expand All @@ -69,6 +83,19 @@ public List<String> getViewNames() {
return views;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Request that = (Request) o;
return views.equals(that.views);
}

@Override
public int hashCode() {
return Objects.hash(views);
}

@Override
public void writeTo(final StreamOutput out) throws IOException {
out.writeStringCollection(views);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.test.AbstractWireSerializingTestCase;
import org.hamcrest.MatcherAssert;

import java.util.List;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.nullValue;

public class CreateViewTests extends AbstractWireSerializingTestCase<CreateViewAction.Request> {
public class CreateViewRequestTests extends AbstractWireSerializingTestCase<CreateViewAction.Request> {

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

assertNull(request.validate());
MatcherAssert.assertThat(request.validate(), nullValue());
}

public void testValidateRequestWithoutName() {
final CreateViewAction.Request request = new CreateViewAction.Request("", null, null);
ActionRequestValidationException e = request.validate();
final ActionRequestValidationException e = request.validate();

assertThat(e.validationErrors(), contains("name cannot be empty or null", "targets cannot be empty"));
MatcherAssert.assertThat(e.validationErrors(), contains("name cannot be empty or null", "targets cannot be empty"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.test.AbstractWireSerializingTestCase;
import org.hamcrest.MatcherAssert;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.nullValue;

public class DeleteViewRequestTests extends AbstractWireSerializingTestCase<DeleteViewAction.Request> {

@Override
protected Writeable.Reader<DeleteViewAction.Request> instanceReader() {
return DeleteViewAction.Request::new;
}

@Override
protected DeleteViewAction.Request createTestInstance() {
return new DeleteViewAction.Request(randomAlphaOfLength(8));
}

public void testValidateRequest() {
final DeleteViewAction.Request request = new DeleteViewAction.Request("my-view");

MatcherAssert.assertThat(request.validate(), nullValue());
}

public void testValidateRequestWithoutName() {
final DeleteViewAction.Request request = new DeleteViewAction.Request("");
final ActionRequestValidationException e = request.validate();

MatcherAssert.assertThat(e.validationErrors(), contains("name cannot be empty or null"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 org.opensearch.cluster.metadata.View;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.test.AbstractWireSerializingTestCase;

public class GetViewResponseTests extends AbstractWireSerializingTestCase<GetViewAction.Response> {

@Override
protected Writeable.Reader<GetViewAction.Response> instanceReader() {
return GetViewAction.Response::new;
}

@Override
protected GetViewAction.Response createTestInstance() {
return new GetViewAction.Response(
new View(
randomAlphaOfLength(8),
randomAlphaOfLength(8),
randomLong(),
randomLong(),
randomList(5, () -> new View.Target(randomAlphaOfLength(8)))
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.test.AbstractWireSerializingTestCase;
import org.hamcrest.MatcherAssert;

import static org.hamcrest.Matchers.nullValue;

public class ListViewNamesRequestTests extends AbstractWireSerializingTestCase<ListViewNamesAction.Request> {

@Override
protected Writeable.Reader<ListViewNamesAction.Request> instanceReader() {
return ListViewNamesAction.Request::new;
}

@Override
protected ListViewNamesAction.Request createTestInstance() {
return new ListViewNamesAction.Request();
}

public void testValidateRequest() {
final ListViewNamesAction.Request request = new ListViewNamesAction.Request();

MatcherAssert.assertThat(request.validate(), nullValue());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.test.AbstractWireSerializingTestCase;

public class ListViewNamesResponseTests extends AbstractWireSerializingTestCase<ListViewNamesAction.Response> {

@Override
protected Writeable.Reader<ListViewNamesAction.Response> instanceReader() {
return ListViewNamesAction.Response::new;
}

@Override
protected ListViewNamesAction.Response createTestInstance() {
return new ListViewNamesAction.Response(randomList(5, () -> randomAlphaOfLength(8)));
}
}
Loading

0 comments on commit 07bcfe0

Please sign in to comment.