/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.addthis.hydra.task.map;

import java.io.IOException;

import com.addthis.bundle.core.Bundle;
import com.addthis.bundle.core.list.ListBundle;
import com.addthis.bundle.value.ValueFactory;
import com.addthis.bundle.value.ValueTranslationException;
import com.addthis.codec.config.Configs;

import com.google.common.collect.ImmutableMap;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class RepeatBuilderTest {

    StreamEmitter emitter;
    Bundle bundle;
    RepeatBuilder builder;

    private static final String repeatField = "reps";

    @Before
    public void setup() {
        emitter = mock(StreamEmitter.class);
        bundle = new ListBundle();
    }

    @Test
    public void basicFunctionality() throws IOException {
        builder = Configs.decodeObject(RepeatBuilder.class, String.format("{repeatField:\"%s\"}", repeatField));
        int reps = 5;
        Bundle bundle = makeBundle(reps);
        builder.process(bundle, emitter);
        ArgumentCaptor<Bundle> bundleCapture = ArgumentCaptor.forClass(Bundle.class);
        verify(emitter, times(reps)).emit(bundleCapture.capture());
    }

    @Test
    public void defaultRepeatCount() throws IOException {
        int defaultRepeatCount = 2;
        builder = Configs.decodeObject(RepeatBuilder.class, String.format("{repeatField:\"%s\", defaultRepeats:%d}", repeatField, defaultRepeatCount));
        Bundle bundle = new ListBundle();
        builder.process(bundle, emitter);
        ArgumentCaptor<Bundle> bundleCapture = ArgumentCaptor.forClass(Bundle.class);
        verify(emitter, times(defaultRepeatCount)).emit(bundleCapture.capture());
    }

    @Test(expected = ValueTranslationException.class)
    public void escalateOnParseFailure() throws IOException {
        builder = Configs.decodeObject(RepeatBuilder.class, String.format("{repeatField:\"%s\", failOnParseException: true}", repeatField));
        Bundle bundle = new ListBundle(ImmutableMap.of(repeatField, ValueFactory.create("zzzzz")));
        builder.process(bundle, emitter);
    }

    private static Bundle makeBundle(int reps) {
        return new ListBundle(ImmutableMap.of(repeatField, ValueFactory.create(reps)));
    }
}

@@ -648,32 +648,72 @@
 le.core.Bundle;%0A
+import com.addthis.bundle.core.Bundles;%0A
 import com.addth
@@ -1023,16 +1023,77 @@
 aptor;%0A%0A
+import static com.addthis.codec.config.Configs.decodeObject;%0A
 import s
@@ -1272,109 +1272,8 @@
 ter;
-%0A    Bundle bundle;%0A    RepeatBuilder builder;%0A%0A    private static final String repeatField = %22reps%22;
 %0A%0A  
@@ -1357,43 +1357,8 @@
 s);%0A
-        bundle = new ListBundle();%0A
     
@@ -1428,32 +1428,68 @@
 ption %7B%0A        
+int reps = 5;%0A        RepeatBuilder 
 builder = Config
@@ -1478,32 +1478,24 @@
 r builder = 
-Configs.
 decodeObject
@@ -1520,81 +1520,28 @@
 ss, 
-String.format(%22%7BrepeatField:%5C%22%25s%5C%22%7D%22, repeatField));%0A        int reps = 5
+%22repeatField: reps%22)
 ;%0A  
@@ -1562,27 +1562,42 @@
 undle = 
-make
 Bundle
-(
+s.decode(%22reps: %22 + 
 reps);%0A 
@@ -1641,94 +1641,8 @@
 r);%0A
-        ArgumentCaptor%3CBundle%3E bundleCapture = ArgumentCaptor.forClass(Bundle.class);%0A
     
@@ -1685,33 +1685,16 @@
 t(bundle
-Capture.capture()
 );%0A    %7D
@@ -1799,32 +1799,46 @@
 nt = 2;%0A        
+RepeatBuilder 
 builder = Config
@@ -1827,32 +1827,24 @@
 r builder = 
-Configs.
 decodeObject
@@ -1864,33 +1864,63 @@
 r.class,
- String.format(%22%7B
+%0A                                             %22
 repeatFi
@@ -1923,22 +1923,21 @@
 atField:
-%5C%22%25s%5C%22
+ reps
 , defaul
@@ -1949,26 +1949,12 @@
 ats:
-%25d%7D%22, repeatField,
+ %22 +
  def
@@ -1969,17 +1969,16 @@
 atCount)
-)
 ;%0A      
@@ -2059,94 +2059,8 @@
 r);%0A
-        ArgumentCaptor%3CBundle%3E bundleCapture = ArgumentCaptor.forClass(Bundle.class);%0A
     
@@ -2121,25 +2121,8 @@
 ndle
-Capture.capture()
 );%0A 
@@ -2247,24 +2247,38 @@
 n %7B%0A        
+RepeatBuilder 
 builder = Co
@@ -2275,24 +2275,16 @@
 ilder = 
-Configs.
 decodeOb
@@ -2313,24 +2313,9 @@
 ss, 
-String.format(%22%7B
+%22
 repe
@@ -2326,14 +2326,13 @@
 eld:
-%5C%22%25s%5C%22
+ reps
 , fa
@@ -2359,24 +2359,9 @@
 true
-%7D%22, repeatField)
+%22
 );%0A 
@@ -2387,82 +2387,38 @@
 e = 
-new ListBundle(ImmutableMap.of(repeatField, ValueFactory.create(%22
+Bundles.decode(%22reps: z
 zzzzz%22)
-))
 ;%0A  
@@ -2466,151 +2466,7 @@
    %7D
-%0A%0A    private static Bundle makeBundle(int reps) %7B%0A        return new ListBundle(ImmutableMap.of(repeatField, ValueFactory.create(reps)));%0A    %7D
 %0A%7D%0A

simplify RepeatBuilder tests