forked from datahub-project/datahub
-
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.
feat(containers) Get and display all parent containers in header and …
…search (datahub-project#4910) Co-authored-by: Chris Collins <chriscollins@Chriss-MBP.lan>
- Loading branch information
1 parent
ef449a0
commit 9401286
Showing
29 changed files
with
829 additions
and
205 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
76 changes: 76 additions & 0 deletions
76
.../main/java/com/linkedin/datahub/graphql/resolvers/container/ParentContainersResolver.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,76 @@ | ||
package com.linkedin.datahub.graphql.resolvers.container; | ||
|
||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.data.DataMap; | ||
import com.linkedin.datahub.graphql.QueryContext; | ||
import com.linkedin.datahub.graphql.exception.DataHubGraphQLException; | ||
import com.linkedin.datahub.graphql.generated.Container; | ||
import com.linkedin.datahub.graphql.generated.Entity; | ||
import com.linkedin.datahub.graphql.generated.ParentContainersResult; | ||
import com.linkedin.datahub.graphql.types.container.mappers.ContainerMapper; | ||
import com.linkedin.entity.EntityResponse; | ||
import com.linkedin.entity.client.EntityClient; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static com.linkedin.metadata.Constants.CONTAINER_ASPECT_NAME; | ||
|
||
public class ParentContainersResolver implements DataFetcher<CompletableFuture<ParentContainersResult>> { | ||
|
||
private final EntityClient _entityClient; | ||
|
||
public ParentContainersResolver(final EntityClient entityClient) { | ||
_entityClient = entityClient; | ||
} | ||
|
||
private void aggregateParentContainers(List<Container> containers, String urn, QueryContext context) { | ||
try { | ||
Urn entityUrn = new Urn(urn); | ||
EntityResponse entityResponse = _entityClient.getV2( | ||
entityUrn.getEntityType(), | ||
entityUrn, | ||
Collections.singleton(CONTAINER_ASPECT_NAME), | ||
context.getAuthentication() | ||
); | ||
|
||
if (entityResponse != null && entityResponse.getAspects().containsKey(CONTAINER_ASPECT_NAME)) { | ||
DataMap dataMap = entityResponse.getAspects().get(CONTAINER_ASPECT_NAME).getValue().data(); | ||
com.linkedin.container.Container container = new com.linkedin.container.Container(dataMap); | ||
Urn containerUrn = container.getContainer(); | ||
EntityResponse response = _entityClient.getV2(containerUrn.getEntityType(), containerUrn, null, context.getAuthentication()); | ||
if (response != null) { | ||
Container mappedContainer = ContainerMapper.map(response); | ||
containers.add(mappedContainer); | ||
aggregateParentContainers(containers, mappedContainer.getUrn(), context); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public CompletableFuture<ParentContainersResult> get(DataFetchingEnvironment environment) { | ||
|
||
final QueryContext context = environment.getContext(); | ||
final String urn = ((Entity) environment.getSource()).getUrn(); | ||
final List<Container> containers = new ArrayList<>(); | ||
|
||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
aggregateParentContainers(containers, urn, context); | ||
final ParentContainersResult result = new ParentContainersResult(); | ||
result.setCount(containers.size()); | ||
result.setContainers(containers); | ||
return result; | ||
} catch (DataHubGraphQLException e) { | ||
throw new RuntimeException("Failed to load all containers", e); | ||
} | ||
}); | ||
} | ||
} |
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
118 changes: 118 additions & 0 deletions
118
...t/java/com/linkedin/datahub/graphql/resolvers/container/ParentContainersResolverTest.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,118 @@ | ||
package com.linkedin.datahub.graphql.resolvers.container; | ||
|
||
import com.datahub.authentication.Authentication; | ||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.container.Container; | ||
import com.linkedin.container.ContainerProperties; | ||
import com.linkedin.datahub.graphql.QueryContext; | ||
import com.linkedin.datahub.graphql.generated.Dataset; | ||
import com.linkedin.datahub.graphql.generated.EntityType; | ||
import com.linkedin.datahub.graphql.generated.ParentContainersResult; | ||
import com.linkedin.entity.Aspect; | ||
import com.linkedin.entity.EntityResponse; | ||
import com.linkedin.entity.EnvelopedAspect; | ||
import com.linkedin.entity.EnvelopedAspectMap; | ||
import com.linkedin.entity.client.EntityClient; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import org.mockito.Mockito; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.linkedin.metadata.Constants.CONTAINER_ASPECT_NAME; | ||
import static com.linkedin.metadata.Constants.CONTAINER_ENTITY_NAME; | ||
import static com.linkedin.metadata.Constants.CONTAINER_PROPERTIES_ASPECT_NAME; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
public class ParentContainersResolverTest { | ||
@Test | ||
public void testGetSuccess() throws Exception { | ||
EntityClient mockClient = Mockito.mock(EntityClient.class); | ||
QueryContext mockContext = Mockito.mock(QueryContext.class); | ||
Mockito.when(mockContext.getAuthentication()).thenReturn(Mockito.mock(Authentication.class)); | ||
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class); | ||
Mockito.when(mockEnv.getContext()).thenReturn(mockContext); | ||
|
||
Urn datasetUrn = Urn.createFromString("urn:li:dataset:(test,test,test)"); | ||
Dataset datasetEntity = new Dataset(); | ||
datasetEntity.setUrn(datasetUrn.toString()); | ||
datasetEntity.setType(EntityType.DATASET); | ||
Mockito.when(mockEnv.getSource()).thenReturn(datasetEntity); | ||
|
||
final Container parentContainer1 = new Container().setContainer(Urn.createFromString("urn:li:container:test-container")); | ||
final Container parentContainer2 = new Container().setContainer(Urn.createFromString("urn:li:container:test-container2")); | ||
|
||
Map<String, EnvelopedAspect> datasetAspects = new HashMap<>(); | ||
datasetAspects.put(CONTAINER_ASPECT_NAME, new EnvelopedAspect().setValue(new Aspect(parentContainer1.data()))); | ||
|
||
Map<String, EnvelopedAspect> parentContainer1Aspects = new HashMap<>(); | ||
parentContainer1Aspects.put(CONTAINER_PROPERTIES_ASPECT_NAME, new EnvelopedAspect().setValue(new Aspect( | ||
new ContainerProperties().setName("test_schema").data() | ||
))); | ||
parentContainer1Aspects.put(CONTAINER_ASPECT_NAME, new EnvelopedAspect().setValue(new Aspect( | ||
parentContainer2.data() | ||
))); | ||
|
||
Map<String, EnvelopedAspect> parentContainer2Aspects = new HashMap<>(); | ||
parentContainer2Aspects.put(CONTAINER_PROPERTIES_ASPECT_NAME, new EnvelopedAspect().setValue(new Aspect( | ||
new ContainerProperties().setName("test_database").data() | ||
))); | ||
|
||
Mockito.when(mockClient.getV2( | ||
Mockito.eq(datasetUrn.getEntityType()), | ||
Mockito.eq(datasetUrn), | ||
Mockito.eq(Collections.singleton(CONTAINER_ASPECT_NAME)), | ||
Mockito.any(Authentication.class) | ||
)).thenReturn(new EntityResponse().setAspects(new EnvelopedAspectMap(datasetAspects))); | ||
|
||
Mockito.when(mockClient.getV2( | ||
Mockito.eq(parentContainer1.getContainer().getEntityType()), | ||
Mockito.eq(parentContainer1.getContainer()), | ||
Mockito.eq(null), | ||
Mockito.any(Authentication.class) | ||
)).thenReturn(new EntityResponse() | ||
.setEntityName(CONTAINER_ENTITY_NAME) | ||
.setUrn(parentContainer1.getContainer()) | ||
.setAspects(new EnvelopedAspectMap(parentContainer1Aspects))); | ||
|
||
Mockito.when(mockClient.getV2( | ||
Mockito.eq(parentContainer1.getContainer().getEntityType()), | ||
Mockito.eq(parentContainer1.getContainer()), | ||
Mockito.eq(Collections.singleton(CONTAINER_ASPECT_NAME)), | ||
Mockito.any(Authentication.class) | ||
)).thenReturn(new EntityResponse().setAspects(new EnvelopedAspectMap(parentContainer1Aspects))); | ||
|
||
Mockito.when(mockClient.getV2( | ||
Mockito.eq(parentContainer2.getContainer().getEntityType()), | ||
Mockito.eq(parentContainer2.getContainer()), | ||
Mockito.eq(null), | ||
Mockito.any(Authentication.class) | ||
)).thenReturn(new EntityResponse() | ||
.setEntityName(CONTAINER_ENTITY_NAME) | ||
.setUrn(parentContainer2.getContainer()) | ||
.setAspects(new EnvelopedAspectMap(parentContainer2Aspects))); | ||
|
||
Mockito.when(mockClient.getV2( | ||
Mockito.eq(parentContainer2.getContainer().getEntityType()), | ||
Mockito.eq(parentContainer2.getContainer()), | ||
Mockito.eq(Collections.singleton(CONTAINER_ASPECT_NAME)), | ||
Mockito.any(Authentication.class) | ||
)).thenReturn(new EntityResponse().setAspects(new EnvelopedAspectMap(parentContainer2Aspects))); | ||
|
||
ParentContainersResolver resolver = new ParentContainersResolver(mockClient); | ||
ParentContainersResult result = resolver.get(mockEnv).get(); | ||
|
||
Mockito.verify(mockClient, Mockito.times(5)).getV2( | ||
Mockito.any(), | ||
Mockito.any(), | ||
Mockito.any(), | ||
Mockito.any() | ||
); | ||
assertEquals(result.getCount(), 2); | ||
assertEquals(result.getContainers().get(0).getUrn(), parentContainer1.getContainer().toString()); | ||
assertEquals(result.getContainers().get(1).getUrn(), parentContainer2.getContainer().toString()); | ||
} | ||
} |
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
Oops, something went wrong.