package org.ambraproject.wombat.util;


import com.google.common.hash.Hashing;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class CacheParamsTest {

  @Test
  public void testGetCacheKey(){
    String exampleUrl1 = "articles?journal=PLoSPathogens&min=3&since=Thu%2C+30+Oct+2014+00%3A55%3A34+GMT&type=" +
     "http%3A%2F%2Frdf.plos.org%2FRDF%2FarticleType%2FResearch%2520Article&exclude=http%3A%2F%2Frdf.plos.org%2" +
     "FRDF%2FarticleType%2FCorrection&exclude=http%3A%2F%2Frdf.plos.org%2FRDF%2FarticleType%2FRetraction&exclu" +
     "de=http%3A%2F%2Frdf.plos.org%2FRDF%2FarticleType%2FExpression%2520of%2520Concern";
    String exampleUrl2 = "articles?journal=PLoSMedicine&min=3&since=Thu%2C+30+Oct+2014+00%3A46%3A37+GMT&type=h" +
     "ttp%3A%2F%2Frdf.plos.org%2FRDF%2FarticleType%2FEditorial&type=http%3A%2F%2Frdf.plos.org%2FRDF%2FarticleT" +
     "ype%2FResearch%2520Article&type=*&exclude=http%3A%2F%2Frdf.plos.org%2FRDF%2FarticleType%2FCorrection&exc" +
     "lude=http%3A%2F%2Frdf.plos.org%2FRDF%2FarticleType%2FRetraction&exclude=http%3A%2F%2Frdf.plos.org%2FRDF%" +
     "2FarticleType%2FExpression%2520of%2520Concern";

    int keyLen = CacheParams.HASH_ALGORITHM.bits() / 4;

    String hash1 = CacheParams.createKeyHash(exampleUrl1);
    String hash2 = CacheParams.createKeyHash(exampleUrl2);

    Assert.assertFalse(hash1.contentEquals(exampleUrl1), "Hash key generation failure");
    Assert.assertTrue(hash1.contentEquals(CacheParams.createKeyHash(exampleUrl1)), "Non-deterministic hash key");
    Assert.assertFalse(hash1.contentEquals(hash2), "Hash keys are not unique");
    Assert.assertTrue(Pattern.matches("^[0-9A-F]+$", hash1), "Hash keys are not hex string format");
    Assert.assertTrue(hash1.length() == keyLen, "Incorrect hash length");

    Map<Object, String> expectedHashes = new HashMap<>();
    expectedHashes.put(Hashing.md5(), "25FF3F5D3A44A1C19DD9F54773C4D32D");
    expectedHashes.put(Hashing.sha1(), "4E435D1AB8B9F08B36D1F4A76F0628644452A339");
    expectedHashes.put(Hashing.sha256(), "63B957877D9909518BAA5FB4F6DDC200AB494E0819887AD96CB6A98B2CADA3FA");
    expectedHashes.put(Hashing.sha512(), "B0D827FF43389ECC322022EBB2A447C045313804CECD324560CD9727285A3AAD87A822C23" +
            "F763BAB884DA75F91A0768DAD30D0E6F5E5AA2361A0B3F5AEF757B7");
    if (expectedHashes.keySet().contains(CacheParams.HASH_ALGORITHM)){
      Assert.assertEquals(hash1, expectedHashes.get(CacheParams.HASH_ALGORITHM), "Incorrect hash");
    }
  }
}

@@ -72,16 +72,58 @@
 ashing;%0A
+import com.google.common.io.BaseEncoding;%0A
 import o
@@ -140,16 +140,16 @@
 Assert;%0A
-
 import o
@@ -228,40 +228,8 @@
 Map;
-%0Aimport java.util.regex.Pattern;
 %0A%0Apu
@@ -1294,17 +1294,25 @@
 its() / 
-4
+Byte.SIZE
 ;%0A%0A    S
@@ -1716,104 +1716,243 @@
     
-Assert.assertTrue(Pattern.matches(%22%5E%5B0-9A-F%5D+$%22, hash1), %22Hash keys are not hex string format%22);
+byte%5B%5D hash1Bytes;%0A    try %7B%0A      hash1Bytes = CacheParams.HASH_BASE.decode(hash1);%0A    %7D catch (IllegalArgumentException e) %7B%0A      Assert.fail(%22Hash keys are not in base-encoded format%22);%0A      throw e; // satisfy the compiler%0A    %7D
 %0A   
@@ -1979,17 +1979,20 @@
 ash1
+Bytes
 .length
-()
  == 
@@ -2613,24 +2613,25 @@
 ORITHM))
+ 
 %7B%0A      
 Assert.a
@@ -2626,81 +2626,158 @@
     
-Assert.assertEquals(hash1, expectedHashes.get(CacheParams.HASH_ALGORITHM)
+byte%5B%5D expectedHash = BaseEncoding.base16().decode(expectedHashes.get(CacheParams.HASH_ALGORITHM));%0A      Assert.assertEquals(hash1Bytes, expectedHash
 , %22I

Fix broken unit test for CacheParams