1+ /**
2+ * Copyright (c) 2013, The National Archives <[email protected] > 3+ * http://www.nationalarchives.gov.uk
4+ *
5+ * This Source Code Form is subject to the terms of the Mozilla Public
6+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8+ */
9+ package uk .gov .nationalarchives .csv .validator
10+
11+ import java .io .{Reader , StringReader }
12+
13+ import org .junit .runner .RunWith
14+ import org .specs2 .mutable .Specification
15+ import org .specs2 .runner .JUnitRunner
16+ import uk .gov .nationalarchives .csv .validator .schema .{Schema , _ }
17+
18+ import scala .language .reflectiveCalls
19+ import scalaz .{Failure , IList , Success }
20+
21+ @ RunWith (classOf [JUnitRunner ])
22+ class NotEmptyBugTest extends Specification with TestResources {
23+
24+ implicit def stringToStringReader (s : String ): StringReader = new StringReader (s.replaceAll(" \n\\ s+" , " \n " ))
25+
26+ implicit def stringToSchema (s : String ): Schema = {
27+ val schemaParser = new SchemaParser () {
28+ val pathSubstitutions = List [(String ,String )]()
29+ val enforceCaseSensitivePathChecks = false
30+ val trace = false
31+
32+ override def parse (reader : Reader ): ParseResult [Schema ] = {
33+ super .parse(reader) match {
34+ case s@ Success (schema : Schema , _) => s
35+ case NoSuccess (message, next) => throw new RuntimeException (message)
36+ }
37+ }
38+ }
39+
40+ schemaParser.parse(s).get
41+ }
42+
43+ object TestMetaDataValidator extends AllErrorsMetaDataValidator {
44+ val pathSubstitutions = List [(String ,String )]();
45+ val trace = false
46+ }
47+
48+ import TestMetaDataValidator ._
49+
50+
51+ " Validation" should {
52+
53+ " succeed for more than 1 notEmpty rule with different columns" in {
54+ val schema =
55+ """ version 1.1
56+ @totalColumns 2
57+ file_name: notEmpty if($curated_file_name/notEmpty, is(noext($curated_file_name) ))
58+ curated_file_name: @optional
59+ """
60+
61+ val metaData =
62+ """ file_name,curated_file_name
63+ content,
64+ """
65+ validate(metaData, schema, None ) must beLike { case Success (_) => ok }
66+
67+ }
68+
69+ " succeed for more than 1 empty rule with different columns" in {
70+ val schema =
71+ """ version 1.1
72+ @totalColumns 2
73+ file_name: empty if($curated_file_name/empty, is(noext($curated_file_name)))
74+ curated_file_name: @optional
75+ """
76+
77+ val metaData =
78+ """ file_name,curated_file_name
79+ ,content
80+ """
81+ validate(metaData, schema, None ) must beLike { case Success (_) => ok }
82+ }
83+
84+ " succeed for more than 1 uuid4 rule with different columns" in {
85+ val schema =
86+ """ version 1.1
87+ @totalColumns 2
88+ file_name: uuid4 if($curated_file_name/uuid4, is(noext($curated_file_name)))
89+ curated_file_name: @optional
90+ """
91+
92+ val metaData =
93+ """ file_name,curated_file_name
94+ 8f60aab0-f66d-48d8-9382-f692b26b34dc,not-uuidv4-value
95+ """
96+ validate(metaData, schema, None ) must beLike { case Success (_) => ok }
97+ }
98+
99+ " succeed for more than 1 positiveInteger rule with different columns" in {
100+ val schema =
101+ """ version 1.1
102+ @totalColumns 2
103+ file_name: positiveInteger if($curated_file_name/positiveInteger, is(noext($curated_file_name)))
104+ curated_file_name: @optional
105+ """
106+
107+ val metaData =
108+ """ file_name,curated_file_name
109+ 111,-1224544
110+ """
111+ validate(metaData, schema, None ) must beLike { case Success (_) => ok }
112+ }
113+
114+ " succeed for more than 1 uri rule with different columns" in {
115+ val schema =
116+ """ version 1.1
117+ @totalColumns 2
118+ file_name: uri if($curated_file_name/uri, is(noext($curated_file_name)))
119+ curated_file_name: @optional
120+ """
121+
122+ val metaData =
123+ """ file_name,curated_file_name
124+ http://www.root.cz/,not a URI
125+ """
126+ validate(metaData, schema, None ) must beLike { case Success (_) => ok }
127+ }
128+
129+ " succeed for more than 1 partUkDate rule with different columns" in {
130+ val schema =
131+ """ version 1.1
132+ @totalColumns 2
133+ file_name: partUkDate if($curated_file_name/partUkDate, is(noext($curated_file_name)))
134+ curated_file_name: @optional
135+ """
136+
137+ val metaData =
138+ """ file_name,curated_file_name
139+ 04/February/1981,not a date
140+ """
141+ validate(metaData, schema, None ) must beLike { case Success (_) => ok }
142+ }
143+
144+ " succeed for more than 1 lowerCase rule with different columns" in {
145+ val schema =
146+ """ version 1.1
147+ @totalColumns 2
148+ file_name: lowerCase if($curated_file_name/lowerCase, is(noext($curated_file_name)))
149+ curated_file_name: @optional
150+ """
151+
152+ val metaData =
153+ """ file_name,curated_file_name
154+ lowercase text,CamelCaseText
155+ """
156+ validate(metaData, schema, None ) must beLike { case Success (_) => ok }
157+ }
158+
159+ " succeed for more than 1 upperCase rule with different columns" in {
160+ val schema =
161+ """ version 1.1
162+ @totalColumns 2
163+ file_name: upperCase if($curated_file_name/upperCase, is(noext($curated_file_name)))
164+ curated_file_name: @optional
165+ """
166+
167+ val metaData =
168+ """ file_name,curated_file_name
169+ UPPERCASE,CamelCaseText
170+ """
171+ validate(metaData, schema, None ) must beLike { case Success (_) => ok }
172+ }
173+
174+ " succeed for more than 1 identical rule with different columns" in {
175+ val schema =
176+ """ version 1.1
177+ @totalColumns 2
178+ file_name: identical or lowerCase
179+ curated_file_name: identical
180+ """
181+
182+ val metaData =
183+ """ file_name,curated_file_name
184+ ble,sameValue
185+ elb,sameValue
186+ """
187+ val result = validate(metaData, schema, None )
188+
189+ result must beLike { case Success (_) => ok }
190+ }
191+ }
192+ }
0 commit comments