Skip to content

Commit

Permalink
Merge pull request #998 from nickgrippin/tsvalidated-cerealizer
Browse files Browse the repository at this point in the history
TsValidated Serializer
  • Loading branch information
jmarin authored Jun 2, 2017
2 parents 57214ca + 9755434 commit c7481a1
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ option optimize_for = SPEED;

import "SubmissionEvents.proto";
import "LoanApplicationRegister.proto";
import "TransmittalSheet.proto";

message LarValidatedMessage {
LoanApplicationRegisterMessage lar = 1;
SubmissionIdMessage submissionId = 2;
}

message TsValidatedMessage {
TransmittalSheetMessage ts = 1;
}
5 changes: 3 additions & 2 deletions persistence-model/src/main/resources/application-dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ akka {
submission = "hmda.persistence.serialization.submission.SubmissionProtobufSerializer"
filing = "hmda.persistence.serialization.filing.FilingProtobufSerializer"
parser = "hmda.persistence.serialization.parser.HmdaFileParserProtobufSerializer"
larValidated = "hmda.persistence.serialization.validation.LarValidatedProtobufSerializer"
hmdaValidator = "hmda.persistence.serialization.validation.HmdaValidatorProtobufSerializer"
upload = "hmda.persistence.serialization.upload.UploadProtobufSerializer"
larStats = "hmda.persistence.serialization.validation.SubmissionLarStatsProtobufSerializer"
submissionFSM = "hmda.persistence.serialization.submission.SubmissionFSMProtobufSerializer"
Expand All @@ -22,7 +22,8 @@ akka {
"hmda.persistence.messages.events.institutions.FilingEvents$FilingStatusUpdated" = filing
"hmda.persistence.messages.events.institutions.SubmissionEvents$SubmissionCreated" = submission
"hmda.persistence.messages.events.institutions.SubmissionEvents$SubmissionStatusUpdated" = submission
"hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents$LarValidated" = larValidated
"hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents$LarValidated" = hmdaValidator
"hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents$TsValidated" = hmdaValidator
"hmda.persistence.messages.events.processing.FileUploadEvents$LineAdded" = upload
"hmda.persistence.messages.events.processing.FileUploadEvents$FileNameAdded" = upload
"hmda.persistence.messages.events.processing.HmdaFileParserEvents$TsParsed" = parser
Expand Down
5 changes: 3 additions & 2 deletions persistence-model/src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ akka {
submission = "hmda.persistence.serialization.submission.SubmissionProtobufSerializer"
filing = "hmda.persistence.serialization.filing.FilingProtobufSerializer"
parser = "hmda.persistence.serialization.parser.HmdaFileParserProtobufSerializer"
larValidated = "hmda.persistence.serialization.validation.LarValidatedProtobufSerializer"
hmdaValidator = "hmda.persistence.serialization.validation.HmdaValidatorProtobufSerializer"
upload = "hmda.persistence.serialization.upload.UploadProtobufSerializer"
larStats = "hmda.persistence.serialization.validation.SubmissionLarStatsProtobufSerializer"
submissionFSM = "hmda.persistence.serialization.submission.SubmissionFSMProtobufSerializer"
Expand All @@ -21,7 +21,8 @@ akka {
"hmda.persistence.messages.events.institutions.FilingEvents$FilingStatusUpdated" = filing
"hmda.persistence.messages.events.institutions.SubmissionEvents$SubmissionCreated" = submission
"hmda.persistence.messages.events.institutions.SubmissionEvents$SubmissionStatusUpdated" = submission
"hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents$LarValidated" = larValidated
"hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents$LarValidated" = hmdaValidator
"hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents$TsValidated" = hmdaValidator
"hmda.persistence.messages.events.processing.FileUploadEvents$LineAdded" = upload
"hmda.persistence.messages.events.processing.FileUploadEvents$FileNameAdded" = upload
"hmda.persistence.messages.events.processing.HmdaFileParserEvents$TsParsed" = parser
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package hmda.persistence.serialization.validation

import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.LarValidated
import hmda.persistence.model.serialization.HmdaFilingEvents.LarValidatedMessage
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.{ LarValidated, TsValidated }
import hmda.persistence.model.serialization.CommonHmdaValidator.{ LarValidatedMessage, TsValidatedMessage }
import hmda.persistence.model.serialization.LoanApplicationRegister.LoanApplicationRegisterMessage
import hmda.persistence.model.serialization.SubmissionEvents.SubmissionIdMessage
import hmda.persistence.model.serialization.TransmittalSheet.TransmittalSheetMessage
import hmda.persistence.serialization.lar.LARProtobufConverter._
import hmda.persistence.serialization.ts.TsProtobufConverter._
import hmda.persistence.serialization.submission.SubmissionProtobufConverter._

object LarValidatedProtobufConverter {
object HmdaValidatorProtobufConverter {

def larValidatedToProtobuf(obj: LarValidated): LarValidatedMessage = {
LarValidatedMessage(
Expand All @@ -23,4 +25,15 @@ object LarValidatedProtobufConverter {
)
}

def tsValidatedToProtobuf(obj: TsValidated): TsValidatedMessage = {
TsValidatedMessage(
ts = Some(tsToProtobuf(obj.ts))
)
}

def tsValidatedFromProtobuf(msg: TsValidatedMessage): TsValidated = {
TsValidated(
ts = tsFromProtobuf(msg.ts.getOrElse(TransmittalSheetMessage()))
)
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package hmda.persistence.serialization.validation

import akka.serialization.SerializerWithStringManifest
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.LarValidated
import hmda.persistence.model.serialization.HmdaFilingEvents.LarValidatedMessage
import hmda.persistence.serialization.validation.LarValidatedProtobufConverter._
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.{ LarValidated, TsValidated }
import hmda.persistence.model.serialization.CommonHmdaValidator.{ LarValidatedMessage, TsValidatedMessage }
import hmda.persistence.serialization.validation.HmdaValidatorProtobufConverter._

class LarValidatedProtobufSerializer extends SerializerWithStringManifest {
class HmdaValidatorProtobufSerializer extends SerializerWithStringManifest {
override def identifier: Int = 1004

override def manifest(o: AnyRef): String = o.getClass.getName

final val LarValidatedManifest = classOf[LarValidated].getName
final val TsValidatedManifest = classOf[TsValidated].getName

override def toBinary(o: AnyRef): Array[Byte] = o match {
case evt: LarValidated => larValidatedToProtobuf(evt).toByteArray
case evt: TsValidated => tsValidatedToProtobuf(evt).toByteArray
case msg: Any => throw new RuntimeException(s"Cannot serialize this message: ${msg.toString}")
}

override def fromBinary(bytes: Array[Byte], manifest: String): AnyRef = manifest match {
case LarValidatedManifest =>
larValidatedFromProtobuf(LarValidatedMessage.parseFrom(bytes))
case TsValidatedManifest =>
tsValidatedFromProtobuf(TsValidatedMessage.parseFrom(bytes))
case msg: Any => throw new RuntimeException(s"Cannot deserialize this message: ${msg.toString}")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hmda.persistence.serialization.validation

import hmda.model.fi.lar.LarGenerators
import hmda.model.fi.ts.TsGenerators
import hmda.model.institution.SubmissionGenerators._
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.{ LarValidated, TsValidated }
import hmda.persistence.model.serialization.CommonHmdaValidator.{ LarValidatedMessage, TsValidatedMessage }
import HmdaValidatorProtobufConverter._
import org.scalatest.prop.PropertyChecks
import org.scalatest.{ MustMatchers, PropSpec }

class HmdaValidatorProtobufConverterSpec extends PropSpec with PropertyChecks with MustMatchers with LarGenerators with TsGenerators {

property("Lar Validated must serialize to protobuf and back") {
forAll(larGen, submissionIdGen) { (lar, submissionId) =>
val larValidated = LarValidated(lar, submissionId)
val protobuf = larValidatedToProtobuf(larValidated).toByteArray
larValidatedFromProtobuf(LarValidatedMessage.parseFrom(protobuf)) mustBe larValidated
}
}

property("Ts Validated must serialize to protobuf and back") {
forAll(tsGen) { ts =>
val tsValidated = TsValidated(ts)
val protobuf = tsValidatedToProtobuf(tsValidated).toByteArray
tsValidatedFromProtobuf(TsValidatedMessage.parseFrom(protobuf)) mustBe tsValidated
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package hmda.persistence.serialization.validation

import hmda.model.fi.lar.LarGenerators
import hmda.model.fi.ts.TsGenerators
import hmda.model.institution.SubmissionGenerators._
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.LarValidated
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.{ LarValidated, TsValidated }
import org.scalatest.prop.PropertyChecks
import org.scalatest.{ MustMatchers, PropSpec }

class LarValidatedProtobufSerializerSpec extends PropSpec with PropertyChecks with MustMatchers with LarGenerators {
class HmdaValidatorProtobufSerializerSpec extends PropSpec with PropertyChecks with MustMatchers with LarGenerators with TsGenerators {

val serializer = new LarValidatedProtobufSerializer()
val serializer = new HmdaValidatorProtobufSerializer()

property("Lar Validated must be serialized to binary and back") {
forAll(larGen, submissionIdGen) { (lar, submissionId) =>
Expand All @@ -17,4 +18,12 @@ class LarValidatedProtobufSerializerSpec extends PropSpec with PropertyChecks wi
serializer.fromBinary(bytes, serializer.LarValidatedManifest) mustBe msg
}
}

property("Ts Validated must be serialized to binary and back") {
forAll(tsGen) { ts =>
val msg = TsValidated(ts)
val bytes = serializer.toBinary(msg)
serializer.fromBinary(bytes, serializer.TsValidatedManifest) mustBe msg
}
}
}

This file was deleted.

0 comments on commit c7481a1

Please sign in to comment.