package com.indeed.proctor.consumer;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.indeed.proctor.common.ProctorResult;
import com.indeed.proctor.common.model.Allocation;
import com.indeed.proctor.common.model.Audit;
import com.indeed.proctor.common.model.ConsumableTestDefinition;
import com.indeed.proctor.common.model.TestBucket;
import com.indeed.proctor.common.model.TestType;

import java.util.Collections;
import java.util.Map;

/**
 * @author matts
 */
public class CannedProctorResults {
    public static ProctorResult of(
            Bucket<?>... results
    ) {
        Map<String, TestBucket> buckets = Maps.newHashMapWithExpectedSize(results.length);
        Map<String, ConsumableTestDefinition> testVersions = Collections.emptyMap();

        for (final Bucket<?> result: results) {
            final String testName = result.getClass().getSimpleName().toLowerCase();
            final int testValue = result.getValue();

            TestBucket testBucket = new TestBucket(testName, testValue, testName);

            Preconditions.checkState(!buckets.containsKey(testName), "Attempted to provide two values for dummy test bucket %s", testName);
            buckets.put(testName, testBucket);
        }

        return (new ProctorResult(Audit.EMPTY_VERSION, buckets, testVersions));
    }

    public static ProctorResult of(
            CannedProctorResult<?>... results
    ) {
        Map<String, TestBucket> buckets = Maps.newHashMapWithExpectedSize(results.length);
        Map<String, ConsumableTestDefinition> testVersions = Maps.newHashMap();

        for (final CannedProctorResult<?> result: results) {
            final String testName = result.testVal.getClass().getSimpleName().toLowerCase();
            final int testValue = result.testVal.getValue();

            TestBucket testBucket = new TestBucket(testName, testValue, testName);
            if (result.payload != null) {
                testBucket.setPayload(result.payload);
            }

            Preconditions.checkState(!buckets.containsKey(testName), "Attempted to provide two values for dummy test bucket %s", testName);
            buckets.put(testName, testBucket);

            // add a minimal test definition for each test
            ConsumableTestDefinition testDefinition = testVersions.get(testName);
            if (null == testDefinition) {
                testDefinition = new ConsumableTestDefinition(
                        "",
                        null, // no rule
                        TestType.RANDOM,
                        null, // no salt
                        Lists.<TestBucket>newArrayList(),
                        Lists.<Allocation>newArrayListWithCapacity(1),
                        Collections.<String, Object>emptyMap(),
                        null);

                testVersions.put(testName, testDefinition);
            }

            testDefinition.getBuckets().add(testBucket);
        }

        return (new ProctorResult(Audit.EMPTY_VERSION, buckets, testVersions));
    }
}

@@ -813,25 +813,20 @@
 s = 
-Collections.empty
+Maps.newHash
 Map(
@@ -1279,32 +1279,187 @@
 e, testBucket);%0A
+            ConsumableTestDefinition testDefinition = addTestDefinition(testName, testVersions);%0A%0A            testDefinition.getBuckets().add(testBucket);%0A
         %7D%0A%0A     
@@ -2514,16 +2514,466 @@
 ition = 
+addTestDefinition(testName, testVersions);%0A%0A            testDefinition.getBuckets().add(testBucket);%0A        %7D%0A%0A        return (new ProctorResult(Audit.EMPTY_VERSION, buckets, testVersions));%0A    %7D%0A%0A%0A    private static ConsumableTestDefinition addTestDefinition(final String testName, final Map%3CString, ConsumableTestDefinition%3E testVersions) %7B%0A        // add a minimal test definition for each test%0A        ConsumableTestDefinition testDefinition = 
 testVers
@@ -2984,36 +2984,32 @@
 .get(testName);%0A
-    
         if (null
@@ -3026,28 +3026,24 @@
 finition) %7B%0A
-    
             
@@ -3089,20 +3089,16 @@
 nition(%0A
-    
         
@@ -3125,36 +3125,32 @@
                 
-    
 null, // no rule
@@ -3150,20 +3150,16 @@
 no rule%0A
-    
         
@@ -3199,36 +3199,32 @@
                 
- 
-   
 null, // no salt
@@ -3224,20 +3224,16 @@
 no salt%0A
-    
         
@@ -3294,28 +3294,24 @@
             
-   
- 
 Lists.%3CAlloc
@@ -3345,20 +3345,16 @@
 ity(1),%0A
-    
         
@@ -3421,28 +3421,24 @@
             
-    
 null);%0A%0A    
@@ -3441,28 +3441,24 @@
             
- 
-   
 testVersions
@@ -3501,161 +3501,39 @@
     
-    %7D%0A%0A            testDefinition.getBuckets().add(testBucket);%0A        %7D%0A%0A        return (new ProctorResult(Audit.EMPTY_VERSION, buckets, testVersions))
+%7D%0A        return testDefinition
 ;%0A  

add test definition for both overloaded test methods