1
+ #! /usr/bin/env groovy
2
+
3
+ /**
4
+ * Copyright (c) 2018 Informatics Matters Ltd.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ /**
20
+ * The MediaTypes class. A class to provide validation of files produced
21
+ * by an executing pipeline.
22
+ */
23
+ class MediaChecker {
24
+
25
+ final static String mediaTypesFile = ' MediaTypes.txt'
26
+
27
+ // A map of media type and expected file extension(s).
28
+ // This is populated by the constructor, which reads the content
29
+ // of 'mediaTypesFile'.
30
+ def mediaTypesLookup = null
31
+
32
+ /**
33
+ * Default constructor.
34
+ * Reads media types config file.
35
+ */
36
+ MediaChecker () {
37
+
38
+ mediaTypesLookup = new ConfigSlurper (). parse(new File (mediaTypesFile). toURI(). toURL())
39
+
40
+ }
41
+
42
+ /**
43
+ * Checks that a pipeline's expected files exist.
44
+ *
45
+ * @param serviceDescriptor The test service descriptor
46
+ * @param path The test path (where the pipeline's files are to be found)
47
+ * @return True on success, False otherwise.
48
+ */
49
+ boolean check (serviceDescriptor , File path ) {
50
+
51
+ boolean retVal = true
52
+
53
+ // Check output descriptors for expected output file names...
54
+ // Do all, regardless of individual failure.
55
+ def opDescriptors = serviceDescriptor. serviceConfig. outputDescriptors
56
+ opDescriptors. each { desc ->
57
+
58
+ // There must be a matching media type in our txt/lookup file.
59
+ def typeExtList = mediaTypesLookup. media_types[desc. mediaType]
60
+ if (typeExtList == null || typeExtList. size() == 0 ) {
61
+ Log . err(" Pipeline service descriptor output mediaType" +
62
+ " '$desc . mediaType ' is not listed in '$mediaTypesFile '" )
63
+ } else {
64
+ // What are the expected file extensions for this media type?
65
+ // It's a list of 1 or more entries.
66
+ typeExtList. each { extension ->
67
+
68
+ String opName = desc. name + extension
69
+ Log . info(' Media check' , opName)
70
+ String opPath = path. toString() + File . separator + opName
71
+ if (! new File (opPath). exists()) {
72
+ Log . err(" The pipeline's 'outputDescriptor'" +
73
+ " expected '$opName ' but the file wasn't found" )
74
+ retVal = false
75
+ }
76
+
77
+ }
78
+ }
79
+
80
+ }
81
+
82
+ return retVal
83
+
84
+ }
85
+
86
+ }
0 commit comments