Skip to content

Commit

Permalink
Merge pull request #53 from hmrc/RAS-435-v2
Browse files Browse the repository at this point in the history
[RAS-435] - update to callbackURL
  • Loading branch information
RaminEsfandiari authored Dec 21, 2017
2 parents 9d04564 + 8fbd622 commit 8b8409f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ trait FileProcessingController extends BaseController {
val fileProcessingService: FileProcessingService
val sessionCacheService: SessionCacheService

def statusCallback(): Action[AnyContent] = Action.async {
def statusCallback(userId:String): Action[AnyContent] = Action.async {
implicit request =>
Logger.warn(s"callback request received : " )
withValidJson.fold(Future.successful(BadRequest(""))){ callbackData =>
callbackData.status match {
case STATUS_AVAILABLE =>
if(Try(Future(fileProcessingService.processFile(callbackData))).isFailure) {
sessionCacheService.updateRasSession(callbackData.envelopeId,callbackData,None)
sessionCacheService.updateFileSession(userId,callbackData,None)
}
case STATUS_ERROR => Logger.error(s"There is a problem with the file (${callbackData.fileId}), the status is:" +
case STATUS_ERROR => Logger.error(s"There is a problem with the file ERROR (${callbackData.fileId}), the status is:" +
s" ${callbackData.status} and the reason is: ${callbackData.reason.get}")
sessionCacheService.updateRasSession(callbackData.envelopeId,callbackData,None)
sessionCacheService.updateFileSession(userId,callbackData,None)
case _ => Logger.error(s"There is a problem with the file (${callbackData.fileId}), the status is:" +
s" ${callbackData.status}")
sessionCacheService.updateRasSession(callbackData.envelopeId,callbackData,None)
sessionCacheService.updateFileSession(userId,callbackData,None)
}

Future(Ok(""))
Expand Down
2 changes: 1 addition & 1 deletion app/uk/gov/hmrc/rasapi/models/CallbackData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object ResultsFileMetaData {

}

case class FileSession(envelopeId:String, userFile:Option[CallbackData], resultsFile: Option[ResultsFileMetaData], userId:String)
case class FileSession(userFile: Option[CallbackData], resultsFile: Option[ResultsFileMetaData], userId: String)

object FileSession {
implicit val format = Json.format[FileSession]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ trait FileProcessingService extends RasFileReader with RasFileWriter with Result
fileUploadConnector.deleteUploadedFile(callbackData.envelopeId,callbackData.fileId)
//update status as success for the envelope in session-cache to confirm it is processed
//if exception mark status as error and save into session
SessionCacheService.updateRasSession(callbackData.envelopeId,callbackData,
SessionCacheService.updateFileSession(callbackData.envelopeId,callbackData,
Some(ResultsFileMetaData(file.id.toString, file.filename, file.uploadDate,file.chunkSize,file.length)))
}
}
Expand Down
22 changes: 6 additions & 16 deletions app/uk/gov/hmrc/rasapi/services/SessionCacheService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,21 @@ trait SessionCacheService {

val sessionCache: ShortLivedHttpCaching = RasShortLivedHttpCaching
private val source = "ras"
private val cacheId = "fileUpload"
def updateRasSession(envelopeId : String, userFile:CallbackData, resultsFile:Option[ResultsFileMetaData])(implicit hc: HeaderCarrier) = {
private val cacheId = "fileSession"
def updateFileSession(userId : String, userFile:CallbackData, resultsFile:Option[ResultsFileMetaData])(implicit hc: HeaderCarrier) = {
implicit val executionContext = MdcLoggingExecutionContext.fromLoggingDetails(hc)
sessionCache.fetchAndGetEntry[FileSession](source,cacheId,envelopeId).flatMap{ session =>
sessionCache.cache[FileSession](source,cacheId,envelopeId,
FileSession(session.get.envelopeId, Some(userFile),resultsFile,session.get.userId) ).recover {
sessionCache.cache[FileSession](source,cacheId,userId,
FileSession(Some(userFile), resultsFile, userId) ).recover {
case ex: Throwable => Logger.error(s"unable to save FileSession to cache => " +
s"${envelopeId} , userFile : ${userFile.toString} , resultsFile id : " +
s"${userId} , userFile : ${userFile.toString} , resultsFile id : " +
s"${if(resultsFile.isDefined) resultsFile.get.id}, \n Exception is ${ex.getMessage}" )
throw new RuntimeException("Error in saving sessionCache" + ex.getMessage)
/*
Logger.warn("retrying to save cache")
updateRasSession(envelopeId,userFile,resultsFile)
*/
}
}.recover {
case ex: Throwable => Logger.error(s"cannot fetch data to cache for FileSession => " +
s"${envelopeId} , userFile : ${userFile.toString} , resultsFile id : " +
s"${if(resultsFile.isDefined) resultsFile.get.id}, \n Exception is ${ex.getMessage}" )
throw new RuntimeException("Error in saving sessionCache" + ex.getMessage)
/*
Logger.warn("retrying to save cache")
updateRasSession(envelopeId,userFile,resultsFile)
*/
}

}

}
Expand Down
2 changes: 1 addition & 1 deletion conf/app.routes
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

GET /customer/:customerUUID/residency-status uk.gov.hmrc.rasapi.controllers.LookupController.getResidencyStatus(customerUUID)

POST /ras-api/file-processing/status uk.gov.hmrc.rasapi.controllers.FileProcessingController.statusCallback
POST /ras-api/file-processing/status/:userId uk.gov.hmrc.rasapi.controllers.FileProcessingController.statusCallback(userId)

GET /ras-api/file/getFile/:name uk.gov.hmrc.rasapi.controllers.FileController.serveFile(name)
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import uk.gov.hmrc.rasapi.models.{CallbackData, ResultsFileMetaData}
import uk.gov.hmrc.rasapi.services.{FileProcessingService, SessionCacheService}
import play.api.http.Status.OK

import scala.util.Random

class FileProcessingControllerSpec extends UnitSpec with MockitoSugar with OneAppPerSuite with BeforeAndAfter {

val envelopeId = "0b215ey97-11d4-4006-91db-c067e74fc653"
Expand All @@ -36,6 +38,7 @@ class FileProcessingControllerSpec extends UnitSpec with MockitoSugar with OneAp
val reason: Option[String] = None
val callbackData = CallbackData(envelopeId, fileId, fileStatus, reason)
val resultsFile = ResultsFileMetaData(fileId,Some("fileName.csv"),Some(1234L),123,1234L)
val userId = Random.nextString(5)

val mockFileProcessingService = mock[FileProcessingService]
val mockSessionCacheService = mock[SessionCacheService]
Expand All @@ -54,7 +57,7 @@ class FileProcessingControllerSpec extends UnitSpec with MockitoSugar with OneAp
"return Ok and interact with FileProcessingService and SessionCacheService" when {
"an 'AVAILABLE' status is given" in {

val result = await(SUT.statusCallback().apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
val result = await(SUT.statusCallback(userId).apply(FakeRequest(Helpers.POST, s"/ras-api/file-processing/status")
.withJsonBody(Json.toJson(callbackData))))

verify(mockFileProcessingService).processFile(Meq(callbackData))(any())
Expand All @@ -71,11 +74,11 @@ class FileProcessingControllerSpec extends UnitSpec with MockitoSugar with OneAp
val reason: Option[String] = Some("VirusDetected")
val callbackData = CallbackData(envelopeId, fileId, fileStatus, reason)

val result = SUT.statusCallback().apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
val result = SUT.statusCallback(userId).apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
.withJsonBody(Json.toJson(callbackData)))

verifyZeroInteractions(mockFileProcessingService)
verify(mockSessionCacheService).updateRasSession(Meq(envelopeId),Meq(callbackData),Meq(None))(any())
verify(mockSessionCacheService).updateFileSession(Meq(userId),Meq(callbackData),Meq(None))(any())

status(result) shouldBe OK
}
Expand All @@ -87,11 +90,11 @@ class FileProcessingControllerSpec extends UnitSpec with MockitoSugar with OneAp
val reason: Option[String] = None
val callbackData = CallbackData(envelopeId, fileId, fileStatus, reason)

val result = SUT.statusCallback().apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
val result = SUT.statusCallback(userId).apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
.withJsonBody(Json.toJson(callbackData)))

verifyZeroInteractions(mockFileProcessingService)
verify(mockSessionCacheService).updateRasSession(Meq(envelopeId),Meq(callbackData),Meq(None))(any())
verify(mockSessionCacheService).updateFileSession(Meq(userId),Meq(callbackData),Meq(None))(any())

status(result) shouldBe OK
}
Expand All @@ -103,11 +106,11 @@ class FileProcessingControllerSpec extends UnitSpec with MockitoSugar with OneAp
val reason: Option[String] = None
val callbackData = CallbackData(envelopeId, fileId, fileStatus, reason)

val result = SUT.statusCallback().apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
val result = SUT.statusCallback(userId).apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
.withJsonBody(Json.toJson(callbackData)))

verifyZeroInteractions(mockFileProcessingService)
verify(mockSessionCacheService).updateRasSession(Meq(envelopeId),Meq(callbackData),Meq(None))(any())
verify(mockSessionCacheService).updateFileSession(Meq(userId),Meq(callbackData),Meq(None))(any())

status(result) shouldBe OK
}
Expand All @@ -119,11 +122,11 @@ class FileProcessingControllerSpec extends UnitSpec with MockitoSugar with OneAp
val reason: Option[String] = None
val callbackData = CallbackData(envelopeId, fileId, fileStatus, reason)

val result = SUT.statusCallback().apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
val result = SUT.statusCallback(userId).apply(FakeRequest(Helpers.POST, "/ras-api/file-processing/status")
.withJsonBody(Json.toJson(callbackData)))

verifyZeroInteractions(mockFileProcessingService)
verify(mockSessionCacheService).updateRasSession(Meq(envelopeId),Meq(callbackData),Meq(None))(any())
verify(mockSessionCacheService).updateFileSession(Meq(userId),Meq(callbackData),Meq(None))(any())

status(result) shouldBe OK
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@ class SessionCacheServiceSpec extends UnitSpec with OneServerPerSuite with Scala
val reason: Option[String] = None
val callbackData = CallbackData("1234", fileId, fileStatus, reason)
val resultsFile = ResultsFileMetaData(fileId,Some("fileName.csv"),Some(1234L),123,1234L)
val rasSession = FileSession("1234",Some(callbackData),Some(resultsFile),"userId")
val rasSession = FileSession(Some(callbackData), Some(resultsFile), "userId")
val json = Json.toJson(rasSession)

val mockSessionCache = mock[ShortLivedHttpCaching]
val SUT = new SessionCacheService {
override val sessionCache: ShortLivedHttpCaching = mockSessionCache
when(sessionCache.fetchAndGetEntry[FileSession] (any(), any(),any())
(any(),any(), any()))
.thenReturn(Future.successful(Some(rasSession)))
when(sessionCache.cache[FileSession] (any(), any(),any(),any())
(any[Writes[FileSession]], any[HeaderCarrier], any()))
.thenReturn(Future.successful(CacheMap("sessionValue", Map("1234" -> json))))
Expand All @@ -54,7 +51,7 @@ class SessionCacheServiceSpec extends UnitSpec with OneServerPerSuite with Scala
"SessionCacheService" should {
"update session cache with processing status" in {
val results = List("Nino, firstName, lastName, dob, cyResult, cy+1Result")
val res = await(SUT.updateRasSession("1234",callbackData,Some(resultsFile)))
val res = await(SUT.updateFileSession("1234",callbackData,Some(resultsFile)))
res.data.get("1234").get shouldBe json}

}
Expand Down

0 comments on commit 8b8409f

Please sign in to comment.