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);
    }


}

@@ -324,21 +324,17 @@
 rTest %7B%0A
-    
 %0A
+
     @Tes
@@ -474,29 +474,25 @@
 ing);%0A    %7D%0A
-    
 %0A
+
     @Test%0A  
@@ -806,29 +806,25 @@
     %7D%0A    %7D%0A
-    
 %0A
+
     @Test%0A  
@@ -1137,27 +1137,24 @@
 Of('a') %3E -1
- &&
 %0A           
@@ -1153,32 +1153,35 @@
                 
+ &&
  token.indexOf('
@@ -1184,27 +1184,24 @@
 Of('A') %3E -1
- &&
 %0A           
@@ -1200,32 +1200,35 @@
                 
+ &&
  token.indexOf('
@@ -1231,27 +1231,24 @@
 Of('z') %3E -1
- &&
 %0A           
@@ -1247,32 +1247,35 @@
                 
+ &&
  token.indexOf('
@@ -1278,27 +1278,24 @@
 Of('Z') %3E -1
- &&
 %0A           
@@ -1294,32 +1294,35 @@
                 
+ &&
  token.indexOf('
@@ -1329,19 +1329,16 @@
 0') %3E -1
- &&
 %0A       
@@ -1349,16 +1349,19 @@
         
+ &&
  token.i
@@ -1390,16 +1390,24 @@
         
+     
+   
 if (foun
@@ -1414,16 +1414,24 @@
 dChars)%0A
+        
         
@@ -1497,21 +1497,17 @@
   %7D%0A
-    
 %0A
+
     /**%0A
     
@@ -1502,16 +1502,16 @@
     /**%0A
+
      * N
@@ -1581,17 +1581,16 @@
 e amount
- 
 %0A     * 

Fix checkstyle errors.