-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from CJSCommonPlatform/add-send-text-messge-co…
…mmand Add command to send a text message
- Loading branch information
Showing
18 changed files
with
442 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
12 changes: 12 additions & 0 deletions
12
...main/java/uk/gov/justice/artemis/manager/connector/filehandling/MessageFileException.java
This file contains 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,12 @@ | ||
package uk.gov.justice.artemis.manager.connector.filehandling; | ||
|
||
public class MessageFileException extends RuntimeException { | ||
|
||
public MessageFileException(final String message) { | ||
super(message); | ||
} | ||
|
||
public MessageFileException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../uk/gov/justice/artemis/manager/connector/filehandling/TextMessageFileContentsReader.java
This file contains 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 uk.gov.justice.artemis.manager.connector.filehandling; | ||
|
||
import static java.lang.String.format; | ||
import static java.nio.charset.Charset.defaultCharset; | ||
import static org.apache.commons.io.FileUtils.getFile; | ||
import static org.apache.commons.io.FileUtils.readFileToString; | ||
|
||
import java.io.File; | ||
|
||
public class TextMessageFileContentsReader { | ||
|
||
public String readContentsOf(final String pathToFile) { | ||
|
||
try { | ||
final File file = getFile(pathToFile); | ||
|
||
if(! file.exists()) { | ||
throw new MessageFileException(format("Failed to find file '%s'", file.getAbsolutePath())); | ||
} | ||
|
||
return readFileToString(file, defaultCharset()); | ||
} catch (final Exception e) { | ||
throw new MessageFileException(format("Failed to read file '%s'", pathToFile), e); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
39 changes: 39 additions & 0 deletions
39
src/main/java/uk/gov/justice/framework/tools/command/SendMessage.java
This file contains 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 uk.gov.justice.framework.tools.command; | ||
|
||
|
||
import uk.gov.justice.artemis.manager.connector.filehandling.MessageFileException; | ||
import uk.gov.justice.artemis.manager.connector.filehandling.TextMessageFileContentsReader; | ||
import uk.gov.justice.framework.tools.common.command.ShellCommand; | ||
|
||
public class SendMessage extends AbstractArtemisCommand implements ShellCommand { | ||
|
||
private final TextMessageFileContentsReader textMessageFileContentsReader; | ||
|
||
public SendMessage() { | ||
this(new TextMessageFileContentsReader()); | ||
} | ||
|
||
public SendMessage(final TextMessageFileContentsReader textMessageFileContentsReader) { | ||
this.textMessageFileContentsReader = textMessageFileContentsReader; | ||
} | ||
|
||
@Override | ||
public void run(final String[] args) { | ||
|
||
try { | ||
super.setup(); | ||
|
||
if(textMessageFile == null || textMessageFile.isEmpty()) { | ||
throw new MessageFileException("Cannot read message file. No file name set. Please use -messageFile parameter when calling this command"); | ||
} | ||
|
||
final String message = textMessageFileContentsReader.readContentsOf(textMessageFile); | ||
|
||
final String results = artemisConnector.sendTextMessage("DLQ", message); | ||
outputPrinter.write(results); | ||
|
||
} catch (final Exception exception) { | ||
outputPrinter.writeStackTrace(exception); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -194,4 +194,4 @@ public void shouldReturnTopicsAndCounts() throws Exception { | |
throw e; | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...gov/justice/artemis/manager/connector/filehandling/TextMessageFileContentsReaderTest.java
This file contains 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,40 @@ | ||
package uk.gov.justice.artemis.manager.connector.filehandling; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class TextMessageFileContentsReaderTest { | ||
|
||
@InjectMocks | ||
private TextMessageFileContentsReader textMessageFileContentsReader; | ||
|
||
@Test | ||
public void shouldReadContentsOfAMessageFileAsString() throws Exception { | ||
|
||
final String pathToFile = "src/test/resources/messages/messageForSendingToArtemis.txt"; | ||
|
||
final String message = textMessageFileContentsReader.readContentsOf(pathToFile); | ||
|
||
assertThat(message, is("{\n \"message\": \"All your base are belong to us\"\n}\n")); | ||
} | ||
|
||
@Test | ||
public void shouldFailIfFileNotFound() throws Exception { | ||
|
||
final String pathToFile = "this/file/does/not/exist.txt"; | ||
|
||
try { | ||
textMessageFileContentsReader.readContentsOf(pathToFile); | ||
fail(); | ||
} catch (final MessageFileException expected) { | ||
assertThat(expected.getMessage(), is("Failed to read file 'this/file/does/not/exist.txt'")); | ||
} | ||
} | ||
} |
Oops, something went wrong.