Skip to content

Commit

Permalink
Mather GetResponseContainOnlyDocumentIdMatcher added so that DNFOF te…
Browse files Browse the repository at this point in the history
…st still are green after rebase.

Signed-off-by: Lukasz Soszynski <[email protected]>
  • Loading branch information
lukasz-soszynski-eliatra committed Nov 10, 2022
1 parent bfa2fca commit e40de27
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions.Type.ADD;
import static org.opensearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
Expand All @@ -66,6 +65,7 @@
import static org.opensearch.test.framework.cluster.SearchRequestFactory.statsAggregationRequest;
import static org.opensearch.test.framework.matcher.ExceptionMatcherAssert.assertThatThrownBy;
import static org.opensearch.test.framework.matcher.GetResponseMatchers.containDocument;
import static org.opensearch.test.framework.matcher.GetResponseMatchers.containOnlyDocumentId;
import static org.opensearch.test.framework.matcher.GetResponseMatchers.documentContainField;
import static org.opensearch.test.framework.matcher.OpenSearchExceptionMatchers.statusException;
import static org.opensearch.test.framework.matcher.SearchResponseMatchers.containAggregationWithNameAndType;
Expand Down Expand Up @@ -238,8 +238,7 @@ public void shouldMGetDocument_positive() throws IOException {
containDocument(MARVELOUS_SONGS, ID_1),
documentContainField(FIELD_TITLE, TITLE_MAGNUM_OPUS))
);
assertThat(secondResult.getResponse(), containDocument(MARVELOUS_SONGS, ID_4));
assertThat(secondResult.getResponse().isExists(), is(false));
assertThat(secondResult.getResponse(), containOnlyDocumentId(MARVELOUS_SONGS, ID_4));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright OpenSearch Contributors
* 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.test.framework.matcher;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import org.opensearch.action.get.GetResponse;

import static java.util.Objects.requireNonNull;

class GetResponseContainOnlyDocumentIdMatcher extends TypeSafeDiagnosingMatcher<GetResponse> {

private final String indexName;
private final String documentId;

public GetResponseContainOnlyDocumentIdMatcher(String indexName, String documentId) {
this.indexName = requireNonNull(indexName, "Index name is required");
this.documentId = requireNonNull(documentId, "Document id is required");
}

@Override
protected boolean matchesSafely(GetResponse response, Description mismatchDescription) {
if(indexName.equals(response.getIndex()) == false ) {
mismatchDescription.appendText(" index name ").appendValue(response.getIndex()).appendText(" is incorrect ");
return false;
}
if(documentId.equals(response.getId()) == false) {
mismatchDescription.appendText(" id ").appendValue(response.getId()).appendText(" is incorrect ");
return false;
}
if(response.isExists()) {
mismatchDescription.appendText(" document exist what is not desired ");
return false;
}
return true;
}

@Override
public void describeTo(Description description) {
description.appendText("Response should contain document id from index ").appendValue(indexName).appendText(" with id ")
.appendValue(documentId).appendText(" but document should not be present ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static Matcher<GetResponse> containDocument(String indexName, String docu
return new GetResponseDocumentIdMatcher(indexName, documentId);
}

public static Matcher<GetResponse> containOnlyDocumentId(String indexName, String documentId) {
return new GetResponseContainOnlyDocumentIdMatcher(indexName, documentId);
}

public static Matcher<GetResponse> documentContainField(String fieldName, Object fieldValue) {
return new GetResponseDocumentFieldValueMatcher(fieldName, fieldValue);
}
Expand Down

0 comments on commit e40de27

Please sign in to comment.