From e40de27e1e054418aa18a4c8f0153c176b9ec7fe Mon Sep 17 00:00:00 2001 From: Lukasz Soszynski Date: Thu, 10 Nov 2022 12:38:46 +0100 Subject: [PATCH] Mather GetResponseContainOnlyDocumentIdMatcher added so that DNFOF test still are green after rebase. Signed-off-by: Lukasz Soszynski --- .../security/DoNotFailOnForbiddenTests.java | 5 +- ...tResponseContainOnlyDocumentIdMatcher.java | 51 +++++++++++++++++++ .../matcher/GetResponseMatchers.java | 4 ++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/integrationTest/java/org/opensearch/test/framework/matcher/GetResponseContainOnlyDocumentIdMatcher.java diff --git a/src/integrationTest/java/org/opensearch/security/DoNotFailOnForbiddenTests.java b/src/integrationTest/java/org/opensearch/security/DoNotFailOnForbiddenTests.java index e9e7332abb..b4e582507e 100644 --- a/src/integrationTest/java/org/opensearch/security/DoNotFailOnForbiddenTests.java +++ b/src/integrationTest/java/org/opensearch/security/DoNotFailOnForbiddenTests.java @@ -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; @@ -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; @@ -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)); } } diff --git a/src/integrationTest/java/org/opensearch/test/framework/matcher/GetResponseContainOnlyDocumentIdMatcher.java b/src/integrationTest/java/org/opensearch/test/framework/matcher/GetResponseContainOnlyDocumentIdMatcher.java new file mode 100644 index 0000000000..b677d6e6e1 --- /dev/null +++ b/src/integrationTest/java/org/opensearch/test/framework/matcher/GetResponseContainOnlyDocumentIdMatcher.java @@ -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 { + + 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 "); + } +} diff --git a/src/integrationTest/java/org/opensearch/test/framework/matcher/GetResponseMatchers.java b/src/integrationTest/java/org/opensearch/test/framework/matcher/GetResponseMatchers.java index 04cdcb1508..87f346d704 100644 --- a/src/integrationTest/java/org/opensearch/test/framework/matcher/GetResponseMatchers.java +++ b/src/integrationTest/java/org/opensearch/test/framework/matcher/GetResponseMatchers.java @@ -21,6 +21,10 @@ public static Matcher containDocument(String indexName, String docu return new GetResponseDocumentIdMatcher(indexName, documentId); } + public static Matcher containOnlyDocumentId(String indexName, String documentId) { + return new GetResponseContainOnlyDocumentIdMatcher(indexName, documentId); + } + public static Matcher documentContainField(String fieldName, Object fieldValue) { return new GetResponseDocumentFieldValueMatcher(fieldName, fieldValue); }