@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 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); }
@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); }
private static Map<String, Object> attributeToMap(AttributeMetaData attributeMetaData) { Map<String, Object> map = new HashMap<String, Object>(); map.put(AttributeMetaDataMetaData.NAME, attributeMetaData.getName()); map.put(AttributeMetaDataMetaData.LABEL, attributeMetaData.getLabel()); map.put(AttributeMetaDataMetaData.DESCRIPTION, attributeMetaData.getDescription()); map.put(AttributeMetaDataMetaData.DATA_TYPE, attributeMetaData.getDataType().toString()); map.put(AttributeMetaDataMetaData.NILLABLE, attributeMetaData.isNillable()); map.put(AttributeMetaDataMetaData.UNIQUE, attributeMetaData.isUnique()); if (attributeMetaData.getRefEntity() != null) { map.put(AttributeMetaDataMetaData.REF_ENTITY, attributeMetaData.getRefEntity().getName()); } return map; }
/** * Create default fetch for the given attribute. For attributes referencing entities the id and * label value are fetched. Additionally for file entities the URL is fetched. For other * attributes the default fetch is null; * * @param attr * @return default attribute fetch or null */ public static Fetch createDefaultAttributeFetch(AttributeMetaData attr, String languageCode) { Fetch fetch; if (isReferenceType(attr)) { fetch = new Fetch(); EntityMetaData refEntityMeta = attr.getRefEntity(); String idAttrName = refEntityMeta.getIdAttribute().getName(); fetch.field(idAttrName); String labelAttrName = refEntityMeta.getLabelAttribute(languageCode).getName(); if (!labelAttrName.equals(idAttrName)) { fetch.field(labelAttrName); } if (attr.getDataType() == FILE) { fetch.field(FileMetaMetaData.URL); } } else { fetch = null; } return fetch; }
private static void createFetchContentRec( AttributeFilter attrFilter, EntityMetaData entityMeta, AttributeMetaData attr, Fetch fetch, String languageCode) { AttributeType attrType = attr.getDataType(); switch (attrType) { case COMPOUND: { AttributeFilter subAttrFilter = attrFilter != null ? attrFilter.getAttributeFilter(entityMeta, attr) : null; if (subAttrFilter != null && !subAttrFilter.isIncludeAllAttrs()) { // include compound attribute parts defined by filter if (subAttrFilter.isIncludeIdAttr()) { createFetchContentRec( subAttrFilter, entityMeta, entityMeta.getIdAttribute(), fetch, languageCode); } if (subAttrFilter.isIncludeLabelAttr()) { createFetchContentRec( subAttrFilter, entityMeta, entityMeta.getLabelAttribute(languageCode), fetch, languageCode); } subAttrFilter.forEach( entry -> { String attrPartName = entry.getKey(); AttributeMetaData attrPart = attr.getAttributePart(attrPartName); createFetchContentRec(subAttrFilter, entityMeta, attrPart, fetch, languageCode); }); } else { // include all compound attribute parts attr.getAttributeParts() .forEach( attrPart -> { createFetchContentRec( subAttrFilter, entityMeta, attrPart, fetch, languageCode); }); } break; } case CATEGORICAL: case CATEGORICAL_MREF: case FILE: case MREF: case XREF: { AttributeFilter subAttrFilter = attrFilter != null ? attrFilter.getAttributeFilter(entityMeta, attr) : null; Fetch subFetch; if (subAttrFilter != null) { subFetch = convert(subAttrFilter, attr.getRefEntity(), languageCode); } else { subFetch = createDefaultAttributeFetch(attr, languageCode); } fetch.field(attr.getName(), subFetch); break; } // $CASES-OMITTED$ default: fetch.field(attr.getName()); break; } }