@Test
public void testUserHomeDomainResource() {

    ZMSImpl zmsImpl = zmsInit();
    
    PrincipalAuthority principalAuthority = new com.yahoo.athenz.auth.impl.PrincipalAuthority();
    PrincipalAuthority testPrincipalAuthority = new com.yahoo.athenz.zms.TestUserPrincipalAuthority();
    
    // no changes expected
    
    zmsImpl.userDomain = "user";
    zmsImpl.userDomainPrefix = "user.";
    zmsImpl.homeDomain = "user";
    zmsImpl.homeDomainPrefix = "user.";
    zmsImpl.userAuthority = principalAuthority;
    assertEquals(zmsImpl.userHomeDomainResource("user.hga:domain"), "user.hga:domain");
    assertEquals(zmsImpl.userHomeDomainResource("user.john.smith:domain"), "user.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("testuser.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("product.john.smith:domain"), "product.john.smith:domain");
    // no changes expected
    
    zmsImpl.userDomain = "user";
    zmsImpl.userDomainPrefix = "user.";
    zmsImpl.homeDomain = "user";
    zmsImpl.homeDomainPrefix = "user.";
    zmsImpl.userAuthority = testPrincipalAuthority;
    assertEquals(zmsImpl.userHomeDomainResource("user.hga:domain"), "user.hga:domain");
    assertEquals(zmsImpl.userHomeDomainResource("user.john.smith:domain"), "user.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("testuser.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("product.john.smith:domain"), "product.john.smith:domain");
    
    // only domain name is changed - no username changes since user/home are same
    
    zmsImpl.userDomain = "testuser";
    zmsImpl.userDomainPrefix = "testuser.";
    zmsImpl.homeDomain = "testuser";
    zmsImpl.homeDomainPrefix = "testuser.";
    zmsImpl.userAuthority = principalAuthority;
    assertEquals(zmsImpl.userHomeDomainResource("user.hga:domain"), "testuser.hga:domain");
    assertEquals(zmsImpl.userHomeDomainResource("user.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("testuser.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("product.john.smith:domain"), "product.john.smith:domain");
    
    // only domain name is changed - no username changes since user/home are same
    zmsImpl.userDomain = "testuser";
    zmsImpl.userDomainPrefix = "testuser.";
    zmsImpl.homeDomain = "testuser";
    zmsImpl.homeDomainPrefix = "testuser.";
    zmsImpl.userAuthority = testPrincipalAuthority;
    assertEquals(zmsImpl.userHomeDomainResource("user.hga:domain"), "testuser.hga:domain");
    assertEquals(zmsImpl.userHomeDomainResource("user.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("testuser.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("product.john.smith:domain"), "product.john.smith:domain");
    
    // domain and username are changed since user/home namespaces are different
    // username impl in authority is default so we'll end up with same username
    
    zmsImpl.userDomain = "user";
    zmsImpl.userDomainPrefix = "user.";
    zmsImpl.homeDomain = "home";
    zmsImpl.homeDomainPrefix = "home.";
    zmsImpl.userAuthority = principalAuthority;
    assertEquals(zmsImpl.userHomeDomainResource("user.hga:domain"), "home.hga:domain");
    assertEquals(zmsImpl.userHomeDomainResource("user.john.smith:domain"), "home.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("testuser.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("product.john.smith:domain"), "product.john.smith:domain");
    
    // domain and username are changed since user/home namespaces are different
    // username impl in authority will replace .'s with -'s
    zmsImpl.userDomain = "user";
    zmsImpl.userDomainPrefix = "user.";
    zmsImpl.homeDomain = "home";
    zmsImpl.homeDomainPrefix = "home.";
    zmsImpl.userAuthority = testPrincipalAuthority;
    assertEquals(zmsImpl.userHomeDomainResource("user.hga:domain"), "home.hga:domain");
    assertEquals(zmsImpl.userHomeDomainResource("user.john.smith:domain"), "home.john-smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("testuser.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("product.john.smith:domain"), "product.john.smith:domain");
    
    // domain and username are changed since user/home namespaces are different
    // username impl in authority is default so we'll end up with same username
    
    zmsImpl.userDomain = "testuser";
    zmsImpl.userDomainPrefix = "testuser.";
    zmsImpl.homeDomain = "home";
    zmsImpl.homeDomainPrefix = "home.";
    zmsImpl.userAuthority = principalAuthority;
    assertEquals(zmsImpl.userHomeDomainResource("user.hga:domain"), "home.hga:domain");
    assertEquals(zmsImpl.userHomeDomainResource("user.john.smith:domain"), "home.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("testuser.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("product.john.smith:domain"), "product.john.smith:domain");
    
    // domain and username are changed since user/home namespaces are different
    // username impl in authority will replace .'s with -'s
    
    zmsImpl.userDomain = "testuser";
    zmsImpl.userDomainPrefix = "testuser.";
    zmsImpl.homeDomain = "home";
    zmsImpl.homeDomainPrefix = "home.";
    zmsImpl.userAuthority = testPrincipalAuthority;
    assertEquals(zmsImpl.userHomeDomainResource("user.hga:domain"), "home.hga:domain");
    assertEquals(zmsImpl.userHomeDomainResource("user.john.smith:domain"), "home.john-smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("testuser.john.smith:domain"), "testuser.john.smith:domain");
    assertEquals(zmsImpl.userHomeDomainResource("product.john.smith:domain"), "product.john.smith:domain");
    zmsImpl.objectStore.clearConnections();
}