/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.config;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import net.ranides.assira.io.FileHelper;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author ranides
 */
public class JSONConfigurationTest {
    
    public JSONConfigurationTest() {
    }
    
    

    @Test
    public void testSimpleRW() throws IOException {
        File file = FileHelper.createTempFile(".example.json", JSONConfiguration.class);
        
        Configuration source = new JSONConfiguration("file:" + file);
        
        source.put(String.class, "author.name", "ranides");
        source.put(String.class, "author.mail", "ranides@gmail.com");
        source.put(int.class, "version.major", 1);
        source.put(int.class, "version.minor", 27);
        source.put(int.class, "version.build", 2043);
        
        assertEquals(2, ((Map)source.get("author")).size() );
        assertEquals("ranides", source.get(String.class, "author.name"));
        assertEquals("ranides@gmail.com", source.get(String.class, "author.mail"));
        
        assertEquals(3, ((Map)source.get("version")).size() );
        
        assertEquals((Long)1L, source.get(long.class, "version.major"));
        assertEquals((Long)27L, source.get(long.class, "version.minor"));

        source.save();
        
        Configuration target = new JSONConfiguration("file:" + file).load(); // ConfigService.getInstance("temp:file:"+file);

        assertEquals(2, ((Map)target.get("author")).size() );
        assertEquals("ranides", target.get(String.class, "author.name"));
        assertEquals("ranides@gmail.com", target.get(String.class, "author.mail"));
        
        assertEquals(3, ((Map)target.get("version")).size() );
        assertEquals((Long)1L, target.get(long.class, "version.major"));
        assertEquals((Long)27L, target.get(long.class, "version.minor"));
        
        assertEquals((Long)55L, target.get(long.class, "version.release", 55L));
        assertEquals("#default", target.get(String.class, "root.unknown.property", "#default"));
        
    }
}
