aboutsummaryrefslogtreecommitdiff
path: root/src/batchtools/rsTester/src/main/java/com/galois/fiveui/RSTestDescription.java
blob: 8341c6f25451cfdffd3f60830c97f1a25a7915bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.galois.fiveui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.List;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

/**
 * RSTestDescriptions represent a set of tests for a specific RuleSet.
 * 
 * RSTestDescriptions are also JSON-serializiable (and deserializable).
 * 
 * An example of the JSON is given below:
 * 
 * <pre>
 * {@code
 * { 'ruleSet': '../../exampleData/ruleSets/headingGuidelines.json',
 *   'tests': [ { 'url': 'http://localhost:8000/exampleData/basic/headings.html',
 *                'oracle': [ { 'ruleName': "Headings are capitalized"
 *                            , 'results': ['Error', 'Error']
 *                            },
 *                            { 'ruleName': "Disallow Empty Headers"
 *                            , 'results': ['Error']
 *                            }
 *                          ]
 *              }
 *            ]
 * }
 * }
 * </pre>
 * 
 * {@code tests} is as list that may contain a number of urls and sets of 
 * expected Problems for the specified rule set and url combination.
 * 
 * {@code ruleSet} is a string file path that is relative to the rule set 
 * description json file.
 * 
 * @author creswick
 *
 */
public class RSTestDescription {

    /**
     * Parse a JSON file into a RSTestDescription
     * 
     * @param runDescFileName The file to load.
     * @return A populated RunDescription object.
     * @throws FileNotFoundException if runDescFile can't be found.
     */
    public static RSTestDescription parse(String runDescFileName) 
            throws FileNotFoundException {

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(RSTestDescription.class, 
                new RSTestDescription.Deserializer(runDescFileName));
        Gson gson = gsonBuilder.create();
        
        Reader in = new InputStreamReader(new FileInputStream(runDescFileName));
        
        return gson.fromJson(in, RSTestDescription.class);
    }

    public static class Deserializer implements JsonDeserializer<RSTestDescription> {

        private final String _descFile;

        public Deserializer(String descFile) {
            _descFile = descFile;
        }

        public RSTestDescription deserialize(JsonElement json, Type typeOfT,
                JsonDeserializationContext context) throws JsonParseException {
            
            JsonObject obj = json.getAsJsonObject();
            
            String ruleSet = obj.get("ruleSet").getAsString();
            JsonArray testArray = obj.get("tests").getAsJsonArray();
            
            List<URIMap> tests = Lists.newArrayList(); 
            for (JsonElement jsonElement : testArray) {
                tests.add((URIMap)context.deserialize(jsonElement, URIMap.class));
            }
            
            String descDir = new File(_descFile).getParent();
            if (null == descDir) {
                descDir = ".";
            }
            
            // This probably is not portable, because the ruleSet path separator
            // may not match that of the file system.
            // TODO if File.separator is not "/", then replace "\" with File.separator.
            String rsPath = descDir + File.separator + ruleSet;

            RuleSet parsed;
			try {
				parsed = RuleSet.parseFile(rsPath);
			} catch (IOException e) {
				e.printStackTrace();
				throw new JsonParseException(e.getMessage());
			}
            //RuleSet parsed = gson.fromJson(rsPath, RuleSet.class);
            
            return new RSTestDescription(rsPath, tests, parsed);
        }
    }
    
    /**
     * The path to the selected rule set.
     */
    private final String _ruleSetLoc;
    
    /**
     * A list of urls and oracles that define tests for the specified RuleSet.
     */
    private final List<URIMap> _tests;

    private final RuleSet _ruleSet;
    
    public RSTestDescription(String ruleSetLoc, List<URIMap> tests, RuleSet ruleSet) {
        _ruleSetLoc = ruleSetLoc;
        _tests   = tests;
        _ruleSet = ruleSet;
    }

    public RuleSet getRuleSet() {
        return _ruleSet;
    }

    public String getRuleSetLoc() {
        return _ruleSetLoc;
    }

    /**
     * Combines the URI in each test from the rule set test description with
     * each rule that accompanies it.
     * <p>
     * The result is a list of RuleTest objects
     * that each contain: a URI and rule set (to be run), a rule ID and list
     * of results the we expect from the run.
     * 
     * @return a list of RuleTest objects
     * @throws IOException
     */
    public ImmutableList<RuleTest> getTests() throws IOException {
        Builder<RuleTest> builder = ImmutableList.builder();
        for (URIMap uriMap : _tests) {
            for (RuleMap rMap : uriMap.getOracle()) {
                builder.add(
                 new RuleTest(uriMap.getUrl(), getRuleSet(), rMap.getRuleName(), rMap.getResults()));
            }
        }
        return builder.build();
    }
    
    public String toString() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    @Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((_ruleSet == null) ? 0 : _ruleSet.hashCode());
		result = prime * result
				+ ((_ruleSetLoc == null) ? 0 : _ruleSetLoc.hashCode());
		result = prime * result + ((_tests == null) ? 0 : _tests.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RSTestDescription other = (RSTestDescription) obj;
		if (_ruleSet == null) {
			if (other._ruleSet != null)
				return false;
		} else if (!_ruleSet.equals(other._ruleSet))
			return false;
		if (_ruleSetLoc == null) {
			if (other._ruleSetLoc != null)
				return false;
		} else if (!_ruleSetLoc.equals(other._ruleSetLoc))
			return false;
		if (_tests == null) {
			if (other._tests != null)
				return false;
		} else if (!_tests.equals(other._tests))
			return false;
		return true;
	}

	public static class URIMap {
        private URI url;
        private List<RuleMap> oracle;
        
        URIMap(){}

        public URIMap(URI url, List<RuleMap> oracle) {
			super();
			this.url = url;
			this.oracle = oracle;
		}

        public URI getUrl() {
            return url;
        }

        public List<RuleMap> getOracle() {
            return oracle;
        }

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((oracle == null) ? 0 : oracle.hashCode());
			result = prime * result + ((url == null) ? 0 : url.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			URIMap other = (URIMap) obj;
			if (oracle == null) {
				if (other.oracle != null)
					return false;
			} else if (!oracle.equals(other.oracle))
				return false;
			if (url == null) {
				if (other.url != null)
					return false;
			} else if (!url.equals(other.url))
				return false;
			return true;
		}
    }
    
    public static class RuleMap {
        private String ruleName;
        private List<ResType> results;
        
        public RuleMap(String ruleName, List<ResType> results) {
			super();
			this.ruleName = ruleName;
			this.results = results;
		}
        
        public String getRuleName() {
            return ruleName;
        }
        
        public List<ResType> getResults() {
            return results;
        }

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((results == null) ? 0 : results.hashCode());
			result = prime * result
					+ ((ruleName == null) ? 0 : ruleName.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			RuleMap other = (RuleMap) obj;
			if (results == null) {
				if (other.results != null)
					return false;
			} else if (!results.equals(other.results))
				return false;
			if (ruleName == null) {
				if (other.ruleName != null)
					return false;
			} else if (!ruleName.equals(other.ruleName))
				return false;
			return true;
		}
    }

}