-
Notifications
You must be signed in to change notification settings - Fork 8
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
[BE2] Added RumorState handler #1890
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c3148b5
handle rumor state
DanielTavaresA 6a313b8
create rumorstateans
DanielTavaresA 2c4ede6
added rumor state handler and Db
DanielTavaresA 7dfb153
added handler to graph
DanielTavaresA 6879e3b
Merge branch 'work-be2-daniel-response-pull-state-json' into work-be2…
DanielTavaresA 582fc4c
added generation of result
DanielTavaresA 03c1bd4
added tests and creation of result from rumorState query
DanielTavaresA 38ef5b2
optimized imports
DanielTavaresA 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
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
59 changes: 59 additions & 0 deletions
59
be2-scala/src/test/scala/ch/epfl/pop/model/network/method/RumorStateSuite.scala
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,59 @@ | ||
package ch.epfl.pop.model.network.method | ||
|
||
import ch.epfl.pop.model.objects.{Base64Data, PublicKey} | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.funsuite.AnyFunSuite as FunSuite | ||
|
||
class RumorStateSuite extends FunSuite with Matchers { | ||
|
||
test("constructor from json works for RumorState") { | ||
val state: Map[PublicKey, Int] = Map( | ||
PublicKey(Base64Data.encode("1")) -> 1, | ||
PublicKey(Base64Data.encode("2")) -> 2, | ||
PublicKey(Base64Data.encode("3")) -> 3 | ||
) | ||
|
||
val rumorState: RumorState = RumorState(state) | ||
|
||
val encodedDecoded = RumorState.buildFromJson(rumorState.toJsonString) | ||
|
||
encodedDecoded.state shouldBe state | ||
} | ||
|
||
test("difference from rumorState works well if second element as more rumors") { | ||
val rumorState1: RumorState = RumorState(Map( | ||
PublicKey(Base64Data.encode("1")) -> 1, | ||
PublicKey(Base64Data.encode("2")) -> 2 | ||
)) | ||
|
||
val rumorState2: RumorState = RumorState(Map( | ||
PublicKey(Base64Data.encode("1")) -> 3, | ||
PublicKey(Base64Data.encode("2")) -> 5, | ||
PublicKey(Base64Data.encode("3")) -> 3 | ||
)) | ||
|
||
val diffResult = rumorState1.isMissingRumorsFrom(rumorState2) | ||
|
||
diffResult shouldBe Map( | ||
PublicKey(Base64Data.encode("1")) -> List(2, 3), | ||
PublicKey(Base64Data.encode("2")) -> List(3, 4, 5), | ||
PublicKey(Base64Data.encode("3")) -> List(0, 1, 2, 3) | ||
) | ||
} | ||
|
||
test("difference from rumorState works well if first element as more rumors") { | ||
val rumorState1: RumorState = RumorState(Map( | ||
PublicKey(Base64Data.encode("1")) -> 3, | ||
PublicKey(Base64Data.encode("2")) -> 5, | ||
PublicKey(Base64Data.encode("3")) -> 3 | ||
)) | ||
|
||
val rumorState2: RumorState = RumorState(Map( | ||
PublicKey(Base64Data.encode("1")) -> 1, | ||
PublicKey(Base64Data.encode("2")) -> 2 | ||
)) | ||
|
||
val diffResult = rumorState1.isMissingRumorsFrom(rumorState2) | ||
diffResult shouldBe Map.empty | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
be2-scala/src/test/scala/ch/epfl/pop/pubsub/graph/handlers/RumorStateHandlerSuite.scala
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,85 @@ | ||
package ch.epfl.pop.pubsub.graph.handlers | ||
|
||
import akka.NotUsed | ||
import akka.actor.{ActorRef, ActorSystem, Props} | ||
import akka.pattern.AskableActorRef | ||
import akka.testkit.TestKit | ||
import ch.epfl.pop.pubsub.{AskPatternConstants, MessageRegistry, PubSubMediator} | ||
import ch.epfl.pop.storage.{DbActor, InMemoryStorage} | ||
import org.scalatest.BeforeAndAfterAll | ||
import org.scalatest.funsuite.AnyFunSuiteLike | ||
import akka.pattern.ask | ||
import akka.stream.scaladsl.{Flow, Sink, Source} | ||
import ch.epfl.pop.IOHelper | ||
import ch.epfl.pop.model.network.MethodType.rumor_state | ||
import ch.epfl.pop.model.network.{JsonRpcRequest, JsonRpcResponse, ResultEmptyList, ResultObject, ResultRumor} | ||
import ch.epfl.pop.model.network.method.{Rumor, RumorState} | ||
import ch.epfl.pop.model.objects.{Base64Data, PublicKey} | ||
import ch.epfl.pop.pubsub.graph.GraphMessage | ||
import ch.epfl.pop.pubsub.graph.validators.RpcValidator | ||
import ch.epfl.pop.storage.DbActor.DbActorAck | ||
import org.scalatest.matchers.should.Matchers.{a, shouldBe} | ||
import util.examples.Rumor.RumorExample | ||
|
||
import scala.concurrent.Await | ||
|
||
class RumorStateHandlerSuite extends TestKit(ActorSystem("RumorStateSuiteActorSystem")) with AnyFunSuiteLike with AskPatternConstants with BeforeAndAfterAll { | ||
|
||
private var inMemoryStorage: InMemoryStorage = _ | ||
private var messageRegistry: MessageRegistry = _ | ||
private var pubSubMediatorRef: ActorRef = _ | ||
private var dbActorRef: AskableActorRef = _ | ||
private var rumorStateHandler: Flow[GraphMessage, GraphMessage, NotUsed] = _ | ||
private val rumorState = JsonRpcRequest.buildFromJson(IOHelper.readJsonFromPath("src/test/scala/util/examples/json/rumor_state/rumor_state.json")) | ||
|
||
override def beforeAll(): Unit = { | ||
inMemoryStorage = InMemoryStorage() | ||
messageRegistry = MessageRegistry() | ||
pubSubMediatorRef = system.actorOf(PubSubMediator.props, "pubSubRumorState") | ||
dbActorRef = system.actorOf(Props(DbActor(pubSubMediatorRef, messageRegistry, inMemoryStorage)), "dbRumorState") | ||
rumorStateHandler = ParamsHandler.rumorStateHandler(dbActorRef) | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
TestKit.shutdownActorSystem(system) | ||
} | ||
|
||
test("rumor state handler generate right type of response") { | ||
val output = Source.single(Right(rumorState)).via(rumorStateHandler).runWith(Sink.head) | ||
|
||
Await.result(output, duration) shouldBe a[Right[_, _]] | ||
} | ||
|
||
test("rumor state handler fails on wrong input") { | ||
val rumorRpc = JsonRpcRequest.buildFromJson(IOHelper.readJsonFromPath("src/test/scala/util/examples/json/rumor/rumor.json")) | ||
val outputRumor = Source.single(Right(rumorRpc)).via(rumorStateHandler).runWith(Sink.head) | ||
|
||
Await.result(outputRumor, duration) shouldBe a[Left[_, _]] | ||
|
||
val responseRpc = JsonRpcResponse(RpcValidator.JSON_RPC_VERSION, ResultObject(0), None) | ||
val outputResponse = Source.single(Right(responseRpc)).via(rumorStateHandler).runWith(Sink.head) | ||
|
||
Await.result(outputResponse, duration) shouldBe a[Left[_, _]] | ||
} | ||
|
||
test("rumor state handler should return the right list of rumors") { | ||
val publicKey = PublicKey(Base64Data.encode("publicKey")) | ||
val rumorList: List[Rumor] = (0 to 10).map(i => Rumor(publicKey, i, Map.empty)).toList | ||
|
||
for (rumor <- rumorList) | ||
val writeResult = dbActorRef ? DbActor.WriteRumor(rumor) | ||
Await.result(writeResult, duration) shouldBe a[DbActorAck] | ||
|
||
val rumorState = RumorState(Map( | ||
publicKey -> 5 | ||
)) | ||
|
||
val rumorStateRpc = JsonRpcRequest(RpcValidator.JSON_RPC_VERSION, rumor_state, rumorState, Some(0)) | ||
val output = Source.single(Right(rumorStateRpc)).via(rumorStateHandler).runWith(Sink.head) | ||
val rumorListResult = rumorList.filter(_.rumorId > 5) | ||
val resultObject = ResultObject(ResultRumor(rumorListResult)) | ||
Await.result(output, duration) shouldBe Right(JsonRpcResponse(RpcValidator.JSON_RPC_VERSION, resultObject, rumorStateRpc.id)) | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Why delete that ?
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.
Moved to RumorStateSuite apparently