/* * Media Converter * Extracts media files from Fedora repository * Copyright (C) 2020 Memoriav * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package ch.memobase import java.io.ByteArrayOutputStream import ch.memobase.models._ import org.apache.kafka.clients.consumer.ConsumerRecord import org.scalamock.scalatest.MockFactory import org.scalatest.funsuite.AnyFunSuite import scala.io.Source import scala.util.Success class RecordProcessorTest extends AnyFunSuite with MockFactory { def createIncomingMessage(eventType: Event, mimeType: MediaFileType, hasBinaryResource: Boolean = true): (String, ByteArrayOutputStream) = { if (hasBinaryResource) { val eT = eventType match { case Create => "Create" case Delete => "Delete" case Update => "Update" } val mT = mimeType match { case JpegFile => "image/jpeg" case Mp3File => "audio/mpeg" case VideoMpeg4File => "video/mpeg" } replaceTokensInIncomingMessage(eT, mT) } else { val is = Source.fromFile("src/test/resources/incoming_message_without_binary.json") val incomingMessage = is.mkString val baos = copyIncomingMessage(incomingMessage) is.close (incomingMessage, baos) } } private def replaceTokensInIncomingMessage(eventType: String, mimeType: String, locator: String = "https://memobase.ch/digital/BAZ-MEI_77466-1/binary") = { val is = Source.fromFile("src/test/resources/incoming_message_with_binary.json") val incomingMessage = is.mkString .replaceAll(raw"\{\{mimeType\}\}", mimeType) .replaceAll(raw"\{\{eventType\}\}", eventType) .replaceAll(raw"\{\{locator\}\}", locator) val baos = copyIncomingMessage(incomingMessage) is.close (incomingMessage, baos) } private def copyIncomingMessage(incomingMessage: String): ByteArrayOutputStream = { val incomingMessageAsBytes = incomingMessage.getBytes() val baos = new ByteArrayOutputStream() baos.write(incomingMessageAsBytes, 0, incomingMessageAsBytes.length) baos } private val fixture = new { val externalBaseUrl = "https://memobase.ch" val resourceId = "BAZ-MEI_77466-1" val pathToResource = s"$externalBaseUrl/digital/$resourceId/binary" val mockDisseminationCopyHandler: DisseminationCopyHandler = mock[DisseminationCopyHandler] val mockFedoraClientWrapper: FedoraClientWrapper = mock[FedoraClientWrapper] } test("an object of mimeType image/jpeg and eventType Create should trigger copyImage") { val f = fixture val mockDCH = f.mockDisseminationCopyHandler val mockFCW = f.mockFedoraClientWrapper val (incomingMessage, baos) = createIncomingMessage(Create, JpegFile) (mockFCW.fetchBinaryResource _).expects(f.pathToResource).returning(Success(FileWithMetadata(baos, JpegFile))) (mockDCH.createImageCopy _).expects(baos, f.resourceId, JpegFile) val rP = new RecordProcessor(mockDCH, mockFCW, f.externalBaseUrl) val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", incomingMessage) rP.process(cR) } test("an object of mimeType audio/mpeg and eventType Delete should trigger deleteAudio") { val f = fixture val mockDCH = f.mockDisseminationCopyHandler val mockFCW = f.mockFedoraClientWrapper val (incomingMessage, baos) = createIncomingMessage(Delete, Mp3File) (mockFCW.fetchBinaryResource _).expects(f.pathToResource).returning(Success(FileWithMetadata(baos, Mp3File))) (mockDCH.deleteAudioCopy _).expects(f.resourceId) val rP = new RecordProcessor(mockDCH, mockFCW, f.externalBaseUrl) val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", incomingMessage) rP.process(cR) } test("a message which does not refer to a binary file should be ignored") { val f = fixture val mockDCH = f.mockDisseminationCopyHandler val mockFCW = f.mockFedoraClientWrapper val (incomingMessage, _) = createIncomingMessage(Delete, Mp3File, hasBinaryResource = false) (mockFCW.fetchBinaryResource _).expects(*).never() val rP = new RecordProcessor(mockDCH, mockFCW, f.externalBaseUrl) val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", incomingMessage) rP.process(cR) } }