use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class RepositoryValidationDecoratorTest method beforeMethod.
@SuppressWarnings("unchecked")
@BeforeMethod
public void beforeMethod() {
// ref entity meta
refEntityName = "refEntity";
refAttrIdName = "refId";
Attribute refIdAttr = when(mock(Attribute.class).getName()).thenReturn(refAttrIdName).getMock();
when(refIdAttr.getDataType()).thenReturn(STRING);
refEntityType = mock(EntityType.class);
when(refEntityType.getId()).thenReturn(refEntityName);
when(refEntityType.getLabel()).thenReturn(refEntityName);
when(refEntityType.getIdAttribute()).thenReturn(refIdAttr);
when(refEntityType.getAtomicAttributes()).thenReturn(Arrays.asList(refIdAttr));
// entity meta
entityTypeId = "entity";
attrIdName = "id";
attrXrefName = "xrefAttr";
attrNillableXrefName = "nillableXrefAttr";
attrMrefName = "mrefAttr";
attrNillableMrefName = "nillableMrefAttr";
attrUniqueStringName = "uniqueStringAttr";
attrUniqueXrefName = "uniqueXrefAttr";
idAttr = when(mock(Attribute.class).getName()).thenReturn(attrIdName).getMock();
when(idAttr.getDataType()).thenReturn(STRING);
xrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrXrefName).getMock();
when(xrefAttr.getRefEntity()).thenReturn(refEntityType);
when(xrefAttr.getDataType()).thenReturn(XREF);
when(xrefAttr.isNillable()).thenReturn(false);
nillableXrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrNillableXrefName).getMock();
when(nillableXrefAttr.getRefEntity()).thenReturn(refEntityType);
when(nillableXrefAttr.getDataType()).thenReturn(XREF);
when(nillableXrefAttr.isNillable()).thenReturn(true);
mrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrMrefName).getMock();
when(mrefAttr.getRefEntity()).thenReturn(refEntityType);
when(mrefAttr.getDataType()).thenReturn(MREF);
when(mrefAttr.isNillable()).thenReturn(false);
nillableMrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrNillableMrefName).getMock();
when(nillableMrefAttr.getRefEntity()).thenReturn(refEntityType);
when(nillableMrefAttr.getDataType()).thenReturn(MREF);
when(nillableMrefAttr.isNillable()).thenReturn(true);
uniqueStringAttr = when(mock(Attribute.class).getName()).thenReturn(attrUniqueStringName).getMock();
when(uniqueStringAttr.getDataType()).thenReturn(STRING);
when(uniqueStringAttr.isUnique()).thenReturn(true);
uniqueXrefAttr = when(mock(Attribute.class).getName()).thenReturn(attrUniqueXrefName).getMock();
when(uniqueXrefAttr.getRefEntity()).thenReturn(refEntityType);
when(uniqueXrefAttr.getDataType()).thenReturn(XREF);
when(uniqueXrefAttr.isUnique()).thenReturn(true);
entityType = mock(EntityType.class);
when(entityType.getId()).thenReturn(entityTypeId);
when(entityType.getLabel()).thenReturn(entityTypeId);
when(entityType.getIdAttribute()).thenReturn(idAttr);
when(entityType.getAttribute(attrIdName)).thenReturn(idAttr);
when(entityType.getAttribute(attrXrefName)).thenReturn(xrefAttr);
when(entityType.getAttribute(attrNillableXrefName)).thenReturn(nillableXrefAttr);
when(entityType.getAttribute(attrMrefName)).thenReturn(mrefAttr);
when(entityType.getAttribute(attrNillableMrefName)).thenReturn(nillableMrefAttr);
when(entityType.getAttribute(attrUniqueStringName)).thenReturn(uniqueStringAttr);
when(entityType.getAttribute(attrUniqueXrefName)).thenReturn(uniqueXrefAttr);
when(entityType.getAtomicAttributes()).thenReturn(Arrays.asList(idAttr, xrefAttr, nillableXrefAttr, mrefAttr, nillableMrefAttr, uniqueStringAttr, uniqueXrefAttr));
// ref entities
String refEntity0Id = "idref0";
refEntity0 = mock(Entity.class);
when(refEntity0.getEntityType()).thenReturn(refEntityType);
when(refEntity0.getIdValue()).thenReturn(refEntity0Id);
when(refEntity0.get(refAttrIdName)).thenReturn(refEntity0Id);
when(refEntity0.getString(refAttrIdName)).thenReturn(refEntity0Id);
refEntity0Clone = mock(Entity.class);
when(refEntity0Clone.getEntityType()).thenReturn(refEntityType);
when(refEntity0Clone.getIdValue()).thenReturn(refEntity0Id);
when(refEntity0Clone.get(refAttrIdName)).thenReturn(refEntity0Id);
when(refEntity0Clone.getString(refAttrIdName)).thenReturn(refEntity0Id);
String refEntity1Id = "idref1";
refEntity1 = mock(Entity.class);
when(refEntity1.getEntityType()).thenReturn(refEntityType);
when(refEntity1.getIdValue()).thenReturn(refEntity1Id);
when(refEntity1.get(refAttrIdName)).thenReturn(refEntity1Id);
when(refEntity1.getString(refAttrIdName)).thenReturn(refEntity1Id);
refEntity1Clone = mock(Entity.class);
when(refEntity1Clone.getEntityType()).thenReturn(refEntityType);
when(refEntity1Clone.getIdValue()).thenReturn(refEntity1Id);
when(refEntity1Clone.get(refAttrIdName)).thenReturn(refEntity1Id);
when(refEntity1Clone.getString(refAttrIdName)).thenReturn(refEntity1Id);
// beans
delegateRepository = mock(Repository.class);
when(delegateRepository.getEntityType()).thenReturn(entityType);
when(delegateRepository.getName()).thenReturn(entityTypeId);
when(delegateRepository.findAll(new QueryImpl<>().fetch(new Fetch().field(attrUniqueStringName).field(attrUniqueXrefName)))).thenReturn(Stream.empty());
refRepo = mock(Repository.class);
when(refRepo.getEntityType()).thenReturn(refEntityType);
dataService = mock(DataService.class);
when(dataService.getRepository(entityTypeId)).thenReturn(delegateRepository);
when(dataService.getRepository(refEntityName)).thenReturn(refRepo);
when(dataService.findAll(refEntityName, new QueryImpl<>().fetch(new Fetch().field(refAttrIdName)))).thenReturn(Stream.of(refEntity0, refEntity1));
metaDataService = mock(MetaDataService.class);
when(dataService.getMeta()).thenReturn(metaDataService);
expressionValidator = mock(ExpressionValidator.class);
entityAttributesValidator = mock(EntityAttributesValidator.class);
defaultValueReferenceValidator = mock(DefaultValueReferenceValidator.class);
repositoryValidationDecorator = new RepositoryValidationDecorator(dataService, delegateRepository, entityAttributesValidator, defaultValueReferenceValidator);
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class UserServiceImplTest method getUser.
@Test
public void getUser() {
String username = "username";
User existingUser = mock(User.class);
when(existingUser.getId()).thenReturn("1");
when(existingUser.getUsername()).thenReturn(username);
when(existingUser.getPassword()).thenReturn("encrypted-password");
when(dataService.findOne(USER, new QueryImpl<User>().eq(UserMetaData.USERNAME, username), User.class)).thenReturn(existingUser);
assertEquals(molgenisUserServiceImpl.getUser(username), existingUser);
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class DefaultEntityValidator method checkUniques.
private Set<ConstraintViolation> checkUniques(Iterable<? extends Entity> entities, EntityType meta, DatabaseAction dbAction) {
Set<ConstraintViolation> violations = Sets.newLinkedHashSet();
for (Attribute attr : meta.getAtomicAttributes()) {
if (attr.isUnique() && !attr.equals(meta.getIdAttribute()) && !(attr.equals(meta.getLabelAttribute()) && (dbAction == DatabaseAction.ADD_UPDATE_EXISTING))) {
// Gather all attribute values
// FIXME: keeping everything in memory is not scaleable
List<Object> values = Lists.newArrayList();
for (Entity entity : entities) {
Object value = entity.get(attr.getName());
values.add(value);
}
// Create 'in' query, should find only find itself or nothing
if (!values.isEmpty()) {
String entityTypeId = meta.getId();
long count = dataService.count(entityTypeId, new QueryImpl<>().in(attr.getName(), values));
if (count > 0) {
// Go through the list to find the violators
long found = 0;
Iterator<? extends Entity> it = entities.iterator();
while (it.hasNext() && (found < count)) {
Entity entity = it.next();
Object value = entity.get(attr.getName());
Entity existing = dataService.findOne(entityTypeId, new QueryImpl<>().eq(attr.getName(), value));
if (existing != null) {
// if dbAction is ADD
if (((dbAction == null) && !idEquals(entity, existing, meta)) || ((dbAction != null) && (dbAction == DatabaseAction.ADD))) {
String message = String.format("The attribute '%s' of entity '%s' with key '%s' must be unique, but the value '%s' already exists.", attr.getName(), meta.getId(), entity.getString(meta.getLabelAttribute().getName()), value);
violations.add(new ConstraintViolation(message));
}
found++;
}
}
}
}
}
}
return violations;
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class QueryValidatorTest method validateValidProvider.
@DataProvider(name = "validateValidProvider")
public static Iterator<Object[]> validateValidProvider() {
List<Object[]> queries = new ArrayList<>(256);
EnumSet.of(EQUALS).forEach(operator -> {
// BOOL
Entity boolEntityType = createEntityType(BOOL);
asList(Boolean.TRUE, Boolean.FALSE, null, "true", "false", "True", "False").forEach(value -> queries.add(new Object[] { boolEntityType, new QueryImpl<>().eq("attr", value) }));
// CATEGORICAL, XREF, CATEGORICAL_MREF, MREF, ONE_TO_MANY
EnumSet.of(STRING, INT, LONG, EMAIL, HYPERLINK).forEach(refIdAttrType -> EnumSet.of(CATEGORICAL, XREF, CATEGORICAL_MREF, MREF, ONE_TO_MANY).forEach(refAttrType -> {
Entity refEntityType = createEntityType(refAttrType, refIdAttrType);
asList("1", 1, 1L, null).forEach(idValue -> queries.add(new Object[] { refEntityType, new QueryImpl<>().eq("attr", idValue) }));
Entity refEntity = when(mock(Entity.class).getIdValue()).thenReturn("1").getMock();
queries.add(new Object[] { refEntityType, new QueryImpl<>().eq("attr", refEntity) });
}));
// DATE
Entity dateEntityType = createEntityType(DATE);
asList(LocalDate.now(), "2016-11-25", null).forEach(value -> queries.add(new Object[] { dateEntityType, new QueryImpl<>().eq("attr", value) }));
// DATE_TIME
Entity dateTimeEntityType = createEntityType(DATE_TIME);
asList(Instant.now(), "1985-08-12T11:12:13+0500", null).forEach(value -> queries.add(new Object[] { dateTimeEntityType, new QueryImpl<>().eq("attr", value) }));
// DECIMAL
Entity decimalEntityType = createEntityType(DECIMAL);
asList(1.23, "1.23", 1, 1L, null).forEach(value -> queries.add(new Object[] { decimalEntityType, new QueryImpl<>().eq("attr", value) }));
// EMAIL, HTML, HYPERLINK, SCRIPT, STRING, TEXT
EnumSet.of(EMAIL, HTML, HYPERLINK, SCRIPT, STRING, TEXT).forEach(attrType -> {
Entity entityType = createEntityType(attrType);
asList("abc", 1, 1L, 1.23, null).forEach(value -> queries.add(new Object[] { entityType, new QueryImpl<>().eq("attr", value) }));
});
// INT, LONG
EnumSet.of(INT, LONG).forEach(attrType -> {
Entity entityType = createEntityType(attrType);
asList(1, 1L, "1", null).forEach(value -> queries.add(new Object[] { entityType, new QueryImpl<>().eq("attr", value) }));
});
// FILE
Entity fileEntityType = createEntityType(FILE, STRING);
asList("file0", mock(FileMeta.class), null).forEach(idValue -> queries.add(new Object[] { fileEntityType, new QueryImpl<>().eq("attr", idValue) }));
// ENUM
Entity enumEntityType = createEntityType(ENUM);
asList(TestEnum.ENUM0, TestEnum.ENUM1, "ENUM0", "ENUM1", null).forEach(value -> queries.add(new Object[] { enumEntityType, new QueryImpl<>().eq("attr", value) }));
});
EnumSet.of(GREATER, GREATER_EQUAL, LESS, LESS_EQUAL).forEach(operator -> {
Entity entityType = createEntityType(INT);
QueryImpl<Entity> query = new QueryImpl<>();
query.addRule(new QueryRule("attr", operator, 1));
queries.add(new Object[] { entityType, query });
});
EnumSet.of(FUZZY_MATCH, FUZZY_MATCH_NGRAM, LIKE).forEach(operator -> {
Entity entityType = createEntityType(STRING);
QueryImpl<Entity> query = new QueryImpl<>();
query.addRule(new QueryRule("attr", operator, "abc"));
queries.add(new Object[] { entityType, query });
});
EnumSet.of(IN, RANGE).forEach(operator -> {
Entity entityType = createEntityType(INT);
QueryImpl<Entity> query = new QueryImpl<>();
query.addRule(new QueryRule("attr", operator, asList(1, 2)));
queries.add(new Object[] { entityType, query });
});
return queries.iterator();
}
use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.
the class AttributeRepositorySecurityDecoratorTest method findOneQueryUserPermissionDenied.
@WithMockUser(username = USERNAME)
@Test
public void findOneQueryUserPermissionDenied() throws Exception {
String entityType0Name = "entity0";
EntityType entityType0 = when(mock(EntityType.class).getId()).thenReturn(entityType0Name).getMock();
String attr0Name = "entity0attr0";
Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn(attr0Name).getMock();
when(attr0.getEntity()).thenReturn(entityType0);
Query<Attribute> q = new QueryImpl<>();
when(delegateRepository.findOne(q)).thenReturn(attr0);
when(permissionService.hasPermission(new EntityTypeIdentity(entityType0Name), EntityTypePermission.COUNT)).thenReturn(false);
assertNull(repo.findOne(q));
}
Aggregations