|
| 1 | +package io.cloudquery.schema; |
| 2 | + |
| 3 | +import io.cloudquery.scalar.ValidationException; |
| 4 | +import io.cloudquery.transformers.TransformerException; |
| 5 | +import io.cloudquery.types.UUIDType; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | + |
| 13 | +class ParentCQUUIDResolverTest { |
| 14 | + private static final java.util.UUID PARENT_UUID = java.util.UUID.randomUUID(); |
| 15 | + private static final java.util.UUID CHILD_UID = java.util.UUID.randomUUID(); |
| 16 | + private static final String CHILD_COLUMN = "child_column"; |
| 17 | + private static final Column COLUMN = Column.builder().name(CHILD_COLUMN).type(new UUIDType()).build(); |
| 18 | + private static final Table parentTable = Table.builder().name("parent").columns(List.of(Column.CQ_ID_COLUMN)).build(); |
| 19 | + private static final Table childTable = Table.builder().name("child").columns(List.of(COLUMN)).build(); |
| 20 | + |
| 21 | + private ColumnResolver resolver; |
| 22 | + |
| 23 | + @BeforeEach |
| 24 | + void setUp() { |
| 25 | + resolver = new ParentCQUUIDResolver(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void shouldSetColumnToNullIfWeDoNotHaveAParent() throws TransformerException, ValidationException { |
| 30 | + Resource resourceWithNoParent = Resource.builder().table(childTable).build(); |
| 31 | + resourceWithNoParent.set(CHILD_COLUMN, CHILD_UID); |
| 32 | + |
| 33 | + assertEquals(CHILD_UID, resourceWithNoParent.get(CHILD_COLUMN).get()); |
| 34 | + resolver.resolve(null, resourceWithNoParent, COLUMN); |
| 35 | + |
| 36 | + assertEquals(null, resourceWithNoParent.get(CHILD_COLUMN).get()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void shouldSetColumnToNullIfParentDoesNotHaveCQID() throws TransformerException, ValidationException { |
| 41 | + Resource resource = Resource.builder(). |
| 42 | + table(childTable). |
| 43 | + parent(Resource.builder().table(parentTable).build()). |
| 44 | + build(); |
| 45 | + resource.set(CHILD_COLUMN, CHILD_UID); |
| 46 | + |
| 47 | + assertEquals(CHILD_UID, resource.get(CHILD_COLUMN).get()); |
| 48 | + resolver.resolve(null, resource, COLUMN); |
| 49 | + |
| 50 | + assertEquals(null, resource.get(CHILD_COLUMN).get()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void shouldSetColumnToUUIDIfParentHasACQID() throws TransformerException, ValidationException { |
| 55 | + Resource parentResource = Resource.builder().table(parentTable).build(); |
| 56 | + parentResource.set(Column.CQ_ID_COLUMN.getName(), PARENT_UUID); |
| 57 | + |
| 58 | + Resource resource = Resource.builder().table(childTable).parent(parentResource).build(); |
| 59 | + resource.set(CHILD_COLUMN, CHILD_UID); |
| 60 | + |
| 61 | + assertEquals(CHILD_UID, resource.get(CHILD_COLUMN).get()); |
| 62 | + resolver.resolve(null, resource, COLUMN); |
| 63 | + |
| 64 | + assertEquals(PARENT_UUID, resource.get(CHILD_COLUMN).get()); |
| 65 | + } |
| 66 | +} |
0 commit comments