package org.slc.sli.api.security.oauth;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.junit.Test;

/**
 * 
 * @author pwolf
 *
 */
public class TokenGeneratorTest {
    
    @Test
    public void testIdGeneratorZeroLength() {
        String string = TokenGenerator.generateToken(0);
        assertEquals("", string);
    }
    
    @Test
    public void testIdGeneratorLargeLength() {
        String string = TokenGenerator.generateToken(1000);
        assertEquals(1000, string.length());
        for (int i = 0; i < string.length(); i++) {
            assertTrue(Character.isDigit(string.charAt(i)) || Character.isLetter(string.charAt(i)));
        }
    }
    
    @Test
    public void testEdgeCharacters() {
        //can find random As, Zs, 0s, and 9s.
        boolean foundChars = false;
        for (int i = 0; i < 25; i++) {   //try a few times to increase our probability
            String token = TokenGenerator.generateToken(10000);
            foundChars = token.indexOf('a') > -1 &&
                    token.indexOf('A') > -1 &&
                    token.indexOf('z') > -1 &&
                    token.indexOf('Z') > -1 &&
                    token.indexOf('0') > -1 &&
                    token.indexOf('9') > -1;
            if (foundChars)
                break;
        }
        assertTrue(foundChars);
    }
    
    /**
     * Not enabled, but would be a quick and dirty way of verifying some amount 
     * of entropy in our pseudo-randomness by checking out well it compresses
     */
    public void testEntropy() throws Exception {
        String string = TokenGenerator.generateToken(1000);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        GZIPOutputStream out = new GZIPOutputStream(bytes);
        out.write(string.getBytes());
        out.close();
        assertTrue("Entropy was too low..." + bytes.toByteArray().length, bytes.toByteArray().length > 700);
    }


}

@@ -1146,34 +1146,32 @@
 dexOf('a') %3E -1 
-&&
 %0A               
@@ -1166,32 +1166,35 @@
                 
+ &&
  token.indexOf('
@@ -1197,28 +1197,26 @@
 Of('A') %3E -1
- &&
 %0A
+ 
             
@@ -1214,32 +1214,34 @@
                 
+&&
  token.indexOf('
@@ -1244,27 +1244,24 @@
 Of('z') %3E -1
- &&
 %0A           
@@ -1260,32 +1260,35 @@
                 
+ &&
  token.indexOf('
@@ -1291,27 +1291,24 @@
 Of('Z') %3E -1
- &&
 %0A           
@@ -1307,32 +1307,35 @@
                 
+ &&
  token.indexOf('
@@ -1346,12 +1346,10 @@
 %3E -1
- &&
 %0A
+ 
     
@@ -1363,16 +1363,18 @@
         
+&&
  token.i

fix checkstyle