Skip to content

Commit

Permalink
Refactor some code.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdriton committed Nov 7, 2024
1 parent 5550e2c commit ddd8807
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ void testValidEmailReadFromFile() {
try {
node = mapper.readTree(mockFlowFile.getContent());
} catch (Exception e) {
// helps with debugging
e.printStackTrace();
}
JsonNode actualTo = node.get("to");
String firstPhoneNumber = null;
try {
List<String> toList = mapper.readerForListOf(String.class).readValue(actualTo);
firstPhoneNumber = toList.get(0);
} catch(Exception e){

// help debugging
e.printStackTrace();
}
Assertions.assertEquals("+11004445555", firstPhoneNumber);
}
Expand All @@ -112,4 +115,10 @@ void testValidEmailGenerated() {
String expectedJson = "{\"to\":[\"+11239994444\"],\"body\":\"This is my text to send as SMS\"}";
Assertions.assertEquals(expectedJson, actualJson);
}

@Test
void testGetProcessor(){
ExtractEmailToJson processor = (ExtractEmailToJson) runner.getProcessor();
Assertions.assertNotNull(processor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,35 @@ public GenerateEmail(String from, String to, String subject, String message, Str
}

public byte[] emailMessage(final String to) {
Properties prop = new Properties();
Session session = Session.getDefaultInstance(prop);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(this.from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(this.to));
message.setSubject(this.subject);
message.setText(this.message);
message.setHeader("hostname", this.hostName);
} catch (Exception e) {
e.printStackTrace();
}
MimeMessage msg = this.emailMessageAsMimeMessage(to);

final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] result = null;
try {
message.writeTo(byteArrayOutputStream);
msg.writeTo(byteArrayOutputStream);
result = byteArrayOutputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

public MimeMessage emailMessageAsMimeMessage(final String to){
Properties prop = new Properties();
Session session = Session.getDefaultInstance(prop);
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(this.from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(this.to));
msg.setSubject(this.subject);
msg.setText(this.message);
msg.setHeader("hostname", this.hostName);
} catch (Exception e) {
e.printStackTrace();
}
return msg;
}

public byte[] fromFile(String fileName) {
byte[] fileContent = null;
try {
Expand Down

0 comments on commit ddd8807

Please sign in to comment.