Skip to content

Commit a5ab151

Browse files
authored
Merge pull request #5 from SOFTNETWORK-APP/feature/grpc
update grpc api
2 parents 33a007f + b9cb66c commit a5ab151

File tree

7 files changed

+189
-31
lines changed

7 files changed

+189
-31
lines changed

api/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dockerRepository := Some("softnetwork.jfrog.io/default-docker-local")
2323

2424
bashScriptDefines / scriptClasspath ~= (cp => "../conf" +: cp)
2525

26-
organization := "app.softnetwork.persistence"
26+
organization := "app.softnetwork.notification"
2727

2828
name := "notification-api"
2929

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ThisBuild / organization := "app.softnetwork"
3131

3232
name := "notification"
3333

34-
ThisBuild / version := "0.1.3"
34+
ThisBuild / version := "0.1.4-SNAPSHOT"
3535

3636
ThisBuild / scalaVersion := "2.12.15"
3737

common/src/main/protobuf/api/notification.proto

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
syntax = "proto3";
22

33
import "scalapb/scalapb.proto";
4-
import "google/protobuf/wrappers.proto";
54
import "model/notifications.proto";
65

76
package app.softnetwork.notification.api;
@@ -15,6 +14,11 @@ service NotificationServiceApi {
1514
rpc AddMail (AddMailRequest) returns (AddNotificationResponse) {}
1615
rpc AddSMS (AddSMSRequest) returns (AddNotificationResponse) {}
1716
rpc AddPush (AddPushRequest) returns (AddNotificationResponse) {}
17+
rpc RemoveNotification (RemoveNotificationRequest) returns (RemoveNotificationResponse) {}
18+
rpc SendMail (SendMailRequest) returns (SendNotificationResponse) {}
19+
rpc SendSMS (SendSMSRequest) returns (SendNotificationResponse) {}
20+
rpc SendPush (SendPushRequest) returns (SendNotificationResponse) {}
21+
rpc GetNotificationStatus (GetNotificationStatusRequest) returns (GetNotificationStatusResponse) {}
1822
}
1923

2024
message AddMailRequest{
@@ -32,3 +36,35 @@ message AddPushRequest{
3236
message AddNotificationResponse{
3337
bool succeeded = 1;
3438
}
39+
40+
message RemoveNotificationRequest{
41+
string uuid = 1;
42+
}
43+
44+
message RemoveNotificationResponse{
45+
bool succeeded = 1;
46+
}
47+
48+
message SendMailRequest{
49+
org.softnetwork.notification.model.Mail mail = 1;
50+
}
51+
52+
message SendSMSRequest{
53+
org.softnetwork.notification.model.SMS sms = 1;
54+
}
55+
56+
message SendPushRequest{
57+
org.softnetwork.notification.model.Push push = 1;
58+
}
59+
60+
message SendNotificationResponse{
61+
repeated org.softnetwork.notification.model.NotificationStatusResult results = 1;
62+
}
63+
64+
message GetNotificationStatusRequest{
65+
string uuid = 1;
66+
}
67+
68+
message GetNotificationStatusResponse{
69+
repeated org.softnetwork.notification.model.NotificationStatusResult results = 1;
70+
}

common/src/main/scala/app/softnetwork/notification/api/NotificationClient.scala

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package app.softnetwork.notification.api
22

33
import akka.actor.typed.ActorSystem
44
import akka.grpc.GrpcClientSettings
5-
import org.softnetwork.notification.model.{Mail, Push, SMS}
5+
import org.softnetwork.notification.model.{Mail, NotificationStatusResult, Push, SMS}
66

77
import scala.concurrent.Future
88

@@ -24,6 +24,27 @@ trait NotificationClient extends GrpcClient {
2424
def addPush(push: Push): Future[Boolean] = {
2525
grpcClient.addPush(AddPushRequest(Some(push))) map (_.succeeded)
2626
}
27+
28+
def removeNotification(uuid: String): Future[Boolean] = {
29+
grpcClient.removeNotification(RemoveNotificationRequest(uuid)) map (_.succeeded)
30+
}
31+
32+
def sendMail(mail: Mail): Future[Seq[NotificationStatusResult]] = {
33+
grpcClient.sendMail(SendMailRequest(Some(mail))) map (_.results)
34+
}
35+
36+
def sendSMS(sms: SMS): Future[Seq[NotificationStatusResult]] = {
37+
grpcClient.sendSMS(SendSMSRequest(Some(sms))) map (_.results)
38+
}
39+
40+
def sendPush(push: Push): Future[Seq[NotificationStatusResult]] = {
41+
grpcClient.sendPush(SendPushRequest(Some(push))) map (_.results)
42+
}
43+
44+
def getNotificationStatus(uuid: String): Future[Seq[NotificationStatusResult]] = {
45+
grpcClient.getNotificationStatus(GetNotificationStatusRequest(uuid)) map (_.results)
46+
}
47+
2748
}
2849

2950
object NotificationClient extends GrpcClientFactory[NotificationClient] {

common/src/main/scala/app/softnetwork/notification/message/package.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,21 @@ package object message {
4444

4545
case object NotificationRemoved extends NotificationCommandResult
4646

47+
trait NotificationResults extends NotificationCommandResult {
48+
def results: Seq[NotificationStatusResult]
49+
}
50+
4751
@SerialVersionUID(0L)
4852
case class NotificationSent(uuid: String, results: Seq[NotificationStatusResult])
49-
extends NotificationCommandResult
53+
extends NotificationResults
5054

5155
@SerialVersionUID(0L)
5256
case class NotificationDelivered(uuid: String, results: Seq[NotificationStatusResult])
53-
extends NotificationCommandResult
57+
extends NotificationResults
5458

5559
@SerialVersionUID(0L)
5660
case class NotificationPending(uuid: String, results: Seq[NotificationStatusResult])
57-
extends NotificationCommandResult
61+
extends NotificationResults
5862

5963
case class Schedule4NotificationTriggered(schedule: Schedule) extends NotificationCommandResult
6064

@@ -66,10 +70,12 @@ package object message {
6670
@SerialVersionUID(0L)
6771
case class NotificationUndelivered(uuid: String, results: Seq[NotificationStatusResult])
6872
extends NotificationErrorMessage("NotificationUndelivered")
73+
with NotificationResults
6974

7075
@SerialVersionUID(0L)
7176
case class NotificationRejected(uuid: String, results: Seq[NotificationStatusResult])
7277
extends NotificationErrorMessage("NotificationRejected")
78+
with NotificationResults
7379

7480
case object NotificationNotFound extends NotificationErrorMessage("NotificationNotFound")
7581

core/src/main/scala/app/softnetwork/notification/api/NotificationServer.scala

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ package app.softnetwork.notification.api
22

33
import akka.actor.typed.ActorSystem
44
import app.softnetwork.notification.handlers.NotificationHandler
5-
import app.softnetwork.notification.message.{AddNotification, NotificationAdded}
5+
import app.softnetwork.notification.message.{
6+
AddNotification,
7+
GetNotificationStatus,
8+
NotificationAdded,
9+
NotificationRemoved,
10+
NotificationResults,
11+
RemoveNotification,
12+
SendNotification
13+
}
14+
import app.softnetwork.notification.model.Notification
615

716
import scala.concurrent.{ExecutionContextExecutor, Future}
817

@@ -13,35 +22,66 @@ trait NotificationServer extends NotificationServiceApi {
1322
implicit lazy val ec: ExecutionContextExecutor = system.executionContext
1423

1524
override def addMail(in: AddMailRequest): Future[AddNotificationResponse] = {
16-
in.mail match {
17-
case Some(mail) =>
18-
?(mail.uuid, AddNotification(mail)) map {
19-
case _: NotificationAdded => AddNotificationResponse(true)
20-
case _ => AddNotificationResponse()
21-
}
22-
case _ => Future.successful(AddNotificationResponse())
23-
}
25+
addNotification(in.mail)
2426
}
2527

2628
override def addSMS(in: AddSMSRequest): Future[AddNotificationResponse] = {
27-
in.sms match {
28-
case Some(sms) =>
29-
?(sms.uuid, AddNotification(sms)) map {
29+
addNotification(in.sms)
30+
}
31+
32+
override def addPush(in: AddPushRequest): Future[AddNotificationResponse] = {
33+
addNotification(in.push)
34+
}
35+
36+
protected def addNotification(maybe: Option[Notification]): Future[AddNotificationResponse] = {
37+
maybe match {
38+
case Some(notification) =>
39+
?(notification.uuid, AddNotification(notification)) map {
3040
case _: NotificationAdded => AddNotificationResponse(true)
3141
case _ => AddNotificationResponse()
3242
}
3343
case _ => Future.successful(AddNotificationResponse())
3444
}
3545
}
3646

37-
override def addPush(in: AddPushRequest): Future[AddNotificationResponse] = {
38-
in.push match {
39-
case Some(push) =>
40-
?(push.uuid, AddNotification(push)) map {
41-
case _: NotificationAdded => AddNotificationResponse(true)
42-
case _ => AddNotificationResponse()
47+
override def removeNotification(
48+
in: RemoveNotificationRequest
49+
): Future[RemoveNotificationResponse] = {
50+
?(in.uuid, RemoveNotification(in.uuid)) map {
51+
case NotificationRemoved => RemoveNotificationResponse(true)
52+
case _ => RemoveNotificationResponse()
53+
}
54+
}
55+
56+
override def sendMail(in: SendMailRequest): Future[SendNotificationResponse] = {
57+
sendNotification(in.mail)
58+
}
59+
60+
override def sendSMS(in: SendSMSRequest): Future[SendNotificationResponse] = {
61+
sendNotification(in.sms)
62+
}
63+
64+
override def sendPush(in: SendPushRequest): Future[SendNotificationResponse] = {
65+
sendNotification(in.push)
66+
}
67+
68+
protected def sendNotification(maybe: Option[Notification]): Future[SendNotificationResponse] = {
69+
maybe match {
70+
case Some(notification) =>
71+
?(notification.uuid, SendNotification(notification)) map {
72+
case r: NotificationResults => SendNotificationResponse(r.results)
73+
case _ => SendNotificationResponse()
4374
}
44-
case _ => Future.successful(AddNotificationResponse())
75+
case _ => Future.successful(SendNotificationResponse())
76+
}
77+
}
78+
79+
override def getNotificationStatus(
80+
in: GetNotificationStatusRequest
81+
): Future[GetNotificationStatusResponse] = {
82+
?(in.uuid, GetNotificationStatus(in.uuid)) map {
83+
case r: NotificationResults => GetNotificationStatusResponse(r.results)
84+
case _ => GetNotificationStatusResponse()
4585
}
4686
}
4787
}

testkit/src/test/scala/app/softnetwork/notification/handlers/AllNotificationsHandlerSpec.scala

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import org.scalatest.wordspec.AnyWordSpecLike
44
import app.softnetwork.notification.message._
55
import app.softnetwork.notification.scalatest.AllNotificationsTestKit
66

7+
import scala.util.{Failure, Success}
8+
79
/** Created by smanciot on 14/04/2020.
810
*/
911
class AllNotificationsHandlerSpec
@@ -96,22 +98,75 @@ class AllNotificationsHandlerSpec
9698
}
9799
}
98100

99-
"add mail" in {
101+
"add mail using client" in {
100102
val uuid = "mail"
101103
assert(client.addMail(generateMail(uuid)) complete ())
102104
assert(probe.receiveMessage().schedule.uuid == s"Notification#$uuid#NotificationTimerKey")
103105
}
104106

105-
"add sms" in {
107+
"send mail using client" in {
108+
val uuid = "mail2"
109+
val mail = generateMail(uuid)
110+
client.sendMail(mail) complete () match {
111+
case Success(result) =>
112+
assert(result.exists(r => r.recipient == mail.to.head && r.status.isSent))
113+
case Failure(_) => fail()
114+
}
115+
}
116+
117+
"remove notification using client" in {
118+
assert(client.removeNotification("mail") complete ())
119+
}
120+
121+
"add sms using client" in {
106122
val uuid = "sms"
107123
assert(client.addSMS(generateSMS(uuid)) complete ())
108-
assert(probe.receiveMessage().schedule.uuid == s"Notification#$uuid#NotificationTimerKey")
124+
assert(
125+
probe.receiveMessage().schedule.uuid == s"Notification#$uuid#NotificationTimerKey"
126+
) // pending
127+
assert(
128+
probe.receiveMessage().schedule.uuid == s"Notification#$uuid#NotificationTimerKey"
129+
) // ack
109130
}
110131

111-
"add push" in {
132+
"send sms using client" in {
133+
val uuid = "sms2"
134+
val sms = generateSMS(uuid)
135+
client.sendSMS(sms) complete () match {
136+
case Success(result) =>
137+
assert(result.exists(r => r.recipient == sms.to.head && r.status.isPending))
138+
assert(
139+
probe.receiveMessage().schedule.uuid == s"Notification#$uuid#NotificationTimerKey"
140+
) // ack
141+
case Failure(_) => fail()
142+
}
143+
}
144+
145+
"add push using client" in {
112146
val uuid = "push"
113-
assert(client.addPush(generatePush(uuid)) complete ())
147+
assert(client.addPush(generatePush(uuid, androidDevice, iosDevice)) complete ())
114148
assert(probe.receiveMessage().schedule.uuid == s"Notification#$uuid#NotificationTimerKey")
115149
}
150+
151+
"retrieve push notification status using client" in {
152+
client.getNotificationStatus("push") complete () match {
153+
case Success(result) =>
154+
assert(result.exists(r => r.recipient == androidDevice.regId && r.status.isSent))
155+
assert(result.exists(r => r.recipient == iosDevice.regId && r.status.isSent))
156+
case Failure(_) => fail()
157+
}
158+
}
159+
160+
"send push using client" in {
161+
val uuid = "push2"
162+
val push = generatePush(uuid, androidDevice, iosDevice)
163+
client.sendPush(push) complete () match {
164+
case Success(result) =>
165+
assert(result.exists(r => r.recipient == androidDevice.regId && r.status.isSent))
166+
assert(result.exists(r => r.recipient == iosDevice.regId && r.status.isSent))
167+
case Failure(_) => fail()
168+
}
169+
}
170+
116171
}
117172
}

0 commit comments

Comments
 (0)