use of org.molgenis.data.meta.MetaDataService in project molgenis by molgenis.
the class PlatformIT method testDeletePackageWithOneToMany.
@WithMockUser(username = USERNAME)
@Test(singleThreaded = true)
public void testDeletePackageWithOneToMany() {
populateUserPermissions();
runAsSystem(() -> {
MetaDataService metadataService = dataService.getMeta();
Package package_ = packageFactory.create("package_onetomany").setLabel("package");
metadataService.upsertPackages(Stream.of(package_));
EntityType refEntityType = testHarness.createDynamicRefEntityType("entityType_onetomany", package_);
EntityType entityType = testHarness.createDynamicTestEntityType("refEntityType_onetomany", package_, refEntityType);
Attribute oneToManyAttribute = attributeFactory.create("onetomany").setName("onetomany").setDataType(AttributeType.ONE_TO_MANY).setRefEntity(entityType).setMappedBy(entityType.getAttribute(ATTR_XREF));
refEntityType.addAttribute(oneToManyAttribute);
metadataService.upsertEntityTypes(asList(refEntityType, entityType));
List<Entity> entities = createAndAdd(entityType, refEntityType, 5);
Set<Entity> refEntities = entities.stream().map(e -> e.getEntity(ATTR_XREF)).collect(toSet());
assertPresent(entityType, entities);
assertPresent(refEntityType, newArrayList(refEntities));
dataService.deleteById(PACKAGE, "package_onetomany");
assertNull(metadataService.getPackage("package_onetomany"));
assertNull(dataService.getEntityType(entityType.getId()));
assertNull(dataService.getEntityType(refEntityType.getId()));
entities.forEach(this::assertNotPresent);
refEntities.forEach(this::assertNotPresent);
});
}
use of org.molgenis.data.meta.MetaDataService in project molgenis by molgenis.
the class DataPersisterImplTest method setUpBeforeMethod.
@SuppressWarnings("unchecked")
@BeforeMethod
public void setUpBeforeMethod() {
dataService = mock(DataService.class, RETURNS_DEEP_STUBS);
dataPersisterImpl = new DataPersisterImpl(metaDataService, dataService, entityTypeDependencyResolver);
String entityTypeId0 = "entityTypeId0";
entityType0 = mock(EntityType.class);
when(entityType0.getId()).thenReturn(entityTypeId0).getMock();
String entityTypeId1 = "entityTypeId1";
entityType1 = mock(EntityType.class);
when(entityType1.getId()).thenReturn(entityTypeId1).getMock();
String entityTypeId2 = "entityTypeId2";
entityType2 = mock(EntityType.class);
when(entityType2.getId()).thenReturn(entityTypeId2).getMock();
when(dataProvider.getEntityTypes()).thenReturn(Stream.of(entityType0, entityType1, entityType2));
when(dataProvider.hasEntities(entityType0)).thenReturn(true);
Entity entity0a = mock(Entity.class);
Entity entity0b = mock(Entity.class);
when(dataProvider.getEntities(entityType0)).thenReturn(Stream.of(entity0a, entity0b));
when(dataProvider.hasEntities(entityType1)).thenReturn(true);
Entity entity1a = mock(Entity.class);
Entity entity1b = mock(Entity.class);
Entity entity1c = mock(Entity.class);
when(dataProvider.getEntities(entityType1)).thenReturn(Stream.of(entity1a, entity1b, entity1c));
when(dataProvider.hasEntities(entityType2)).thenReturn(false);
when(entityTypeDependencyResolver.resolve(asList(entityType0, entityType1, entityType2))).thenReturn(asList(entityType2, entityType1, entityType0));
doAnswer(invocation -> {
Stream<Entity> entityStream = (Stream<Entity>) invocation.getArguments()[1];
try {
// noinspection ResultOfMethodCallIgnored
entityStream.collect(toList());
} catch (IllegalStateException ignored) {
}
return null;
}).when(dataService).add(anyString(), any(Stream.class));
doAnswer(invocation -> {
Stream<Entity> entityStream = (Stream<Entity>) invocation.getArguments()[1];
try {
// noinspection ResultOfMethodCallIgnored
entityStream.collect(toList());
} catch (IllegalStateException ignored) {
}
return null;
}).when(dataService).update(anyString(), any(Stream.class));
}
use of org.molgenis.data.meta.MetaDataService 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.meta.MetaDataService in project molgenis by molgenis.
the class DataServiceImplTest method beforeMethod.
@SuppressWarnings("unchecked")
@BeforeMethod
public void beforeMethod() {
dataService = new DataServiceImpl();
repo1 = when(mock(Repository.class).getName()).thenReturn("Entity1").getMock();
repo2 = mock(Repository.class);
repo2 = when(mock(Repository.class).getName()).thenReturn("Entity2").getMock();
repoToRemove = mock(Repository.class);
repoToRemove = when(mock(Repository.class).getName()).thenReturn("Entity3").getMock();
metaDataService = mock(MetaDataService.class);
when(metaDataService.getRepository("Entity1")).thenReturn(repo1);
when(metaDataService.getRepository("Entity2")).thenReturn(repo2);
when(metaDataService.getRepository("Entity3")).thenReturn(repoToRemove);
EntityType entityType1 = when(mock(EntityType.class).getId()).thenReturn("Entity1").getMock();
EntityType entityType2 = when(mock(EntityType.class).getId()).thenReturn("Entity2").getMock();
EntityType entityType3 = when(mock(EntityType.class).getId()).thenReturn("Entity3").getMock();
when(metaDataService.getEntityTypes()).thenAnswer(invocation -> Stream.of(entityType1, entityType2, entityType3));
dataService.setMetaDataService(metaDataService);
}
use of org.molgenis.data.meta.MetaDataService in project molgenis by molgenis.
the class DataExplorerControllerTest method initSortEntitiesByLabel.
@Test
public void initSortEntitiesByLabel() {
MetaDataService metaDataService = mock(MetaDataService.class);
when(dataService.getMeta()).thenReturn(metaDataService);
EntityType entity1 = mock(EntityType.class);
when(entity1.getId()).thenReturn("1");
when(entity1.getLabel()).thenReturn("zzz");
EntityType entity2 = mock(EntityType.class);
when(entity2.getId()).thenReturn("2");
when(entity2.getLabel()).thenReturn("aaa");
Stream<EntityType> entityStream = Stream.of(entity1, entity2);
when(metaDataService.getEntityTypes()).thenReturn(entityStream);
controller.init(null, null, model);
LinkedHashMap expected = new LinkedHashMap<>(Stream.of(entity1, entity2).sorted(Comparator.comparing(EntityType::getLabel)).collect(Collectors.toMap(EntityType::getId, Function.identity())));
verify(model).addAttribute("entitiesMeta", expected);
}
Aggregations