summaryrefslogtreecommitdiff
path: root/src/main/JUTests/conf/SSSyncConfParserTest.java
blob: 100df16c473d6f0d130aee7919641140e97b63a8 (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
package conf;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

import org.junit.Before;
import org.junit.Test;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

public class SSSyncConfParserTest {
	
	private File currentFolder;
	
	@Before
	public void setup() {
		URL main = SSSyncConfParserTest.class.getResource("SSSyncConfParserTest.class");
		if (!"file".equalsIgnoreCase(main.getProtocol()))
			throw new IllegalStateException("This class is not stored in a file");
		currentFolder = new File(main.getPath()).getParentFile();
	}
	
	@Test
	public void loadConfigTest() throws Exception {
		
		String expectedMain = readEntireFile(new File(currentFolder, "testExpectedMain.yaml"));
		String expectedConn = readEntireFile(new File(currentFolder, "testExpectedConn.yaml"));
		String mainConfigFile = new File(currentFolder, "testMain.yaml").getAbsolutePath();
		String connConfigFile = new File(currentFolder, "testConn.yaml").getAbsolutePath();

		// Loading (config => beans)
		ConfigRootBean confMain = SSSyncConfParser.loadMainConfig(mainConfigFile);
		ConfigConnectionsBean confConn = SSSyncConfParser.loadConnConfig(connConfigFile);
		
		
		System.out.println(confMain);
		System.out.println(confConn);
		
		// Dumping (beans => config)
		DumperOptions options = new DumperOptions();
		options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
		Yaml yamlDump = new Yaml(options);
		String dumpMain = yamlDump.dump(confMain);
		String dumpConn = yamlDump.dump(confConn);
		
		// Checking that everything is kept
		assertEquals(expectedMain, dumpMain);
		assertEquals(expectedConn, dumpConn);
	}
	
    private static String readEntireFile(File file) throws IOException {
        FileReader in = new FileReader(file);
        StringBuilder contents = new StringBuilder((int) file.length());
        char[] buffer = new char[4096];
        int read = 0;
        do {
            contents.append(buffer, 0, read);
            read = in.read(buffer);
        } while (read >= 0);
        in.close();
        
        return contents.toString();
    }

}