forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove static metaFields list and get version-specific values from co…
…re (opensearch-project#4370) Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
5 changed files
with
123 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...tionTest/java/org/opensearch/test/framework/matcher/SearchHitDoesContainFieldMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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 java.util.Map; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
|
||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.search.SearchHit; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static org.opensearch.test.framework.matcher.SearchResponseMatchers.readTotalHits; | ||
|
||
class SearchHitDoesContainFieldMatcher extends TypeSafeDiagnosingMatcher<SearchResponse> { | ||
|
||
private final int hitIndex; | ||
|
||
private final String fieldName; | ||
|
||
public SearchHitDoesContainFieldMatcher(int hitIndex, String fieldName) { | ||
this.hitIndex = hitIndex; | ||
this.fieldName = requireNonNull(fieldName, "Field name is required."); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(SearchResponse searchResponse, Description mismatchDescription) { | ||
Long numberOfHits = readTotalHits(searchResponse); | ||
if (numberOfHits == null) { | ||
mismatchDescription.appendText("Total number of hits is unknown."); | ||
return false; | ||
} | ||
if (hitIndex >= numberOfHits) { | ||
mismatchDescription.appendText("Search result contain only ").appendValue(numberOfHits).appendText(" hits"); | ||
return false; | ||
} | ||
SearchHit searchHit = searchResponse.getHits().getAt(hitIndex); | ||
Map<String, Object> source = searchHit.getSourceAsMap(); | ||
if (source == null) { | ||
mismatchDescription.appendText("Source document is null, is fetch source option set to true?"); | ||
return false; | ||
} | ||
if (!source.containsKey(fieldName)) { | ||
mismatchDescription.appendText(" document does not contain field ").appendValue(fieldName); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("search hit with index ").appendValue(hitIndex).appendText(" does contain field ").appendValue(fieldName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters