@Test
public void testPostDomainMetrics() {

    SignedDomain signedDomain = createSignedDomain("coretech", "weather", "storage", false);
    store.processDomain(signedDomain, false);
    
    Principal principal = SimplePrincipal.create("user_domain", "user",
            "v=U1;d=user_domain;n=user;s=signature", 0, null);
    ResourceContext context = createResourceContext(principal);
    // create zts with a metric we can verify
    
    CloudStore cloudStore = new CloudStore();
    cloudStore.setHttpClient(null);
    ZtsMetricTester metric = new ZtsMetricTester();
    ZTSImpl ztsImpl = new ZTSImpl(cloudStore, store);
    ZTSImpl.serverHostName = "localhost";
    ztsImpl.metric = metric;
    
    String testDomain = "coretech";
    // create some metrics
    
    List<DomainMetric> metricList = new ArrayList<>();
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).
            setMetricVal(99));
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED).
            setMetricVal(27));
    DomainMetrics req = new DomainMetrics().
        setDomainName(testDomain).
        setMetricList(metricList);
    // send the metrics
    
    ztsImpl.postDomainMetrics(context, testDomain, req);
    // verify metrics were recorded
    
    Map<String, Integer> metrixMap = metric.getMap();
    String key = "dom_metric_" + 
        DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH.toString().toLowerCase() +
        testDomain;
    Integer val = metrixMap.get(key);
    assertNotNull(val);
    assertEquals(val.intValue(), 99);
    key = "dom_metric_" +
        DomainMetricType.ACCESS_ALLOWED.toString().toLowerCase() +
        testDomain;
    val = metrixMap.get(key);
    assertNotNull(val);
    assertEquals(val.intValue(), 27);
    // test - failure case - invalid domain
    
    testDomain = "not_coretech";
    
    // create some metrics
    
    metricList = new ArrayList<>();
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).
            setMetricVal(999));
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED).
            setMetricVal(277));
    req = new DomainMetrics().
        setDomainName(testDomain).
        setMetricList(metricList);
    // send the metrics
    
    metrixMap.clear();
    String errMsg = "No such domain";
    try {
        ztsImpl.postDomainMetrics(context, testDomain, req);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 404);
        assertTrue(ex.getMessage().contains(errMsg));
    }
    // verify no metrics were recorded
    
    assertEquals(metrixMap.size(), 0);
    // test - failure case - missing domain name in metric data
    
    testDomain = "coretech";
    
    // create some metrics
    
    metricList = new ArrayList<>();
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).
            setMetricVal(999));
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED).
            setMetricVal(277));
    req = new DomainMetrics().
        setMetricList(metricList);
    errMsg = "Missing required field: domainName";
    
    // send the metrics
    
    metrixMap.clear();
    try {
        ztsImpl.postDomainMetrics(context, testDomain, req);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 400);
        assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
    }
    
    // verify no metrics were recorded
    
    assertEquals(metrixMap.size(), 0);
    // test - failure case - mismatch domain in uri and metric data
    
    testDomain = "coretech";
    
    // create some metrics
    
    metricList = new ArrayList<>();
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).
            setMetricVal(999));
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED).
            setMetricVal(277));
    req = new DomainMetrics().
        setDomainName("not_coretech").
        setMetricList(metricList);
    errMsg = "mismatched domain names";
    
    // send the metrics
    
    metrixMap.clear();
    try {
        ztsImpl.postDomainMetrics(context, testDomain, req);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 400);
        assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
    }
    // verify no metrics were recorded
    
    assertEquals(metrixMap.size(), 0);
    // test - failure case - empty metric list
    testDomain = "coretech";
    
    // create some metrics
    
    metricList = new ArrayList<>();
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).
            setMetricVal(999));
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED).
            setMetricVal(277));
    req = new DomainMetrics().
        setDomainName(testDomain);
    errMsg = "Missing required field: metricList";
    
    // send the metrics
    
    metrixMap.clear();
    try {
        ztsImpl.postDomainMetrics(context, testDomain, req);
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getCode(), 400);
        assertTrue(ex.getMessage().contains(errMsg), ex.getMessage());
    }
    
    // verify no metrics were recorded
    
    assertEquals(metrixMap.size(), 0);
    // test - failure case - metric count is missing
    
    testDomain = "coretech";
    
    // create a single metric without a count
    
    metricList = new ArrayList<>();
    metricList.add(
        new DomainMetric().
            setMetricType(DomainMetricType.ACCESS_ALLOWED_DENY_NO_MATCH).
            setMetricVal(-1));
    req = new DomainMetrics().
        setDomainName(testDomain).
        setMetricList(metricList);
    
    // verify no metrics were recorded
    
    metrixMap.clear();
    ztsImpl.postDomainMetrics(context, testDomain, req);
    assertEquals(metrixMap.size(), 0);
}