Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package jenkins.plugins.logstash.configuration;

import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import jenkins.plugins.logstash.persistence.LogstashDao;

public class Logstash extends HostBasedLogstashIndexer<LogstashDao>
{

@DataBoundConstructor
public Logstash()
{
}

@Override
protected LogstashDao createIndexerInstance()
{
return new LogstashDao(getHost(), getPort());
}

@Extension
public static class Descriptor extends LogstashIndexerDescriptor
{

@Override
public String getDisplayName()
{
return "Logstash";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "Logstash TCP" would be clearer

}

@Override
public int getDefaultPort()
{
return 9000;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package jenkins.plugins.logstash.persistence;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class LogstashDao extends HostBasedLogstashIndexerDao {

public LogstashDao(String logstashHostString, int logstashPortInt) {
super(logstashHostString, logstashPortInt);
}

@Override
public void push(String data) throws IOException {

try (Socket logstashClientSocket = new Socket(getHost(), getPort()))
{
OutputStream out = logstashClientSocket.getOutputStream();
out.write(data.getBytes(StandardCharsets.UTF_8));
out.write(10);
out.flush();
out.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package jenkins.plugins.logstash.persistence;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.net.SocketException;
import java.nio.charset.Charset;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import com.rabbitmq.client.AuthenticationFailureException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

@RunWith(MockitoJUnitRunner.class)
public class LogstashDaoTest {
LogstashDao dao;

LogstashDao createDao(String host, int port) {
LogstashDao factory = new LogstashDao(host, port);

factory.setCharset(Charset.defaultCharset());

return factory;
}

@Test(expected = IllegalArgumentException.class)
public void constructorFailNullHost() throws Exception {
try {
createDao(null, 9000);
} catch (IllegalArgumentException e) {
assertEquals("Wrong error message was thrown", "host name is required", e.getMessage());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] I think it would be better to use the ExceptionRule here

throw e;
}
}

@Test(expected = IllegalArgumentException.class)
public void constructorFailEmptyHost() throws Exception {
try {
createDao(" ", 9000);
} catch (IllegalArgumentException e) {
assertEquals("Wrong error message was thrown", "host name is required", e.getMessage());
throw e;
}
}

@Test
public void constructorSuccess() throws Exception {
// Unit under test
dao = createDao("localhost", 5672);

// Verify results
assertEquals("Wrong host name", "localhost", dao.getHost());
assertEquals("Wrong port", 5672, dao.getPort());
}
}