Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Commit

Permalink
fix new modules since last rebase (linkedin#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
John Plaisted committed Jan 14, 2021
1 parent c5faa27 commit 4953f15
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static final class TraversalPath {
* Finds a list of entities based on the given traversing paths.
*
* @param sourceEntityClass the source entity class as the starting point for the query. Must be a type defined in
* com.linkedin.metadata.entity.
* com.linkedin.metadata.entity.
* @param sourceEntityFilter the filter to apply to the source entity when querying
* @param traversePaths specify the traverse paths via a list of (relationship type, relationship filter,
* intermediate entities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ private ModelUtils() {
* Gets the corresponding aspect name for a specific aspect type.
*
* @param aspectClass the aspect type
* @param <T> must be a valid aspect type
* @return the corresponding aspect name, which is actually the FQCN of type
*/
public static String getAspectName(@Nonnull Class<? extends DataTemplate<?>> aspectClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
@Slf4j
public class ESBrowseDAO extends BaseBrowseDAO {
private final RestHighLevelClient _client;
private final BaseBrowseConfig _config;
private final BaseBrowseConfig<?> _config;

public ESBrowseDAO(@Nonnull RestHighLevelClient esClient, @Nonnull BaseBrowseConfig config) {
public ESBrowseDAO(@Nonnull RestHighLevelClient esClient, @Nonnull BaseBrowseConfig<?> config) {
this._client = esClient;
this._config = config;
}
Expand Down Expand Up @@ -235,7 +235,7 @@ List<BrowseResultEntity> extractEntitiesResponse(@Nonnull SearchResponse entitie
final List<BrowseResultEntity> entityMetadataArray = new ArrayList<>();
Arrays.stream(entitiesResponse.getHits().getHits()).forEach(hit -> {
try {
final List<String> allPaths = (List<String>) hit.getSourceAsMap().get(_config.getBrowsePathFieldName());
final List<?> allPaths = (List<?>) hit.getSourceAsMap().get(_config.getBrowsePathFieldName());
final String nextLevelPath = getNextLevelPath(allPaths, currentPath);
if (nextLevelPath != null) {
entityMetadataArray.add(new BrowseResultEntity().setName(getSimpleName(nextLevelPath))
Expand Down Expand Up @@ -263,10 +263,12 @@ private String getSimpleName(@Nonnull String path) {

@VisibleForTesting
@Nullable
static String getNextLevelPath(@Nonnull List<String> paths, @Nonnull String currentPath) {
static String getNextLevelPath(@Nonnull List<?> paths, @Nonnull String currentPath) {
final String normalizedCurrentPath = currentPath.toLowerCase();
final int pathDepth = getPathDepth(currentPath);
return paths.stream()
.filter(x -> x instanceof String)
.map(x -> (String) x)
.filter(x -> x.toLowerCase().startsWith(normalizedCurrentPath) && getPathDepth(x) == (pathDepth + 1))
.findFirst()
.orElse(null);
Expand Down Expand Up @@ -298,10 +300,12 @@ public List<String> getBrowsePaths(@Nonnull Urn urn) {
if (searchHits.length == 0) {
return Collections.emptyList();
}
final Map sourceMap = searchHits[0].getSourceAsMap();
final Map<String, Object> sourceMap = searchHits[0].getSourceAsMap();
if (!sourceMap.containsKey(_config.getBrowsePathFieldName())) {
return Collections.emptyList();
}
return (List<String>) sourceMap.get(_config.getBrowsePathFieldName());
@SuppressWarnings("unchecked")
final List<String> paths = (List<String>) sourceMap.get(_config.getBrowsePathFieldName());
return paths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
@Slf4j
public class ESAutoCompleteQueryForHighCardinalityFields extends BaseESAutoCompleteQuery {
private static final Integer DEFAULT_AUTOCOMPLETE_QUERY_SIZE = 100;
private BaseSearchConfig _config;
private final BaseSearchConfig<?> _config;

ESAutoCompleteQueryForHighCardinalityFields(BaseSearchConfig config) {
ESAutoCompleteQueryForHighCardinalityFields(BaseSearchConfig<?> config) {
this._config = config;
}

Expand Down Expand Up @@ -63,7 +63,7 @@ StringArray getSuggestionList(@Nonnull SearchResponse searchResponse, @Nonnull S
@Nonnull String input, int limit) {
Set<String> autoCompletionList = new LinkedHashSet<>();
SearchHit[] hits = searchResponse.getHits().getHits();
Integer count = 0;
int count = 0;
for (SearchHit hit : hits) {
Map<String, Object> source = hit.getSourceAsMap();
if (count >= limit) {
Expand Down Expand Up @@ -97,7 +97,7 @@ static List<String> decoupleArrayToGetSubstringMatch(@Nonnull Object fieldVal, @
if (!(fieldVal instanceof List)) {
return Collections.singletonList(fieldVal.toString());
}
List<Object> stringVals = (List<Object>) fieldVal;
List<?> stringVals = (List<?>) fieldVal;
return stringVals.stream()
.map(Object::toString)
.filter(x -> x.toLowerCase().contains(input.toLowerCase()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
public class ESAutoCompleteQueryForLowCardinalityFields extends BaseESAutoCompleteQuery {

private static final String DEFAULT_QUERY_ANALYZER = "lowercase_keyword";
private BaseSearchConfig _config;
private BaseSearchConfig<?> _config;

ESAutoCompleteQueryForLowCardinalityFields(BaseSearchConfig config) {
ESAutoCompleteQueryForLowCardinalityFields(BaseSearchConfig<?> config) {
this._config = config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.linkedin.metadata.query.SearchResultMetadata;
import com.linkedin.metadata.query.SortCriterion;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -202,6 +201,7 @@ SearchRequest getFilteredSearchQuery(@Nullable Filter filters, @Nullable SortCri
* @return a valid search request
* @deprecated please use {@link #constructSearchQuery(String, Filter, SortCriterion, String, int, int)} instead
*/
@Deprecated
@Nonnull
public SearchRequest constructSearchQuery(@Nonnull String input, @Nullable Filter filter,
@Nullable SortCriterion sortCriterion, int from, int size) {
Expand Down Expand Up @@ -319,8 +319,19 @@ DataMap buildDocumentsDataMap(@Nonnull Map<String, Object> objectMap) {

final DataMap dataMap = new DataMap();
for (Map.Entry<String, Object> entry : objectMap.entrySet()) {
if (entry.getValue() instanceof ArrayList) {
dataMap.put(entry.getKey(), new DataList((ArrayList<String>) entry.getValue()));
if (entry.getValue() instanceof List) {
final List<?> values = (List<?>) entry.getValue();
final DataList dataList = new DataList();

for (Object value : values) {
if (value instanceof String) {
dataList.add(value);
} else {
log.error("Expected all values to be Strings, but found `{}`", value.getClass().getSimpleName());
}
}

dataMap.put(entry.getKey(), dataList);
} else if (entry.getValue() != null) {
dataMap.put(entry.getKey(), entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static QueryBuilder getQueryBuilderFromCriterion(@Nonnull Criterion crite
}

@Nonnull
public static String toEntityType(@Nonnull Class c) {
public static String toEntityType(@Nonnull Class<?> c) {
String result = c.getSimpleName().toLowerCase();
if (result.endsWith("entity")) {
result = result.substring(0, result.length() - 6);
Expand All @@ -93,7 +93,7 @@ public static String toEntityType(@Nonnull Class c) {
}

@Nonnull
public static String readResourceFile(@Nonnull Class clazz, @Nonnull String filePath) {
public static String readResourceFile(@Nonnull Class<?> clazz, @Nonnull String filePath) {
try (InputStream inputStream = clazz.getClassLoader().getResourceAsStream(filePath)) {
return IOUtils.toString(inputStream);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


public class BrowseDAOTest {
private BaseBrowseConfig _browseConfig;
private BaseBrowseConfig<?> _browseConfig;
private RestHighLevelClient _mockClient;
private ESBrowseDAO _browseDAO;

Expand Down Expand Up @@ -70,29 +70,44 @@ public void testMatchingPaths() {
}

@Test
public void testGetBrowsePath() throws Exception {
public void testEmptyGetBrowsePaths() throws Exception {
SearchResponse mockSearchResponse = mock(SearchResponse.class);
SearchHits mockSearchHits = mock(SearchHits.class);
SearchHit mockSearchHit = mock(SearchHit.class);
Urn dummyUrn = TestUtils.makeUrn(0);
Map<String, Object> sourceMap = new HashMap<>();

// Test when there is no search hit for getBrowsePaths
when(mockSearchHits.getHits()).thenReturn(new SearchHit[0]);
when(mockSearchResponse.getHits()).thenReturn(mockSearchHits);
when(_mockClient.search(any(), eq(RequestOptions.DEFAULT))).thenReturn(mockSearchResponse);
assertEquals(_browseDAO.getBrowsePaths(dummyUrn).size(), 0);
}

@Test
public void testGetBrowsePathMissingField() throws Exception {
SearchResponse mockSearchResponse = mock(SearchResponse.class);
SearchHits mockSearchHits = mock(SearchHits.class);
SearchHit mockSearchHit = mock(SearchHit.class);
Urn dummyUrn = TestUtils.makeUrn(0);
Map<String, Object> sourceMap = new HashMap<>();

// Test the case of single search hit & browsePaths field doesn't exist
sourceMap.remove(_browseConfig.getBrowsePathFieldName());
when(mockSearchHit.getSourceAsMap()).thenReturn(sourceMap);
when(mockSearchHits.getHits()).thenReturn(new SearchHit[]{mockSearchHit});
when(mockSearchResponse.getHits()).thenReturn(mockSearchHits);
when(_mockClient.search(any(), eq(RequestOptions.DEFAULT))).thenReturn(mockSearchResponse);
assertEquals(_browseDAO.getBrowsePaths(dummyUrn).size(), 0);
}

// Test the case of single search hit & browsePaths field exists
@Test
public void testGetBrowsePathFoundField() throws Exception {
SearchResponse mockSearchResponse = mock(SearchResponse.class);
SearchHits mockSearchHits = mock(SearchHits.class);
SearchHit mockSearchHit = mock(SearchHit.class);
Urn dummyUrn = TestUtils.makeUrn(0);
Map<String, Object> sourceMap = new HashMap<>();
sourceMap.put(_browseConfig.getBrowsePathFieldName(), Collections.singletonList("foo"));

// Test the case of single search hit & browsePaths field exists
when(mockSearchHit.getSourceAsMap()).thenReturn(sourceMap);
when(mockSearchHits.getHits()).thenReturn(new SearchHit[]{mockSearchHit});
when(mockSearchResponse.getHits()).thenReturn(mockSearchHits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static String loadJsonFromResource(String resourceName) throws IOExcepti
@BeforeMethod
public void setup() throws Exception {
_testSearchConfig = new TestSearchConfig();
_searchDAO = new ESSearchDAO(null, EntityDocument.class, _testSearchConfig);
_searchDAO = new ESSearchDAO<>(null, EntityDocument.class, _testSearchConfig);
_esAutoCompleteQuery = new ESAutoCompleteQueryForHighCardinalityFields(_testSearchConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void testGetRequestMap() {
assertTrue(actual1.isEmpty());

// Filter with criteria with default condition
final Map requestParams = Collections.unmodifiableMap(new HashMap() {
final Map<String, String> requestParams = Collections.unmodifiableMap(new HashMap<String, String>() {
{
put("key1", "value1");
put("key2", "value2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.assertj.core.api.Assertions;
import org.assertj.core.api.IterableAssert;
import org.assertj.core.api.MapAssert;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.bulk.BulkRequest;


Expand All @@ -28,7 +27,7 @@ public static BulkRequestsContainerAssert assertThat(@Nonnull BulkRequestsContai
private static Collection<String> getIds(Collection<BulkRequest> requests) {
return requests.stream()
.flatMap(request -> request.requests().stream())
.map(DocWriteRequest::id)
.map(request -> request.id())
.collect(Collectors.toList());
}

Expand Down

0 comments on commit 4953f15

Please sign in to comment.