|
| 1 | +package com.linkedin.datahub.graphql.resolvers.form; |
| 2 | + |
| 3 | +import static com.linkedin.datahub.graphql.TestUtils.getMockAllowContext; |
| 4 | +import static org.testng.Assert.assertThrows; |
| 5 | +import static org.testng.Assert.assertTrue; |
| 6 | + |
| 7 | +import com.datahub.authentication.Authentication; |
| 8 | +import com.linkedin.common.urn.UrnUtils; |
| 9 | +import com.linkedin.datahub.graphql.QueryContext; |
| 10 | +import com.linkedin.datahub.graphql.generated.BatchAssignFormInput; |
| 11 | +import com.linkedin.metadata.service.FormService; |
| 12 | +import graphql.com.google.common.collect.ImmutableList; |
| 13 | +import graphql.schema.DataFetchingEnvironment; |
| 14 | +import java.util.concurrent.CompletionException; |
| 15 | +import org.mockito.Mockito; |
| 16 | +import org.testng.annotations.Test; |
| 17 | + |
| 18 | +public class BatchRemoveFormResolverTest { |
| 19 | + |
| 20 | + private static final String TEST_DATASET_URN = |
| 21 | + "urn:li:dataset:(urn:li:dataPlatform:hive,name,PROD)"; |
| 22 | + private static final String TEST_FORM_URN = "urn:li:form:1"; |
| 23 | + |
| 24 | + private static final BatchAssignFormInput TEST_INPUT = |
| 25 | + new BatchAssignFormInput(TEST_FORM_URN, ImmutableList.of(TEST_DATASET_URN)); |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testGetSuccess() throws Exception { |
| 29 | + FormService mockFormService = initMockFormService(true); |
| 30 | + BatchRemoveFormResolver resolver = new BatchRemoveFormResolver(mockFormService); |
| 31 | + |
| 32 | + // Execute resolver |
| 33 | + QueryContext mockContext = getMockAllowContext(); |
| 34 | + DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class); |
| 35 | + Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(TEST_INPUT); |
| 36 | + Mockito.when(mockEnv.getContext()).thenReturn(mockContext); |
| 37 | + |
| 38 | + boolean success = resolver.get(mockEnv).get(); |
| 39 | + |
| 40 | + assertTrue(success); |
| 41 | + |
| 42 | + // Validate that we called unassign on the service |
| 43 | + Mockito.verify(mockFormService, Mockito.times(1)) |
| 44 | + .batchUnassignFormForEntities( |
| 45 | + Mockito.eq(ImmutableList.of(UrnUtils.getUrn(TEST_DATASET_URN))), |
| 46 | + Mockito.eq(UrnUtils.getUrn(TEST_FORM_URN)), |
| 47 | + Mockito.any(Authentication.class)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testThrowsError() throws Exception { |
| 52 | + FormService mockFormService = initMockFormService(false); |
| 53 | + BatchRemoveFormResolver resolver = new BatchRemoveFormResolver(mockFormService); |
| 54 | + |
| 55 | + // Execute resolver |
| 56 | + QueryContext mockContext = getMockAllowContext(); |
| 57 | + DataFetchingEnvironment mockEnv = Mockito.mock(DataFetchingEnvironment.class); |
| 58 | + Mockito.when(mockEnv.getArgument(Mockito.eq("input"))).thenReturn(TEST_INPUT); |
| 59 | + Mockito.when(mockEnv.getContext()).thenReturn(mockContext); |
| 60 | + |
| 61 | + assertThrows(CompletionException.class, () -> resolver.get(mockEnv).join()); |
| 62 | + |
| 63 | + // Validate that we called unassign on the service - but it throws an error |
| 64 | + Mockito.verify(mockFormService, Mockito.times(1)) |
| 65 | + .batchUnassignFormForEntities( |
| 66 | + Mockito.eq(ImmutableList.of(UrnUtils.getUrn(TEST_DATASET_URN))), |
| 67 | + Mockito.eq(UrnUtils.getUrn(TEST_FORM_URN)), |
| 68 | + Mockito.any(Authentication.class)); |
| 69 | + } |
| 70 | + |
| 71 | + private FormService initMockFormService(final boolean shouldSucceed) throws Exception { |
| 72 | + FormService service = Mockito.mock(FormService.class); |
| 73 | + |
| 74 | + if (!shouldSucceed) { |
| 75 | + Mockito.doThrow(new RuntimeException()) |
| 76 | + .when(service) |
| 77 | + .batchUnassignFormForEntities( |
| 78 | + Mockito.any(), Mockito.any(), Mockito.any(Authentication.class)); |
| 79 | + } |
| 80 | + |
| 81 | + return service; |
| 82 | + } |
| 83 | +} |
0 commit comments