Example usage for org.apache.commons.collections CollectionUtils isEqualCollection

List of usage examples for org.apache.commons.collections CollectionUtils isEqualCollection

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils isEqualCollection.

Prototype

public static boolean isEqualCollection(final Collection a, final Collection b) 

Source Link

Document

Returns true iff the given Collection s contain exactly the same elements with exactly the same cardinalities.

Usage

From source file:org.openengsb.connector.userprojects.ldap.internal.ldap.DefaultModelManagerTest.java

@Test
@Ignore//from  w w  w.j  av a2  s .c  o  m
public void testFindUsers() throws InterruptedException {
    User user = createTestUser();
    List<User> expected = Arrays.asList(user);
    ldapService.updateUsers(expected);
    assertTrue(CollectionUtils.isEqualCollection(modelManager.findUsers(), expected));
}

From source file:org.openengsb.connector.userprojects.ldap.internal.UserProjectsLdapServiceImplTest.java

private void assertCorrectlyStored(Assignment assignment)
        throws NoSuchNodeException, MissingParentException, LdapInvalidAttributeValueException {
    Entry entry = ldapDao.lookup(DnFactory.assignmentProject(assignment));
    assertThat(entry.get(SchemaConstants.STRING_ATTRIBUTE).getString(), is(assignment.getProject()));

    entry = ldapDao.lookup(DnFactory.assignmentUser(assignment));
    assertThat(entry.get(SchemaConstants.STRING_ATTRIBUTE).getString(), is(assignment.getUser()));

    List<Entry> entryList = ldapDao.getDirectChildren(DnFactory.assignmentPermissions(assignment));
    Collection<String> actualCollection = Lists.newArrayList();
    for (Entry entry2 : entryList) {
        actualCollection.add(entry2.getDn().getRdn().getValue().getString());
    }//from  w w w.  ja v  a  2 s .  co m
    assertTrue(CollectionUtils.isEqualCollection(actualCollection, assignment.getPermissions()));

    entryList = ldapDao.getDirectChildren(DnFactory.assignmentRoles(assignment));
    actualCollection.clear();
    for (Entry entry2 : entryList) {
        actualCollection.add(entry2.getDn().getRdn().getValue().getString());
    }
    assertTrue(CollectionUtils.isEqualCollection(actualCollection, assignment.getRoles()));
}

From source file:org.openengsb.connector.userprojects.ldap.internal.UserProjectsLdapServiceImplTest.java

private void assertCorrectlyStored(Role role) throws NoSuchNodeException, MissingParentException {
    List<Entry> entryList = ldapDao.getDirectChildren(DnFactory.rolePermissions(role));
    List<String> actualCollection = Lists.newArrayList();
    for (Entry entry : entryList) {
        actualCollection.add(entry.getDn().getRdn().getValue().getString());
    }//from w  ww  .  ja va  2  s  .c  o  m
    assertTrue(CollectionUtils.isEqualCollection(actualCollection, role.getPermissions()));

    entryList = ldapDao.getDirectChildren(DnFactory.roleSubroles(role));
    actualCollection.clear();
    for (Entry entry : entryList) {
        actualCollection.add(entry.getDn().getRdn().getValue().getString());
    }
    assertTrue(CollectionUtils.isEqualCollection(actualCollection, role.getRoles()));
}

From source file:org.openhie.openempi.service.UserSecurityAdvice.java

/**
 * Method to enforce security and only allow administrators to modify users. Regular
 * users are allowed to modify themselves.
 *
 * @param method the name of the method executed
 * @param args the arguments to the method
 * @param target the target class//from   ww w. j a va 2 s . co  m
 * @throws Throwable thrown when args[0] is null or not a User object
 */
public void before(Method method, Object[] args, Object target) throws Throwable {
    SecurityContext ctx = SecurityContextHolder.getContext();

    if (ctx.getAuthentication() != null) {
        Authentication auth = ctx.getAuthentication();
        boolean administrator = false;
        GrantedAuthority[] roles = auth.getAuthorities();
        for (GrantedAuthority role1 : roles) {
            if (role1.getAuthority().equals(Constants.ADMIN_ROLE)) {
                administrator = true;
                break;
            }
        }

        User user = (User) args[0];

        AuthenticationTrustResolver resolver = new AuthenticationTrustResolverImpl();
        // allow new users to signup - this is OK b/c Signup doesn't allow setting of roles
        boolean signupUser = resolver.isAnonymous(auth);

        if (!signupUser) {
            User currentUser = getCurrentUser(auth);

            if (user.getId() != null && !user.getId().equals(currentUser.getId()) && !administrator) {
                log.warn("Access Denied: '" + currentUser.getUsername() + "' tried to modify '"
                        + user.getUsername() + "'!");
                throw new AccessDeniedException(ACCESS_DENIED);
            } else if (user.getId() != null && user.getId().equals(currentUser.getId()) && !administrator) {
                // get the list of roles the user is trying add
                Set<String> userRoles = new HashSet<String>();
                if (user.getRoles() != null) {
                    for (Object o : user.getRoles()) {
                        Role role = (Role) o;
                        userRoles.add(role.getName());
                    }
                }

                // get the list of roles the user currently has
                Set<String> authorizedRoles = new HashSet<String>();
                for (GrantedAuthority role : roles) {
                    authorizedRoles.add(role.getAuthority());
                }

                // if they don't match - access denied
                // regular users aren't allowed to change their roles
                if (!CollectionUtils.isEqualCollection(userRoles, authorizedRoles)) {
                    log.warn("Access Denied: '" + currentUser.getUsername()
                            + "' tried to change their role(s)!");
                    throw new AccessDeniedException(ACCESS_DENIED);
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Registering new user '" + user.getUsername() + "'");
            }
        }
    }
}

From source file:org.openmrs.contrib.metadatarepository.service.UserSecurityAdvice.java

/**
 * Method to enforce security and only allow administrators to modify users. Regular
 * users are allowed to modify themselves.
 *
 * @param method the name of the method executed
 * @param args the arguments to the method
 * @param target the target class//from w ww  .  java  2  s  . co m
 * @throws Throwable thrown when args[0] is null or not a User object
 */
public void before(Method method, Object[] args, Object target) throws Throwable {
    SecurityContext ctx = SecurityContextHolder.getContext();

    if (ctx.getAuthentication() != null) {
        Authentication auth = ctx.getAuthentication();
        boolean administrator = false;
        Collection<GrantedAuthority> roles = auth.getAuthorities();
        for (GrantedAuthority role1 : roles) {
            if (role1.getAuthority().equals(Constants.ADMIN_ROLE)) {
                administrator = true;
                break;
            }
        }

        User user = (User) args[0];

        AuthenticationTrustResolver resolver = new AuthenticationTrustResolverImpl();
        // allow new users to signup - this is OK b/c Signup doesn't allow setting of roles
        boolean signupUser = resolver.isAnonymous(auth);

        if (!signupUser) {
            User currentUser = getCurrentUser(auth);

            if (user.getId() != null && !user.getId().equals(currentUser.getId()) && !administrator) {
                log.warn("Access Denied: '" + currentUser.getUsername() + "' tried to modify '"
                        + user.getUsername() + "'!");
                throw new AccessDeniedException(ACCESS_DENIED);
            } else if (user.getId() != null && user.getId().equals(currentUser.getId()) && !administrator) {
                // get the list of roles the user is trying add
                Set<String> userRoles = new HashSet<String>();
                if (user.getRoles() != null) {
                    for (Object o : user.getRoles()) {
                        Role role = (Role) o;
                        userRoles.add(role.getName());
                    }
                }

                // get the list of roles the user currently has
                Set<String> authorizedRoles = new HashSet<String>();
                for (GrantedAuthority role : roles) {
                    authorizedRoles.add(role.getAuthority());
                }

                // if they don't match - access denied
                // regular users aren't allowed to change their roles
                if (!CollectionUtils.isEqualCollection(userRoles, authorizedRoles)) {
                    log.warn("Access Denied: '" + currentUser.getUsername()
                            + "' tried to change their role(s)!");
                    throw new AccessDeniedException(ACCESS_DENIED);
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Registering new user '" + user.getUsername() + "'");
            }
        }
    }
}

From source file:org.openmrs.module.adminui.TestUtils.java

public static boolean sameProviders(Map<EncounterRole, Set<Provider>> a, Map<EncounterRole, Set<Provider>> b) {
    Collection<EncounterRole> roles = CollectionUtils.union(a.keySet(), b.keySet());
    for (EncounterRole role : roles) {
        Set<Provider> aSet = a.get(role);
        Set<Provider> bSet = b.get(role);
        if (aSet == null) {
            aSet = Collections.emptySet();
        }//from   w  w w  .j av a  2 s .c om
        if (bSet == null) {
            bSet = Collections.emptySet();
        }
        if (!CollectionUtils.isEqualCollection(aSet, bSet)) {
            return false;
        }
    }
    return true;
}

From source file:org.openmrs.module.kenyaemr.regimen.RegimenOrder.java

/**
 * Checks the equality of regimen orders considering drugs only
 * @param order the other order/*  w ww .  j a v a 2  s  . com*/
 * @return true if the order has same drugs as this order
 */
public boolean hasSameDrugs(RegimenOrder order) {
    if (order == null) {
        return false;
    }

    return CollectionUtils.isEqualCollection(getDrugReferences(), order.getDrugReferences());
}

From source file:org.openmrs.module.reporting.calculation.PatientDataCalculationBehaviorTest.java

@Test
public void evaluate_shouldEvaluateAPatientCalculation() throws Exception {
    Integer patientId1 = 2;// w w  w.  j ava  2s  . com
    Integer patientId2 = 7;
    Set<PatientIdentifier> identifiers1 = ps.getPatient(patientId1).getIdentifiers();
    Set<PatientIdentifier> identifiers2 = ps.getPatient(patientId2).getIdentifiers();
    PatientDataCalculation calculation = new PatientDataCalculationProvider().getCalculation(
            "org.openmrs.module.reporting.data.patient.definition.PatientIdentifierDataDefinition", null);
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("types", ps.getAllPatientIdentifierTypes(false));

    CalculationResultMap results = calculation.evaluate(Arrays.asList(patientId1, patientId2), parameters,
            null);

    Assert.assertEquals(identifiers2.iterator().next(),
            ResultUtil.getFirst(results.get(patientId2)).getValue());

    ListResult lr = (ListResult) results.get(patientId1);
    Assert.assertEquals(3, lr.size());

    Assert.assertTrue(CollectionUtils.isEqualCollection(identifiers1, lr.getValues()));
}

From source file:org.opensubsystems.pattern.parameter.data.impl.ParameterImpl.java

/**
 * {@inheritDoc}/*from   w w  w . j  a v a2 s . com*/
 */
@Override
public boolean isSame(Object oObject) {
    boolean bReturn = false;
    Parameter helper;

    if (oObject == this) {
        bReturn = true;
    } else if ((oObject != null) && (oObject instanceof IdentifiableDataObject)) {
        helper = (Parameter) oObject;
        bReturn = CollectionUtils.isEqualCollection(getValues(), helper.getValues()) && (super.isSame(oObject));
    }

    return bReturn;
}

From source file:org.opensubsystems.pattern.parameter.data.impl.ParametrizedIdentifiableDataObjectImpl.java

/**
 * {@inheritDoc}//from w  w  w.j  a va2  s  .  c  o m
 */
@Override
public boolean isSame(Object oObject) {
    boolean bReturn = false;
    ParametrizedIdentifiableDataObject helper;

    if (oObject == this) {
        bReturn = true;
    } else if ((oObject != null) && (oObject instanceof IdentifiableDataObject)) {
        helper = (ParametrizedIdentifiableDataObject) oObject;
        bReturn = CollectionUtils.isEqualCollection(getParameters(), helper.getParameters())
                && (super.isSame(oObject));
    }

    return bReturn;
}