@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); }
private static AttributeMetaData getAttribute(EntityMetaData entityMeta, String attrName) { AttributeMetaData attr = entityMeta.getAttribute(attrName); if (attr == null) { throw new UnknownAttributeException( format("Unknown attribute [%s] of entity [%s]", attrName, entityMeta.getName())); } return attr; }
@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)); }
@BeforeMethod public void beforeMethod() { MockitoAnnotations.initMocks(this); EntityMetaData emd = when(mock(EntityMetaData.class).getName()).thenReturn("repo").getMock(); when(emd.getLabel()).thenReturn("My repo"); repository = new InMemoryRepository(emd); annotationJob = new AnnotationJob( crudRepositoryAnnotator, "fdlk", ImmutableList.of(exac, cadd), repository, progress, authentication, new TransactionTemplate(transactionManager)); }
/** * 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; }
/** * 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); }); }
@Test public void checkMrefNullValue() { 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)); Entity refEntity0 = when(mock(Entity.class).getEntityMetaData()).thenReturn(refEntityMeta).getMock(); when(refEntity0.getIdValue()).thenReturn("refId0"); Entity refEntity1 = when(mock(Entity.class).getEntityMetaData()).thenReturn(refEntityMeta).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, null, refEntity1)); Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity0, entity0.getEntityMetaData()); assertEquals(constraints.size(), 1); }
@Test public void checkXrefEntityWrongType() { 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)); 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)); String idAttrName = "id"; String xrefAttrName = "xref"; AttributeMetaData idAttr = when(mock(AttributeMetaData.class).getName()).thenReturn(idAttrName).getMock(); when(idAttr.getDataType()).thenReturn(STRING); AttributeMetaData xrefAttr = when(mock(AttributeMetaData.class).getName()).thenReturn(xrefAttrName).getMock(); when(xrefAttr.getDataType()).thenReturn(XREF); when(xrefAttr.getRefEntity()).thenReturn(refEntityMeta); EntityMetaData entityMeta = mock(EntityMetaData.class); when(entityMeta.getName()).thenReturn("entity"); when(entityMeta.getIdAttribute()).thenReturn(idAttr); when(entityMeta.getAtomicAttributes()).thenReturn(asList(idAttr, xrefAttr)); Entity refEntity0 = when(mock(Entity.class).getEntityMetaData()) .thenReturn(otherRefEntityMeta) .getMock(); // wrong // intRangeMinMeta when(refEntity0.getIdValue()).thenReturn("otherRefId0"); Entity entity0 = when(mock(Entity.class).getEntityMetaData()).thenReturn(entityMeta).getMock(); when(entity0.getIdValue()).thenReturn("id0"); when(entity0.getEntity(xrefAttrName)).thenReturn(refEntity0); Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity0, entity0.getEntityMetaData()); assertEquals(constraints.size(), 1); }
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; } }