RecordProcessorTest.scala 4.85 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * 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 <https://www.gnu.org/licenses/>.
 */

package org.memobase

import java.io.IOException
import java.nio.file.{Path, Paths}

import org.apache.kafka.clients.consumer.ConsumerRecord
import org.scalatest.funsuite.AnyFunSuite

import scala.util.{Failure, Success, Try}

class RecordProcessorTest extends AnyFunSuite {

  private def generateJsonString(objectId: String, eventType: String, isBinary: Boolean): String = {
    s"""{
       |  "eventTimestamp": "20200602T083300+02:00",
       |  "eventId": "urn:uuid:550e8400-e2df-2314-a7ef-432432943201",
       |  "eventType": "$eventType",
       |  "objectPath": "http://localhost:8080/rest/test1",
       |  "objectId": "$objectId",
       |  "objectTypes": ["http://www.w3.org/ns/ldp#Container", "http://fedora.info/definitions/v4/repository#Resource", "http://fedora.info/definitions/v4/repository#Container"${if (isBinary) ", \"https://binaryObject\"" else ""}]
       |}""".stripMargin

  }

  test("an object of mimeType audio/mpeg and eventType Delete should trigger deleteAudio") {
    val mockFileHandler = new MockFileHandler(false)
    val mockApiHandler = new MockApiHandler(Mp3File)
    val rP = new RecordProcessor(mockFileHandler, mockApiHandler)
    val recVal = generateJsonString("test1", "Delete", isBinary = true)
    val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", recVal)
    val res = rP.process(cR)
    assert(ProcessSuccess("test1", Mp3File, "Delete") == res)
  }

  test("an object of mimeType image/jpg and eventType Create should trigger copyImage") {
    val mockFileHandler = new MockFileHandler(false)
    val mockApiHandler = new MockApiHandler(JpegFile)
    val rP = new RecordProcessor(mockFileHandler, mockApiHandler)
    val recVal = generateJsonString("test2", "Create", isBinary = true)
    val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", recVal)
    val res = rP.process(cR)
    assert(ProcessSuccess("test2", JpegFile, "Create") == res)
  }

  test("a message which does not refer to a binary file should be ignored") {
    val mockFileHandler = new MockFileHandler(false)
    val mockApiHandler = new MockApiHandler(OtherFile(List("application/json")))
    val rP = new RecordProcessor(mockFileHandler, mockApiHandler)
    val recVal = generateJsonString("test3", "Create", isBinary = false)
    val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", recVal)
    val res = rP.process(cR)
    assert(ProcessIgnore("test3", "is no binary object") == res)
  }

  test("an API connection error should return a Failure") {
    val mockFileHandler = new MockFileHandler(false)
    val mockApiHandler = new MockApiHandler(OtherFile(List("image/jpg")), true)
    val rP = new RecordProcessor(mockFileHandler, mockApiHandler)
    val recVal = generateJsonString("test4", "Create", isBinary = true)
    val cR = new ConsumerRecord[String, String]("_void", 1, 0, "1", recVal)
    val res = rP.process(cR)
    assert(res.asInstanceOf[ProcessFailure].ex.isInstanceOf[IOException])
  }

}

sealed class MockFileHandler(fail: Boolean) extends FileHandler {
  private val p = Paths.get("/bawef")

  override def copyAudio(data: Array[Byte], destId: String): Try[Path] =
    if (fail) Failure(new Exception("should fail")) else Success(p)

  override def copyImage(data: Array[Byte], destId: String): Try[Path] =
    if (fail) Failure(new Exception("should fail")) else Success(p)

  override def copyVideo(data: Array[Byte], destId: String): Try[Path] =
    if (fail) Failure(new Exception("should fail")) else Success(p)

  override def deleteAudio(destId: String): Try[Boolean] =
    if (fail) Failure(new Exception("should fail")) else Success(true)

  override def deleteImage(destId: String): Try[Boolean] =
    if (fail) Failure(new Exception("should fail")) else Success(true)

  override def deleteVideo(destId: String): Try[Boolean] =
    if (fail) Failure(new Exception("should fail")) else Success(true)
}

sealed class MockApiHandler(fileType: MediaFileType, fail: Boolean = false) extends ApiHandler {
  override def getFile(uri: String): Try[FileWithMetadata] = {
    if (fail) {
      Failure(new IOException("can't connect to API!"))
    } else {
      Success(FileWithMetadata(new Array[Byte](10), fileType))
    }
  }
}