|
| 1 | +package com.linkedin.metadata.aspect.plugins.validation; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.*; |
| 4 | +import static org.testng.Assert.*; |
| 5 | + |
| 6 | +import com.datahub.test.TestEntityProfile; |
| 7 | +import com.linkedin.common.Status; |
| 8 | +import com.linkedin.common.urn.Urn; |
| 9 | +import com.linkedin.common.urn.UrnUtils; |
| 10 | +import com.linkedin.data.schema.annotation.PathSpecBasedSchemaAnnotationVisitor; |
| 11 | +import com.linkedin.metadata.aspect.batch.BatchItem; |
| 12 | +import com.linkedin.metadata.models.registry.ConfigEntityRegistry; |
| 13 | +import com.linkedin.metadata.models.registry.EntityRegistry; |
| 14 | +import com.linkedin.test.metadata.aspect.batch.TestMCP; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Set; |
| 19 | +import org.testng.annotations.BeforeMethod; |
| 20 | +import org.testng.annotations.BeforeTest; |
| 21 | +import org.testng.annotations.Test; |
| 22 | + |
| 23 | +public class ValidationExceptionCollectionTest { |
| 24 | + private final Urn TEST_URN = UrnUtils.getUrn("urn:li:chart:123"); |
| 25 | + |
| 26 | + private ValidationExceptionCollection collection; |
| 27 | + private EntityRegistry testEntityRegistry; |
| 28 | + |
| 29 | + private static final String ERROR_MESSAGE = "Test error message"; |
| 30 | + |
| 31 | + @BeforeTest |
| 32 | + public void disableAssert() { |
| 33 | + PathSpecBasedSchemaAnnotationVisitor.class |
| 34 | + .getClassLoader() |
| 35 | + .setClassAssertionStatus(PathSpecBasedSchemaAnnotationVisitor.class.getName(), false); |
| 36 | + } |
| 37 | + |
| 38 | + @BeforeMethod |
| 39 | + public void setUp() { |
| 40 | + collection = ValidationExceptionCollection.newCollection(); |
| 41 | + testEntityRegistry = |
| 42 | + new ConfigEntityRegistry( |
| 43 | + TestEntityProfile.class |
| 44 | + .getClassLoader() |
| 45 | + .getResourceAsStream("test-entity-registry.yml")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testNewCollection() { |
| 50 | + assertNotNull(collection); |
| 51 | + assertTrue(collection.isEmpty()); |
| 52 | + assertFalse(collection.hasFatalExceptions()); |
| 53 | + assertEquals(collection.getSubTypes().size(), 0); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testAddException() { |
| 58 | + BatchItem testItem = |
| 59 | + TestMCP.ofOneMCP(TEST_URN, new Status(), testEntityRegistry).stream().findFirst().get(); |
| 60 | + AspectValidationException exception = |
| 61 | + new AspectValidationException(testItem, ERROR_MESSAGE, ValidationSubType.VALIDATION, null); |
| 62 | + |
| 63 | + collection.addException(exception); |
| 64 | + |
| 65 | + assertEquals(collection.size(), 1); |
| 66 | + assertTrue(collection.containsKey(exception.getAspectGroup())); |
| 67 | + assertTrue(collection.get(exception.getAspectGroup()).contains(exception)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testAddExceptionWithMessage() { |
| 72 | + BatchItem testItem = |
| 73 | + TestMCP.ofOneMCP(TEST_URN, new Status(), testEntityRegistry).stream().findFirst().get(); |
| 74 | + collection.addException(testItem, ERROR_MESSAGE); |
| 75 | + |
| 76 | + assertEquals(collection.size(), 1); |
| 77 | + assertTrue(collection.hasFatalExceptions()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testHasFatalExceptionsWithMultipleTypes() { |
| 82 | + BatchItem testItem = |
| 83 | + TestMCP.ofOneMCP(TEST_URN, new Status(), testEntityRegistry).stream().findFirst().get(); |
| 84 | + |
| 85 | + // Add FILTER exception |
| 86 | + collection.addException( |
| 87 | + new AspectValidationException(testItem, ERROR_MESSAGE, ValidationSubType.FILTER, null)); |
| 88 | + assertFalse(collection.hasFatalExceptions()); |
| 89 | + |
| 90 | + // Add VALIDATION exception |
| 91 | + collection.addException( |
| 92 | + new AspectValidationException(testItem, ERROR_MESSAGE, ValidationSubType.VALIDATION, null)); |
| 93 | + assertTrue(collection.hasFatalExceptions()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testGetSubTypesWithAllTypes() { |
| 98 | + BatchItem testItem = |
| 99 | + TestMCP.ofOneMCP(TEST_URN, new Status(), testEntityRegistry).stream().findFirst().get(); |
| 100 | + |
| 101 | + collection.addException( |
| 102 | + new AspectValidationException(testItem, ERROR_MESSAGE, ValidationSubType.FILTER, null)); |
| 103 | + collection.addException( |
| 104 | + new AspectValidationException(testItem, ERROR_MESSAGE, ValidationSubType.VALIDATION, null)); |
| 105 | + collection.addException( |
| 106 | + new AspectValidationException( |
| 107 | + testItem, ERROR_MESSAGE, ValidationSubType.PRECONDITION, null)); |
| 108 | + |
| 109 | + Set<ValidationSubType> subTypes = collection.getSubTypes(); |
| 110 | + assertEquals(subTypes.size(), 3); |
| 111 | + assertTrue( |
| 112 | + subTypes.containsAll( |
| 113 | + Arrays.asList( |
| 114 | + ValidationSubType.FILTER, |
| 115 | + ValidationSubType.VALIDATION, |
| 116 | + ValidationSubType.PRECONDITION))); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testSuccessfulAndExceptionItems() { |
| 121 | + BatchItem validationItem = |
| 122 | + TestMCP.ofOneMCP(UrnUtils.getUrn("urn:li:chart:111"), new Status(), testEntityRegistry) |
| 123 | + .stream() |
| 124 | + .findFirst() |
| 125 | + .get(); |
| 126 | + BatchItem filterItem = |
| 127 | + TestMCP.ofOneMCP(UrnUtils.getUrn("urn:li:chart:222"), new Status(), testEntityRegistry) |
| 128 | + .stream() |
| 129 | + .findFirst() |
| 130 | + .get(); |
| 131 | + BatchItem successItem = |
| 132 | + TestMCP.ofOneMCP(UrnUtils.getUrn("urn:li:chart:333"), new Status(), testEntityRegistry) |
| 133 | + .stream() |
| 134 | + .findFirst() |
| 135 | + .get(); |
| 136 | + |
| 137 | + collection.addException( |
| 138 | + new AspectValidationException( |
| 139 | + validationItem, ERROR_MESSAGE, ValidationSubType.VALIDATION, null)); |
| 140 | + collection.addException( |
| 141 | + new AspectValidationException(filterItem, ERROR_MESSAGE, ValidationSubType.FILTER, null)); |
| 142 | + |
| 143 | + Collection<BatchItem> items = Arrays.asList(validationItem, filterItem, successItem); |
| 144 | + |
| 145 | + // Test successful items |
| 146 | + Collection<BatchItem> successful = collection.successful(items); |
| 147 | + assertEquals(successful.size(), 1); |
| 148 | + assertTrue(successful.contains(successItem)); |
| 149 | + |
| 150 | + // Test exception items |
| 151 | + Collection<BatchItem> exceptions = collection.exceptions(items); |
| 152 | + assertEquals(exceptions.size(), 1); |
| 153 | + assertTrue(exceptions.contains(validationItem)); |
| 154 | + assertFalse(exceptions.contains(filterItem)); // FILTER type should not be included |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testStreamOperations() { |
| 159 | + BatchItem validationItem = |
| 160 | + TestMCP.ofOneMCP(UrnUtils.getUrn("urn:li:chart:111"), new Status(), testEntityRegistry) |
| 161 | + .stream() |
| 162 | + .findFirst() |
| 163 | + .get(); |
| 164 | + BatchItem successItem = |
| 165 | + TestMCP.ofOneMCP(UrnUtils.getUrn("urn:li:chart:222"), new Status(), testEntityRegistry) |
| 166 | + .stream() |
| 167 | + .findFirst() |
| 168 | + .get(); |
| 169 | + |
| 170 | + collection.addException( |
| 171 | + new AspectValidationException( |
| 172 | + validationItem, ERROR_MESSAGE, ValidationSubType.VALIDATION, null)); |
| 173 | + |
| 174 | + List<BatchItem> items = Arrays.asList(validationItem, successItem); |
| 175 | + |
| 176 | + // Test streamSuccessful |
| 177 | + List<BatchItem> successful = collection.streamSuccessful(items.stream()).toList(); |
| 178 | + assertEquals(successful.size(), 1); |
| 179 | + assertTrue(successful.contains(successItem)); |
| 180 | + |
| 181 | + // Test streamExceptions |
| 182 | + List<BatchItem> exceptions = collection.streamExceptions(items.stream()).toList(); |
| 183 | + assertEquals(exceptions.size(), 1); |
| 184 | + assertTrue(exceptions.contains(validationItem)); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testMultipleExceptionsForSameEntityDifferentAspects() { |
| 189 | + BatchItem item1 = |
| 190 | + TestMCP.ofOneMCP(UrnUtils.getUrn("urn:li:chart:111"), new Status(), testEntityRegistry) |
| 191 | + .stream() |
| 192 | + .findFirst() |
| 193 | + .get(); |
| 194 | + BatchItem item2 = |
| 195 | + TestMCP.ofOneMCP(UrnUtils.getUrn("urn:li:chart:222"), new Status(), testEntityRegistry) |
| 196 | + .stream() |
| 197 | + .findFirst() |
| 198 | + .get(); |
| 199 | + |
| 200 | + collection.addException( |
| 201 | + new AspectValidationException(item1, ERROR_MESSAGE, ValidationSubType.VALIDATION, null)); |
| 202 | + collection.addException( |
| 203 | + new AspectValidationException(item2, ERROR_MESSAGE, ValidationSubType.VALIDATION, null)); |
| 204 | + |
| 205 | + assertEquals(collection.size(), 2); |
| 206 | + assertEquals(collection.getSubTypes().size(), 1); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void testToString() { |
| 211 | + BatchItem testItem = |
| 212 | + TestMCP.ofOneMCP(TEST_URN, new Status(), testEntityRegistry).stream().findFirst().get(); |
| 213 | + AspectValidationException exception = |
| 214 | + new AspectValidationException(testItem, ERROR_MESSAGE, ValidationSubType.VALIDATION, null); |
| 215 | + |
| 216 | + collection.addException(exception); |
| 217 | + |
| 218 | + String result = collection.toString(); |
| 219 | + assertTrue(result.contains("ValidationExceptionCollection")); |
| 220 | + assertTrue(result.contains("EntityAspect:")); |
| 221 | + assertTrue(result.contains("urn:li:chart:123")); |
| 222 | + } |
| 223 | +} |
0 commit comments