http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/ResourceTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/ResourceTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/ResourceTest.java new file mode 100644 index 0000000..0ab257a --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/ResourceTest.java @@ -0,0 +1,266 @@ +/* + * 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)); + } + } +}
http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/RoleTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/RoleTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/RoleTest.java new file mode 100644 index 0000000..7bab7d4 --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/RoleTest.java @@ -0,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.entity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.List; +import org.apache.syncope.core.persistence.api.dao.PolicyDAO; +import org.apache.syncope.core.persistence.api.dao.RoleDAO; +import org.apache.syncope.core.persistence.api.entity.AccountPolicy; +import org.apache.syncope.core.persistence.api.entity.PasswordPolicy; +import org.apache.syncope.core.persistence.api.entity.role.Role; +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 RoleTest extends AbstractTest { + + @Autowired + private RoleDAO roleDAO; + + @Autowired + private PolicyDAO policyDAO; + + @Test + public void findAll() { + List<Role> list = roleDAO.findAll(); + assertEquals("did not get expected number of roles ", 14, list.size()); + } + + @Test + public void findChildren() { + assertEquals(3, roleDAO.findChildren(roleDAO.find(4L)).size()); + } + + @Test + public void find() { + Role role = roleDAO.find("root", null); + assertNotNull("did not find expected role", role); + role = roleDAO.find(null, null); + assertNull("found role but did not expect it", role); + } + + @Test + public void inheritedAttributes() { + Role director = roleDAO.find(7L); + + assertEquals(1, director.findLastInheritedAncestorPlainAttrs().size()); + } + + @Test + public void inheritedDerivedAttributes() { + Role director = roleDAO.find(7L); + + assertEquals(1, director.findLastInheritedAncestorDerAttrs().size()); + } + + @Test + public void inheritedVirtualAttributes() { + Role director = roleDAO.find(7L); + + assertEquals(1, director.findLastInheritedAncestorVirAttrs().size()); + } + + @Test + public void inheritedPolicy() { + Role role = roleDAO.find(7L); + + assertNotNull(role); + + assertNotNull(role.getAccountPolicy()); + assertNotNull(role.getPasswordPolicy()); + + assertEquals(4, role.getPasswordPolicy().getKey(), 0); + + role = roleDAO.find(5L); + + assertNotNull(role); + + assertNull(role.getAccountPolicy()); + assertNull(role.getPasswordPolicy()); + } + + @Test + public void save() { + Role role = entityFactory.newEntity(Role.class); + role.setName("secondChild"); + + // verify inheritance password and account policies + role.setInheritAccountPolicy(false); + // not inherited so setter execution shouldn't be ignored + role.setAccountPolicy((AccountPolicy) policyDAO.find(6L)); + + role.setInheritPasswordPolicy(true); + // inherited so setter execution should be ignored + role.setPasswordPolicy((PasswordPolicy) policyDAO.find(4L)); + + Role rootRole = roleDAO.find("root", null); + role.setParent(rootRole); + + role = roleDAO.save(role); + + Role actual = roleDAO.find(role.getKey()); + assertNotNull("expected save to work", actual); + + assertNull(role.getPasswordPolicy()); + assertNotNull(role.getAccountPolicy()); + assertEquals(Long.valueOf(6), role.getAccountPolicy().getKey()); + } + + @Test + public void delete() { + Role role = roleDAO.find(4L); + roleDAO.delete(role.getKey()); + + Role actual = roleDAO.find(4L); + assertNull("delete did not work", actual); + + Role children = roleDAO.find(7L); + assertNull("delete of successors did not work", children); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/SecurityQuestionTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/SecurityQuestionTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/SecurityQuestionTest.java new file mode 100644 index 0000000..950bccc --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/SecurityQuestionTest.java @@ -0,0 +1,68 @@ +/* + * 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.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.List; +import org.apache.syncope.core.persistence.api.dao.SecurityQuestionDAO; +import org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion; +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 SecurityQuestionTest extends AbstractTest { + + @Autowired + private SecurityQuestionDAO securityQuestionDAO; + + @Test + public void find() { + SecurityQuestion securityQuestion = securityQuestionDAO.find(1L); + assertNotNull(securityQuestion); + assertNotNull(securityQuestion.getContent()); + } + + @Test + public void findAll() { + List<SecurityQuestion> securityQuestions = securityQuestionDAO.findAll(); + assertNotNull(securityQuestions); + assertFalse(securityQuestions.isEmpty()); + } + + @Test + public void save() { + SecurityQuestion securityQuestion = entityFactory.newEntity(SecurityQuestion.class); + securityQuestion.setContent("What is your favorite pet's name?"); + + SecurityQuestion actual = securityQuestionDAO.save(securityQuestion); + assertNotNull(actual); + assertNotNull(actual.getKey()); + } + + @Test + public void delete() { + securityQuestionDAO.delete(1L); + assertNull(securityQuestionDAO.find(1L)); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/TaskExecTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/TaskExecTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/TaskExecTest.java new file mode 100644 index 0000000..99f44ae --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/TaskExecTest.java @@ -0,0 +1,94 @@ +/* + * 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.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import java.util.List; +import org.apache.syncope.common.lib.types.PropagationTaskExecStatus; +import org.apache.syncope.common.lib.types.TaskType; +import org.apache.syncope.core.persistence.api.dao.TaskDAO; +import org.apache.syncope.core.persistence.api.dao.TaskExecDAO; +import org.apache.syncope.core.persistence.api.entity.task.PropagationTask; +import org.apache.syncope.core.persistence.api.entity.task.TaskExec; +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 TaskExecTest extends AbstractTest { + + @Autowired + private TaskExecDAO taskExecDAO; + + @Autowired + private TaskDAO taskDAO; + + @Test + public void findAll() { + List<TaskExec> list = taskExecDAO.findAll(TaskType.PROPAGATION); + assertEquals(2, list.size()); + + list = taskExecDAO.findAll(TaskType.SCHEDULED); + assertTrue(list.isEmpty()); + + list = taskExecDAO.findAll(TaskType.SYNCHRONIZATION); + assertTrue(list.isEmpty()); + + list = taskExecDAO.findAll(TaskType.NOTIFICATION); + assertTrue(list.isEmpty()); + } + + @Test + public void findLatestStarted() { + PropagationTask task = taskDAO.find(1L); + assertNotNull(task); + + TaskExec latestStarted = taskExecDAO.findLatestStarted(task); + assertNotNull(latestStarted); + assertEquals(Long.valueOf(1L), latestStarted.getKey()); + } + + @Test + public void issueSYNCOPE214() { + PropagationTask task = taskDAO.find(1L); + assertNotNull(task); + + String faultyMessage = "A faulty message"; + faultyMessage = faultyMessage.replace('a', '\0'); + + TaskExec exec = entityFactory.newEntity(TaskExec.class); + exec.setStartDate(new Date()); + exec.setEndDate(new Date()); + exec.setStatus(PropagationTaskExecStatus.SUCCESS.name()); + exec.setMessage(faultyMessage); + + task.addExec(exec); + exec.setTask(task); + + exec = taskExecDAO.save(exec); + assertNotNull(exec); + + assertEquals(faultyMessage.replace('\0', '\n'), exec.getMessage()); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/TaskTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/TaskTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/TaskTest.java new file mode 100644 index 0000000..f7ebed7 --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/TaskTest.java @@ -0,0 +1,117 @@ +/* + * 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 java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.syncope.common.lib.types.AttributableType; +import org.apache.syncope.common.lib.types.PropagationMode; +import org.apache.syncope.common.lib.types.ResourceOperation; +import org.apache.syncope.common.lib.types.TaskType; +import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO; +import org.apache.syncope.core.persistence.api.dao.TaskDAO; +import org.apache.syncope.core.persistence.api.dao.UserDAO; +import org.apache.syncope.core.persistence.api.entity.ExternalResource; +import org.apache.syncope.core.persistence.api.entity.task.PropagationTask; +import org.apache.syncope.core.persistence.api.entity.user.User; +import org.apache.syncope.core.persistence.jpa.AbstractTest; +import org.identityconnectors.framework.common.objects.Attribute; +import org.identityconnectors.framework.common.objects.AttributeBuilder; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +public class TaskTest extends AbstractTest { + + @Autowired + private TaskDAO taskDAO; + + @Autowired + private ExternalResourceDAO resourceDAO; + + @Autowired + private UserDAO userDAO; + + @Test + public void findWithoutExecs() { + List<PropagationTask> tasks = taskDAO.findToExec(TaskType.PROPAGATION); + assertNotNull(tasks); + assertEquals(2, tasks.size()); + } + + @Test + public void findAll() { + assertEquals(4, taskDAO.findAll(TaskType.PROPAGATION).size()); + assertEquals(1, taskDAO.findAll(TaskType.NOTIFICATION).size()); + assertEquals(1, taskDAO.findAll(TaskType.SCHEDULED).size()); + assertEquals(9, taskDAO.findAll(TaskType.SYNCHRONIZATION).size()); + assertEquals(11, taskDAO.findAll(TaskType.PUSH).size()); + } + + @Test + public void savePropagationTask() { + ExternalResource resource = resourceDAO.find("ws-target-resource-1"); + assertNotNull(resource); + + User user = userDAO.find(2L); + assertNotNull(user); + + PropagationTask task = entityFactory.newEntity(PropagationTask.class); + task.setResource(resource); + task.setSubjectType(AttributableType.USER); + task.setPropagationMode(PropagationMode.TWO_PHASES); + task.setPropagationOperation(ResourceOperation.CREATE); + task.setAccountId("[email protected]"); + + Set<Attribute> attributes = new HashSet<Attribute>(); + attributes.add(AttributeBuilder.build("testAttribute", "testValue1", "testValue2")); + attributes.add(AttributeBuilder.buildPassword("password".toCharArray())); + task.setAttributes(attributes); + + task = taskDAO.save(task); + assertNotNull(task); + + PropagationTask actual = taskDAO.find(task.getKey()); + assertEquals(task, actual); + } + + @Test + public void delete() { + PropagationTask task = taskDAO.find(1L); + assertNotNull(task); + + ExternalResource resource = task.getResource(); + assertNotNull(resource); + + taskDAO.delete(task); + task = taskDAO.find(1L); + assertNull(task); + + resource = resourceDAO.find(resource.getKey()); + assertNotNull(resource); + assertFalse(taskDAO.findAll(resource, TaskType.PROPAGATION).contains(task)); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/UserTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/UserTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/UserTest.java new file mode 100644 index 0000000..38be1ac --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/UserTest.java @@ -0,0 +1,250 @@ +/* + * 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.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import java.util.Date; +import java.util.List; +import java.util.Set; +import org.apache.syncope.common.lib.types.CipherAlgorithm; +import org.apache.syncope.core.persistence.api.RoleEntitlementUtil; +import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException; +import org.apache.syncope.core.persistence.api.dao.EntitlementDAO; +import org.apache.syncope.core.persistence.api.dao.UserDAO; +import org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue; +import org.apache.syncope.core.persistence.api.entity.user.User; +import org.apache.syncope.core.persistence.jpa.AbstractTest; +import org.apache.syncope.core.misc.policy.InvalidPasswordPolicySpecException; +import org.apache.syncope.core.misc.security.PasswordGenerator; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +public class UserTest extends AbstractTest { + + @Autowired + private PasswordGenerator passwordGenerator; + + @Autowired + private UserDAO userDAO; + + @Autowired + private EntitlementDAO entitlementDAO; + + @Test + public void findAll() { + List<User> list = userDAO.findAll(RoleEntitlementUtil.getRoleKeys(entitlementDAO.findAll()), 1, 100); + assertEquals("did not get expected number of users ", 5, list.size()); + } + + @Test + public void count() { + Integer count = userDAO.count(RoleEntitlementUtil.getRoleKeys(entitlementDAO.findAll())); + assertNotNull(count); + assertEquals(5, count.intValue()); + } + + @Test + public void findAllByPageAndSize() { + Set<Long> allRoleIds = RoleEntitlementUtil.getRoleKeys(entitlementDAO.findAll()); + + // get first page + List<User> list = userDAO.findAll(allRoleIds, 1, 2); + assertEquals("did not get expected number of users ", 2, list.size()); + + // get second page + list = userDAO.findAll(allRoleIds, 2, 2); + assertEquals("did not get expected number of users ", 2, list.size()); + + // get second page with uncomplete set + list = userDAO.findAll(allRoleIds, 2, 3); + assertEquals("did not get expected number of users ", 2, list.size()); + + // get unexistent page + list = userDAO.findAll(allRoleIds, 3, 2); + assertEquals("did not get expected number of users ", 1, list.size()); + } + + @Test + public void findByDerAttributeValue() { + final List<User> list = userDAO.findByDerAttrValue("cn", "Vivaldi, Antonio"); + assertEquals("did not get expected number of users ", 1, list.size()); + } + + @Test(expected = IllegalArgumentException.class) + public void findByInvalidDerAttrValue() { + userDAO.findByDerAttrValue("cn", "Antonio, Maria, Rossi"); + } + + @Test(expected = IllegalArgumentException.class) + public void findByInvalidDerAttrExpression() { + userDAO.findByDerAttrValue("noschema", "Antonio, Maria"); + } + + @Test + public void findByAttributeValue() { + final UPlainAttrValue fullnameValue = entityFactory.newEntity(UPlainAttrValue.class); + fullnameValue.setStringValue("Gioacchino Rossini"); + + final List<User> list = userDAO.findByAttrValue("fullname", fullnameValue); + assertEquals("did not get expected number of users ", 1, list.size()); + } + + @Test + public void findByAttributeBooleanValue() { + final UPlainAttrValue coolValue = entityFactory.newEntity(UPlainAttrValue.class); + coolValue.setBooleanValue(true); + + final List<User> list = userDAO.findByAttrValue("cool", coolValue); + assertEquals("did not get expected number of users ", 1, list.size()); + } + + @Test + public void findById() { + User user = userDAO.find(1L); + assertNotNull("did not find expected user", user); + user = userDAO.find(3L); + assertNotNull("did not find expected user", user); + user = userDAO.find(6L); + assertNull("found user but did not expect it", user); + } + + @Test + public void findByUsername() { + User user = userDAO.find("rossini"); + assertNotNull("did not find expected user", user); + user = userDAO.find("vivaldi"); + assertNotNull("did not find expected user", user); + user = userDAO.find("user6"); + assertNull("found user but did not expect it", user); + } + + @Test + public void save() { + User user = entityFactory.newEntity(User.class); + user.setUsername("username"); + user.setCreationDate(new Date()); + + user.setPassword("pass", CipherAlgorithm.SHA256); + + Throwable t = null; + try { + userDAO.save(user); + } catch (InvalidEntityException e) { + t = e; + } + assertNotNull(t); + + user.setPassword("password", CipherAlgorithm.SHA256); + + user.setUsername("username!"); + + t = null; + try { + userDAO.save(user); + } catch (InvalidEntityException e) { + t = e; + } + assertNotNull(t); + + user.setUsername("username"); + + User actual = userDAO.save(user); + assertNotNull("expected save to work", actual); + assertEquals(1, actual.getPasswordHistory().size()); + } + + @Test + public void delete() { + User user = userDAO.find(3L); + + userDAO.delete(user.getKey()); + + User actual = userDAO.find(3L); + assertNull("delete did not work", actual); + } + + @Test + public void issue237() { + User user = entityFactory.newEntity(User.class); + user.setUsername("username"); + user.setCreationDate(new Date()); + + user.setPassword("password", CipherAlgorithm.AES); + + User actual = userDAO.save(user); + assertNotNull(actual); + } + + @Test + public void issueSYNCOPE391() { + User user = entityFactory.newEntity(User.class); + user.setUsername("username"); + user.setPassword(null, CipherAlgorithm.AES); + + User actual = null; + Throwable t = null; + try { + actual = userDAO.save(user); + } catch (InvalidEntityException e) { + t = e; + } + assertNull(t); + assertNull(user.getPassword()); + assertNotNull(actual); + } + + @Test + public void issueSYNCOPE226() { + User user = userDAO.find(5L); + String password = ""; + try { + password = passwordGenerator.generate(user); + } catch (InvalidPasswordPolicySpecException ex) { + fail(ex.getMessage()); + } + assertNotNull(password); + + user.setPassword(password, CipherAlgorithm.AES); + + User actual = userDAO.save(user); + assertNotNull(actual); + } + + @Test + public void testPasswordGenerator() { + User user = userDAO.find(5L); + + String password = ""; + try { + password = passwordGenerator.generate(user); + + } catch (InvalidPasswordPolicySpecException ex) { + fail(ex.getMessage()); + } + assertNotNull(password); + user.setPassword(password, CipherAlgorithm.SHA); + userDAO.save(user); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/VirAttrTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/VirAttrTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/VirAttrTest.java new file mode 100644 index 0000000..72c5509 --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/VirAttrTest.java @@ -0,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.entity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.List; +import org.apache.syncope.core.persistence.api.dao.MembershipDAO; +import org.apache.syncope.core.persistence.api.dao.RoleDAO; +import org.apache.syncope.core.persistence.api.dao.UserDAO; +import org.apache.syncope.core.persistence.api.dao.VirAttrDAO; +import org.apache.syncope.core.persistence.api.dao.VirSchemaDAO; +import org.apache.syncope.core.persistence.api.entity.membership.MVirAttr; +import org.apache.syncope.core.persistence.api.entity.membership.MVirAttrTemplate; +import org.apache.syncope.core.persistence.api.entity.membership.Membership; +import org.apache.syncope.core.persistence.api.entity.role.RVirAttr; +import org.apache.syncope.core.persistence.api.entity.role.RVirAttrTemplate; +import org.apache.syncope.core.persistence.api.entity.role.Role; +import org.apache.syncope.core.persistence.api.entity.user.UVirAttr; +import org.apache.syncope.core.persistence.api.entity.user.UVirSchema; +import org.apache.syncope.core.persistence.api.entity.user.User; +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 VirAttrTest extends AbstractTest { + + @Autowired + private VirAttrDAO virAttrDAO; + + @Autowired + private UserDAO userDAO; + + @Autowired + private RoleDAO roleDAO; + + @Autowired + private MembershipDAO membershipDAO; + + @Autowired + private VirSchemaDAO virSchemaDAO; + + @Test + public void findAll() { + List<UVirAttr> list = virAttrDAO.findAll(UVirAttr.class); + assertEquals("did not get expected number of derived attributes ", 1, list.size()); + } + + @Test + public void findById() { + UVirAttr attribute = virAttrDAO.find(1000L, UVirAttr.class); + assertNotNull("did not find expected attribute schema", attribute); + } + + @Test + public void saveUVirAttribute() { + UVirSchema virSchema = virSchemaDAO.find("virtualdata", UVirSchema.class); + assertNotNull(virSchema); + + User owner = userDAO.find(3L); + assertNotNull("did not get expected user", owner); + + UVirAttr virAttr = entityFactory.newEntity(UVirAttr.class); + virAttr.setOwner(owner); + virAttr.setSchema(virSchema); + + virAttr = virAttrDAO.save(virAttr); + + UVirAttr actual = virAttrDAO.find(virAttr.getKey(), UVirAttr.class); + assertNotNull("expected save to work", actual); + assertEquals(virAttr, actual); + } + + @Test + public void saveMVirAttribute() { + Membership owner = membershipDAO.find(3L); + assertNotNull("did not get expected membership", owner); + + MVirAttr virAttr = entityFactory.newEntity(MVirAttr.class); + virAttr.setOwner(owner); + virAttr.setTemplate(owner.getRole().getAttrTemplate(MVirAttrTemplate.class, "mvirtualdata")); + + virAttr = virAttrDAO.save(virAttr); + assertNotNull(virAttr.getTemplate()); + + MVirAttr actual = virAttrDAO.find(virAttr.getKey(), MVirAttr.class); + assertNotNull("expected save to work", actual); + assertEquals(virAttr, actual); + } + + @Test + public void saveRVirAttribute() { + Role owner = roleDAO.find(3L); + assertNotNull("did not get expected membership", owner); + + RVirAttr virAttr = entityFactory.newEntity(RVirAttr.class); + virAttr.setOwner(owner); + virAttr.setTemplate(owner.getAttrTemplate(RVirAttrTemplate.class, "rvirtualdata")); + + virAttr = virAttrDAO.save(virAttr); + assertNotNull(virAttr.getTemplate()); + + RVirAttr actual = virAttrDAO.find(virAttr.getKey(), RVirAttr.class); + assertNotNull("expected save to work", actual); + assertEquals(virAttr, actual); + } + + @Test + public void delete() { + UVirAttr attribute = virAttrDAO.find(1000L, UVirAttr.class); + String attributeSchemaName = attribute.getSchema().getKey(); + + virAttrDAO.delete(attribute.getKey(), UVirAttr.class); + + UVirAttr actual = virAttrDAO.find(1000L, UVirAttr.class); + assertNull("delete did not work", actual); + + UVirSchema attributeSchema = virSchemaDAO.find(attributeSchemaName, UVirSchema.class); + + assertNotNull("user virtual attribute schema deleted " + "when deleting values", attributeSchema); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/VirSchemaTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/VirSchemaTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/VirSchemaTest.java new file mode 100644 index 0000000..b6738d4 --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/entity/VirSchemaTest.java @@ -0,0 +1,102 @@ +/* + * 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.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.List; +import org.apache.syncope.common.lib.types.AttributableType; +import org.apache.syncope.common.lib.types.EntityViolationType; +import org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException; +import org.apache.syncope.core.persistence.api.dao.VirSchemaDAO; +import org.apache.syncope.core.persistence.api.entity.VirSchema; +import org.apache.syncope.core.persistence.api.entity.role.RVirSchema; +import org.apache.syncope.core.persistence.api.entity.user.UVirSchema; +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 VirSchemaTest extends AbstractTest { + + @Autowired + private VirSchemaDAO virSchemaDAO; + + @Test + public void findAll() { + List<UVirSchema> list = virSchemaDAO.findAll(UVirSchema.class); + assertEquals(2, list.size()); + } + + @Test + public void findByName() { + UVirSchema attributeSchema = virSchemaDAO.find("virtualdata", UVirSchema.class); + assertNotNull("did not find expected virtual attribute schema", attributeSchema); + } + + @Test + public void save() { + UVirSchema virtualAttributeSchema = entityFactory.newEntity(UVirSchema.class); + virtualAttributeSchema.setKey("virtual"); + virtualAttributeSchema.setReadonly(true); + + virSchemaDAO.save(virtualAttributeSchema); + + UVirSchema actual = virSchemaDAO.find("virtual", UVirSchema.class); + assertNotNull("expected save to work", actual); + assertTrue(actual.isReadonly()); + } + + @Test + public void delete() { + UVirSchema virtualdata = virSchemaDAO.find("virtualdata", UVirSchema.class); + + virSchemaDAO.delete(virtualdata.getKey(), attrUtilFactory.getInstance(AttributableType.USER)); + + VirSchema actual = virSchemaDAO.find("virtualdata", UVirSchema.class); + assertNull("delete did not work", actual); + + // ------------- // + RVirSchema rvirtualdata = virSchemaDAO.find("rvirtualdata", RVirSchema.class); + assertNotNull(rvirtualdata); + + virSchemaDAO.delete(rvirtualdata.getKey(), attrUtilFactory.getInstance(AttributableType.ROLE)); + + actual = virSchemaDAO.find("rvirtualdata", RVirSchema.class); + assertNull("delete did not work", actual); + } + + @Test + public void issueSYNCOPE418() { + UVirSchema schema = entityFactory.newEntity(UVirSchema.class); + schema.setKey("http://schemas.examples.org/security/authorization/organizationUnit"); + + try { + virSchemaDAO.save(schema); + fail(); + } catch (InvalidEntityException e) { + assertTrue(e.hasViolation(EntityViolationType.InvalidName)); + } + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/AttrTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/AttrTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/AttrTest.java new file mode 100644 index 0000000..bc1e4cd --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/AttrTest.java @@ -0,0 +1,191 @@ +/* + * 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.relationship; + +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 org.apache.syncope.common.lib.SyncopeConstants; +import org.apache.syncope.common.lib.types.AttrSchemaType; +import org.apache.syncope.common.lib.types.AttributableType; +import org.apache.syncope.core.persistence.api.dao.DerAttrDAO; +import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO; +import org.apache.syncope.core.persistence.api.dao.MembershipDAO; +import org.apache.syncope.core.persistence.api.dao.PlainAttrDAO; +import org.apache.syncope.core.persistence.api.dao.PlainAttrValueDAO; +import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO; +import org.apache.syncope.core.persistence.api.dao.RoleDAO; +import org.apache.syncope.core.persistence.api.dao.UserDAO; +import org.apache.syncope.core.persistence.api.entity.membership.MPlainAttr; +import org.apache.syncope.core.persistence.api.entity.membership.MPlainAttrTemplate; +import org.apache.syncope.core.persistence.api.entity.membership.MPlainSchema; +import org.apache.syncope.core.persistence.api.entity.membership.Membership; +import org.apache.syncope.core.persistence.api.entity.role.RPlainAttrTemplate; +import org.apache.syncope.core.persistence.api.entity.role.Role; +import org.apache.syncope.core.persistence.api.entity.user.UDerAttr; +import org.apache.syncope.core.persistence.api.entity.user.UDerSchema; +import org.apache.syncope.core.persistence.api.entity.user.UPlainAttr; +import org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue; +import org.apache.syncope.core.persistence.api.entity.user.User; +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 AttrTest extends AbstractTest { + + @Autowired + private PlainAttrDAO plainAttrDAO; + + @Autowired + private DerAttrDAO derAttrDAO; + + @Autowired + private PlainAttrValueDAO plainAttrValueDAO; + + @Autowired + private PlainSchemaDAO plainSchemaDAO; + + @Autowired + private DerSchemaDAO derSchemaDAO; + + @Autowired + private MembershipDAO membershipDAO; + + @Autowired + private RoleDAO roleDAO; + + @Autowired + private UserDAO userDAO; + + @Test + public void deleteAttribute() { + plainAttrDAO.delete(117L, UPlainAttr.class); + + plainAttrDAO.flush(); + + assertNull(plainAttrDAO.find(117L, UPlainAttr.class)); + assertNull(plainAttrValueDAO.find(28L, UPlainAttrValue.class)); + } + + @Test + public void deleteAttributeValue() { + UPlainAttrValue value = plainAttrValueDAO.find(14L, UPlainAttrValue.class); + int attributeValueNumber = value.getAttr().getValues().size(); + + plainAttrValueDAO.delete(value.getKey(), UPlainAttrValue.class); + + plainAttrValueDAO.flush(); + + assertNull(plainAttrValueDAO.find(value.getKey(), UPlainAttrValue.class)); + + UPlainAttr attribute = plainAttrDAO.find(104L, UPlainAttr.class); + assertEquals(attribute.getValues().size(), attributeValueNumber - 1); + } + + @Test + public void checkForEnumType() { + User user = userDAO.find(1L); + Membership membership = user.getMembership(1L); + assertNotNull(membership); + + MPlainSchema schema = entityFactory.newEntity(MPlainSchema.class); + schema.setType(AttrSchemaType.Enum); + schema.setKey("color"); + schema.setEnumerationValues("red" + SyncopeConstants.ENUM_VALUES_SEPARATOR + "yellow"); + + MPlainSchema actualSchema = plainSchemaDAO.save(schema); + assertNotNull(actualSchema); + + MPlainAttrTemplate template = entityFactory.newEntity(MPlainAttrTemplate.class); + template.setSchema(actualSchema); + membership.getRole().getAttrTemplates(MPlainAttrTemplate.class).add(template); + + MPlainAttr attr = entityFactory.newEntity(MPlainAttr.class); + attr.setTemplate(template); + attr.setOwner(membership); + attr.addValue("yellow", attrUtilFactory.getInstance(AttributableType.MEMBERSHIP)); + membership.addPlainAttr(attr); + + MPlainAttr actualAttribute = userDAO.save(user).getMembership(1L).getPlainAttr("color"); + assertNotNull(actualAttribute); + + membership = membershipDAO.find(1L); + assertNotNull(membership); + assertNotNull(membership.getPlainAttr(schema.getKey())); + assertNotNull(membership.getPlainAttr(schema.getKey()).getValues()); + + assertEquals(membership.getPlainAttr(schema.getKey()).getValues().size(), 1); + } + + @Test + public void derAttrFromSpecialAttrs() { + UDerSchema sderived = entityFactory.newEntity(UDerSchema.class); + sderived.setKey("sderived"); + sderived.setExpression("username + ' - ' + creationDate + '[' + failedLogins + ']'"); + + sderived = derSchemaDAO.save(sderived); + derSchemaDAO.flush(); + + UDerSchema actual = derSchemaDAO.find("sderived", UDerSchema.class); + assertNotNull("expected save to work", actual); + assertEquals(sderived, actual); + + User owner = userDAO.find(3L); + assertNotNull("did not get expected user", owner); + + UDerAttr derAttr = entityFactory.newEntity(UDerAttr.class); + derAttr.setOwner(owner); + derAttr.setSchema(sderived); + + derAttr = derAttrDAO.save(derAttr); + derAttrDAO.flush(); + + derAttr = derAttrDAO.find(derAttr.getKey(), UDerAttr.class); + assertNotNull("expected save to work", derAttr); + + String value = derAttr.getValue(owner.getPlainAttrs()); + assertNotNull(value); + assertFalse(value.isEmpty()); + assertTrue(value.startsWith("vivaldi - 2010-10-20")); + assertTrue(value.endsWith("[0]")); + } + + @Test + public void unmatchedRoleAttr() { + Role role = roleDAO.find(1L); + assertNotNull(role); + + assertNotNull(role.getAttrTemplate(RPlainAttrTemplate.class, "icon")); + assertNotNull(role.getPlainAttr("icon")); + + assertTrue(role.getAttrTemplates(RPlainAttrTemplate.class). + remove(role.getAttrTemplate(RPlainAttrTemplate.class, "icon"))); + + role = roleDAO.save(role); + roleDAO.flush(); + + assertNull(role.getAttrTemplate(RPlainAttrTemplate.class, "icon")); + assertNull(role.getPlainAttr("icon")); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/AttributableSearchTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/AttributableSearchTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/AttributableSearchTest.java new file mode 100644 index 0000000..ba67807 --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/AttributableSearchTest.java @@ -0,0 +1,76 @@ +/* + * 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.relationship; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.syncope.common.lib.types.SubjectType; +import org.apache.syncope.core.persistence.api.RoleEntitlementUtil; +import org.apache.syncope.core.persistence.api.dao.EntitlementDAO; +import org.apache.syncope.core.persistence.api.dao.RoleDAO; +import org.apache.syncope.core.persistence.api.dao.SubjectSearchDAO; +import org.apache.syncope.core.persistence.api.dao.search.AttributeCond; +import org.apache.syncope.core.persistence.api.dao.search.SearchCond; +import org.apache.syncope.core.persistence.api.entity.role.Role; +import org.apache.syncope.core.persistence.api.entity.user.User; +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 AttributableSearchTest extends AbstractTest { + + @Autowired + private RoleDAO roleDAO; + + @Autowired + private SubjectSearchDAO searchDAO; + + @Autowired + private EntitlementDAO entitlementDAO; + + @Test + public void issueSYNCOPE95() { + Set<Role> roles = new HashSet<>(roleDAO.findAll()); + for (Role role : roles) { + roleDAO.delete(role.getKey()); + } + roleDAO.flush(); + + final AttributeCond coolLeafCond = new AttributeCond(AttributeCond.Type.EQ); + coolLeafCond.setSchema("cool"); + coolLeafCond.setExpression("true"); + + final SearchCond cond = SearchCond.getLeafCond(coolLeafCond); + assertTrue(cond.isValid()); + + final List<User> users = + searchDAO.search(RoleEntitlementUtil.getRoleKeys(entitlementDAO.findAll()), cond, SubjectType.USER); + assertNotNull(users); + assertEquals(1, users.size()); + + assertEquals(Long.valueOf(4L), users.get(0).getKey()); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/ConnInstanceTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/ConnInstanceTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/ConnInstanceTest.java new file mode 100644 index 0000000..ce85dc5 --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/ConnInstanceTest.java @@ -0,0 +1,103 @@ +/* + * 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.relationship; + +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 java.util.List; +import org.apache.syncope.common.lib.types.ConnectorCapability; +import org.apache.syncope.core.persistence.api.dao.ConnInstanceDAO; +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.jpa.AbstractTest; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +public class ConnInstanceTest extends AbstractTest { + + @Autowired + private ExternalResourceDAO resourceDAO; + + @Autowired + private ConnInstanceDAO connInstanceDAO; + + @Test + public void deleteCascade() { + ConnInstance connInstance = connInstanceDAO.find(103L); + assertNotNull(connInstance); + + List<? extends ExternalResource> resources = connInstance.getResources(); + assertNotNull(resources); + assertFalse(resources.isEmpty()); + + connInstanceDAO.delete(connInstance.getKey()); + + connInstanceDAO.flush(); + + ConnInstance actual = connInstanceDAO.find(103L); + assertNull(actual); + + for (ExternalResource resource : resources) { + assertNull(resourceDAO.find(resource.getKey())); + } + } + + /** + * Connector change used to miss connector bean registration. + * + * http://code.google.com/p/syncope/issues/detail?id=176 + */ + @Test + public void issue176() { + ConnInstance connInstance = connInstanceDAO.find(103L); + assertNotNull(connInstance); + assertTrue(connInstance.getCapabilities().isEmpty()); + + List<? extends ExternalResource> resources = connInstance.getResources(); + assertNotNull(resources); + assertEquals(4, resources.size()); + assertTrue( + "ws-target-resource-nopropagation".equalsIgnoreCase(resources.get(0).getKey()) + || "ws-target-resource-nopropagation".equalsIgnoreCase(resources.get(1).getKey()) + || "ws-target-resource-nopropagation".equalsIgnoreCase(resources.get(2).getKey()) + || "ws-target-resource-nopropagation".equalsIgnoreCase(resources.get(3).getKey())); + + connInstance.addCapability(ConnectorCapability.SEARCH); + + connInstance = connInstanceDAO.save(connInstance); + assertNotNull(connInstance); + assertFalse(connInstance.getCapabilities().isEmpty()); + + resources = connInstance.getResources(); + assertNotNull(resources); + assertEquals(4, resources.size()); + assertTrue( + "ws-target-resource-nopropagation".equalsIgnoreCase(resources.get(0).getKey()) + || "ws-target-resource-nopropagation".equalsIgnoreCase(resources.get(1).getKey()) + || "ws-target-resource-nopropagation".equalsIgnoreCase(resources.get(2).getKey()) + || "ws-target-resource-nopropagation".equalsIgnoreCase(resources.get(3).getKey())); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/DerSchemaTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/DerSchemaTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/DerSchemaTest.java new file mode 100644 index 0000000..e96a9e5 --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/DerSchemaTest.java @@ -0,0 +1,58 @@ +/* + * 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.relationship; + +import static org.junit.Assert.assertNull; + +import org.apache.syncope.common.lib.types.AttributableType; +import org.apache.syncope.core.persistence.api.dao.DerAttrDAO; +import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO; +import org.apache.syncope.core.persistence.api.dao.UserDAO; +import org.apache.syncope.core.persistence.api.entity.user.UDerAttr; +import org.apache.syncope.core.persistence.api.entity.user.UDerSchema; +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 DerSchemaTest extends AbstractTest { + + @Autowired + private UserDAO userDAO; + + @Autowired + private DerSchemaDAO derSchemaDAO; + + @Autowired + private DerAttrDAO derAttrDAO; + + @Test + public void test() { + UDerSchema schema = derSchemaDAO.find("cn", UDerSchema.class); + + derSchemaDAO.delete(schema.getKey(), attrUtilFactory.getInstance(AttributableType.USER)); + + derSchemaDAO.flush(); + + assertNull(derSchemaDAO.find(schema.getKey(), UDerSchema.class)); + assertNull(derAttrDAO.find(100L, UDerAttr.class)); + assertNull(userDAO.find(3L).getDerAttr(schema.getKey())); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/EntitlementTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/EntitlementTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/EntitlementTest.java new file mode 100644 index 0000000..0e2321d --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/EntitlementTest.java @@ -0,0 +1,57 @@ +/* + * 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.relationship; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.List; +import org.apache.syncope.core.persistence.api.dao.EntitlementDAO; +import org.apache.syncope.core.persistence.api.dao.RoleDAO; +import org.apache.syncope.core.persistence.api.entity.Entitlement; +import org.apache.syncope.core.persistence.api.entity.role.Role; +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 EntitlementTest extends AbstractTest { + + @Autowired + private EntitlementDAO entitlementDAO; + + @Autowired + private RoleDAO roleDAO; + + @Test + public void delete() { + Entitlement entitlement = entitlementDAO.find("base"); + assertNotNull("did not find expected entitlement", entitlement); + + List<Role> roles = roleDAO.findByEntitlement(entitlement); + assertEquals("expected two roles", 2, roles.size()); + + entitlementDAO.delete("base"); + + roles = roleDAO.findByEntitlement(entitlement); + assertTrue(roles.isEmpty()); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/MembershipTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/MembershipTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/MembershipTest.java new file mode 100644 index 0000000..3a73d02 --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/MembershipTest.java @@ -0,0 +1,81 @@ +/* + * 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.relationship; + +import static org.junit.Assert.assertTrue; + +import org.apache.syncope.core.persistence.api.dao.MembershipDAO; +import org.apache.syncope.core.persistence.api.dao.RoleDAO; +import org.apache.syncope.core.persistence.api.entity.membership.Membership; +import org.apache.syncope.core.persistence.api.entity.role.Role; +import org.apache.syncope.core.persistence.api.entity.user.User; +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 MembershipTest extends AbstractTest { + + @Autowired + private MembershipDAO membershipDAO; + + @Autowired + private RoleDAO roleDAO; + + @Test + public void delete() { + Membership membership = membershipDAO.find(4L); + User user = membership.getUser(); + Role role = membership.getRole(); + + membershipDAO.delete(4L); + + membershipDAO.flush(); + + for (Membership m : user.getMemberships()) { + assertTrue(m.getKey() != 4L); + } + for (Membership m : roleDAO.findMemberships(role)) { + assertTrue(m.getKey() != 4L); + } + } + + @Test + public void deleteAndCreate() { + Membership membership = membershipDAO.find(3L); + User user = membership.getUser(); + Role role = membership.getRole(); + + // 1. delete that membership + membershipDAO.delete(membership.getKey()); + + // if not flushing here, the INSERT below will be executed + // before the DELETE above + membershipDAO.flush(); + + // 2. (in the same transaction) create new membership with same user + // and role (in order to check the UNIQE constraint on Membership) + membership = entityFactory.newEntity(Membership.class); + membership.setUser(user); + membership.setRole(role); + + membership = membershipDAO.save(membership); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/PlainSchemaTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/PlainSchemaTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/PlainSchemaTest.java new file mode 100644 index 0000000..a66843e --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/PlainSchemaTest.java @@ -0,0 +1,157 @@ +/* + * 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.relationship; + +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 java.util.HashSet; +import java.util.Set; +import org.apache.syncope.common.lib.types.AttributableType; +import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO; +import org.apache.syncope.core.persistence.api.dao.PlainAttrDAO; +import org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO; +import org.apache.syncope.core.persistence.api.dao.UserDAO; +import org.apache.syncope.core.persistence.api.entity.ExternalResource; +import org.apache.syncope.core.persistence.api.entity.MappingItem; +import org.apache.syncope.core.persistence.api.entity.user.UPlainAttr; +import org.apache.syncope.core.persistence.api.entity.user.UPlainSchema; +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 PlainSchemaTest extends AbstractTest { + + @Autowired + private UserDAO userDAO; + + @Autowired + private PlainSchemaDAO plainSchemaDAO; + + @Autowired + private PlainAttrDAO plainAttrDAO; + + @Autowired + private ExternalResourceDAO resourceDAO; + + @Test + public void deleteFullname() { + // fullname is mapped as AccountId for ws-target-resource-2, need to swap it otherwise validation errors + // will be raised + for (MappingItem item : resourceDAO.find("ws-target-resource-2").getUmapping().getItems()) { + if ("fullname".equals(item.getIntAttrName())) { + item.setAccountid(false); + } else if ("surname".equals(item.getIntAttrName())) { + item.setAccountid(true); + } + } + + // search for user schema fullname + UPlainSchema schema = plainSchemaDAO.find("fullname", UPlainSchema.class); + assertNotNull(schema); + + // check for associated mappings + Set<MappingItem> mapItems = new HashSet<>(); + for (ExternalResource resource : resourceDAO.findAll()) { + if (resource.getUmapping() != null) { + for (MappingItem mapItem : resource.getUmapping().getItems()) { + if (schema.getKey().equals(mapItem.getIntAttrName())) { + mapItems.add(mapItem); + } + } + } + } + assertFalse(mapItems.isEmpty()); + + // delete user schema fullname + plainSchemaDAO.delete("fullname", attrUtilFactory.getInstance(AttributableType.USER)); + + plainSchemaDAO.flush(); + + // check for schema deletion + schema = plainSchemaDAO.find("fullname", UPlainSchema.class); + assertNull(schema); + + plainSchemaDAO.clear(); + + // check for mappings deletion + mapItems = new HashSet<>(); + for (ExternalResource resource : resourceDAO.findAll()) { + if (resource.getUmapping() != null) { + for (MappingItem mapItem : resource.getUmapping().getItems()) { + if ("fullname".equals(mapItem.getIntAttrName())) { + mapItems.add(mapItem); + } + } + } + } + assertTrue(mapItems.isEmpty()); + + assertNull(plainAttrDAO.find(100L, UPlainAttr.class)); + assertNull(plainAttrDAO.find(300L, UPlainAttr.class)); + assertNull(userDAO.find(1L).getPlainAttr("fullname")); + assertNull(userDAO.find(3L).getPlainAttr("fullname")); + } + + @Test + public void deleteSurname() { + // search for user schema fullname + UPlainSchema schema = plainSchemaDAO.find("surname", UPlainSchema.class); + assertNotNull(schema); + + // check for associated mappings + Set<MappingItem> mappings = new HashSet<>(); + for (ExternalResource resource : resourceDAO.findAll()) { + if (resource.getUmapping() != null) { + for (MappingItem mapItem : resource.getUmapping().getItems()) { + if (schema.getKey().equals(mapItem.getIntAttrName())) { + mappings.add(mapItem); + } + } + } + } + assertFalse(mappings.isEmpty()); + + // delete user schema fullname + plainSchemaDAO.delete("surname", attrUtilFactory.getInstance(AttributableType.USER)); + + plainSchemaDAO.flush(); + + // check for schema deletion + schema = plainSchemaDAO.find("surname", UPlainSchema.class); + assertNull(schema); + } + + @Test + public void deleteALong() { + assertEquals(6, resourceDAO.find("resource-db-sync").getUmapping().getItems().size()); + + plainSchemaDAO.delete("aLong", attrUtilFactory.getInstance(AttributableType.USER)); + assertNull(plainSchemaDAO.find("aLong", UPlainSchema.class)); + + plainSchemaDAO.flush(); + + assertEquals(5, resourceDAO.find("resource-db-sync").getUmapping().getItems().size()); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/d30c8526/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/ReportTest.java ---------------------------------------------------------------------- diff --git a/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/ReportTest.java b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/ReportTest.java new file mode 100644 index 0000000..9c49aae --- /dev/null +++ b/syncope620/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/relationship/ReportTest.java @@ -0,0 +1,120 @@ +/* + * 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.relationship; + +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 java.util.Date; +import javax.persistence.EntityExistsException; +import org.apache.syncope.common.lib.types.ReportExecStatus; +import org.apache.syncope.core.persistence.api.dao.ReportDAO; +import org.apache.syncope.core.persistence.api.dao.ReportExecDAO; +import org.apache.syncope.core.persistence.api.entity.Report; +import org.apache.syncope.core.persistence.api.entity.ReportExec; +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 ReportTest extends AbstractTest { + + @Autowired + private ReportDAO reportDAO; + + @Autowired + private ReportExecDAO reportExecDAO; + + @Test + public void find() { + Report report = reportDAO.find(1L); + assertNotNull(report); + + assertNotNull(report.getExecs()); + assertFalse(report.getExecs().isEmpty()); + assertEquals(1, report.getExecs().size()); + } + + @Test(expected = EntityExistsException.class) + public void saveWithExistingName() { + Report report = reportDAO.find(1L); + assertNotNull(report); + + String name = report.getName(); + + report = entityFactory.newEntity(Report.class); + report.setName(name); + + reportDAO.save(report); + reportDAO.flush(); + } + + @Test + public void save() { + Report report = reportDAO.find(1L); + assertNotNull(report); + assertEquals(1, report.getExecs().size()); + + ReportExec reportExec = entityFactory.newEntity(ReportExec.class); + reportExec.setReport(report); + reportExec.setStartDate(new Date()); + reportExec.setEndDate(new Date()); + reportExec.setStatus(ReportExecStatus.SUCCESS); + + report.addExec(reportExec); + + reportExec = reportExecDAO.save(reportExec); + assertNotNull(reportExec); + assertNotNull(reportExec.getKey()); + + reportExecDAO.flush(); + + report = reportDAO.find(1L); + assertNotNull(report); + assertEquals(2, report.getExecs().size()); + } + + @Test + public void deleteReport() { + reportDAO.delete(1L); + + reportDAO.flush(); + + assertNull(reportDAO.find(1L)); + assertNull(reportExecDAO.find(1L)); + } + + @Test + public void deleteReportExecution() { + ReportExec execution = reportExecDAO.find(1L); + int executionNumber = execution.getReport().getExecs().size(); + + reportExecDAO.delete(1L); + + reportExecDAO.flush(); + + assertNull(reportExecDAO.find(1L)); + + Report report = reportDAO.find(1L); + assertEquals(report.getExecs().size(), executionNumber - 1); + } +}
