aboutsummaryrefslogtreecommitdiff
path: root/test/uk/ac/ox/cs/pagoda/global_tests/CheckAnswersOverDataset.java
blob: 424afa2193406783f4a00869cc053bc3db18717a (plain) (blame)
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
package uk.ac.ox.cs.pagoda.global_tests;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.testng.Assert;
import uk.ac.ox.cs.pagoda.Pagoda;
import uk.ac.ox.cs.pagoda.query.QueryRecord;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

/**
 * Given an instance of Pagoda, it checks the returned answers.
 * */
public class CheckAnswersOverDataset {

    public static void check(Pagoda pagoda, Path givenAnswers) {
        try {
//            Utility.setLogLevel(Level.DEBUG); // uncomment for outputting partial results
            Path computedAnswers = Paths.get(File.createTempFile("answers", ".json").getAbsolutePath());
            new File(computedAnswers.toString()).deleteOnExit();

            pagoda.run();
            assertSameContent(computedAnswers, givenAnswers);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void assertSameContent(Path computedAnswersFile, Path givenAnswersFile) throws IOException {
        BufferedReader computedReader = Files.newBufferedReader(computedAnswersFile);
        BufferedReader givenReader = Files.newBufferedReader(givenAnswersFile);

        Gson gson = QueryRecord.GsonCreator.getInstance();

        Type cqType = new TypeToken<Set<QueryRecord>>() {}.getType();
        Set<QueryRecord> computedAnswers = gson.fromJson(computedReader, cqType);
        Set<QueryRecord> givenAnswers = gson.fromJson(givenReader, cqType);

        Assert.assertEquals(computedAnswers, givenAnswers);
    }
}