fix(ownership): Corrects graphQL resolver for entity operations (#8219)

This commit is contained in:
Pedro Silva 2023-06-13 16:23:36 +01:00 committed by GitHub
parent 4cfb795c1c
commit 460f96d683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 49 additions and 37 deletions

View File

@ -74,7 +74,7 @@ public class CreateDomainResolver implements DataFetcher<CompletableFuture<Strin
String domainUrn = _entityClient.ingestProposal(proposal, context.getAuthentication());
OwnershipType ownershipType = OwnershipType.TECHNICAL_OWNER;
if (!_entityService.exists(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType)))) {
if (!_entityService.exists(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType.name())))) {
log.warn("Technical owner does not exist, defaulting to None ownership.");
ownershipType = OwnershipType.NONE;
}

View File

@ -69,7 +69,7 @@ public class CreateGlossaryNodeResolver implements DataFetcher<CompletableFuture
String glossaryNodeUrn = _entityClient.ingestProposal(proposal, context.getAuthentication());
OwnershipType ownershipType = OwnershipType.TECHNICAL_OWNER;
if (!_entityService.exists(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType)))) {
if (!_entityService.exists(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType.name())))) {
log.warn("Technical owner does not exist, defaulting to None ownership.");
ownershipType = OwnershipType.NONE;
}

View File

@ -68,7 +68,7 @@ public class CreateGlossaryTermResolver implements DataFetcher<CompletableFuture
String glossaryTermUrn = _entityClient.ingestProposal(proposal, context.getAuthentication());
OwnershipType ownershipType = OwnershipType.TECHNICAL_OWNER;
if (!_entityService.exists(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType)))) {
if (!_entityService.exists(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType.name())))) {
log.warn("Technical owner does not exist, defaulting to None ownership.");
ownershipType = OwnershipType.NONE;
}

View File

@ -36,7 +36,7 @@ public class AddOwnerResolver implements DataFetcher<CompletableFuture<Boolean>>
Urn ownerUrn = Urn.createFromString(input.getOwnerUrn());
OwnerEntityType ownerEntityType = input.getOwnerEntityType();
OwnershipType type = input.getType() == null ? OwnershipType.NONE : input.getType();
String ownershipUrn = input.getOwnershipTypeUrn() == null ? mapOwnershipTypeToEntity(type) : input.getOwnershipTypeUrn();
String ownershipUrn = input.getOwnershipTypeUrn() == null ? mapOwnershipTypeToEntity(type.name()) : input.getOwnershipTypeUrn();
Urn targetUrn = Urn.createFromString(input.getResourceUrn());
if (!OwnerUtils.isAuthorizedToUpdateOwners(environment.getContext(), targetUrn)) {

View File

@ -116,7 +116,8 @@ public class OwnerUtils {
}
// Fall back to mapping deprecated type to the new ownership entity, if it matches remove
return mapOwnershipTypeToEntity(OwnershipType.valueOf(owner.getType().toString())).equals(ownershipUrn.toString());
return mapOwnershipTypeToEntity(OwnershipType.valueOf(owner.getType().toString()).name())
.equals(ownershipUrn.toString());
});
Owner newOwner = new Owner();
@ -158,7 +159,7 @@ public class OwnerUtils {
}
// Fall back to mapping deprecated type to the new ownership entity, if it matches remove
return mapOwnershipTypeToEntity(OwnershipType.valueOf(owner.getType().toString()))
return mapOwnershipTypeToEntity(OwnershipType.valueOf(owner.getType().toString()).name())
.equals(maybeOwnershipTypeUrn.get().toString());
});
} else {
@ -283,7 +284,7 @@ public class OwnerUtils {
EntityService entityService) {
try {
Urn actorUrn = CorpuserUrn.createFromString(context.getActorUrn());
String ownershipTypeUrn = mapOwnershipTypeToEntity(ownershipType);
String ownershipTypeUrn = mapOwnershipTypeToEntity(ownershipType.name());
if (!entityService.exists(UrnUtils.getUrn(ownershipTypeUrn))) {
throw new RuntimeException(String.format("Unknown ownership type urn %s", ownershipTypeUrn));
@ -300,8 +301,8 @@ public class OwnerUtils {
}
}
public static String mapOwnershipTypeToEntity(OwnershipType type) {
final String typeName = SYSTEM_ID + type.name().toLowerCase();
public static String mapOwnershipTypeToEntity(String type) {
final String typeName = SYSTEM_ID + type.toLowerCase();
return Urn.createFromTuple(Constants.OWNERSHIP_TYPE_ENTITY_NAME, typeName).toString();
}
}

View File

@ -73,7 +73,7 @@ public class CreateTagResolver implements DataFetcher<CompletableFuture<String>>
String tagUrn = _entityClient.ingestProposal(proposal, context.getAuthentication());
OwnershipType ownershipType = OwnershipType.TECHNICAL_OWNER;
if (!_entityService.exists(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType)))) {
if (!_entityService.exists(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType.name())))) {
log.warn("Technical owner does not exist, defaulting to None ownership.");
ownershipType = OwnershipType.NONE;
}

View File

@ -33,7 +33,7 @@ public class OwnerMapper {
if (owner.getTypeUrn() == null) {
OwnershipType ownershipType = OwnershipType.valueOf(owner.getType().toString());
owner.setTypeUrn(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType)));
owner.setTypeUrn(UrnUtils.getUrn(mapOwnershipTypeToEntity(ownershipType.name())));
}
if (owner.getTypeUrn() != null) {

View File

@ -1,5 +1,6 @@
package com.linkedin.datahub.graphql.types.common.mappers;
import com.linkedin.common.urn.UrnUtils;
import javax.annotation.Nonnull;
import com.linkedin.common.Owner;
@ -34,7 +35,14 @@ public class OwnerUpdateMapper implements ModelMapper<OwnerUpdate, Owner> {
} catch (URISyntaxException e) {
e.printStackTrace();
}
owner.setType(OwnershipType.valueOf(input.getType().toString()));
if (input.getOwnershipTypeUrn() != null) {
owner.setTypeUrn(UrnUtils.getUrn(input.getOwnershipTypeUrn()));
} else if (input.getType() != null) {
owner.setType(OwnershipType.valueOf(input.getType().toString()));
} else {
throw new RuntimeException("Ownership type not specified. Please define the ownership type urn.");
}
owner.setSource(new OwnershipSource().setType(OwnershipSourceType.SERVICE));
return owner;
}

View File

@ -8,6 +8,7 @@ import com.linkedin.common.OwnershipSource;
import com.linkedin.common.OwnershipSourceType;
import com.linkedin.common.OwnershipType;
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.data.template.SetMode;
import com.linkedin.datahub.graphql.generated.TagUpdateInput;
import com.linkedin.datahub.graphql.types.common.mappers.util.UpdateMappingHelper;
@ -18,6 +19,7 @@ import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.Nonnull;
import static com.linkedin.datahub.graphql.resolvers.mutate.util.OwnerUtils.*;
import static com.linkedin.metadata.Constants.*;
@ -47,6 +49,7 @@ public class TagUpdateInputMapper implements InputModelMapper<TagUpdateInput, Co
final Owner owner = new Owner();
owner.setOwner(actor);
owner.setType(OwnershipType.NONE);
owner.setTypeUrn(UrnUtils.getUrn(mapOwnershipTypeToEntity(OwnershipType.NONE.name())));
owner.setSource(new OwnershipSource().setType(OwnershipSourceType.SERVICE));
ownership.setOwners(new OwnerArray(owner));
ownership.setLastModified(auditStamp);

View File

@ -43,7 +43,7 @@ public class AddOwnersResolverTest {
Mockito.when(mockService.exists(Urn.createFromString(TEST_OWNER_2_URN))).thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.TECHNICAL_OWNER))))
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.TECHNICAL_OWNER.name()))))
.thenReturn(true);
AddOwnersResolver resolver = new AddOwnersResolver(mockService);
@ -53,9 +53,9 @@ public class AddOwnersResolverTest {
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
AddOwnersInput input = new AddOwnersInput(ImmutableList.of(
new OwnerInput(TEST_OWNER_1_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER)),
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name())),
new OwnerInput(TEST_OWNER_2_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER))
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))
), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);
@ -88,7 +88,7 @@ public class AddOwnersResolverTest {
Mockito.when(mockService.exists(Urn.createFromString(TEST_OWNER_2_URN))).thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.TECHNICAL_OWNER))))
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.TECHNICAL_OWNER.name()))))
.thenReturn(true);
AddOwnersResolver resolver = new AddOwnersResolver(mockService);
@ -98,9 +98,9 @@ public class AddOwnersResolverTest {
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
AddOwnersInput input = new AddOwnersInput(ImmutableList.of(
new OwnerInput(TEST_OWNER_1_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER)),
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name())),
new OwnerInput(TEST_OWNER_2_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER))
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))
), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);
@ -138,7 +138,7 @@ public class AddOwnersResolverTest {
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
AddOwnersInput input = new AddOwnersInput(ImmutableList.of(
new OwnerInput(TEST_OWNER_1_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER))), TEST_ENTITY_URN);
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);
@ -166,7 +166,7 @@ public class AddOwnersResolverTest {
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
AddOwnersInput input = new AddOwnersInput(ImmutableList.of(
new OwnerInput(TEST_OWNER_1_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER))), TEST_ENTITY_URN);
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);
@ -184,7 +184,7 @@ public class AddOwnersResolverTest {
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);
AddOwnersInput input = new AddOwnersInput(ImmutableList.of(
new OwnerInput(TEST_OWNER_1_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER))), TEST_ENTITY_URN);
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
QueryContext mockContext = getMockDenyContext();
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);
@ -208,7 +208,7 @@ public class AddOwnersResolverTest {
QueryContext mockContext = getMockAllowContext();
AddOwnersInput input = new AddOwnersInput(ImmutableList.of(
new OwnerInput(TEST_OWNER_1_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER))), TEST_ENTITY_URN);
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);

View File

@ -56,7 +56,7 @@ public class BatchAddOwnersResolverTest {
Mockito.when(mockService.exists(Urn.createFromString(TEST_OWNER_URN_2))).thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER))))
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name()))))
.thenReturn(true);
BatchAddOwnersResolver resolver = new BatchAddOwnersResolver(mockService);
@ -68,12 +68,12 @@ public class BatchAddOwnersResolverTest {
TEST_OWNER_URN_1,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER)),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name())),
new OwnerInput(
TEST_OWNER_URN_2,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER))),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name()))),
null,
ImmutableList.of(
new ResourceRefInput(TEST_ENTITY_URN_1, null, null),
@ -119,11 +119,11 @@ public class BatchAddOwnersResolverTest {
Mockito.when(mockService.exists(Urn.createFromString(TEST_OWNER_URN_2))).thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.TECHNICAL_OWNER))))
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.TECHNICAL_OWNER.name()))))
.thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER))))
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name()))))
.thenReturn(true);
BatchAddOwnersResolver resolver = new BatchAddOwnersResolver(mockService);
@ -136,12 +136,12 @@ public class BatchAddOwnersResolverTest {
TEST_OWNER_URN_1,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER)),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name())),
new OwnerInput(
TEST_OWNER_URN_2,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER))),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name()))),
null,
ImmutableList.of(
new ResourceRefInput(TEST_ENTITY_URN_1, null, null),
@ -183,12 +183,12 @@ public class BatchAddOwnersResolverTest {
TEST_OWNER_URN_1,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER)),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name())),
new OwnerInput(
TEST_OWNER_URN_2,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER))),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name()))),
null,
ImmutableList.of(
new ResourceRefInput(TEST_ENTITY_URN_1, null, null),
@ -228,12 +228,12 @@ public class BatchAddOwnersResolverTest {
TEST_OWNER_URN_1,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER)),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name())),
new OwnerInput(
TEST_OWNER_URN_2,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER))),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name()))),
null,
ImmutableList.of(
new ResourceRefInput(TEST_ENTITY_URN_1, null, null),
@ -257,12 +257,12 @@ public class BatchAddOwnersResolverTest {
TEST_OWNER_URN_1,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER)),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name())),
new OwnerInput(
TEST_OWNER_URN_2,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER))),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name()))),
null,
ImmutableList.of(
new ResourceRefInput(TEST_ENTITY_URN_1, null, null),
@ -292,12 +292,12 @@ public class BatchAddOwnersResolverTest {
TEST_OWNER_URN_1,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER)),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name())),
new OwnerInput(
TEST_OWNER_URN_2,
OwnerEntityType.CORP_USER,
com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER))),
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.BUSINESS_OWNER.name()))),
null,
ImmutableList.of(
new ResourceRefInput(TEST_ENTITY_URN_1, null, null),