syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdisabat...@apache.org
Subject [4/4] syncope git commit: [SYNCOPE-645] Merge from 1_2_X
Date Thu, 19 Feb 2015 11:50:15 GMT
[SYNCOPE-645] Merge from 1_2_X


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/9cbeffd3
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/9cbeffd3
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/9cbeffd3

Branch: refs/heads/master
Commit: 9cbeffd396b76cdd52768145be1cb9a5460211a8
Parents: fc06e74 583e839
Author: Marco Di Sabatino Di Diodoro <mdisabatino@apache.org>
Authored: Thu Feb 19 12:11:38 2015 +0100
Committer: Marco Di Sabatino Di Diodoro <mdisabatino@apache.org>
Committed: Thu Feb 19 12:11:38 2015 +0100

----------------------------------------------------------------------
 .../entity/ExternalResourceValidator.java       | 12 ++++++++++
 .../persistence/jpa/entity/ResourceTest.java    | 23 ++++++++++++++++++++
 2 files changed, 35 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/9cbeffd3/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/validation/entity/ExternalResourceValidator.java
----------------------------------------------------------------------
diff --cc core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/validation/entity/ExternalResourceValidator.java
index 1d512bb,0000000..2cc90f0
mode 100644,000000..100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/validation/entity/ExternalResourceValidator.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/validation/entity/ExternalResourceValidator.java
@@@ -1,130 -1,0 +1,142 @@@
 +/*
 + * Licensed to the Apache Software Foundation (ASF) under one
 + * or more contributor license agreements.  See the NOTICE file
 + * distributed with this work for additional information
 + * regarding copyright ownership.  The ASF licenses this file
 + * to you under the Apache License, Version 2.0 (the
 + * "License"); you may not use this file except in compliance
 + * with the License.  You may obtain a copy of the License at
 + *
 + *   http://www.apache.org/licenses/LICENSE-2.0
 + *
 + * Unless required by applicable law or agreed to in writing,
 + * software distributed under the License is distributed on an
 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 + * KIND, either express or implied.  See the License for the
 + * specific language governing permissions and limitations
 + * under the License.
 + */
 +package org.apache.syncope.core.persistence.jpa.validation.entity;
 +
 +import javax.validation.ConstraintValidatorContext;
 +import org.apache.commons.lang3.StringUtils;
++import org.apache.syncope.common.lib.types.AttributableType;
 +import org.apache.syncope.common.lib.types.EntityViolationType;
 +import org.apache.syncope.core.persistence.api.entity.ExternalResource;
 +import org.apache.syncope.core.persistence.api.entity.Mapping;
 +import org.apache.syncope.core.persistence.api.entity.MappingItem;
 +import org.apache.syncope.core.provisioning.api.propagation.PropagationActions;
++import org.apache.syncope.core.persistence.api.entity.user.UMapping;
 +
 +public class ExternalResourceValidator extends AbstractValidator<ExternalResourceCheck,
ExternalResource> {
 +
 +    private boolean isValid(final MappingItem item, final ConstraintValidatorContext context)
{
 +        if (StringUtils.isBlank(item.getExtAttrName())) {
 +            context.buildConstraintViolationWithTemplate(
 +                    getTemplate(EntityViolationType.InvalidMapping, item + ".extAttrName
is null")).
 +                    addPropertyNode("extAttrName").addConstraintViolation();
 +
 +            return false;
 +        }
 +
 +        if (StringUtils.isBlank(item.getIntAttrName())) {
 +            context.buildConstraintViolationWithTemplate(
 +                    getTemplate(EntityViolationType.InvalidMapping, item + ".intAttrName
is null")).
 +                    addPropertyNode("intAttrName").addConstraintViolation();
 +
 +            return false;
 +        }
 +
 +        if (item.getPurpose() == null) {
 +            context.buildConstraintViolationWithTemplate(
 +                    getTemplate(EntityViolationType.InvalidMapping, item + ".purpose is
null")).
 +                    addPropertyNode("purpose").addConstraintViolation();
 +
 +            return false;
 +        }
 +
 +        return true;
 +    }
 +
 +    private boolean isValid(final Mapping<?> mapping, final ConstraintValidatorContext
context) {
 +        if (mapping == null) {
 +            return true;
 +        }
 +
 +        int accountIds = 0;
 +        for (MappingItem item : mapping.getItems()) {
 +            if (item.isAccountid()) {
 +                accountIds++;
 +            }
 +        }
 +        if (accountIds != 1) {
 +            context.buildConstraintViolationWithTemplate(
 +                    getTemplate(EntityViolationType.InvalidMapping, "One and only one accountId
mapping is needed")).
 +                    addPropertyNode("accountId.size").addConstraintViolation();
 +            return false;
 +        }
 +
++        final MappingItem accountId = mapping.getAccountIdItem();
++        if (mapping instanceof UMapping
++                && AttributableType.ROLE == accountId.getIntMappingType().getAttributableType())
{
++            context.buildConstraintViolationWithTemplate(
++                    getTemplate(EntityViolationType.InvalidMapping,
++                            "Role attribute as accountId is not permitted")).
++                    addPropertyNode("attributableType").addConstraintViolation();
++            return false;
++        }
++
 +        boolean isValid = true;
 +
 +        int passwords = 0;
 +        for (MappingItem item : mapping.getItems()) {
 +            isValid &= isValid(item, context);
 +
 +            if (item.isPassword()) {
 +                passwords++;
 +            }
 +        }
 +        if (passwords > 1) {
 +            context.buildConstraintViolationWithTemplate(
 +                    getTemplate(EntityViolationType.InvalidMapping, "One and only one password
mapping is allowed")).
 +                    addPropertyNode("password.size").addConstraintViolation();
 +            isValid = false;
 +        }
 +
 +        return isValid;
 +    }
 +
 +    @Override
 +    public boolean isValid(final ExternalResource resource, final ConstraintValidatorContext
context) {
 +        context.disableDefaultConstraintViolation();
 +
 +        if (!NAME_PATTERN.matcher(resource.getKey()).matches()) {
 +            context.buildConstraintViolationWithTemplate(
 +                    getTemplate(EntityViolationType.InvalidName, "Invalid Resource name")).
 +                    addPropertyNode("name").addConstraintViolation();
 +            return false;
 +        }
 +
 +        if (!resource.getPropagationActionsClassNames().isEmpty()) {
 +            for (String className : resource.getPropagationActionsClassNames()) {
 +                Class<?> actionsClass = null;
 +                boolean isAssignable = false;
 +                try {
 +                    actionsClass = Class.forName(className);
 +                    isAssignable = PropagationActions.class.isAssignableFrom(actionsClass);
 +                } catch (Exception e) {
 +                    LOG.error("Invalid PropagationActions specified: {}", className, e);
 +                }
 +
 +                if (actionsClass == null || !isAssignable) {
 +                    context.buildConstraintViolationWithTemplate(
 +                            getTemplate(EntityViolationType.InvalidResource, "Invalid actions
class name")).
 +                            addPropertyNode("actionsClassName").addConstraintViolation();
 +                    return false;
 +                }
 +            }
 +        }
 +
 +        return isValid(resource.getUmapping(), context) && isValid(resource.getRmapping(),
context);
 +    }
 +}

http://git-wip-us.apache.org/repos/asf/syncope/blob/9cbeffd3/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/ResourceTest.java
----------------------------------------------------------------------
diff --cc core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/ResourceTest.java
index 0ab257a,0000000..103b9bb
mode 100644,000000..100644
--- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/ResourceTest.java
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/ResourceTest.java
@@@ -1,266 -1,0 +1,289 @@@
 +/*
 + * Licensed to the Apache Software Foundation (ASF) under one
 + * or more contributor license agreements.  See the NOTICE file
 + * distributed with this work for additional information
 + * regarding copyright ownership.  The ASF licenses this file
 + * to you under the Apache License, Version 2.0 (the
 + * "License"); you may not use this file except in compliance
 + * with the License.  You may obtain a copy of the License at
 + *
 + *   http://www.apache.org/licenses/LICENSE-2.0
 + *
 + * Unless required by applicable law or agreed to in writing,
 + * software distributed under the License is distributed on an
 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 + * KIND, either express or implied.  See the License for the
 + * specific language governing permissions and limitations
 + * under the License.
 + */
 +package org.apache.syncope.core.persistence.jpa.entity;
 +
 +import static org.junit.Assert.assertEquals;
 +import static org.junit.Assert.assertFalse;
 +import static org.junit.Assert.assertNotNull;
 +import static org.junit.Assert.assertNull;
 +import static org.junit.Assert.assertTrue;
 +import static org.junit.Assert.fail;
 +
 +import java.util.ArrayList;
 +import java.util.List;
 +import org.apache.syncope.common.lib.types.AttributableType;
 +import org.apache.syncope.common.lib.types.EntityViolationType;
 +import org.apache.syncope.common.lib.types.IntMappingType;
 +import org.apache.syncope.common.lib.types.MappingPurpose;
 +import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException;
 +import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
 +import org.apache.syncope.core.persistence.api.entity.ConnInstance;
 +import org.apache.syncope.core.persistence.api.entity.ExternalResource;
 +import org.apache.syncope.core.persistence.api.entity.user.UMapping;
 +import org.apache.syncope.core.persistence.api.entity.user.UMappingItem;
 +import org.apache.syncope.core.persistence.jpa.AbstractTest;
 +import org.junit.Test;
 +import org.springframework.beans.factory.annotation.Autowired;
 +import org.springframework.transaction.annotation.Transactional;
 +
 +@Transactional
 +public class ResourceTest extends AbstractTest {
 +
 +    @Autowired
 +    private ExternalResourceDAO resourceDAO;
 +
 +    @Test
 +    public void findById() {
 +        ExternalResource resource = resourceDAO.find("ws-target-resource-1");
 +        assertNotNull("findById did not work", resource);
 +
 +        ConnInstance connector = resource.getConnector();
 +        assertNotNull("connector not found", connector);
 +        assertEquals("invalid connector name",
 +                "net.tirasa.connid.bundles.soap.WebServiceConnector", connector.getConnectorName());
 +        assertEquals("invalid bundle name", "net.tirasa.connid.bundles.soap", connector.getBundleName());
 +
 +        assertFalse("no mapping specified", resource.getUmapping().getItems().isEmpty());
 +
 +        List<Long> mappingIds = new ArrayList<>();
 +        for (UMappingItem item : resource.getUmapping().getItems()) {
 +            mappingIds.add(item.getKey());
 +        }
 +        assertTrue(mappingIds.contains(100L));
 +    }
 +
 +    @Test
 +    public void findAll() {
 +        List<ExternalResource> resources = resourceDAO.findAll();
 +        assertNotNull(resources);
 +        assertEquals(18, resources.size());
 +    }
 +
 +    @Test
 +    public void findAllByPriority() {
 +        List<ExternalResource> resources = resourceDAO.findAllByPriority();
 +        assertNotNull(resources);
 +        assertFalse(resources.isEmpty());
 +    }
 +
 +    @Test
 +    public void getAccountId() {
 +        ExternalResource resource = resourceDAO.find("ws-target-resource-2");
 +        assertNotNull(resource);
 +        assertEquals("fullname", resource.getUmapping().getAccountIdItem().getIntAttrName());
 +    }
 +
 +    @Test
 +    public void save() {
 +        ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
 +        resource.setKey("ws-target-resource-basic-save");
 +        resource.setPropagationPriority(2);
 +        resource.setPropagationPrimary(true);
 +
 +        UMapping mapping = entityFactory.newEntity(UMapping.class);
 +        resource.setUmapping(mapping);
 +
 +        UMappingItem accountId = entityFactory.newEntity(UMappingItem.class);
 +        accountId.setExtAttrName("username");
 +        accountId.setIntAttrName("fullname");
 +        accountId.setIntMappingType(IntMappingType.UserId);
 +        accountId.setPurpose(MappingPurpose.BOTH);
 +        mapping.setAccountIdItem(accountId);
 +
 +        ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
 +        resource.setConnector(connector);
 +
 +        // save the resource
 +        ExternalResource actual = resourceDAO.save(resource);
 +
 +        assertNotNull(actual);
 +        assertNotNull(actual.getConnector());
 +        assertNotNull(actual.getUmapping());
 +        assertFalse(actual.getUmapping().getItems().isEmpty());
 +        assertEquals(Integer.valueOf(2), actual.getPropagationPriority());
 +        assertTrue(actual.isPropagationPrimary());
 +    }
 +
 +    @Test(expected = InvalidEntityException.class)
 +    public void saveInvalidMappingIntAttr() {
 +        ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
 +        resource.setKey("ws-target-resource-basic-save-invalid");
 +
 +        ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
 +        resource.setConnector(connector);
 +
 +        UMapping mapping = entityFactory.newEntity(UMapping.class);
 +        resource.setUmapping(mapping);
 +
 +        UMappingItem accountId = entityFactory.newEntity(UMappingItem.class);
 +        accountId.setAccountid(true);
 +        accountId.setIntMappingType(IntMappingType.UserPlainSchema);
 +        mapping.addItem(accountId);
 +
 +        // save the resource
 +        ExternalResource actual = resourceDAO.save(resource);
 +        assertNotNull(actual);
 +    }
 +
 +    @Test(expected = IllegalArgumentException.class)
 +    public void saveInvalidAccountIdMapping() {
 +        ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
 +        resource.setKey("ws-target-resource-basic-save-invalid");
 +
 +        ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
 +        resource.setConnector(connector);
 +
 +        UMapping mapping = entityFactory.newEntity(UMapping.class);
 +        resource.setUmapping(mapping);
 +
 +        UMappingItem accountId = entityFactory.newEntity(UMappingItem.class);
 +        accountId.setAccountid(true);
 +        accountId.setIntMappingType(IntMappingType.UserVirtualSchema);
 +        mapping.setAccountIdItem(accountId);
 +
 +        // save the resource
 +        ExternalResource actual = resourceDAO.save(resource);
 +        assertNotNull(actual);
 +    }
 +
 +    @Test(expected = InvalidEntityException.class)
 +    public void saveInvalidMappingExtAttr() {
 +        ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
 +        resource.setKey("ws-target-resource-basic-save-invalid");
 +
 +        ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
 +        resource.setConnector(connector);
 +
 +        UMapping mapping = entityFactory.newEntity(UMapping.class);
 +        resource.setUmapping(mapping);
 +
 +        UMappingItem item = entityFactory.newEntity(UMappingItem.class);
 +        item.setAccountid(true);
 +        item.setIntAttrName("fullname");
 +        item.setIntMappingType(IntMappingType.UserPlainSchema);
 +        mapping.addItem(item);
 +
 +        item = entityFactory.newEntity(UMappingItem.class);
 +        item.setIntAttrName("userId");
 +        item.setIntMappingType(IntMappingType.UserPlainSchema);
 +        mapping.addItem(item);
 +
 +        ExternalResource actual = resourceDAO.save(resource);
 +        assertNotNull(actual);
 +    }
 +
 +    @Test
 +    public void saveWithRoleMappingType() {
 +        ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
 +        resource.setKey("ws-target-resource-basic-save-invalid");
 +
 +        ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
 +        resource.setConnector(connector);
 +
 +        UMapping mapping = entityFactory.newEntity(UMapping.class);
 +        resource.setUmapping(mapping);
 +
 +        UMappingItem item = entityFactory.newEntity(UMappingItem.class);
 +        item.setIntAttrName("fullname");
 +        item.setExtAttrName("fullname");
 +        item.setIntMappingType(IntMappingType.UserPlainSchema);
 +        item.setPurpose(MappingPurpose.BOTH);
 +        mapping.setAccountIdItem(item);
 +
 +        item = entityFactory.newEntity(UMappingItem.class);
 +        item.setIntAttrName("icon");
 +        item.setExtAttrName("icon");
 +        item.setIntMappingType(IntMappingType.RolePlainSchema);
 +        item.setPurpose(MappingPurpose.BOTH);
 +        mapping.addItem(item);
 +
 +        item = entityFactory.newEntity(UMappingItem.class);
 +        item.setIntAttrName("mderiveddata");
 +        item.setExtAttrName("mderiveddata");
 +        item.setIntMappingType(IntMappingType.MembershipDerivedSchema);
 +        item.setPurpose(MappingPurpose.BOTH);
 +        mapping.addItem(item);
 +
 +        // save the resource
 +        ExternalResource actual = resourceDAO.save(resource);
 +        assertNotNull(actual);
 +
 +        int items = 0;
 +        for (UMappingItem mapItem : actual.getUmapping().getItems()) {
 +            items++;
 +
 +            if ("icon".equals(mapItem.getIntAttrName())) {
 +                assertTrue(IntMappingType.contains(AttributableType.ROLE,
 +                        mapItem.getIntMappingType().toString()));
 +            }
 +            if ("mderiveddata".equals(mapItem.getIntAttrName())) {
 +                assertTrue(IntMappingType.contains(AttributableType.MEMBERSHIP,
 +                        mapItem.getIntMappingType().toString()));
 +            }
 +        }
 +        assertEquals(3, items);
 +    }
 +
 +    @Test
 +    public void delete() {
 +        ExternalResource resource = resourceDAO.find("ws-target-resource-2");
 +        assertNotNull(resource);
 +
 +        resourceDAO.delete(resource.getKey());
 +
 +        ExternalResource actual = resourceDAO.find("ws-target-resource-2");
 +        assertNull(actual);
 +    }
 +
 +    @Test
 +    public void issueSYNCOPE418() {
 +        ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
 +        resource.setKey("http://schemas.examples.org/security/authorization/organizationUnit");
 +
 +        try {
 +            resourceDAO.save(resource);
 +            fail();
 +        } catch (InvalidEntityException e) {
 +            assertTrue(e.hasViolation(EntityViolationType.InvalidName));
 +        }
 +    }
++
++    @Test(expected = InvalidEntityException.class)
++    public void issueSYNCOPE645() {
++        ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
++        resource.setKey("ws-target-resource-basic-save-invalid");
++
++        ConnInstance connector = resourceDAO.find("ws-target-resource-1").getConnector();
++        resource.setConnector(connector);
++
++        UMapping mapping = entityFactory.newEntity(UMapping.class);
++        resource.setUmapping(mapping);
++
++        final UMappingItem item = entityFactory.newEntity(UMappingItem.class);
++        item.setIntAttrName("icon");
++        item.setExtAttrName("icon");
++        item.setIntMappingType(IntMappingType.RolePlainSchema);
++        item.setPurpose(MappingPurpose.BOTH);
++        mapping.setAccountIdItem(item);
++
++        // save the resource
++        ExternalResource actual = resourceDAO.save(resource);
++        assertNotNull(actual);
++    }
 +}


Mime
View raw message