Example usage for org.springframework.jdbc.core JdbcTemplate queryForObject

List of usage examples for org.springframework.jdbc.core JdbcTemplate queryForObject

Introduction

In this page you can find the example usage for org.springframework.jdbc.core JdbcTemplate queryForObject.

Prototype

@Override
    public <T> T queryForObject(String sql, Class<T> requiredType, @Nullable Object... args)
            throws DataAccessException 

Source Link

Usage

From source file:org.apache.syncope.core.rest.UserTestITCase.java

@Test(expected = EmptyResultDataAccessException.class)
public void issue213() {
    UserTO userTO = getUniqueSampleTO("issue213@syncope.apache.org");
    userTO.getResources().add(RESOURCE_NAME_TESTDB);

    userTO = createUser(userTO);/*from   w  w  w . ja  v  a 2 s  . co  m*/
    assertNotNull(userTO);
    assertEquals(1, userTO.getResources().size());

    JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

    String username = jdbcTemplate.queryForObject("SELECT id FROM test WHERE id=?", String.class,
            userTO.getUsername());

    assertEquals(userTO.getUsername(), username);

    UserMod userMod = new UserMod();

    userMod.setId(userTO.getId());
    userMod.getResourcesToRemove().add(RESOURCE_NAME_TESTDB);

    userTO = updateUser(userMod);
    assertTrue(userTO.getResources().isEmpty());

    jdbcTemplate.queryForObject("SELECT id FROM test WHERE id=?", String.class, userTO.getUsername());
}

From source file:org.apache.syncope.core.rest.UserTestITCase.java

@Test
public void issueSYNCOPE505DB() throws Exception {
    // 1. create user
    UserTO user = UserTestITCase.getUniqueSampleTO("syncope505-db@syncope.apache.org");
    user.setPassword("security");
    user = createUser(user);/*ww  w . java  2  s.  co m*/
    assertNotNull(user);
    assertTrue(user.getResources().isEmpty());

    // 2. Add DBPasswordPropagationActions
    ResourceTO resourceTO = resourceService.read(RESOURCE_NAME_TESTDB);
    assertNotNull(resourceTO);
    resourceTO.getPropagationActionsClassNames().add(DBPasswordPropagationActions.class.getName());
    resourceService.update(RESOURCE_NAME_TESTDB, resourceTO);

    // 3. Add a db resource to the User
    UserMod userMod = new UserMod();
    userMod.setId(user.getId());
    userMod.getResourcesToAdd().add(RESOURCE_NAME_TESTDB);
    user = updateUser(userMod);
    assertNotNull(user);
    assertEquals(1, user.getResources().size());

    // 4. Check that the DB resource has the correct password
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);
    String value = jdbcTemplate.queryForObject("SELECT PASSWORD FROM test WHERE ID=?", String.class,
            user.getUsername());
    assertEquals(Encryptor.getInstance().encode("security", CipherAlgorithm.SHA1), value.toUpperCase());

    // 5. Remove DBPasswordPropagationActions
    resourceTO = resourceService.read(RESOURCE_NAME_TESTDB);
    assertNotNull(resourceTO);
    resourceTO.getPropagationActionsClassNames().remove(DBPasswordPropagationActions.class.getName());
    resourceService.update(RESOURCE_NAME_TESTDB, resourceTO);
}

From source file:org.apache.syncope.core.rest.VirAttrTestITCase.java

@Test
public void virAttrCache() {
    UserTO userTO = UserTestITCase.getUniqueSampleTO("virattrcache@apache.org");
    userTO.getVirAttrs().clear();//from  www .j a  v  a  2s  .  co m

    AttributeTO virAttrTO = new AttributeTO();
    virAttrTO.setSchema("virtualdata");
    virAttrTO.getValues().add("virattrcache");
    userTO.getVirAttrs().add(virAttrTO);

    userTO.getMemberships().clear();
    userTO.getResources().clear();
    userTO.getResources().add(RESOURCE_NAME_DBVIRATTR);

    // 1. create user
    UserTO actual = createUser(userTO);
    assertNotNull(actual);

    // 2. check for virtual attribute value
    actual = userService.read(actual.getId());
    assertEquals("virattrcache", actual.getVirAttrMap().get("virtualdata").getValues().get(0));

    // 3. update virtual attribute directly
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

    String value = jdbcTemplate.queryForObject("SELECT USERNAME FROM testsync WHERE ID=?", String.class,
            actual.getId());
    assertEquals("virattrcache", value);

    jdbcTemplate.update("UPDATE testsync set USERNAME='virattrcache2' WHERE ID=?", actual.getId());

    value = jdbcTemplate.queryForObject("SELECT USERNAME FROM testsync WHERE ID=?", String.class,
            actual.getId());
    assertEquals("virattrcache2", value);

    // 4. check for cached attribute value
    actual = userService.read(actual.getId());
    assertEquals("virattrcache", actual.getVirAttrMap().get("virtualdata").getValues().get(0));

    UserMod userMod = new UserMod();
    userMod.setId(actual.getId());

    AttributeMod virtualdata = new AttributeMod();
    virtualdata.setSchema("virtualdata");
    virtualdata.getValuesToBeAdded().add("virtualupdated");

    userMod.getVirAttrsToRemove().add("virtualdata");
    userMod.getVirAttrsToUpdate().add(virtualdata);

    // 5. update virtual attribute
    actual = updateUser(userMod);
    assertNotNull(actual);

    // 6. check for virtual attribute value
    actual = userService.read(actual.getId());
    assertNotNull(actual);
    assertEquals("virtualupdated", actual.getVirAttrMap().get("virtualdata").getValues().get(0));
}

From source file:org.apache.syncope.core.rest.VirAttrTestITCase.java

@Test
public void issueSYNCOPE442() {
    UserTO userTO = UserTestITCase.getUniqueSampleTO("syncope442@apache.org");
    userTO.getVirAttrs().clear();//from   ww  w  .  j a  va2s .c o  m

    AttributeTO virAttrTO = new AttributeTO();
    virAttrTO.setSchema("virtualdata");
    virAttrTO.getValues().add("virattrcache");
    userTO.getVirAttrs().add(virAttrTO);

    userTO.getMemberships().clear();
    userTO.getResources().clear();
    userTO.getResources().add(RESOURCE_NAME_DBVIRATTR);

    // 1. create user
    UserTO actual = createUser(userTO);
    assertNotNull(actual);

    // 2. check for virtual attribute value
    actual = userService.read(actual.getId());
    assertEquals("virattrcache", actual.getVirAttrMap().get("virtualdata").getValues().get(0));

    // ----------------------------------------
    // 3. force cache expiring without any modification
    // ----------------------------------------
    String jdbcURL = null;
    ConnInstanceTO connInstanceBean = connectorService.readByResource(RESOURCE_NAME_DBVIRATTR);
    for (ConnConfProperty prop : connInstanceBean.getConfiguration()) {
        if ("jdbcUrlTemplate".equals(prop.getSchema().getName())) {
            jdbcURL = prop.getValues().iterator().next().toString();
            prop.getValues().clear();
            prop.getValues().add("jdbc:h2:tcp://localhost:9092/xxx");
        }
    }

    connectorService.update(connInstanceBean.getId(), connInstanceBean);

    UserMod userMod = new UserMod();
    userMod.setId(actual.getId());

    AttributeMod virtualdata = new AttributeMod();
    virtualdata.setSchema("virtualdata");
    virtualdata.getValuesToBeAdded().add("virtualupdated");

    userMod.getVirAttrsToRemove().add("virtualdata");
    userMod.getVirAttrsToUpdate().add(virtualdata);

    actual = updateUser(userMod);
    assertNotNull(actual);
    // ----------------------------------------

    // ----------------------------------------
    // 4. update virtual attribute
    // ----------------------------------------
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

    String value = jdbcTemplate.queryForObject("SELECT USERNAME FROM testsync WHERE ID=?", String.class,
            actual.getId());
    assertEquals("virattrcache", value);

    jdbcTemplate.update("UPDATE testsync set USERNAME='virattrcache2' WHERE ID=?", actual.getId());

    value = jdbcTemplate.queryForObject("SELECT USERNAME FROM testsync WHERE ID=?", String.class,
            actual.getId());
    assertEquals("virattrcache2", value);
    // ----------------------------------------

    actual = userService.read(actual.getId());
    assertEquals("virattrcache", actual.getVirAttrMap().get("virtualdata").getValues().get(0));

    // ----------------------------------------
    // 5. restore connector
    // ----------------------------------------
    for (ConnConfProperty prop : connInstanceBean.getConfiguration()) {
        if ("jdbcUrlTemplate".equals(prop.getSchema().getName())) {
            prop.getValues().clear();
            prop.getValues().add(jdbcURL);
        }
    }

    connectorService.update(connInstanceBean.getId(), connInstanceBean);
    // ----------------------------------------

    actual = userService.read(actual.getId());
    assertEquals("virattrcache2", actual.getVirAttrMap().get("virtualdata").getValues().get(0));
}

From source file:org.apache.syncope.core.rest.VirAttrTestITCase.java

@Test
public void issueSYNCOPE458() {
    // -------------------------------------------
    // Create a role ad-hoc
    // -------------------------------------------
    final String roleName = "issueSYNCOPE458-Role-" + getUUIDString();
    RoleTO roleTO = new RoleTO();
    roleTO.setName(roleName);// ww  w .jav a  2s  . co m
    roleTO.setParent(2L);
    roleTO.setInheritTemplates(true);
    roleTO = createRole(roleTO);
    // -------------------------------------------

    // -------------------------------------------
    // Update resource-db-virattr mapping adding new membership virtual schema mapping
    // -------------------------------------------
    ResourceTO resourceDBVirAttr = resourceService.read(RESOURCE_NAME_DBVIRATTR);
    assertNotNull(resourceDBVirAttr);

    final MappingTO resourceUMapping = resourceDBVirAttr.getUmapping();

    MappingItemTO item = new MappingItemTO();
    item.setIntAttrName("mvirtualdata");
    item.setIntMappingType(IntMappingType.MembershipVirtualSchema);
    item.setExtAttrName("EMAIL");
    item.setPurpose(MappingPurpose.BOTH);

    resourceUMapping.addItem(item);

    resourceDBVirAttr.setUmapping(resourceUMapping);

    resourceService.update(RESOURCE_NAME_DBVIRATTR, resourceDBVirAttr);
    // -------------------------------------------

    // -------------------------------------------
    // Create new user
    // -------------------------------------------
    UserTO userTO = UserTestITCase.getUniqueSampleTO("syncope458@syncope.apache.org");
    userTO.getResources().clear();
    userTO.getResources().add(RESOURCE_NAME_DBVIRATTR);
    userTO.getVirAttrs().clear();
    userTO.getDerAttrs().clear();
    userTO.getMemberships().clear();

    // add membership, with virtual attribute populated, to user
    MembershipTO membership = new MembershipTO();
    membership.setRoleId(roleTO.getId());
    membership.getVirAttrs().add(attributeTO("mvirtualdata", "syncope458@syncope.apache.org"));
    userTO.getMemberships().add(membership);

    //propagate user
    userTO = createUser(userTO);
    assertEquals(1, userTO.getPropagationStatusTOs().size());
    assertTrue(userTO.getPropagationStatusTOs().get(0).getStatus().isSuccessful());
    // -------------------------------------------

    // 1. check if membership has virtual attribute populated
    assertNotNull(userTO.getMemberships().get(0).getVirAttrMap().get("mvirtualdata"));
    assertEquals("syncope458@syncope.apache.org",
            userTO.getMemberships().get(0).getVirAttrMap().get("mvirtualdata").getValues().get(0));
    // -------------------------------------------

    // 2. update membership virtual attribute
    MembershipMod membershipMod = new MembershipMod();
    membershipMod.setRole(roleTO.getId());
    membershipMod.getVirAttrsToUpdate().add(attributeMod("mvirtualdata", "syncope458_NEW@syncope.apache.org"));

    UserMod userMod = new UserMod();
    userMod.setId(userTO.getId());
    userMod.getMembershipsToAdd().add(membershipMod);
    userMod.getMembershipsToRemove().add(userTO.getMemberships().iterator().next().getId());

    userTO = updateUser(userMod);
    assertNotNull(userTO);
    // 3. check again after update if membership has virtual attribute populated with new value
    assertNotNull(userTO.getMemberships().get(0).getVirAttrMap().get("mvirtualdata"));
    assertEquals("syncope458_NEW@syncope.apache.org",
            userTO.getMemberships().get(0).getVirAttrMap().get("mvirtualdata").getValues().get(0));

    // ----------------------------------------
    // force cache expiring without any modification
    // ----------------------------------------
    String jdbcURL = null;
    ConnInstanceTO connInstanceBean = connectorService.readByResource(RESOURCE_NAME_DBVIRATTR);
    for (ConnConfProperty prop : connInstanceBean.getConfiguration()) {
        if ("jdbcUrlTemplate".equals(prop.getSchema().getName())) {
            jdbcURL = prop.getValues().iterator().next().toString();
            prop.getValues().clear();
            prop.getValues().add("jdbc:h2:tcp://localhost:9092/xxx");
        }
    }

    connectorService.update(connInstanceBean.getId(), connInstanceBean);

    membershipMod = new MembershipMod();
    membershipMod.setRole(roleTO.getId());
    membershipMod.getVirAttrsToUpdate()
            .add(attributeMod("mvirtualdata", "syncope458_updated@syncope.apache.org"));

    userMod = new UserMod();
    userMod.setId(userTO.getId());
    userMod.getMembershipsToAdd().add(membershipMod);
    userMod.getMembershipsToRemove().add(userTO.getMemberships().iterator().next().getId());

    userTO = updateUser(userMod);
    assertNotNull(userTO);
    // ----------------------------------

    // change attribute value directly on resource
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

    String value = jdbcTemplate.queryForObject("SELECT EMAIL FROM testsync WHERE ID=?", String.class,
            userTO.getId());
    assertEquals("syncope458_NEW@syncope.apache.org", value);

    jdbcTemplate.update("UPDATE testsync set EMAIL='syncope458_NEW_TWO@syncope.apache.org' WHERE ID=?",
            userTO.getId());

    value = jdbcTemplate.queryForObject("SELECT EMAIL FROM testsync WHERE ID=?", String.class, userTO.getId());
    assertEquals("syncope458_NEW_TWO@syncope.apache.org", value);
    // ----------------------------------------

    // ----------------------------------------
    // restore connector
    // ----------------------------------------
    for (ConnConfProperty prop : connInstanceBean.getConfiguration()) {
        if ("jdbcUrlTemplate".equals(prop.getSchema().getName())) {
            prop.getValues().clear();
            prop.getValues().add(jdbcURL);
        }
    }
    connectorService.update(connInstanceBean.getId(), connInstanceBean);
    // ----------------------------------------

    userTO = userService.read(userTO.getId());
    assertNotNull(userTO);
    // 4. check virtual attribute synchronization after direct update on resource
    assertEquals("syncope458_NEW_TWO@syncope.apache.org",
            userTO.getMemberships().get(0).getVirAttrMap().get("mvirtualdata").getValues().get(0));

    // 5. remove membership virtual attribute
    membershipMod = new MembershipMod();
    membershipMod.setRole(roleTO.getId());
    membershipMod.getVirAttrsToRemove().add("mvirtualdata");

    userMod = new UserMod();
    userMod.setId(userTO.getId());
    userMod.getMembershipsToAdd().add(membershipMod);
    userMod.getMembershipsToRemove().add(userTO.getMemberships().iterator().next().getId());

    userTO = updateUser(userMod);
    assertNotNull(userTO);
    // check again after update if membership hasn't any virtual attribute
    assertTrue(userTO.getMemberships().get(0).getVirAttrMap().isEmpty());

    // -------------------------------------------
    // Delete role ad-hoc and restore resource mapping
    // -------------------------------------------
    roleService.delete(roleTO.getId());

    resourceUMapping.removeItem(item);
    resourceDBVirAttr.setUmapping(resourceUMapping);
    resourceService.update(RESOURCE_NAME_DBVIRATTR, resourceDBVirAttr);
    // -------------------------------------------
}

From source file:org.apache.syncope.core.rest.VirAttrTestITCase.java

@Test
public void issueSYNCOPE501() {
    // PHASE 1: update only user virtual attributes

    // 1. create user and propagate him on resource-db-virattr
    UserTO userTO = UserTestITCase.getUniqueSampleTO("syncope501@apache.org");
    userTO.getResources().clear();//from w  w w .  ja v a 2  s.c  o  m
    userTO.getMemberships().clear();
    userTO.getVirAttrs().clear();

    userTO.getResources().add(RESOURCE_NAME_DBVIRATTR);

    // virtualdata is mapped with username
    final AttributeTO virtualData = attributeTO("virtualdata", "syncope501@apache.org");
    userTO.getVirAttrs().add(virtualData);

    userTO = createUser(userTO);

    assertNotNull(userTO.getVirAttrMap().get("virtualdata"));
    assertEquals("syncope501@apache.org", userTO.getVirAttrMap().get("virtualdata").getValues().get(0));

    // 2. update virtual attribute
    UserMod userMod = new UserMod();
    userMod.setId(userTO.getId());

    final StatusMod statusMod = new StatusMod();
    statusMod.getResourceNames().addAll(Collections.<String>emptySet());
    statusMod.setOnSyncope(Boolean.FALSE);

    userMod.setPwdPropRequest(statusMod);
    // change virtual attribute value
    final AttributeMod virtualDataMod = new AttributeMod();
    virtualDataMod.setSchema("virtualdata");
    virtualDataMod.getValuesToBeAdded().add("syncope501_updated@apache.org");
    virtualDataMod.getValuesToBeRemoved().add("syncope501@apache.org");
    userMod.getVirAttrsToUpdate().add(virtualDataMod);
    userMod.getVirAttrsToRemove().add("virtualdata");

    userTO = updateUser(userMod);
    assertNotNull(userTO);

    // 3. check that user virtual attribute has really been updated 
    assertFalse(userTO.getVirAttrMap().get("virtualdata").getValues().isEmpty());
    assertEquals("syncope501_updated@apache.org", userTO.getVirAttrMap().get("virtualdata").getValues().get(0));

    // ----------------------------------------------------------
    // PHASE 2: update only membership virtual attributes
    // -------------------------------------------
    // Update resource-db-virattr mapping adding new membership virtual schema mapping
    // -------------------------------------------
    ResourceTO resourceDBVirAttr = resourceService.read(RESOURCE_NAME_DBVIRATTR);
    assertNotNull(resourceDBVirAttr);

    final MappingTO resourceUMapping = resourceDBVirAttr.getUmapping();

    MappingItemTO item = new MappingItemTO();
    item.setIntAttrName("mvirtualdata");
    item.setIntMappingType(IntMappingType.MembershipVirtualSchema);
    item.setExtAttrName("EMAIL");
    item.setPurpose(MappingPurpose.BOTH);

    resourceUMapping.addItem(item);

    resourceDBVirAttr.setUmapping(resourceUMapping);

    resourceService.update(RESOURCE_NAME_DBVIRATTR, resourceDBVirAttr);
    // -------------------------------------------

    // -------------------------------------------
    // Create a role ad-hoc
    // -------------------------------------------
    final String roleName = "issueSYNCOPE501-Role-" + getUUIDString();
    RoleTO roleTO = new RoleTO();
    roleTO.setName(roleName);
    roleTO.setParent(2L);
    roleTO.setInheritTemplates(true);
    roleTO = createRole(roleTO);
    // -------------------------------------------

    // 1. add membership, with virtual attribute populated, to user
    MembershipMod membershipMod = new MembershipMod();
    membershipMod.setRole(roleTO.getId());
    membershipMod.getVirAttrsToUpdate().add(attributeMod("mvirtualdata", "syncope501membership@test.org"));

    userMod = new UserMod();
    userMod.setId(userTO.getId());
    userMod.getMembershipsToAdd().add(membershipMod);
    userMod.setPwdPropRequest(statusMod);

    userTO = updateUser(userMod);
    assertNotNull(userTO);
    assertEquals("syncope501membership@test.org",
            userTO.getMemberships().get(0).getVirAttrMap().get("mvirtualdata").getValues().get(0));

    // 2. update only membership virtual attribute and propagate user
    membershipMod = new MembershipMod();
    membershipMod.setRole(roleTO.getId());
    membershipMod.getVirAttrsToUpdate()
            .add(attributeMod("mvirtualdata", "syncope501membership_updated@test.org"));
    membershipMod.getVirAttrsToRemove().add("syncope501membership@test.org");

    userMod = new UserMod();
    userMod.setId(userTO.getId());
    userMod.getMembershipsToAdd().add(membershipMod);
    userMod.getMembershipsToRemove().add(userTO.getMemberships().iterator().next().getId());
    userMod.setPwdPropRequest(statusMod);

    userTO = updateUser(userMod);
    assertNotNull(userTO);

    // 3. check if change has been propagated
    assertEquals("syncope501membership_updated@test.org",
            userTO.getMemberships().get(0).getVirAttrMap().get("mvirtualdata").getValues().get(0));

    // 4. delete membership and check on resource attribute deletion
    userMod = new UserMod();
    userMod.setId(userTO.getId());
    userMod.getMembershipsToRemove().add(userTO.getMemberships().get(0).getId());
    userMod.setPwdPropRequest(statusMod);

    userTO = updateUser(userMod);
    assertNotNull(userTO);
    assertTrue(userTO.getMemberships().isEmpty());

    // read attribute value directly on resource
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

    final String emailValue = jdbcTemplate.queryForObject("SELECT EMAIL FROM testsync WHERE ID=?", String.class,
            userTO.getId());
    assertTrue(StringUtils.isBlank(emailValue));
    // ----------------------------------------

    // -------------------------------------------
    // Delete role ad-hoc and restore resource mapping
    // -------------------------------------------
    roleService.delete(roleTO.getId());

    resourceUMapping.removeItem(item);
    resourceDBVirAttr.setUmapping(resourceUMapping);
    resourceService.update(RESOURCE_NAME_DBVIRATTR, resourceDBVirAttr);
    // -------------------------------------------
}

From source file:org.apache.syncope.fit.AbstractITCase.java

protected <T> T queryForObject(final JdbcTemplate jdbcTemplate, final int maxWaitSeconds, final String sql,
        final Class<T> requiredType, final Object... args) {

    int i = 0;/*w w  w.  j a va 2 s  . c om*/
    int maxit = maxWaitSeconds;

    T object = null;

    do {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }

        try {
            object = jdbcTemplate.queryForObject(sql, requiredType, args);
        } catch (Exception e) {
            LOG.warn("While executing query {}", sql, e);
        }

        i++;
    } while (object == null && i < maxit);
    if (object == null) {
        fail("Timeout when executing query " + sql);
    }

    return object;
}

From source file:org.apache.syncope.fit.core.MembershipITCase.java

@Test
public void pull() {
    // 0. create ad-hoc resource, with adequate mapping
    ResourceTO newResource = resourceService.read(RESOURCE_NAME_DBPULL);
    newResource.setKey(getUUIDString());

    MappingItemTO item = IterableUtils.find(newResource.getProvision("USER").getMapping().getItems(),
            new Predicate<MappingItemTO>() {

                @Override/*w  ww  . java  2  s  . c  o m*/
                public boolean evaluate(final MappingItemTO object) {
                    return "firstname".equals(object.getIntAttrName());
                }
            });
    assertNotNull(item);
    assertEquals("ID", item.getExtAttrName());
    item.setIntAttrName("memberships[additional].aLong");
    item.setPurpose(MappingPurpose.BOTH);

    item = IterableUtils.find(newResource.getProvision("USER").getMapping().getItems(),
            new Predicate<MappingItemTO>() {

                @Override
                public boolean evaluate(final MappingItemTO object) {
                    return "fullname".equals(object.getIntAttrName());
                }
            });
    item.setPurpose(MappingPurpose.PULL);

    PullTaskTO newTask = null;
    try {
        newResource = createResource(newResource);
        assertNotNull(newResource);

        // 1. create user with new resource assigned
        UserTO user = UserITCase.getUniqueSampleTO("memb@apache.org");
        user.setRealm("/even/two");
        user.getPlainAttrs().remove(user.getPlainAttrMap().get("ctype"));
        user.getResources().clear();
        user.getResources().add(newResource.getKey());

        MembershipTO membership = new MembershipTO.Builder().group("034740a9-fa10-453b-af37-dc7897e98fb1")
                .build();
        membership.getPlainAttrs().add(new AttrTO.Builder().schema("aLong").value("5432").build());
        user.getMemberships().add(membership);

        user = createUser(user).getEntity();
        assertNotNull(user);

        // 2. verify that user was found on resource
        JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);
        String idOnResource = jdbcTemplate.queryForObject("SELECT id FROM testpull WHERE id=?", String.class,
                "5432");
        assertEquals("5432", idOnResource);

        // 3. unlink user from resource, then remove it
        DeassociationPatch patch = new DeassociationPatch();
        patch.setKey(user.getKey());
        patch.setAction(ResourceDeassociationAction.UNLINK);
        patch.getResources().add(newResource.getKey());
        assertNotNull(userService.deassociate(patch).readEntity(BulkActionResult.class));

        userService.delete(user.getKey());

        // 4. create pull task and execute
        newTask = taskService.read("7c2242f4-14af-4ab5-af31-cdae23783655", true);
        newTask.setResource(newResource.getKey());
        newTask.setDestinationRealm("/even/two");

        Response response = taskService.create(newTask);
        newTask = getObject(response.getLocation(), TaskService.class, PullTaskTO.class);
        assertNotNull(newTask);

        ExecTO execution = AbstractTaskITCase.execProvisioningTask(taskService, newTask.getKey(), 50, false);
        assertEquals(PropagationTaskExecStatus.SUCCESS,
                PropagationTaskExecStatus.valueOf(execution.getStatus()));

        // 5. verify that pulled user has
        PagedResult<UserTO> users = userService.search(new AnyQuery.Builder().realm("/").fiql(SyncopeClient
                .getUserSearchConditionBuilder().is("username").equalTo(user.getUsername()).query()).build());
        assertEquals(1, users.getTotalCount());
        assertEquals(1, users.getResult().get(0).getMemberships().size());
        assertEquals("5432", users.getResult().get(0).getMemberships().get(0).getPlainAttrMap().get("aLong")
                .getValues().get(0));
    } catch (Exception e) {
        LOG.error("Unexpected error", e);
        fail(e.getMessage());
    } finally {
        if (newTask != null && !"83f7e85d-9774-43fe-adba-ccd856312994".equals(newTask.getKey())) {
            taskService.delete(newTask.getKey());
        }
        resourceService.delete(newResource.getKey());
    }
}

From source file:org.apache.syncope.fit.core.reference.AuthenticationITCase.java

@Test
public void issueSYNCOPE164() throws Exception {
    // 1. create user with db resource
    UserTO user = UserITCase.getUniqueSampleTO("syncope164@syncope.apache.org");
    user.setRealm("/even/two");
    user.setPassword("password123");
    user.getResources().add(RESOURCE_NAME_TESTDB);
    user = createUser(user);//from  ww w .  j a v  a2  s . com
    assertNotNull(user);

    // 2. unlink the resource from the created user
    assertNotNull(userService
            .bulkDeassociation(user.getKey(), ResourceDeassociationActionType.UNLINK,
                    CollectionWrapper.wrap(RESOURCE_NAME_TESTDB, ResourceKey.class))
            .readEntity(BulkActionResult.class));

    // 3. change password on Syncope
    UserMod userMod = new UserMod();
    userMod.setKey(user.getKey());
    userMod.setPassword("password234");
    user = updateUser(userMod);
    assertNotNull(user);

    // 4. check that the db resource has still the initial password value
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);
    String value = jdbcTemplate.queryForObject("SELECT PASSWORD FROM test WHERE ID=?", String.class,
            user.getUsername());
    assertEquals(Encryptor.getInstance().encode("password123", CipherAlgorithm.SHA1), value.toUpperCase());

    // 5. successfully authenticate with old (on db resource) and new (on internal storage) password values
    Pair<Map<String, Set<String>>, UserTO> self = clientFactory.create(user.getUsername(), "password123")
            .self();
    assertNotNull(self);
    self = clientFactory.create(user.getUsername(), "password234").self();
    assertNotNull(self);
}

From source file:org.apache.syncope.fit.core.reference.SyncTaskITCase.java

@Test
public void issueSYNCOPE307() {
    UserTO userTO = UserITCase.getUniqueSampleTO("s307@apache.org");
    userTO.getAuxClasses().add("csv");

    AttrTO csvuserid = new AttrTO();
    csvuserid.setSchema("csvuserid");
    userTO.getDerAttrs().add(csvuserid);

    userTO.getResources().clear();//from   w  w w  .  j a v a2s.  c o m
    userTO.getResources().add(RESOURCE_NAME_WS2);
    userTO.getResources().add(RESOURCE_NAME_CSV);

    userTO = createUser(userTO);
    assertNotNull(userTO);

    userTO = userService.read(userTO.getKey());
    assertEquals("virtualvalue", userTO.getVirAttrMap().get("virtualdata").getValues().get(0));

    // Update sync task
    SyncTaskTO task = taskService.read(12L);
    assertNotNull(task);

    //  add user template
    UserTO template = new UserTO();
    template.getResources().add(RESOURCE_NAME_DBVIRATTR);

    AttrTO userId = attrTO("userId", "'s307@apache.org'");
    template.getPlainAttrs().add(userId);

    AttrTO email = attrTO("email", "'s307@apache.org'");
    template.getPlainAttrs().add(email);

    task.getTemplates().put(AnyTypeKind.USER.name(), template);

    taskService.update(task);
    execProvisioningTask(task.getKey(), 50, false);

    // check for sync policy
    userTO = userService.read(userTO.getKey());
    assertEquals("virtualvalue", userTO.getVirAttrMap().get("virtualdata").getValues().get(0));

    try {
        final JdbcTemplate jdbcTemplate = new JdbcTemplate(testDataSource);

        String value = jdbcTemplate.queryForObject("SELECT USERNAME FROM testsync WHERE ID=?", String.class,
                userTO.getKey());
        assertEquals("virtualvalue", value);
    } catch (EmptyResultDataAccessException e) {
        fail();
    }
}