Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eboneil committed Oct 18, 2023
1 parent f1eee48 commit e614f99
Showing 1 changed file with 139 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.linkedin.datahub.graphql.resolvers.owner;

import com.google.common.collect.ImmutableList;
import com.linkedin.common.*;
import com.linkedin.common.AuditStamp;
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
Expand Down Expand Up @@ -28,6 +29,7 @@ public class AddOwnersResolverTest {
private static final String TEST_ENTITY_URN = "urn:li:dataset:(urn:li:dataPlatform:mysql,my-test,PROD)";
private static final String TEST_OWNER_1_URN = "urn:li:corpuser:test-id-1";
private static final String TEST_OWNER_2_URN = "urn:li:corpuser:test-id-2";
private static final String TEST_OWNER_3_URN = "urn:li:corpGroup:test-id-3";

@Test
public void testGetSuccessNoExistingOwners() throws Exception {
Expand Down Expand Up @@ -75,33 +77,41 @@ public void testGetSuccessNoExistingOwners() throws Exception {
}

@Test
public void testGetSuccessExistingOwners() throws Exception {
public void testGetSuccessExistingOwnerNewType() throws Exception {
EntityService mockService = getMockEntityService();

com.linkedin.common.Ownership oldOwnership = new Ownership().setOwners(new OwnerArray(
ImmutableList.of(new Owner()
.setOwner(UrnUtils.getUrn(TEST_OWNER_1_URN))
.setType(com.linkedin.common.OwnershipType.NONE)
.setSource(new OwnershipSource().setType(OwnershipSourceType.MANUAL))
)));

Mockito.when(mockService.getAspect(
Mockito.eq(UrnUtils.getUrn(TEST_ENTITY_URN)),
Mockito.eq(Constants.OWNERSHIP_ASPECT_NAME),
Mockito.eq(0L)))
.thenReturn(null);
Mockito.eq(UrnUtils.getUrn(TEST_ENTITY_URN)),
Mockito.eq(Constants.OWNERSHIP_ASPECT_NAME),
Mockito.eq(0L)))
.thenReturn(oldOwnership);

Mockito.when(mockService.exists(Urn.createFromString(TEST_ENTITY_URN))).thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(TEST_OWNER_1_URN))).thenReturn(true);
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.name()))))
.thenReturn(true);
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.TECHNICAL_OWNER.name()))))
.thenReturn(true);

AddOwnersResolver resolver = new AddOwnersResolver(mockService);

// Execute resolver
QueryContext mockContext = getMockAllowContext();
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.name())),
new OwnerInput(TEST_OWNER_2_URN, OwnerEntityType.CORP_USER, OwnershipType.TECHNICAL_OWNER,
OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))
OwnerInput.builder()
.setOwnerUrn(TEST_OWNER_1_URN)
.setOwnershipTypeUrn(OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))
.setOwnerEntityType(OwnerEntityType.CORP_USER)
.build()
), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);
Expand All @@ -111,11 +121,126 @@ public void testGetSuccessExistingOwners() throws Exception {
verifyIngestProposal(mockService, 1);

Mockito.verify(mockService, Mockito.times(1)).exists(
Mockito.eq(Urn.createFromString(TEST_OWNER_1_URN))
Mockito.eq(Urn.createFromString(TEST_OWNER_1_URN))
);
}

@Test
public void testGetSuccessDeprecatedTypeToOwnershipType() throws Exception {
EntityService mockService = getMockEntityService();

com.linkedin.common.Ownership oldOwnership = new Ownership().setOwners(new OwnerArray(
ImmutableList.of(new Owner()
.setOwner(UrnUtils.getUrn(TEST_OWNER_1_URN))
.setType(com.linkedin.common.OwnershipType.TECHNICAL_OWNER)
.setSource(new OwnershipSource().setType(OwnershipSourceType.MANUAL))
)));

Mockito.when(mockService.getAspect(
Mockito.eq(UrnUtils.getUrn(TEST_ENTITY_URN)),
Mockito.eq(Constants.OWNERSHIP_ASPECT_NAME),
Mockito.eq(0L)))
.thenReturn(oldOwnership);

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

Mockito.when(mockService.exists(Urn.createFromString(
OwnerUtils.mapOwnershipTypeToEntity(com.linkedin.datahub.graphql.generated.OwnershipType.TECHNICAL_OWNER.name()))))
.thenReturn(true);

AddOwnersResolver resolver = new AddOwnersResolver(mockService);

// Execute resolver
QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);

AddOwnersInput input = new AddOwnersInput(ImmutableList.of(OwnerInput.builder()
.setOwnerUrn(TEST_OWNER_1_URN)
.setOwnershipTypeUrn(OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))
.setOwnerEntityType(OwnerEntityType.CORP_USER)
.build()
), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);
assertTrue(resolver.get(mockEnv).get());

// Unable to easily validate exact payload due to the injected timestamp
verifyIngestProposal(mockService, 1);

Mockito.verify(mockService, Mockito.times(1)).exists(
Mockito.eq(Urn.createFromString(TEST_OWNER_2_URN))
Mockito.eq(Urn.createFromString(TEST_OWNER_1_URN))
);
}

@Test
public void testGetSuccessMultipleOwnerTypes() throws Exception {
EntityService mockService = getMockEntityService();

com.linkedin.common.Ownership oldOwnership = new Ownership().setOwners(new OwnerArray(
ImmutableList.of(new Owner()
.setOwner(UrnUtils.getUrn(TEST_OWNER_1_URN))
.setType(com.linkedin.common.OwnershipType.NONE)
.setSource(new OwnershipSource().setType(OwnershipSourceType.MANUAL))
)));

Mockito.when(mockService.getAspect(
Mockito.eq(UrnUtils.getUrn(TEST_ENTITY_URN)),
Mockito.eq(Constants.OWNERSHIP_ASPECT_NAME),
Mockito.eq(0L)))
.thenReturn(oldOwnership);

Mockito.when(mockService.exists(Urn.createFromString(TEST_ENTITY_URN))).thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(TEST_OWNER_1_URN))).thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(TEST_OWNER_2_URN))).thenReturn(true);
Mockito.when(mockService.exists(Urn.createFromString(TEST_OWNER_3_URN))).thenReturn(true);

Mockito.when(mockService.exists(Urn.createFromString(
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.name()))))
.thenReturn(true);

AddOwnersResolver resolver = new AddOwnersResolver(mockService);

// Execute resolver
QueryContext mockContext = getMockAllowContext();
DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class);

AddOwnersInput input = new AddOwnersInput(ImmutableList.of(OwnerInput.builder()
.setOwnerUrn(TEST_OWNER_1_URN)
.setOwnershipTypeUrn(OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))
.setOwnerEntityType(OwnerEntityType.CORP_USER)
.build(),
OwnerInput.builder()
.setOwnerUrn(TEST_OWNER_2_URN)
.setOwnershipTypeUrn(OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.BUSINESS_OWNER.name()))
.setOwnerEntityType(OwnerEntityType.CORP_USER)
.build(),
OwnerInput.builder()
.setOwnerUrn(TEST_OWNER_3_URN)
.setOwnershipTypeUrn(OwnerUtils.mapOwnershipTypeToEntity(OwnershipType.TECHNICAL_OWNER.name()))
.setOwnerEntityType(OwnerEntityType.CORP_GROUP)
.build()
), TEST_ENTITY_URN);
Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input);
Mockito.when(mockEnv.getContext()).thenReturn(mockContext);
assertTrue(resolver.get(mockEnv).get());

// Unable to easily validate exact payload due to the injected timestamp
verifyIngestProposal(mockService, 1);

Mockito.verify(mockService, Mockito.times(1)).exists(
Mockito.eq(Urn.createFromString(TEST_OWNER_1_URN))
);

Mockito.verify(mockService, Mockito.times(1)).exists(
Mockito.eq(Urn.createFromString(TEST_OWNER_2_URN))
);

Mockito.verify(mockService, Mockito.times(1)).exists(
Mockito.eq(Urn.createFromString(TEST_OWNER_3_URN))
);
}

Expand Down

0 comments on commit e614f99

Please sign in to comment.