package org.roklib.urifragmentrouting.mapper;

import org.junit.Before;
import org.junit.Test;
import org.roklib.urifragmentrouting.UriActionCommand;
import org.roklib.urifragmentrouting.parameter.ParameterMode;
import org.roklib.urifragmentrouting.parameter.converter.AbstractRegexToStringListParameterValueConverter;
import org.roklib.urifragmentrouting.parameter.value.CapturedParameterValues;
import org.roklib.urifragmentrouting.parameter.value.ParameterValue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.junit.Assert.assertThat;

public class RegexUriPathSegmentActionMapperTest {

    private RegexUriPathSegmentActionMapper mapper;

    @Before
    public void setUp() {
        mapper = new RegexUriPathSegmentActionMapper("regexMapper",  "values", new AbstractRegexToStringListParameterValueConverter("(\\d+)xxx(\\d+)") {
            @Override
            public String convertToString(List<String> value) {
                return null;
            }
        });
    }

    @Test
    public void capturing_groups_go_into_uri_parameter() {
        CapturedParameterValues capturedParameterValues = new CapturedParameterValues();
        mapper.interpretTokensImpl(capturedParameterValues,
                "123xxx456",
                Collections.emptyList(),
                Collections.emptyMap(),
                ParameterMode.DIRECTORY);

        assertThat(capturedParameterValues.hasValueFor("regexMapper", "values"), is(true));
        final ParameterValue<List<String>> values = capturedParameterValues.getValueFor("regexMapper", "values");
        assertThat(values.getValue(), hasItems("123", "456"));
        assertThat(values.getValue(), hasSize(2));
    }

    @Test
    public void test_dispatches_to_sub_mapper() {
        mapper.addSubMapper(new SimpleUriPathSegmentActionMapper("aaa"));
        mapper.addSubMapper(new SimpleUriPathSegmentActionMapper("bbb"));
        mapper.setActionCommandClass(BBBActionCommand.class);
        final Class<? extends UriActionCommand> resultClass = mapper.interpretTokensImpl(new CapturedParameterValues(),
                "123xxx456",
                new ArrayList<>(Collections.singletonList("bbb")),
                Collections.emptyMap(),
                ParameterMode.DIRECTORY);

        assertThat(resultClass, is(equalTo(BBBActionCommand.class)));
    }

    @Test
    public void testIsResponsibleFor() {
        assertThat(mapper.isResponsibleForToken("123xxx456"), is(true));
        assertThat(mapper.isResponsibleForToken("123456"), is(false));
    }

    public static class BBBActionCommand implements UriActionCommand {
        @Override
        public void run() {
        }
    }
}

@@ -1013,17 +1013,16 @@
 Mapper%22,
- 
  %22values
@@ -1023,16 +1023,32 @@
 values%22,
+%0A               
  new Abs
@@ -1118,32 +1118,40 @@
 ) %7B%0A            
+        
 @Override%0A      
@@ -1152,24 +1152,32 @@
             
+    
+    
 public Strin
@@ -1232,16 +1232,24 @@
         
+        
 return n
@@ -1261,26 +1261,42 @@
             
+    
-%7D%0A
+    %7D%0A        
         %7D);%0A
@@ -2208,49 +2208,17 @@
 bbb%22
-));%0A        mapper.setActionCommandClass(
+, %22bbb%22, 
 BBBA
@@ -2236,16 +2236,17 @@
 d.class)
+)
 ;%0A      
@@ -2822,21 +2822,22 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 

Fix unit test