@Test
 public void testIdQuery() throws Exception {
   Reference ref = createReference("_id", DataTypes.STRING);
   Query query = convert(whereClause(EqOperator.NAME, ref, Literal.newLiteral("i1")));
   assertThat(query, instanceOf(TermQuery.class));
   assertThat(query.toString(), is("_uid:default#i1"));
 }
 @Test
 public void testAnyEqArrayReference() throws Exception {
   Reference ref = createReference("d_array", new ArrayType(DataTypes.DOUBLE));
   Literal doubleLiteral = Literal.newLiteral(1.5d);
   Query query = convert(whereClause(AnyEqOperator.NAME, doubleLiteral, ref));
   assertThat(query.toString(), is("d_array:[1.5 TO 1.5]"));
 }
 @Test
 public void testParseConfigurationMultipleAttributes() throws Exception {
   JmxTransExporterConfiguration config =
       new JmxTransConfigurationXmlLoader("classpath:jmxtrans-multiple-attributes-test.xml")
           .loadConfiguration();
   assertThat(config.queries, hasSize(1));
   Query query = config.queries.get(0);
   assertThat(query.getAttributes(), contains("ThreadCount", "TotalStartedThreadCount"));
 }
 @Test
 public void testNoAttributesSpecifiedGeneratesWildcardQuery() throws Exception {
   JmxTransExporterConfiguration config =
       new JmxTransConfigurationXmlLoader(
               "classpath:jmxtrans-no-attributes-specified-generates-wildcard-query-test.xml")
           .loadConfiguration();
   assertThat(config.queries, hasSize(1));
   Query query = config.queries.get(0);
   assertThat(query.getAttributes(), emptyIterable());
 }
  @Test
  public void testParseConfiguration() throws Exception {
    JmxTransExporterConfiguration config =
        new JmxTransConfigurationXmlLoader("classpath:jmxtrans-agent.xml").loadConfiguration();

    assertThat(config.collectInterval, is(11));
    assertThat(config.collectIntervalTimeUnit, is(TimeUnit.SECONDS));
    assertThat(config.getConfigReloadInterval(), equalTo(-1));

    OutputWriter decoratedOutputWriter = config.outputWriter;
    // CircuitBreaker
    assertTrue(decoratedOutputWriter.getClass().equals(OutputWriterCircuitBreakerDecorator.class));
    OutputWriterCircuitBreakerDecorator circuitBreakerDecorator =
        (OutputWriterCircuitBreakerDecorator) decoratedOutputWriter;
    assertThat(circuitBreakerDecorator.isDisabled(), is(false));

    // Graphite Writer
    assertTrue(
        circuitBreakerDecorator.delegate.getClass().equals(GraphitePlainTextTcpOutputWriter.class));
    GraphitePlainTextTcpOutputWriter graphiteWriter =
        (GraphitePlainTextTcpOutputWriter) circuitBreakerDecorator.delegate;
    assertThat(graphiteWriter.graphiteServerHostAndPort.getPort(), is(2203));
    assertThat(graphiteWriter.graphiteServerHostAndPort.getHost(), is("localhost"));
    assertThat(graphiteWriter.getMetricPathPrefix(), is("app_123456.server.i876543."));

    assertThat(config.queries.size(), is(13));

    Map<String, Query> queriesByResultAlias = indexQueriesByResultAlias(config.queries);

    {
      Query query = queriesByResultAlias.get("os.systemLoadAverage");
      assertThat(query.objectName, is(new ObjectName("java.lang:type=OperatingSystem")));
      assertThat(query.getAttributes(), contains("SystemLoadAverage"));
      assertThat(query.resultAlias, is("os.systemLoadAverage"));
      assertThat(query.key, is((String) null));
    }
    {
      Query query = queriesByResultAlias.get("jvm.heapMemoryUsage.used");
      assertThat(query.objectName, is(new ObjectName("java.lang:type=Memory")));
      assertThat(query.getAttributes(), contains("HeapMemoryUsage"));
      assertThat(query.resultAlias, is("jvm.heapMemoryUsage.used"));
      assertThat(query.key, is("used"));
    }
    Map<String, Invocation> invocationsByResultAlias =
        indexInvocationsByResultAlias(config.invocations);
    {
      Invocation invocation = invocationsByResultAlias.get("jvm.gc");
      assertThat(invocation.objectName, is(new ObjectName("java.lang:type=Memory")));
      assertThat(invocation.operationName, is("gc"));
      assertThat(invocation.resultAlias, is("jvm.gc"));
    }
  }
 @Test
 public void testGteQuery() throws Exception {
   Query query =
       convert(
           new WhereClause(
               createFunction(
                   GteOperator.NAME,
                   DataTypes.BOOLEAN,
                   createReference("x", DataTypes.INTEGER),
                   Literal.newLiteral(10))));
   assertThat(query, instanceOf(NumericRangeQuery.class));
   assertThat(query.toString(), is("x:[10 TO *}"));
 }
  @Test
  public void testAnyGreaterAndSmaller() throws Exception {

    Reference arrayRef = createReference("d_array", new ArrayType(DataTypes.DOUBLE));
    Literal doubleLiteral = Literal.newLiteral(1.5d);

    Reference ref = createReference("d", DataTypes.DOUBLE);
    Literal arrayLiteral =
        Literal.newLiteral(new Object[] {1.2d, 3.5d}, new ArrayType(DataTypes.DOUBLE));

    // 1.5d < ANY (d_array)
    Query ltQuery = convert(whereClause(AnyLtOperator.NAME, doubleLiteral, arrayRef));
    assertThat(ltQuery.toString(), is("d_array:{1.5 TO *}"));

    // d < ANY ([1.2, 3.5])
    Query ltQuery2 = convert(whereClause(AnyLtOperator.NAME, ref, arrayLiteral));
    assertThat(ltQuery2.toString(), is("(d:{* TO 1.2} d:{* TO 3.5})~1"));

    // 1.5d <= ANY (d_array)
    Query lteQuery = convert(whereClause(AnyLteOperator.NAME, doubleLiteral, arrayRef));
    assertThat(lteQuery.toString(), is("d_array:[1.5 TO *}"));

    // d <= ANY ([1.2, 3.5])
    Query lteQuery2 = convert(whereClause(AnyLteOperator.NAME, ref, arrayLiteral));
    assertThat(lteQuery2.toString(), is("(d:{* TO 1.2] d:{* TO 3.5])~1"));

    // 1.5d > ANY (d_array)
    Query gtQuery = convert(whereClause(AnyGtOperator.NAME, doubleLiteral, arrayRef));
    assertThat(gtQuery.toString(), is("d_array:{* TO 1.5}"));

    // d > ANY ([1.2, 3.5])
    Query gtQuery2 = convert(whereClause(AnyGtOperator.NAME, ref, arrayLiteral));
    assertThat(gtQuery2.toString(), is("(d:{1.2 TO *} d:{3.5 TO *})~1"));

    // 1.5d >= ANY (d_array)
    Query gteQuery = convert(whereClause(AnyGteOperator.NAME, doubleLiteral, arrayRef));
    assertThat(gteQuery.toString(), is("d_array:{* TO 1.5]"));

    // d >= ANY ([1.2, 3.5])
    Query gteQuery2 = convert(whereClause(AnyGteOperator.NAME, ref, arrayLiteral));
    assertThat(gteQuery2.toString(), is("(d:[1.2 TO *} d:[3.5 TO *})~1"));
  }
  @Test
  public void testParseConfiguration2() throws Exception {
    JmxTransExporterConfiguration config =
        new JmxTransConfigurationXmlLoader("classpath:jmxtrans-agent-2.xml").loadConfiguration();

    assertThat(config.collectInterval, is(12));
    assertThat(config.collectIntervalTimeUnit, is(TimeUnit.SECONDS));
    assertTrue(config.outputWriter.getClass().equals(OutputWritersChain.class));

    OutputWritersChain outputWritersChain = (OutputWritersChain) config.outputWriter;

    assertThat(outputWritersChain.outputWriters.size(), is(2));

    {
      OutputWriter decoratedOutputWriter = outputWritersChain.outputWriters.get(0);
      // CircuitBreaker
      assertTrue(
          decoratedOutputWriter.getClass().equals(OutputWriterCircuitBreakerDecorator.class));
      OutputWriterCircuitBreakerDecorator circuitBreakerDecorator =
          (OutputWriterCircuitBreakerDecorator) decoratedOutputWriter;
      assertThat(circuitBreakerDecorator.isDisabled(), is(true));

      // Graphite Writer
      assertTrue(
          circuitBreakerDecorator
              .delegate
              .getClass()
              .equals(GraphitePlainTextTcpOutputWriter.class));
      GraphitePlainTextTcpOutputWriter writer =
          (GraphitePlainTextTcpOutputWriter) circuitBreakerDecorator.delegate;
      assertThat(writer.graphiteServerHostAndPort.getPort(), is(2003));
      assertThat(writer.graphiteServerHostAndPort.getHost(), is("localhost"));
      assertThat(writer.getMetricPathPrefix(), is("servers.localhost."));
    }
    {
      OutputWriter decoratedOutputWriter = outputWritersChain.outputWriters.get(1);
      // Circuit Breaker
      assertTrue(
          decoratedOutputWriter.getClass().equals(OutputWriterCircuitBreakerDecorator.class));
      OutputWriterCircuitBreakerDecorator circuitBreakerDecorator =
          (OutputWriterCircuitBreakerDecorator) decoratedOutputWriter;
      assertThat(circuitBreakerDecorator.isDisabled(), is(true));

      // Console Writer
      assertTrue(circuitBreakerDecorator.delegate.getClass().equals(ConsoleOutputWriter.class));
    }

    assertThat(config.queries.size(), is(13));

    Map<String, Query> queriesByResultAlias = indexQueriesByResultAlias(config.queries);

    {
      Query query = queriesByResultAlias.get("os.systemLoadAverage");
      assertThat(query.objectName, is(new ObjectName("java.lang:type=OperatingSystem")));
      assertThat(query.getAttributes(), contains("SystemLoadAverage"));
      assertThat(query.resultAlias, is("os.systemLoadAverage"));
      assertThat(query.key, is((String) null));
    }
    {
      Query query = queriesByResultAlias.get("jvm.heapMemoryUsage.used");
      assertThat(query.objectName, is(new ObjectName("java.lang:type=Memory")));
      assertThat(query.getAttributes(), contains("HeapMemoryUsage"));
      assertThat(query.resultAlias, is("jvm.heapMemoryUsage.used"));
      assertThat(query.key, is("used"));
    }
  }