BinaryResourceMetadataTest.scala 2.63 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
/*
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
2
 * Media Converter
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * 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 ch.memobase

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
22
import ch.memobase.models.{BinaryResourceMetadata, NoLocalBinary, UnknownEventType, UnmanageableMediaFileType}
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
23
24
25
26
27
28
29
30
import org.scalatest.funsuite.AnyFunSuite

import scala.io.Source

class BinaryResourceMetadataTest extends AnyFunSuite {

  val externalBaseUrl = "https://memobase.ch"

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
31
  private def loadMessageWithBinaryResource(eventType: String, mimeType: String, locator: String): String = {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
32
    val file = Source.fromFile("src/test/resources/incoming_message_with_binary.json")
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
33
    val result = file.mkString
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
34
35
36
      .replaceAll(raw"\{\{eventType\}\}", eventType)
      .replaceAll(raw"\{\{mimeType\}\}", mimeType)
      .replaceAll(raw"\{\{locator\}\}", locator)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
37
38
39
40
41
    file.close()
    result
  }

  test("the value of the id field of a KafkaMessage should match the id of the parsed object") {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    val km = BinaryResourceMetadata.build(loadMessageWithBinaryResource("Create", "image/jpeg", "https://memobase.ch/digital/BAZ-MEI_77466-1/binary"), externalBaseUrl)
    assert(km.isSuccess)
  }

  test("a reference to a non-local binary should throw a NoLocalBinary exception") {
    assertThrows[NoLocalBinary] {
      BinaryResourceMetadata.build(loadMessageWithBinaryResource("Create", "image/jpeg", "https://example.com"), externalBaseUrl).get
    }
  }

  test("a unmanageable mime type should throw a UnmanageableMediaFileType exception") {
    assertThrows[UnmanageableMediaFileType] {
      BinaryResourceMetadata.build(loadMessageWithBinaryResource("Create",
        "application/pdf",
        "https://memobase.ch/digital/BAZ-MEI_77466-1/binary"), externalBaseUrl).get
    }
  }

  test("a unknown event type should throw a UnknownEventType exception") {
    assertThrows[UnknownEventType] {
      BinaryResourceMetadata.build(loadMessageWithBinaryResource("Upload",
        "image/jpeg",
        "https://memobase.ch/digital/BAZ-MEI_77466-1/binary"), externalBaseUrl).get
    }
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
66
67
  }
}