Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolver): Allow users to add/remove related terms for children glossary terms #9895

Merged
merged 6 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1154,9 +1154,11 @@ private void configureMutationResolvers(final RuntimeWiring.Builder builder) {
new DeleteGlossaryEntityResolver(this.entityClient, this.entityService))
.dataFetcher(
"updateName", new UpdateNameResolver(this.entityService, this.entityClient))
.dataFetcher("addRelatedTerms", new AddRelatedTermsResolver(this.entityService))
.dataFetcher("addRelatedTerms", new AddRelatedTermsResolver(this.entityService,
this.entityClient))
.dataFetcher(
"removeRelatedTerms", new RemoveRelatedTermsResolver(this.entityService))
"removeRelatedTerms", new RemoveRelatedTermsResolver(this.entityService,
this.entityClient))
.dataFetcher(
"createNativeUserResetToken",
new CreateNativeUserResetTokenResolver(this.nativeUserService))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.linkedin.datahub.graphql.generated.RelatedTermsInput;
import com.linkedin.datahub.graphql.generated.TermRelationshipType;
import com.linkedin.datahub.graphql.resolvers.mutate.util.GlossaryUtils;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.glossary.GlossaryRelatedTerms;
import com.linkedin.metadata.Constants;
import com.linkedin.metadata.entity.EntityService;
Expand All @@ -30,17 +31,20 @@
public class AddRelatedTermsResolver implements DataFetcher<CompletableFuture<Boolean>> {

private final EntityService<?> _entityService;
private final EntityClient _entityClient;

@Override
public CompletableFuture<Boolean> get(DataFetchingEnvironment environment) throws Exception {

final QueryContext context = environment.getContext();
final RelatedTermsInput input =
bindArgument(environment.getArgument("input"), RelatedTermsInput.class);
final Urn urn = Urn.createFromString(input.getUrn());

return CompletableFuture.supplyAsync(
() -> {
if (GlossaryUtils.canManageGlossaries(context)) {
final Urn parentUrn = GlossaryUtils.getParentUrn(urn, context, _entityClient);
if (GlossaryUtils.canManageChildrenEntities(context, parentUrn, _entityClient)) {
try {
final TermRelationshipType relationshipType = input.getRelationshipType();
final Urn urn = Urn.createFromString(input.getUrn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.linkedin.datahub.graphql.generated.RelatedTermsInput;
import com.linkedin.datahub.graphql.generated.TermRelationshipType;
import com.linkedin.datahub.graphql.resolvers.mutate.util.GlossaryUtils;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.glossary.GlossaryRelatedTerms;
import com.linkedin.metadata.Constants;
import com.linkedin.metadata.entity.EntityService;
Expand All @@ -28,20 +29,22 @@
public class RemoveRelatedTermsResolver implements DataFetcher<CompletableFuture<Boolean>> {

private final EntityService<?> _entityService;
private final EntityClient _entityClient;

@Override
public CompletableFuture<Boolean> get(DataFetchingEnvironment environment) throws Exception {

final QueryContext context = environment.getContext();
final RelatedTermsInput input =
bindArgument(environment.getArgument("input"), RelatedTermsInput.class);
final Urn urn = Urn.createFromString(input.getUrn());

return CompletableFuture.supplyAsync(
() -> {
if (GlossaryUtils.canManageGlossaries(context)) {
final Urn parentUrn = GlossaryUtils.getParentUrn(urn, context, _entityClient);
if (GlossaryUtils.canManageChildrenEntities(context, parentUrn, _entityClient)) {
try {
final TermRelationshipType relationshipType = input.getRelationshipType();
final Urn urn = Urn.createFromString(input.getUrn());
final List<Urn> termUrnsToRemove =
input.getTermUrns().stream().map(UrnUtils::getUrn).collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.linkedin.datahub.graphql.QueryContext;
import com.linkedin.datahub.graphql.generated.RelatedTermsInput;
import com.linkedin.datahub.graphql.generated.TermRelationshipType;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.metadata.Constants;
import com.linkedin.metadata.entity.EntityService;
import graphql.schema.DataFetchingEnvironment;
Expand Down Expand Up @@ -39,6 +40,7 @@ private EntityService setUpService() {
@Test
public void testGetSuccessIsRelatedNonExistent() throws Exception {
EntityService mockService = setUpService();
EntityClient mockClient = Mockito.mock(EntityClient.class);

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);
Expand All @@ -47,7 +49,7 @@ public void testGetSuccessIsRelatedNonExistent() throws Exception {
Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_TERM_2_URN)), eq(true)))
.thenReturn(true);

AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService);
AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand All @@ -72,6 +74,7 @@ public void testGetSuccessIsRelatedNonExistent() throws Exception {
@Test
public void testGetSuccessHasRelatedNonExistent() throws Exception {
EntityService mockService = setUpService();
EntityClient mockClient = Mockito.mock(EntityClient.class);

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);
Expand All @@ -80,7 +83,7 @@ public void testGetSuccessHasRelatedNonExistent() throws Exception {
Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_TERM_2_URN)), eq(true)))
.thenReturn(true);

AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService);
AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand All @@ -105,11 +108,12 @@ public void testGetSuccessHasRelatedNonExistent() throws Exception {
@Test
public void testGetFailAddSelfAsRelatedTerm() throws Exception {
EntityService mockService = setUpService();
EntityClient mockClient = Mockito.mock(EntityClient.class);

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);

AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService);
AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand All @@ -126,11 +130,12 @@ public void testGetFailAddSelfAsRelatedTerm() throws Exception {
@Test
public void testGetFailAddNonTermAsRelatedTerm() throws Exception {
EntityService mockService = setUpService();
EntityClient mockClient = Mockito.mock(EntityClient.class);

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);

AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService);
AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand All @@ -147,13 +152,14 @@ public void testGetFailAddNonTermAsRelatedTerm() throws Exception {
@Test
public void testGetFailAddNonExistentTermAsRelatedTerm() throws Exception {
EntityService mockService = setUpService();
EntityClient mockClient = Mockito.mock(EntityClient.class);

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);
Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_TERM_1_URN)), eq(true)))
.thenReturn(false);

AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService);
AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand All @@ -170,13 +176,14 @@ public void testGetFailAddNonExistentTermAsRelatedTerm() throws Exception {
@Test
public void testGetFailAddToNonExistentUrn() throws Exception {
EntityService mockService = setUpService();
EntityClient mockClient = Mockito.mock(EntityClient.class);

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(false);
Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_TERM_1_URN)), eq(true)))
.thenReturn(true);

AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService);
AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand All @@ -193,13 +200,14 @@ public void testGetFailAddToNonExistentUrn() throws Exception {
@Test
public void testGetFailAddToNonTerm() throws Exception {
EntityService mockService = setUpService();
EntityClient mockClient = Mockito.mock(EntityClient.class);

Mockito.when(mockService.exists(eq(Urn.createFromString(DATASET_URN)), eq(true)))
.thenReturn(true);
Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_TERM_1_URN)), eq(true)))
.thenReturn(true);

AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService);
AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand All @@ -216,6 +224,7 @@ public void testGetFailAddToNonTerm() throws Exception {
@Test
public void testFailNoPermissions() throws Exception {
EntityService mockService = setUpService();
EntityClient mockClient = Mockito.mock(EntityClient.class);

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);
Expand All @@ -224,7 +233,7 @@ public void testFailNoPermissions() throws Exception {
Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_TERM_2_URN)), eq(true)))
.thenReturn(true);

AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService);
AddRelatedTermsResolver resolver = new AddRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockDenyContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.linkedin.datahub.graphql.QueryContext;
import com.linkedin.datahub.graphql.generated.RelatedTermsInput;
import com.linkedin.datahub.graphql.generated.TermRelationshipType;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.glossary.GlossaryRelatedTerms;
import com.linkedin.metadata.Constants;
import com.linkedin.metadata.entity.EntityService;
Expand Down Expand Up @@ -44,8 +45,9 @@ public void testGetSuccessIsA() throws Exception {

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);
EntityClient mockClient = Mockito.mock(EntityClient.class);

RemoveRelatedTermsResolver resolver = new RemoveRelatedTermsResolver(mockService);
RemoveRelatedTermsResolver resolver = new RemoveRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand Down Expand Up @@ -77,8 +79,9 @@ public void testGetSuccessHasA() throws Exception {

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);
EntityClient mockClient = Mockito.mock(EntityClient.class);

RemoveRelatedTermsResolver resolver = new RemoveRelatedTermsResolver(mockService);
RemoveRelatedTermsResolver resolver = new RemoveRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand Down Expand Up @@ -106,8 +109,9 @@ public void testFailAspectDoesNotExist() throws Exception {

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);
EntityClient mockClient = Mockito.mock(EntityClient.class);

RemoveRelatedTermsResolver resolver = new RemoveRelatedTermsResolver(mockService);
RemoveRelatedTermsResolver resolver = new RemoveRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand Down Expand Up @@ -137,8 +141,9 @@ public void testFailNoPermissions() throws Exception {

Mockito.when(mockService.exists(eq(Urn.createFromString(TEST_ENTITY_URN)), eq(true)))
.thenReturn(true);
EntityClient mockClient = Mockito.mock(EntityClient.class);

RemoveRelatedTermsResolver resolver = new RemoveRelatedTermsResolver(mockService);
RemoveRelatedTermsResolver resolver = new RemoveRelatedTermsResolver(mockService, mockClient);

QueryContext mockContext = getMockDenyContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
Expand Down
Loading