Search in sources :

Example 21 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class PackageRepositoryValidationDecoratorTest method testDeleteAllValid.

@Test
public void testDeleteAllValid() throws Exception {
    Package package_ = mock(Package.class);
    when(delegateRepository.iterator()).thenReturn(singletonList(package_).iterator());
    packageRepositoryValidationDecorator.deleteAll();
    verify(packageValidator).validate(package_);
    verify(delegateRepository).deleteAll();
}
Also used : Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test)

Example 22 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class PackageRepositoryValidationDecoratorTest method testDeleteValid.

@Test
public void testDeleteValid() throws Exception {
    Package package_ = mock(Package.class);
    packageRepositoryValidationDecorator.delete(package_);
    verify(packageValidator).validate(package_);
    verify(delegateRepository).delete(package_);
}
Also used : Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test)

Example 23 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class PackageRepositoryValidationDecoratorTest method testUpdateInvalid.

@Test(expectedExceptions = MolgenisValidationException.class)
public void testUpdateInvalid() throws Exception {
    Package package_ = mock(Package.class);
    doThrow(mock(MolgenisValidationException.class)).when(packageValidator).validate(package_);
    packageRepositoryValidationDecorator.update(package_);
}
Also used : Package(org.molgenis.data.meta.model.Package) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 24 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class PackageValidatorTest method testValidatePackageValidNameNoParent.

@Test
public void testValidatePackageValidNameNoParent() throws Exception {
    Package package_ = when(mock(Package.class).getId()).thenReturn("myPackage").getMock();
    when(package_.getParent()).thenReturn(null);
    packageValidator.validate(package_);
}
Also used : Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test)

Example 25 with Package

use of org.molgenis.data.meta.model.Package in project molgenis by molgenis.

the class EntityTypeValidator method validateOwnAttributes.

/**
 * Validates the attributes owned by this entity:
 * 1) validates that the parent entity doesn't have attributes with the same name
 * 2) validates that this entity doesn't have attributes with the same name
 * 3) validates that this entity has attributes defined at all
 *
 * @param entityType entity meta data
 * @throws MolgenisValidationException if an attribute is owned by another entity or a parent attribute has the same name
 */
private static void validateOwnAttributes(EntityType entityType) {
    // Validate that entity has attributes
    if (asStream(entityType.getAllAttributes()).collect(toList()).isEmpty()) {
        throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] does not contain any attributes. Did you use the correct package+entity name combination in both the entities as well as the attributes sheet?", entityType.getId())));
    }
    // Validate that entity does not contain multiple attributes with the same name
    Multimap<String, Attribute> attrMultiMap = asStream(entityType.getAllAttributes()).collect(MultimapCollectors.toArrayListMultimap(Attribute::getName, Function.identity()));
    attrMultiMap.keySet().forEach(attrName -> {
        if (attrMultiMap.get(attrName).size() > 1) {
            throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] contains multiple attributes with name [%s]", entityType.getId(), attrName)));
        }
    });
    // Validate that entity attributes with same name do no exist in parent entity
    EntityType extendsEntityType = entityType.getExtends();
    if (extendsEntityType != null) {
        Map<String, Attribute> extendsAllAttrMap = stream(extendsEntityType.getAllAttributes().spliterator(), false).collect(toMap(Attribute::getName, Function.identity(), (u, v) -> {
            throw new IllegalStateException(String.format("Duplicate key %s", u));
        }, LinkedHashMap::new));
        entityType.getOwnAllAttributes().forEach(attr -> {
            if (extendsAllAttrMap.containsKey(attr.getName())) {
                throw new MolgenisValidationException(new ConstraintViolation(format("An attribute with name [%s] already exists in entity [%s] or one of its parents", attr.getName(), extendsEntityType.getId())));
            }
        });
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) Multimap(com.google.common.collect.Multimap) Attribute(org.molgenis.data.meta.model.Attribute) Function(java.util.function.Function) EntityUtils.asStream(org.molgenis.data.util.EntityUtils.asStream) LinkedHashMap(java.util.LinkedHashMap) MetaUtils(org.molgenis.data.meta.MetaUtils) ENTITY_TYPE_META_DATA(org.molgenis.data.meta.model.EntityTypeMetadata.ENTITY_TYPE_META_DATA) Collectors.toMap(java.util.stream.Collectors.toMap) AttributeUtils(org.molgenis.data.support.AttributeUtils) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SystemEntityTypeRegistry(org.molgenis.data.meta.system.SystemEntityTypeRegistry) ATTRIBUTE_META_DATA(org.molgenis.data.meta.model.AttributeMetadata.ATTRIBUTE_META_DATA) RepositoryCollection(org.molgenis.data.RepositoryCollection) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) MultimapCollectors(org.molgenis.util.stream.MultimapCollectors) EntityType(org.molgenis.data.meta.model.EntityType) String.format(java.lang.String.format) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) PACKAGE(org.molgenis.data.meta.model.PackageMetadata.PACKAGE) StreamSupport.stream(java.util.stream.StreamSupport.stream) DataService(org.molgenis.data.DataService) Package(org.molgenis.data.meta.model.Package) MolgenisDataException(org.molgenis.data.MolgenisDataException) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Aggregations

Package (org.molgenis.data.meta.model.Package)98 Test (org.testng.annotations.Test)70 EntityType (org.molgenis.data.meta.model.EntityType)22 Stream (java.util.stream.Stream)20 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)13 Attribute (org.molgenis.data.meta.model.Attribute)11 DefaultPackage (org.molgenis.data.meta.DefaultPackage)10 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)10 Objects.requireNonNull (java.util.Objects.requireNonNull)6 L10nString (org.molgenis.data.i18n.model.L10nString)6 PackageIdentity (org.molgenis.data.security.PackageIdentity)6 Collectors.toList (java.util.stream.Collectors.toList)5 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)5 DataService (org.molgenis.data.DataService)5 QueryImpl (org.molgenis.data.support.QueryImpl)5 RepositoryCollection (org.molgenis.data.RepositoryCollection)4 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)3 java.util (java.util)3 Collections.singletonList (java.util.Collections.singletonList)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3