@Test // @Ignore
  public void testResolve() throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);
    try {
      Set<Attribute> attrs = fillInSampleUser(TEST_USER);

      // Delete the account if it already exists
      //
      deleteUser(TEST_USER, info);
      try {
        info.resolveUsername(ObjectClass.ACCOUNT, TEST_USER, new OperationOptions(new HashMap()));
        Assert.fail("exception expected");
      } catch (UnknownUidException ue) {
        // expected
      }

      // Create the account
      //
      Uid newUid = info.create(ObjectClass.ACCOUNT, attrs, null);
      System.out.println(newUid.getValue() + " created");
      Uid retrievedUid =
          info.resolveUsername(ObjectClass.ACCOUNT, TEST_USER, new OperationOptions(new HashMap()));
      Assert.assertEquals(newUid, retrievedUid);
    } finally {
      info.dispose();
    }
  }
  @Test // @Ignore
  public void testChangePassword() throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);
    try {
      Set<Attribute> attrs = fillInSampleUser(TEST_USER);

      // Delete the account if it already exists
      //
      deleteUser(TEST_USER, info);

      // Create the account
      //
      Uid newUid = info.create(ObjectClass.ACCOUNT, attrs, null);
      System.out.println(newUid.getValue() + " created");

      ConnectorObjectBuilder builder = new ConnectorObjectBuilder();
      builder.setUid(newUid);
      Attribute password =
          AttributeBuilder.build(
              OperationalAttributes.PASSWORD_NAME, new GuardedString("xyzzy123".toCharArray()));
      builder.addAttribute(password);
      builder.addAttribute(new Name(TEST_USER));
      ConnectorObject newUser = builder.build();
      info.update(newUser.getObjectClass(), newUser.getAttributes(), null);
    } finally {
      info.dispose();
    }
  }
  @Test // @Ignore
  public void testModifyUser() throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);

    try {
      // Delete the user
      //
      deleteUser(TEST_USER, info);

      Set<Attribute> attrs = fillInSampleUser(TEST_USER);
      info.create(ObjectClass.ACCOUNT, attrs, null);

      ConnectorObject user = getUser(TEST_USER);
      Set<Attribute> changed = new HashSet<Attribute>();
      changed.add(AttributeBuilder.build(ATTR_FIRSTNAME, "abel"));
      changed.add(user.getUid());
      info.update(ObjectClass.ACCOUNT, changed, null);

      ConnectorObject changedUser = getUser(TEST_USER);
      Attribute firstname = changedUser.getAttributeByName(ATTR_FIRSTNAME);
      displayUser(changedUser);
      Assert.assertNotNull(firstname);
      Assert.assertTrue(AttributeUtil.getStringValue(firstname).equalsIgnoreCase("abel"));
    } finally {
      info.dispose();
    }
  }
  @Test // @Ignore
  public void testCreate() throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);
    Set<Attribute> attrs = fillInSampleUser(TEST_USER);

    try {
      // Delete the account if it already exists
      //
      deleteUser(TEST_USER, info);

      // Create the account
      //
      Uid newUid = info.create(ObjectClass.ACCOUNT, attrs, null);
      System.out.println(newUid.getValue() + " created");
      try {
        info.create(ObjectClass.ACCOUNT, attrs, null);
        Assert.fail("should have thrown exception");
      } catch (AlreadyExistsException aee) {
        // expected
      }
    } finally {
      info.dispose();
    }
  }
 @Test // @Ignore
 public void testListSchema() throws Exception {
   SpmlConfiguration config = createConfiguration();
   SpmlConnector info = createConnector(config);
   try {
     Schema schema = info.schema();
     boolean personFound = false;
     boolean firstnameFound = false;
     for (ObjectClassInfo ocInfo : schema.getObjectClassInfo()) {
       if (ocInfo.is(ObjectClass.ACCOUNT_NAME)) {
         System.out.println("Schema for " + ocInfo.getType());
         personFound = true;
         for (AttributeInfo attr : ocInfo.getAttributeInfo()) {
           System.out.println("    " + attr);
           if (attr.getName().equals("firstname")) {
             firstnameFound = true;
           }
         }
       }
     }
     Assert.assertTrue(personFound);
     Assert.assertTrue(firstnameFound);
   } finally {
     info.dispose();
   }
 }
  @Test // @Ignore
  public void testEnableDisable() throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);
    try {
      Set<Attribute> attrs = fillInSampleUser(TEST_USER);

      // Delete the account if it already exists
      //
      deleteUser(TEST_USER, info);

      // Create the account
      //
      Uid newUid = info.create(ObjectClass.ACCOUNT, attrs, null);
      System.out.println(newUid.getValue() + " created");

      // Test disabling the user
      {
        ConnectorObjectBuilder builder = new ConnectorObjectBuilder();
        builder.setUid(newUid);
        Attribute password =
            AttributeBuilder.build(OperationalAttributes.ENABLE_NAME, Boolean.FALSE);
        builder.addAttribute(password);
        builder.addAttribute(new Name(TEST_USER));
        ConnectorObject newUser = builder.build();
        info.update(newUser.getObjectClass(), newUser.getAttributes(), null);

        Map map = new HashMap<String, Object>();
        map.put(
            OperationOptions.OP_ATTRIBUTES_TO_GET,
            new String[] {OperationalAttributes.ENABLE_NAME});
        OperationOptions options = new OperationOptions(map);

        ConnectorObject user = getUser(TEST_USER, options);
        Attribute enabled = user.getAttributeByName(OperationalAttributes.ENABLE_NAME);
        Assert.assertNotNull(enabled);
        Assert.assertFalse(AttributeUtil.getBooleanValue(enabled));
      }

      // Test enabling the user
      {
        ConnectorObjectBuilder builder = new ConnectorObjectBuilder();
        builder.setUid(newUid);
        Attribute password =
            AttributeBuilder.build(OperationalAttributes.ENABLE_NAME, Boolean.TRUE);
        builder.addAttribute(password);
        builder.addAttribute(new Name(TEST_USER));
        ConnectorObject newUser = builder.build();
        info.update(newUser.getObjectClass(), newUser.getAttributes(), null);

        Attribute enabled = newUser.getAttributeByName(OperationalAttributes.ENABLE_NAME);
        Assert.assertNotNull(enabled);
        Assert.assertTrue(AttributeUtil.getBooleanValue(enabled));
      }
    } finally {
      info.dispose();
    }
  }
 @Test // @Ignore
 public void testTest() throws Exception {
   SpmlConfiguration config = createConfiguration();
   SpmlConnector info = createConnector(config);
   try {
     info.checkAlive();
   } finally {
     info.dispose();
   }
 }
 @Test // @Ignore
 public void testListAllUsers() throws Exception {
   SpmlConfiguration config = createConfiguration();
   SpmlConnector info = createConnector(config);
   try {
     TestHandler handler = new TestHandler();
     TestHelpers.search(info, ObjectClass.ACCOUNT, null, handler, null);
     for (ConnectorObject user : handler) {
       System.out.println("Read User:" + user.getUid().getValue());
     }
   } finally {
     info.dispose();
   }
 }
 private void deleteUser(final String testUser, SpmlConnector connector) {
   try {
     connector.delete(ObjectClass.ACCOUNT, new Uid("person:" + testUser), null);
   } catch (UnknownUidException rte) {
     // Ignore
   }
 }
 @Test
 public void testNegative() throws Exception {
   SpmlConfiguration config = createConfiguration();
   config.setPassword(new GuardedString("bogus".toCharArray()));
   try {
     createConnector(config);
     Assert.fail("expected exception");
   } catch (RuntimeException e) {
     // expected
   }
   config = createConfiguration();
   config.setPostConnectCommand(null);
   SpmlConnector info = createConnector(config);
   try {
     Set<Attribute> attrs = fillInSampleUser(TEST_USER);
     info.create(ObjectClass.ACCOUNT, attrs, null);
     Assert.fail("expected exception");
   } catch (RuntimeException e) {
     // expected
   }
   try {
     info.delete(ObjectClass.ACCOUNT, new Uid(TEST_USER), null);
     Assert.fail("expected exception");
   } catch (RuntimeException e) {
     // expected
   }
   try {
     Set<Attribute> attrs = fillInSampleUser(TEST_USER);
     info.update(ObjectClass.ACCOUNT, attrs, null);
     Assert.fail("expected exception");
   } catch (RuntimeException e) {
     // expected
   }
   try {
     TestHandler handler = new TestHandler();
     TestHelpers.search(
         info,
         ObjectClass.ACCOUNT,
         new EqualsFilter(AttributeBuilder.build(Uid.NAME, "asdhjfdaslfh alsk fhasldk ")),
         handler,
         null);
     Assert.fail("expected exception");
   } catch (RuntimeException e) {
     // expected
   }
 }
  @Test // @Ignore
  public void testGetSpecifiedUser() throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);
    try {
      Set<Attribute> attrs = fillInSampleUser(TEST_USER);

      deleteUser(TEST_USER, info);

      info.create(ObjectClass.ACCOUNT, attrs, null);

      ConnectorObject user = getUser(TEST_USER);
      Assert.assertNotNull(user);

      TestHandler handler = new TestHandler();
      TestHelpers.search(
          info,
          ObjectClass.ACCOUNT,
          new EqualsFilter(AttributeBuilder.build(Uid.NAME, "asdhjfdaslfh alsk fhasldk ")),
          handler,
          null);
      Assert.assertFalse(handler.iterator().hasNext());

      handler = new TestHandler();
      String[] attributesToGet = {"firstname"};
      Map<String, Object> optionsMap = new HashMap<String, Object>();
      optionsMap.put(OperationOptions.OP_ATTRIBUTES_TO_GET, attributesToGet);
      OperationOptions options = new OperationOptions(optionsMap);
      TestHelpers.search(
          info,
          ObjectClass.ACCOUNT,
          new EqualsFilter(AttributeBuilder.build(Uid.NAME, TEST_USER)),
          handler,
          options);
      Assert.assertTrue(handler.iterator().hasNext());
      ConnectorObject object = handler.iterator().next();
      Assert.assertNotNull(object.getAttributeByName("firstname"));
      Assert.assertNull(object.getAttributeByName("lastname"));
    } finally {
      info.dispose();
    }
  }
  private ConnectorObject getUser(String accountId, OperationOptions options) throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);

    try {
      TestHandler handler = new TestHandler();
      TestHelpers.search(
          info,
          ObjectClass.ACCOUNT,
          new EqualsFilter(AttributeBuilder.build(Name.NAME, accountId)),
          handler,
          options);
      if (!handler.iterator().hasNext()) return null;
      return handler.iterator().next();
    } catch (UnknownUidException e) {
      return null;
    } finally {
      info.dispose();
    }
  }
  @Test // @Ignore
  public void testDelete() throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);
    Set<Attribute> attrs = fillInSampleUser(TEST_USER);

    try {
      // Create the account if it doesn't already exist
      //
      ConnectorObject user = getUser(TEST_USER);
      Uid newUid = null;
      if (user == null) {
        try {
          newUid = info.create(ObjectClass.ACCOUNT, attrs, null);
          System.out.println(newUid.getValue() + " created");
        } catch (AlreadyExistsException rte) {
          // ignore
        }
      } else {
        newUid = user.getUid();
      }

      // Delete the account
      //
      info.delete(ObjectClass.ACCOUNT, newUid, null);
      System.out.println(newUid.getValue() + " deleted");

      try {
        info.delete(ObjectClass.ACCOUNT, newUid, null);
        Assert.fail("Should have seen exception");
      } catch (UnknownUidException uue) {
        // expected
      }
    } finally {
      info.dispose();
    }
  }
 private SpmlConnector createConnector(SpmlConfiguration config) throws Exception {
   SpmlConnector rv = new SpmlConnector();
   rv.init(config);
   return rv;
 }
  @Test // @Ignore
  public void testSearchSpecifiedUser() throws Exception {
    SpmlConfiguration config = createConfiguration();
    SpmlConnector info = createConnector(config);
    try {
      Set<Attribute> attrs = fillInSampleUser(TEST_USER);

      deleteUser(TEST_USER, info);
      Uid createdUserUid = info.create(ObjectClass.ACCOUNT, attrs, null);

      // Simple test of EqualsFilter
      //
      TestHandler handler = new TestHandler();
      TestHelpers.search(
          info,
          ObjectClass.ACCOUNT,
          new EqualsFilter(AttributeBuilder.build(ATTR_LASTNAME, "User")),
          handler,
          null);
      boolean found = false;
      int count = 0;
      for (ConnectorObject user : handler) {
        if (TEST_USER.equals(user.getName().getNameValue())) found = true;
        count++;
      }
      Assert.assertTrue(count == 1);
      Assert.assertTrue(found);

      // Simple test of StartsWithFilter
      //
      handler = new TestHandler();
      TestHelpers.search(
          info,
          ObjectClass.ACCOUNT,
          new StartsWithFilter(AttributeBuilder.build(ATTR_LASTNAME, "User")),
          handler,
          null);
      found = false;
      for (ConnectorObject user : handler) {
        if (TEST_USER.equals(user.getName().getNameValue())) found = true;
        Assert.assertTrue(
            AttributeUtil.getStringValue(user.getAttributeByName(ATTR_LASTNAME))
                .startsWith("User"));
      }
      Assert.assertTrue(found);

      // Simple test of EndsWithFilter
      //
      handler = new TestHandler();
      TestHelpers.search(
          info,
          ObjectClass.ACCOUNT,
          new EndsWithFilter(AttributeBuilder.build(ATTR_LASTNAME, "User")),
          handler,
          null);
      found = false;
      for (ConnectorObject user : handler) {
        if (TEST_USER.equals(user.getName().getNameValue())) found = true;
        Assert.assertTrue(
            AttributeUtil.getStringValue(user.getAttributeByName(ATTR_LASTNAME)).endsWith("User"));
      }
      Assert.assertTrue(found);

      // Simple test of ContainsFilter
      //
      handler = new TestHandler();
      TestHelpers.search(
          info,
          ObjectClass.ACCOUNT,
          new ContainsFilter(AttributeBuilder.build(ATTR_LASTNAME, "User")),
          handler,
          null);
      found = false;
      for (ConnectorObject user : handler) {
        if (TEST_USER.equals(user.getName().getNameValue())) found = true;
        Assert.assertTrue(
            AttributeUtil.getStringValue(user.getAttributeByName(ATTR_LASTNAME)).contains("User"));
      }
      Assert.assertTrue(found);

      // Simple test of EqualsFilter
      //
      {
        handler = new TestHandler();
        TestHelpers.search(
            info, ObjectClass.ACCOUNT, new EqualsFilter(createdUserUid), handler, null);
        found = false;
        count = 0;
        for (ConnectorObject user : handler) {
          if (TEST_USER.equals(user.getName().getNameValue())) found = true;
          count++;
        }
        Assert.assertTrue(count == 1);
        Assert.assertTrue(found);
      }

      // Test of And
      //
      handler = new TestHandler();
      Filter filter =
          new AndFilter(
              new EqualsFilter(AttributeBuilder.build(ATTR_LASTNAME, "User")),
              new EqualsFilter(AttributeBuilder.build(ATTR_FIRSTNAME, "SPML")));
      TestHelpers.search(info, ObjectClass.ACCOUNT, filter, handler, null);
      found = false;
      count = 0;
      for (ConnectorObject user : handler) {
        if (TEST_USER.equals(user.getName().getNameValue())) found = true;
        count++;
      }

      Assert.assertTrue(count == 1);
      Assert.assertTrue(found);

      // Change the first name
      //
      ConnectorObject updateUser = getUser(TEST_USER);
      Set<Attribute> changed = new HashSet<Attribute>();
      changed.add(AttributeBuilder.build(ATTR_FIRSTNAME, "abel"));
      changed.add(updateUser.getUid());
      info.update(ObjectClass.ACCOUNT, changed, null);

      // Test of And, which should fail, since firstname has changed
      //
      handler = new TestHandler();
      filter =
          new AndFilter(
              new EqualsFilter(AttributeBuilder.build(ATTR_LASTNAME, "User")),
              new EqualsFilter(AttributeBuilder.build(ATTR_FIRSTNAME, "SPML")));
      TestHelpers.search(info, ObjectClass.ACCOUNT, filter, handler, null);
      found = false;
      count = 0;
      for (ConnectorObject user : handler) {
        if (TEST_USER.equals(user.getName().getNameValue())) found = true;
        count++;
      }
      Assert.assertTrue(count == 0);
      Assert.assertTrue(!found);

      // Test of Or, which should succeed, since lastname has not changed
      //
      handler = new TestHandler();
      filter =
          new OrFilter(
              new EqualsFilter(AttributeBuilder.build(ATTR_LASTNAME, "User")),
              new EqualsFilter(AttributeBuilder.build(ATTR_FIRSTNAME, "SPML")));
      TestHelpers.search(info, ObjectClass.ACCOUNT, filter, handler, null);
      found = false;
      count = 0;
      for (ConnectorObject user : handler) {
        if (TEST_USER.equals(user.getName().getNameValue())) found = true;
        count++;
      }
      Assert.assertTrue(count > 0);
      Assert.assertTrue(found);

    } finally {
      info.dispose();
    }
  }