RecordProcessorTest.scala 5.03 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
/*
 * 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/>.
 */

19
package ch.memobase
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
20
21
22
23
24

import org.scalatest.funsuite.AnyFunSuite

class RecordProcessorTest extends AnyFunSuite {

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
25
26
  private val externalUrl = "https://memobase.ch"

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
27
28
29
30
31
32
33
34
35
36
37
38
  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

  }

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
39
40
41
42
43
44
45
46
47
48
  /*
    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, externalUrl)
      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)
    }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
49

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
50
51
52
53
54
55
56
57
58
    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, externalUrl)
      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)
    }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
59

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
60
61
62
63
64
65
66
67
68
    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, externalUrl)
      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)
    }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
69

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
70
71
72
73
74
75
76
77
78
    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, externalUrl)
      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])
    } */
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
79
80
81

}

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
82
83
/*

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
84
85
86
sealed class MockFileHandler(fail: Boolean) extends FileHandler {
  private val p = Paths.get("/bawef")

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
87
  override def copyAudio(data: ByteArrayOutputStream, destId: String): Try[Path] =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
88
89
    if (fail) Failure(new Exception("should fail")) else Success(p)

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
90
  override def copyImage(data: ByteArrayOutputStream, destId: String): Try[Path] =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
91
92
    if (fail) Failure(new Exception("should fail")) else Success(p)

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
93
  override def copyVideo(data: ByteArrayOutputStream, destId: String): Try[Path] =
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
94
95
96
97
98
99
100
101
102
103
104
105
106
    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 {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
107
  override def fetchBinaryResource(uri: String): Try[FileWithMetadata] = {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
108
109
110
    if (fail) {
      Failure(new IOException("can't connect to API!"))
    } else {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
111
112
113
114
      val bytes = Array[Byte](10)
      val os = new ByteArrayOutputStream(bytes.length)
      os.write(bytes, 0, bytes.length)
      Success(FileWithMetadata(os, fileType))
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
115
116
117
    }
  }
}
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
118
*/