Builders.scala 14.7 KB
Newer Older
1
package eu.nomad_lab.integrated_pipeline_tests
2
3
4
5
6

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

import eu.nomad_lab.TreeType
import eu.nomad_lab.TreeType.TreeType
7
import eu.nomad_lab.integrated_pipeline.FileTree
8
import eu.nomad_lab.integrated_pipeline.messages._
9
10
import eu.nomad_lab.integrated_pipeline_tests.EventBuilders._
import eu.nomad_lab.integrated_pipeline_tests.MessageBuilders._
11
12
import eu.nomad_lab.parsers.ParseResult.ParseResult
import eu.nomad_lab.parsers._
13
14

trait TestDataBuilders {
15
  import scala.language.implicitConversions
16

17
  def aFileTree() = BuilderFileTree()
18
19

  def aFileParsingTask() = BuilderFileParsingTask()
20
  def aFileParsingSignalEndTree() = BuilderFileParsingSignalEndTree()
21
  def anInMemoryResult() = BuilderInMemoryResult()
22

23
24
25
  def aTreeParserEventStart() = BuilderTreeParserEventStart()
  def aTreeParserEventCandidate() = BuilderTreeParserEventCandidate()
  def aTreeParserEventEnd() = BuilderTreeParserEventEnd()
26
  def aTreeParserEventTreeFailure() = BuilderTreeParserEventTreeFailure()
27

28
29
30
  def aCalculationParserEventStart() = BuilderCalculationParserEventStart()
  def aCalculationParserEventEnd() = BuilderCalculationParserEventEnd()

31
32
33
34
  def aResultWriterEventStart() = BuilderResultWriterEventStart()
  def aResultWriterEventResult() = BuilderResultWriterEventResult()
  def aResultWriterEventEnd() = BuilderResultWriterEventEnd()

35
36
  def aTreeParserEventScanError() = BuilderTreeParserEventScanError()

37
  implicit def build(x: BuilderFileTree): FileTree = x.build()
38
39
40
41

  implicit def build(x: BuilderFileParsingTask): FileParsingTask = x.build()
  implicit def build(x: BuilderFileParsingSignalEndTree): FileParsingSignalEndTree = x.build()
  implicit def build(x: BuilderInMemoryResult): InMemoryResult = x.build()
42
43
44
45

  implicit def build(x: BuilderTreeParserEventStart): TreeParserEventStart = x.build()
  implicit def build(x: BuilderTreeParserEventCandidate): TreeParserEventCandidate = x.build()
  implicit def build(x: BuilderTreeParserEventEnd): TreeParserEventEnd = x.build()
46
  implicit def build(x: BuilderTreeParserEventTreeFailure): TreeParserEventTreeFailure = x.build()
47
48
49

  implicit def build(x: BuilderCalculationParserEventStart): CalculationParserEventStart = x.build()
  implicit def build(x: BuilderCalculationParserEventEnd): CalculationParserEventEnd = x.build()
50
51
52
53

  implicit def build(x: BuilderResultWriterEventStart): ResultWriterEventStart = x.build()
  implicit def build(x: BuilderResultWriterEventResult): ResultWriterEventResult = x.build()
  implicit def build(x: BuilderResultWriterEventEnd): ResultWriterEventEnd = x.build()
54
55

  implicit def build(x: BuilderTreeParserEventScanError): TreeParserEventScanError = x.build()
56
57
}

58
59
60
61
62
private object Defaults {
  val defaultPath = Paths.get("")
  val defaultParser = "noParser"
  val defaultTreeType = TreeType.Directory
  val defaultParseResult = ParseResult.ParseSkipped
63
  val defaultError = new NotImplementedError("placeholder for real errors")
64
}
65

66
67
object MessageBuilders {
  import Defaults._
68

69
  case class BuilderFileTree(
70
71
      treeBasePath: Path = defaultPath,
      treeType: TreeType = defaultTreeType
72
73
74
75
76
77
  ) {

    def withBasePath(path: Path) = copy(treeBasePath = path)
    def withBasePath(path: String) = copy(treeBasePath = Paths.get(path))
    def withTreeType(newType: TreeType) = copy(treeType = newType)

78
    def build() = FileTree(
79
80
81
82
83
84
      treeBasePath = treeBasePath,
      treeType = treeType
    )
  }

  case class BuilderFileParsingTask(
85
      private val fileTree: FileTree = BuilderFileTree().build(),
86
87
      private val relativePath: Path = defaultPath,
      private val parserName: String = defaultParser,
88
      private val extractedPath: Option[Path] = None
89
90
  ) {

91
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
92
93
94
    def withRelativePath(path: Path) = copy(relativePath = path)
    def withRelativePath(path: String) = copy(relativePath = Paths.get(path))
    def withParserName(name: String) = copy(parserName = name)
95
    def withExtractedPath(newPath: Option[Path]) = copy(extractedPath = newPath)
96
97

    def build() = FileParsingTask(
98
      treeTask = fileTree,
99
      relativePath = relativePath,
100
101
      parserName = parserName,
      extractedPath = extractedPath
102
103
104
    )
  }

105
  case class BuilderFileParsingSignalEndTree(
106
      private val fileTree: FileTree = BuilderFileTree().build(),
107
      private val numTasks: Long = -1
108
109
  ) {

110
111
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
112
    def withBasePath(path: String): BuilderFileParsingSignalEndTree = withBasePath(Paths.get(path))
113
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
114
    def withTaskCount(count: Long) = copy(numTasks = count)
115

116
    def build() = FileParsingSignalEndTree(
117
      treeTask = fileTree,
118
119
      numParsingTasks = numTasks
    )
120
121
  }

122
123
  case class BuilderInMemoryResult(
      private val task: FileParsingTask = BuilderFileParsingTask().build(),
124
      private val result: ParseResult = defaultParseResult,
125
126
      private val start: Option[StartedParsingSession] = None,
      private val events: Seq[ParseEvent] = Seq(),
127
128
      private val end: Option[FinishedParsingSession] = None,
      private val error: Option[String] = None
129
130
131
132
133
134
135
  ) {

    def withTask(newTask: FileParsingTask) = copy(task = newTask)
    def withParseResult(newResult: ParseResult) = copy(result = newResult)
    def withStartEvent(event: Option[StartedParsingSession]) = copy(start = event)
    def withEvents(newEvents: Seq[ParseEvent]) = copy(events = newEvents)
    def withFinishEvent(event: Option[FinishedParsingSession]) = copy(end = event)
136
    def withFileTree(tree: FileTree) = copy(task = task.copy(treeTask = tree))
137
138
    def withRelativePath(newPath: Path) = copy(task = task.copy(relativePath = newPath))
    def withRelativePath(newPath: String) = copy(task = task.copy(relativePath = Paths.get(newPath)))
139
    def withErrorMessage(message: Option[String]) = copy(error = message)
140
141
142
143
144
145

    def build() = InMemoryResult(
      task = task,
      result = result,
      start = start,
      events = events,
146
147
      end = end,
      error = error
148
149
150
    )
  }

151
152
153
154
155
}

object EventBuilders {
  import Defaults._

156
  case class BuilderTreeParserEventStart(
157
      private val fileTree: FileTree = BuilderFileTree().build()
158
159
  ) {

160
161
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
162
    def withBasePath(path: String): BuilderTreeParserEventStart = withBasePath(Paths.get(path))
163
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
164
165

    def build() = TreeParserEventStart(
166
      fileTree = fileTree
167
168
169
170
    )
  }

  case class BuilderTreeParserEventCandidate(
171
      private val fileTree: FileTree = BuilderFileTree().build(),
172
173
      private val relativePath: Path = defaultPath,
      private val parser: String = defaultParser
174
175
  ) {

176
177
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
178
    def withBasePath(path: String): BuilderTreeParserEventCandidate = withBasePath(Paths.get(path))
179
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
180
181
182
183
184
    def withRelativePath(path: Path) = copy(relativePath = path)
    def withRelativePath(path: String): BuilderTreeParserEventCandidate = withRelativePath(Paths.get(path))
    def withParser(parserName: String) = copy(parser = parserName)

    def build() = TreeParserEventCandidate(
185
      fileTree = fileTree,
186
187
188
189
190
      relativePath = relativePath,
      parser = parser
    )
  }

191
  case class BuilderTreeParserEventScanError(
192
      private val fileTree: FileTree = BuilderFileTree().build(),
193
194
195
196
      private val relativePath: Path = defaultPath,
      private val error: Throwable = defaultError
  ) {

197
198
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
199
    def withBasePath(path: String): BuilderTreeParserEventScanError = withBasePath(Paths.get(path))
200
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
201
202
203
204
205
    def withRelativePath(path: Path) = copy(relativePath = path)
    def withRelativePath(path: String): BuilderTreeParserEventScanError = withRelativePath(Paths.get(path))
    def withError(newError: Throwable) = copy(error = newError)

    def build() = TreeParserEventScanError(
206
      fileTree = fileTree,
207
208
209
210
211
      relativePath = relativePath,
      error = error
    )
  }

212
  case class BuilderTreeParserEventEnd(
213
      private val fileTree: FileTree = BuilderFileTree().build(),
214
      private val numCandidates: Long = -1
215
216
  ) {

217
218
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
219
    def withBasePath(path: String): BuilderTreeParserEventEnd = withBasePath(Paths.get(path))
220
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
221
222
223
    def withNumCandidates(count: Long) = copy(numCandidates = count)

    def build() = TreeParserEventEnd(
224
      fileTree = fileTree,
225
226
227
228
      numCandidates = numCandidates
    )
  }

229
  case class BuilderTreeParserEventTreeFailure(
230
      private val fileTree: FileTree = BuilderFileTree().build(),
231
232
233
      private val error: Throwable = defaultError
  ) {

234
235
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
236
    def withBasePath(path: String): BuilderTreeParserEventTreeFailure = withBasePath(Paths.get(path))
237
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
238
239
240
    def withError(newError: Throwable) = copy(error = newError)

    def build() = TreeParserEventTreeFailure(
241
      fileTree = fileTree,
242
243
244
245
      error = error
    )
  }

246
  case class BuilderCalculationParserEventStart(
247
      private val fileTree: FileTree = BuilderFileTree().build(),
248
249
250
251
      private val relativePath: Path = defaultPath,
      private val parser: String = defaultParser
  ) {

252
253
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
254
    def withBasePath(path: String): BuilderCalculationParserEventStart = withBasePath(Paths.get(path))
255
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
256
257
258
259
260
    def withRelativePath(path: Path) = copy(relativePath = path)
    def withRelativePath(path: String): BuilderCalculationParserEventStart = withRelativePath(Paths.get(path))
    def withParser(parserName: String) = copy(parser = parserName)

    def build() = CalculationParserEventStart(
261
      fileTree = fileTree,
262
263
264
265
266
267
      relativePath = relativePath,
      parser = parser
    )
  }

  case class BuilderCalculationParserEventEnd(
268
      private val fileTree: FileTree = BuilderFileTree().build(),
269
270
271
272
273
274
      private val relativePath: Path = defaultPath,
      private val parser: String = defaultParser,
      private val result: ParseResult = defaultParseResult,
      private val error: Option[String] = None
  ) {

275
276
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
277
    def withBasePath(path: String): BuilderCalculationParserEventEnd = withBasePath(Paths.get(path))
278
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
279
280
281
282
283
284
285
    def withRelativePath(path: Path) = copy(relativePath = path)
    def withRelativePath(path: String): BuilderCalculationParserEventEnd = withRelativePath(Paths.get(path))
    def withParser(parserName: String) = copy(parser = parserName)
    def withStatus(newStatus: ParseResult) = copy(result = newStatus)
    def withErrorMessage(newError: Option[String]) = copy(error = newError)

    def build() = CalculationParserEventEnd(
286
      fileTree = fileTree,
287
288
289
290
291
292
293
      relativePath = relativePath,
      parser = parser,
      result = result,
      error = error
    )
  }

294
  case class BuilderResultWriterEventStart(
295
      private val fileTree: FileTree = BuilderFileTree().build()
296
297
  ) {

298
299
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
300
    def withBasePath(path: String): BuilderResultWriterEventStart = withBasePath(Paths.get(path))
301
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
302
303

    def build() = ResultWriterEventStart(
304
      fileTree = fileTree
305
306
307
308
    )
  }

  case class BuilderResultWriterEventResult(
309
      private val fileTree: FileTree = BuilderFileTree().build(),
310
311
312
313
314
315
      private val relativePath: Path = defaultPath,
      private val parser: String = defaultParser,
      private val result: ParseResult = defaultParseResult,
      private val error: Option[String] = None
  ) {

316
317
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
318
    def withBasePath(path: String): BuilderResultWriterEventResult = withBasePath(Paths.get(path))
319
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
320
321
322
323
324
325
326
    def withRelativePath(path: Path) = copy(relativePath = path)
    def withRelativePath(path: String): BuilderResultWriterEventResult = withRelativePath(Paths.get(path))
    def withParser(parserName: String) = copy(parser = parserName)
    def withStatus(newStatus: ParseResult) = copy(result = newStatus)
    def withErrorMessage(newError: Option[String]) = copy(error = newError)

    def build() = ResultWriterEventResult(
327
      fileTree = fileTree,
328
329
330
331
332
333
334
335
      relativePath = relativePath,
      parser = parser,
      result = result,
      error = error
    )
  }

  case class BuilderResultWriterEventEnd(
336
      private val fileTree: FileTree = BuilderFileTree().build(),
337
338
339
340
341
      private val numCalculations: Long = -1,
      private val numParsingFailures: Long = -1,
      private val outputLocation: Path = defaultPath
  ) {

342
343
    def withFileTree(tree: FileTree) = copy(fileTree = tree)
    def withBasePath(path: Path) = copy(fileTree = fileTree.copy(treeBasePath = path))
344
    def withBasePath(path: String): BuilderResultWriterEventEnd = withBasePath(Paths.get(path))
345
    def withTreeType(newType: TreeType) = copy(fileTree = fileTree.copy(treeType = newType))
346
347
348
349
350
351
    def withNumCalculations(count: Long) = copy(numCalculations = count)
    def withNumParsingFailures(count: Long) = copy(numParsingFailures = count)
    def withOutputPath(path: Path) = copy(outputLocation = path)
    def withOutputPath(path: String): BuilderResultWriterEventEnd = withOutputPath(Paths.get(path))

    def build() = ResultWriterEventEnd(
352
      fileTree = fileTree,
353
354
355
356
357
358
      numCalculations = numCalculations,
      numParsingFailures = numParsingFailures,
      outputLocation = outputLocation
    )
  }

359
}