@BeforeMethod public void setUpBeforeMethod() { entityAttributesValidator = new EntityAttributesValidator(); AttributeMetaData idAttr = when(mock(AttributeMetaData.class).getName()).thenReturn("id").getMock(); when(idAttr.getDataType()).thenReturn(STRING); AttributeMetaData intRangeMinAttr = when(mock(AttributeMetaData.class).getName()).thenReturn("intrangemin").getMock(); when(intRangeMinAttr.getDataType()).thenReturn(INT); when(intRangeMinAttr.getRange()).thenReturn(new Range(1l, null)); AttributeMetaData intRangeMaxAttr = when(mock(AttributeMetaData.class).getName()).thenReturn("intrangemin").getMock(); when(intRangeMaxAttr.getDataType()).thenReturn(INT); when(intRangeMaxAttr.getRange()).thenReturn(new Range(null, 1l)); intRangeMinMeta = when(mock(EntityMetaData.class).getName()).thenReturn("entity").getMock(); when(intRangeMinMeta.getIdAttribute()).thenReturn(idAttr); when(intRangeMinMeta.getAttribute("id")).thenReturn(idAttr); when(intRangeMinMeta.getAttribute("intrangemin")).thenReturn(intRangeMinAttr); when(intRangeMinMeta.getAtomicAttributes()).thenReturn(asList(idAttr, intRangeMinAttr)); intRangeMaxMeta = when(mock(EntityMetaData.class).getName()).thenReturn("entity").getMock(); when(intRangeMaxMeta.getIdAttribute()).thenReturn(idAttr); when(intRangeMaxMeta.getAttribute("id")).thenReturn(idAttr); when(intRangeMaxMeta.getAttribute("intrangemin")).thenReturn(intRangeMaxAttr); when(intRangeMaxMeta.getAtomicAttributes()).thenReturn(asList(idAttr, intRangeMaxAttr)); }
@Test public void checkMrefValidWrongType() { AttributeMetaData refIdAttr = when(mock(AttributeMetaData.class).getName()).thenReturn("refId").getMock(); when(refIdAttr.getDataType()).thenReturn(STRING); EntityMetaData refEntityMeta = mock(EntityMetaData.class); when(refEntityMeta.getName()).thenReturn("refEntity"); when(refEntityMeta.getIdAttribute()).thenReturn(refIdAttr); when(refEntityMeta.getAtomicAttributes()).thenReturn(asList(refIdAttr)); String idAttrName = "id"; String mrefAttrName = "mref"; AttributeMetaData idAttr = when(mock(AttributeMetaData.class).getName()).thenReturn(idAttrName).getMock(); when(idAttr.getDataType()).thenReturn(STRING); AttributeMetaData mrefAttr = when(mock(AttributeMetaData.class).getName()).thenReturn(mrefAttrName).getMock(); when(mrefAttr.getDataType()).thenReturn(MREF); when(mrefAttr.getRefEntity()).thenReturn(refEntityMeta); EntityMetaData entityMeta = mock(EntityMetaData.class); when(entityMeta.getName()).thenReturn("entity"); when(entityMeta.getIdAttribute()).thenReturn(idAttr); when(entityMeta.getAtomicAttributes()).thenReturn(asList(idAttr, mrefAttr)); AttributeMetaData otherRefIdAttr = when(mock(AttributeMetaData.class).getName()).thenReturn("otherRefId").getMock(); when(otherRefIdAttr.getDataType()).thenReturn(STRING); EntityMetaData otherRefEntityMeta = mock(EntityMetaData.class); when(otherRefEntityMeta.getName()).thenReturn("otherRefEntity"); when(otherRefEntityMeta.getIdAttribute()).thenReturn(refIdAttr); when(otherRefEntityMeta.getAtomicAttributes()).thenReturn(asList(otherRefIdAttr)); Entity refEntity0 = when(mock(Entity.class).getEntityMetaData()).thenReturn(otherRefEntityMeta).getMock(); when(refEntity0.getIdValue()).thenReturn("refId0"); Entity refEntity1 = when(mock(Entity.class).getEntityMetaData()).thenReturn(otherRefEntityMeta).getMock(); when(refEntity1.getIdValue()).thenReturn("refId1"); Entity entity0 = when(mock(Entity.class).getEntityMetaData()).thenReturn(entityMeta).getMock(); when(entity0.getIdValue()).thenReturn("id0"); when(entity0.getEntities(mrefAttrName)).thenReturn(asList(refEntity0, refEntity1)); Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity0, entity0.getEntityMetaData()); assertEquals(constraints.size(), 1); }
@Test public void createRowMapperXref() throws Exception { AttributeMetaData refIdAttr = mock(AttributeMetaData.class); when(refIdAttr.getDataType()).thenReturn(STRING); EntityMetaData refEntityMeta = mock(EntityMetaData.class); when(refEntityMeta.getIdAttribute()).thenReturn(refIdAttr); String xrefAttr = "xrefAttr"; AttributeMetaData oneToManyAttr = mock(AttributeMetaData.class); when(oneToManyAttr.getName()).thenReturn(xrefAttr); when(oneToManyAttr.getDataType()).thenReturn(XREF); when(oneToManyAttr.getRefEntity()).thenReturn(refEntityMeta); EntityMetaData entityMeta = mock(EntityMetaData.class); when(entityMeta.getAtomicAttributes()).thenReturn(singleton(oneToManyAttr)); ResultSet rs = mock(ResultSet.class); when(rs.getString(xrefAttr)).thenReturn("id0"); int rowNum = 0; Entity entity = mock(Entity.class); Fetch fetch = null; //noinspection ConstantConditions when(entityManager.create(entityMeta, fetch)).thenReturn(entity); Entity refEntity = mock(Entity.class); when(entityManager.getReference(refEntityMeta, "id0")).thenReturn(refEntity); assertEquals( postgreSqlEntityFactory.createRowMapper(entityMeta, null).mapRow(rs, rowNum), entity); verify(entity).set(xrefAttr, refEntity); }
/** * Create default entity fetch that fetches all attributes. * * @param entityMeta * @return default entity fetch or null */ public static Fetch createDefaultEntityFetch(EntityMetaData entityMeta, String languageCode) { boolean hasRefAttr = false; Fetch fetch = new Fetch(); for (AttributeMetaData attr : entityMeta.getAtomicAttributes()) { Fetch subFetch = createDefaultAttributeFetch(attr, languageCode); if (subFetch != null) { hasRefAttr = true; } fetch.field(attr.getName(), subFetch); } return hasRefAttr ? fetch : null; }
private static void createFetchContentRec( AttributeFilter attrFilter, EntityMetaData entityMeta, Fetch fetch, String languageCode) { if (attrFilter.isIncludeAllAttrs()) { entityMeta .getAtomicAttributes() .forEach( attr -> fetch.field(attr.getName(), createDefaultAttributeFetch(attr, languageCode))); } if (attrFilter.isIncludeIdAttr()) { fetch.field(entityMeta.getIdAttribute().getName()); } if (attrFilter.isIncludeLabelAttr()) { fetch.field(entityMeta.getLabelAttribute(languageCode).getName()); } attrFilter.forEach( entry -> { String attrName = entry.getKey(); AttributeMetaData attr = getAttribute(entityMeta, attrName); createFetchContentRec(attrFilter, entityMeta, attr, fetch, languageCode); }); }