Skip to content

Instantly share code, notes, and snippets.

@tonilopezmr
Last active January 5, 2018 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonilopezmr/ac72f0947e7f30dbe52c67a54e2e284e to your computer and use it in GitHub Desktop.
Save tonilopezmr/ac72f0947e7f30dbe52c67a54e2e284e to your computer and use it in GitHub Desktop.
class Sender {
fun send(send: Send, message: String, to: String) {
send.send(to, message)
}
}
interface Send {
fun send(to: String, message: String)
}
class SendSMS(private val phoneValidator: PhoneValidator,
private val smpp: SMPPProxy,
private val username: String,
private val password: String) : Send {
override fun send(to: String, message: String) {
if (!phoneValidator.isValid(to)) {
throw NoPhoneNumber(invalidPhone = to)
}
try {
smpp.openConnection(username, password)
smpp.send(to, message, null, "15")
smpp.closeConnection()
} catch (ex: Exception) {
try {
smpp.closeConnection()
} catch (ex: Exception) {
} finally {
throw SendSMSException(ex.message ?: "unknown message")
}
}
}
}
class SendEmail(private val emailConnection: SendMailConnection) : Send {
var subject: String = ""
override fun send(to: String, message: String) {
if (subject.isEmpty()) {
throw RequiredSubjectEmail()
}
try {
emailConnection.prepareMessage(to, message, subject)
emailConnection.send()
emailConnection.close()
} catch (ex: Exception) {
try {
emailConnection.close()
} catch (ex: Exception) {
} finally {
throw SendEmailException(ex.message ?: "unknown message")
}
}
}
}
class SMPPProxy(private val ip: String,
private val port: String) {
val smpp: SMPP by lazy {
SMPP(ip, port)
}
fun openConnection(username: String, password: String) = smpp.openConnection(username, password)
fun closeConnection() = smpp.closeConnection()
fun send(to: String, message: String, a: String?, s: String) {
smpp.send(to, message, a, s)
}
}
sealed class SendException(message: String = "") : Exception(message)
class SendEmailException(message: String) : SendException(message)
class RequiredSubjectEmail : SendException()
class NoPhoneNumber(invalidPhone: String) : SendException("$invalidPhone is not a phone number")
class SendSMSException(message: String) : SendException(message)
fun main(vararg arg: String) {
try {
val smpp = SMPPProxy("192.168.1.4", "4301")
val emailConnection = SendMailConnection("mail.myserver.com")
val sms = SendSMS(PhoneValidator(), smpp, "your­username", "your­password")
val email = SendEmail(emailConnection)
val sender = Sender()
sender.send(sms, "Message", "675848584")
email.subject = "Subject"
sender.send(email, "Message", "tonilopezmr@gmail.com")
} catch (ex: SendException) {
ex.printStackTrace()
}
}
@tonilopezmr
Copy link
Author

tonilopezmr commented Jan 4, 2018

I have created a Send interface that allows to create different types implementations, in each implementation is provided with all dependencies they need.

When it tries to send something and it fails, it tries to close the connection and if the close method fails then it returns a SendException with the exception message.

For send an Email it verifies that the subject is not empty, and for SMS it verifies that it pass a phone number.

This allows you to create any type of implementation to send a message to someone.

I have an issue with the SMPP constructor or SendMailConnection constructor because if it tries to establish a connection or throw an Exception I think is convenient create inside the Send implementation class, but in this case for testing purposes I think that I need to create a virtual proxy for SMPP class and SendMailConnection to defer the constructor computation only when I need to use a method. (I will implement it later)

@tonilopezmr
Copy link
Author

Imagine that SMPP throw exceptions or stablish some connection, in that case I created a Virtual Proxy to defer the construction and give feedback (throw exceptions) from the construction inside my SendSMS implementation.

Now I can tested my implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment