package org.lantern.state;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;

import java.io.File;

import org.apache.commons.lang.SystemUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import org.lantern.CountryService;
import org.lantern.TestUtils;
import org.lantern.TestingUtils;
import org.lantern.privacy.LocalCipherProvider;

import com.google.common.io.Files;

public class ModelIoTest {

    private static File testFile;

    @BeforeClass
    public static void setup() throws Exception {
        testFile = new File(Files.createTempDir(), "modelTest");
        testFile.delete();
        testFile.deleteOnExit();
    }
    
    @Test
    public void testModelIo() throws Exception {
        final File dir = testFile.getParentFile();
        if (!dir.exists()) {
            assertTrue("Could not make temp directory!", dir.mkdirs());
        }
        assertTrue("Can't write to test directory!", dir.canWrite());
        CountryService countryService = TestUtils.getCountryService();
        ModelIo io = new ModelIo(testFile, TestUtils.getEncryptedFileService(),
                countryService, TestingUtils.newCommandLine(), 
                mock(LocalCipherProvider.class));

        Model model = io.get();

        final String id = model.getNodeId();
        SystemData system = model.getSystem();
        Connectivity connectivity = model.getConnectivity();
        assertEquals("", connectivity.getIp());

        if ("en".equalsIgnoreCase(SystemUtils.USER_LANGUAGE)) {
            assertEquals("en", system.getLang());
        }
        
        model.getSettings().setRefreshToken("refreshToken");
        io.write();

        io = new ModelIo(testFile, TestUtils.getEncryptedFileService(),
                countryService, TestingUtils.newCommandLine(),
                mock(LocalCipherProvider.class));
        final Model model2 = io.get();
        system = model2.getSystem();
        connectivity = model2.getConnectivity();
        final String tok = model2.getSettings().getRefreshToken();
        assertEquals("refreshToken", tok);

        // The user's IP address should not persist to disk
        assertEquals("", connectivity.getIp());

        assertEquals("ID should persist across sessions",
            id, model.getNodeId());

    }

}

@@ -1601,58 +1601,253 @@
     
-model.getSettings().setRefreshToken(%22refreshToken%22
+final String testId = %22test-client-id%22;%0A        final Settings set = model.getSettings();%0A        final String existingId = set.getClientID();%0A        assertNotEquals(%22IDs should not be equal%22, testId, existingId);%0A        set.setClientID(testId
 );%0A 
@@ -2231,20 +2231,16 @@
 .get
-RefreshToken
+ClientID
 ();%0A
@@ -2264,22 +2264,14 @@
 als(
-%22refreshToken%22
+testId
 , to

test something other than the refresh token since we do special handling for the refresh token while testing