|
| 1 | +package com.linkedin.datahub.graphql.resolvers.entity.versioning; |
| 2 | + |
| 3 | +import static com.linkedin.datahub.graphql.TestUtils.*; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.eq; |
| 6 | +import static org.testng.Assert.*; |
| 7 | + |
| 8 | +import com.google.common.collect.ImmutableList; |
| 9 | +import com.linkedin.common.urn.Urn; |
| 10 | +import com.linkedin.common.urn.UrnUtils; |
| 11 | +import com.linkedin.datahub.graphql.QueryContext; |
| 12 | +import com.linkedin.datahub.graphql.featureflags.FeatureFlags; |
| 13 | +import com.linkedin.datahub.graphql.generated.LinkVersionInput; |
| 14 | +import com.linkedin.metadata.entity.IngestResult; |
| 15 | +import com.linkedin.metadata.entity.versioning.EntityVersioningService; |
| 16 | +import com.linkedin.metadata.entity.versioning.VersionPropertiesInput; |
| 17 | +import graphql.schema.DataFetchingEnvironment; |
| 18 | +import org.mockito.Mockito; |
| 19 | +import org.testng.annotations.Test; |
| 20 | + |
| 21 | +public class LinkAssetVersionResolverTest { |
| 22 | + |
| 23 | + private static final String TEST_VERSION_SET_URN = "urn:li:versionSet:test-version-set"; |
| 24 | + private static final String TEST_ENTITY_URN = |
| 25 | + "urn:li:dataset:(urn:li:dataPlatform:mysql,my-test,PROD)"; |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testGetSuccessful() throws Exception { |
| 29 | + EntityVersioningService mockService = Mockito.mock(EntityVersioningService.class); |
| 30 | + FeatureFlags mockFlags = Mockito.mock(FeatureFlags.class); |
| 31 | + |
| 32 | + Mockito.when(mockFlags.isEntityVersioning()).thenReturn(true); |
| 33 | + |
| 34 | + IngestResult mockResult = |
| 35 | + IngestResult.builder().urn(Urn.createFromString(TEST_ENTITY_URN)).build(); |
| 36 | + |
| 37 | + Mockito.when( |
| 38 | + mockService.linkLatestVersion( |
| 39 | + any(), |
| 40 | + eq(UrnUtils.getUrn(TEST_VERSION_SET_URN)), |
| 41 | + eq(UrnUtils.getUrn(TEST_ENTITY_URN)), |
| 42 | + any(VersionPropertiesInput.class))) |
| 43 | + .thenReturn(ImmutableList.of(mockResult)); |
| 44 | + |
| 45 | + LinkAssetVersionResolver resolver = new LinkAssetVersionResolver(mockService, mockFlags); |
| 46 | + |
| 47 | + // Execute resolver |
| 48 | + QueryContext mockContext = getMockAllowContext(); |
| 49 | + DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class); |
| 50 | + LinkVersionInput input = new LinkVersionInput(); |
| 51 | + input.setVersionSet(TEST_VERSION_SET_URN); |
| 52 | + input.setLinkedEntity(TEST_ENTITY_URN); |
| 53 | + input.setComment("Test comment"); |
| 54 | + input.setVersion("v1"); |
| 55 | + |
| 56 | + Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input); |
| 57 | + Mockito.when(mockEnv.getContext()).thenReturn(mockContext); |
| 58 | + |
| 59 | + String result = resolver.get(mockEnv).get(); |
| 60 | + assertEquals(result, TEST_ENTITY_URN); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testGetFeatureFlagDisabled() throws Exception { |
| 65 | + EntityVersioningService mockService = Mockito.mock(EntityVersioningService.class); |
| 66 | + FeatureFlags mockFlags = Mockito.mock(FeatureFlags.class); |
| 67 | + |
| 68 | + Mockito.when(mockFlags.isEntityVersioning()).thenReturn(false); |
| 69 | + |
| 70 | + LinkAssetVersionResolver resolver = new LinkAssetVersionResolver(mockService, mockFlags); |
| 71 | + |
| 72 | + // Execute resolver |
| 73 | + DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class); |
| 74 | + LinkVersionInput input = new LinkVersionInput(); |
| 75 | + input.setVersionSet(TEST_VERSION_SET_URN); |
| 76 | + input.setLinkedEntity(TEST_ENTITY_URN); |
| 77 | + |
| 78 | + Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input); |
| 79 | + |
| 80 | + assertThrows(IllegalAccessError.class, () -> resolver.get(mockEnv)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGetInvalidVersionSetUrn() throws Exception { |
| 85 | + EntityVersioningService mockService = Mockito.mock(EntityVersioningService.class); |
| 86 | + FeatureFlags mockFlags = Mockito.mock(FeatureFlags.class); |
| 87 | + |
| 88 | + Mockito.when(mockFlags.isEntityVersioning()).thenReturn(true); |
| 89 | + |
| 90 | + LinkAssetVersionResolver resolver = new LinkAssetVersionResolver(mockService, mockFlags); |
| 91 | + |
| 92 | + // Execute resolver |
| 93 | + DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class); |
| 94 | + LinkVersionInput input = new LinkVersionInput(); |
| 95 | + input.setVersionSet("urn:li:dataset:invalid-version-set"); // Invalid URN type |
| 96 | + input.setLinkedEntity(TEST_ENTITY_URN); |
| 97 | + |
| 98 | + Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(input); |
| 99 | + |
| 100 | + assertThrows(IllegalArgumentException.class, () -> resolver.get(mockEnv)); |
| 101 | + } |
| 102 | +} |
0 commit comments