Package com.googlecode.jmxtrans.model.output

Source Code of com.googlecode.jmxtrans.model.output.GraphiteWriterTests

package com.googlecode.jmxtrans.model.output;

import com.google.common.collect.ImmutableMap;
import com.googlecode.jmxtrans.model.Query;
import com.googlecode.jmxtrans.model.Result;
import com.googlecode.jmxtrans.model.Server;
import com.googlecode.jmxtrans.model.ValidationException;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import static com.google.common.collect.ImmutableList.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class GraphiteWriterTests {

  @Test(expected = NullPointerException.class)
  public void hostIsRequired() throws ValidationException {
    try {
      GraphiteWriter.builder()
          .setPort(123)
          .build();
    } catch (NullPointerException npe) {
      assertThat(npe).hasMessage("Host cannot be null.");
      throw npe;
    }
  }

  @Test(expected = NullPointerException.class)
  public void portIsRequired() throws ValidationException {
    try {
      GraphiteWriter.builder()
          .setHost("localhost")
          .build();
    } catch (NullPointerException npe) {
      assertThat(npe).hasMessage("Port cannot be null.");
      throw npe;
    }
  }

  @Test
  public void writeSingleResult() throws Exception {
    // a lot of setup for not much of a test ...
    Server server = Server.builder().setHost("host").setPort("123").build();
    Query query = Query.builder().build();
    Result result = new Result("attributeName", "className", "classNameAlias", "typeName", ImmutableMap.of("key", (Object)1));

    GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class);
    Socket socket = mock(Socket.class);
    when(pool.borrowObject(any(InetSocketAddress.class))).thenReturn(socket);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    when(socket.getOutputStream()).thenReturn(out);

    GraphiteWriter writer = GraphiteWriter.builder()
        .setHost("localhost")
        .setPort(2003)
        .build();
    writer.setPool(pool);

    writer.doWrite(server, query, of(result));

    // check that Graphite format is respected
    assertThat(out.toString()).startsWith("servers.host_123.classNameAlias.attributeName_key 1 ");
  }

}
TOP

Related Classes of com.googlecode.jmxtrans.model.output.GraphiteWriterTests

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.