forked from ConsoleCatzirl/jenkins-logstash-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 107
add dao to work with logstash tcp #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/jenkins/plugins/logstash/configuration/Logstash.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| } | ||
|
|
||
| @Override | ||
| public int getDefaultPort() | ||
| { | ||
| return 9000; | ||
| } | ||
|
|
||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
src/main/java/jenkins/plugins/logstash/persistence/LogstashDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
src/test/java/jenkins/plugins/logstash/persistence/LogstashDaoTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
|
||
| 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()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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