@Test
public void testUpdateSystemMetaFields() {

    Domain domain = new Domain();
    DomainMeta meta = new DomainMeta()
            .setAccount("acct")
            .setYpmId(1234)
            .setCertDnsDomain("athenz.cloud");
    zms.dbService.updateSystemMetaFields(domain, "account", true, meta);
    assertEquals(domain.getAccount(), "acct");
    zms.dbService.updateSystemMetaFields(domain, "productid", true, meta);
    assertEquals(domain.getYpmId().intValue(), 1234);
    zms.dbService.updateSystemMetaFields(domain, "certdnsdomain", true, meta);
    assertEquals(domain.getCertDnsDomain(), "athenz.cloud");
    try {
        zms.dbService.updateSystemMetaFields(domain, "unknown", true, meta);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 400);
    }
    // test setting from null to valid values with no delete set
    Domain domain1 = new Domain();
    DomainMeta meta1 = new DomainMeta()
            .setAccount("acct")
            .setYpmId(1234)
            .setCertDnsDomain("athenz.cloud");
    zms.dbService.updateSystemMetaFields(domain1, "account", false, meta1);
    assertEquals(domain1.getAccount(), "acct");
    zms.dbService.updateSystemMetaFields(domain1, "productid", false, meta1);
    assertEquals(domain1.getYpmId().intValue(), 1234);
    zms.dbService.updateSystemMetaFields(domain1, "certdnsdomain", false, meta1);
    assertEquals(domain1.getCertDnsDomain(), "athenz.cloud");
    // setting from set values should be all rejected
    Domain domain2 = new Domain()
            .setAccount("acct")
            .setYpmId(1234)
            .setCertDnsDomain("athenz.cloud");
    DomainMeta meta2 = new DomainMeta()
            .setAccount("acct-new")
            .setYpmId(1235)
            .setCertDnsDomain("athenz.cloud.new");
    // setting from the old value to new value with
    // no delete flag should be rejected
    try {
        zms.dbService.updateSystemMetaFields(domain2, "account", false, meta2);
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 403);
        assertTrue(ex.getMessage().contains("reset system meta attribute"));
    }
    try {
        zms.dbService.updateSystemMetaFields(domain2, "productid", false, meta2);
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 403);
        assertTrue(ex.getMessage().contains("reset system meta attribute"));
    }
    try {
        zms.dbService.updateSystemMetaFields(domain2, "certdnsdomain", false, meta2);
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 403);
        assertTrue(ex.getMessage().contains("reset system meta attribute"));
    }
    // setting from set value to the same value should be allowed
    Domain domain3 = new Domain()
            .setAccount("acct")
            .setYpmId(1234)
            .setCertDnsDomain("athenz.cloud");
    DomainMeta meta3 = new DomainMeta()
            .setAccount("acct")
            .setYpmId(1234)
            .setCertDnsDomain("athenz.cloud");
    zms.dbService.updateSystemMetaFields(domain3, "account", false, meta3);
    assertEquals(domain3.getAccount(), "acct");
    zms.dbService.updateSystemMetaFields(domain3, "productid", false, meta3);
    assertEquals(domain3.getYpmId().intValue(), 1234);
    zms.dbService.updateSystemMetaFields(domain3, "certdnsdomain", false, meta3);
    assertEquals(domain3.getCertDnsDomain(), "athenz.cloud");
}