Ejemplo n.º 1
0
  private void assertUserAssignment(
      JdoUserAssignment expectedAssignment, JdoUserAssignment assignment) {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
    assertNotNull(expectedAssignment.getTimestamp());
    assertNotNull(assignment.getTimestamp());

    assertEquals(
        sdf.format(expectedAssignment.getTimestamp()), sdf.format(assignment.getTimestamp()));
    assertEquals(expectedAssignment.getRoleNames(), assignment.getRoleNames());
  }
Ejemplo n.º 2
0
  @SuppressWarnings("unchecked")
  public void testStax() throws IOException, XMLStreamException {
    RbacDatabase database = new RbacDatabase();

    JdoRole role = new JdoRole();
    role.setAssignable(true);
    role.setDescription("descriptor");
    role.setName("name");
    role.setPermanent(true);
    role.addChildRoleName("childRole1");
    role.addChildRoleName("childRole2");

    JdoPermission permission = new JdoPermission();
    permission.setDescription("permDesc");
    permission.setName("permName");

    JdoOperation operation = new JdoOperation();
    operation.setDescription("opDesc");
    operation.setName("opName");
    operation.setPermanent(true);
    operation.setResourceRequired(true);
    permission.setOperation(operation);
    database.addOperation(operation);

    JdoResource resource = new JdoResource();
    resource.setIdentifier("resId");
    resource.setPattern(true);
    resource.setPermanent(true);
    permission.setResource(resource);
    database.addResource(resource);
    permission.setPermanent(true);
    role.addPermission(permission);
    database.addPermission(permission);

    database.addRole(role);

    JdoUserAssignment assignment = new JdoUserAssignment();
    assignment.setPermanent(false);
    assignment.setPrincipal("principal");
    assignment.setTimestamp(new Date());
    assignment.addRoleName("name");

    database.addUserAssignment(assignment);

    StringWriter w = new StringWriter();
    new RbacJdoModelStaxWriter().write(w, database);

    RbacDatabase newDatabase = new RbacJdoModelStaxReader().read(new StringReader(w.toString()));

    List<JdoRole> expectedRoles = database.getRoles();
    List<JdoRole> roles = newDatabase.getRoles();
    assertEquals(expectedRoles.size(), roles.size());
    for (JdoRole r : roles) {
      boolean found = false;
      for (JdoRole expectedRole : expectedRoles) {
        if (expectedRole.getName().equals(r.getName())) {
          found = true;

          assertRole(expectedRole, r);
        }
      }
      if (!found) {
        fail("Couldn't find role: " + r.getName());
      }
    }

    List<JdoUserAssignment> expectedUserAssignments = database.getUserAssignments();
    List<JdoUserAssignment> userAssignments = newDatabase.getUserAssignments();
    assertEquals(expectedUserAssignments.size(), userAssignments.size());
    for (JdoUserAssignment a : userAssignments) {
      boolean found = false;
      for (JdoUserAssignment expectedAssignment : expectedUserAssignments) {
        if (expectedAssignment.getPrincipal().equals(a.getPrincipal())) {
          found = true;

          assertUserAssignment(expectedAssignment, a);
        }
      }
      if (!found) {
        fail("Couldn't find assignment: " + a.getPrincipal());
      }
    }
  }