package com.tw.go.plugin.provider;

import com.google.gson.internal.LinkedHashTreeMap;
import org.hamcrest.core.Is;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_END_POINT;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_OAUTH_TOKEN;
import static com.tw.go.plugin.setting.DefaultPluginConfigurationView.PLUGIN_SETTINGS_SERVER_BASE_URL;

public class GitLabProviderTest {
    private GitLabProvider provider;

    @Before
    public void setUp() throws Exception {
        provider = new GitLabProvider();
    }

    @Test
    public void checkIdsMatch () throws Exception {
        assertEquals("gitlab.mr.status", provider.pluginId());
        assertEquals("git.fb", provider.pollerPluginId());
    }

    @Test
    public void shouldGetRepositoryFromURL() {
        assertThat(provider.getRepository("http://github.com/group/sample-repo"), is("group/sample-repo"));
        assertThat(provider.getRepository("http://github.com/group/sample-repo.git"), is("group/sample-repo"));
        assertThat(provider.getRepository("http://github.com/group/sample-repo/"), is("group/sample-repo"));
        assertThat(provider.getRepository("http://github.com/group/sample-repo.git/"), is("group/sample-repo"));
        assertThat(provider.getRepository("https://github.com/group/sample-repo"), is("group/sample-repo"));
        assertThat(provider.getRepository("https://github.com/group/sample-repo.git"), is("group/sample-repo"));
        assertThat(provider.getRepository("git@code.corp.yourcompany.com:group/sample-repo"), is("group/sample-repo"));
        assertThat(provider.getRepository("git@code.corp.yourcompany.com:group/sample-repo.git"), is("group/sample-repo"));
        assertThat(provider.getRepository("git@github.com:group/sample-repo.git"), is("group/sample-repo"));
        assertThat(provider.getRepository("http://github.com/group/sub-group/sample-repo.git/"), is("group/sub-group/sample-repo"));
        assertThat(provider.getRepository("https://github.com/group/sub-group/another-sub-group/sample-repo"), is("group/sub-group/another-sub-group/sample-repo"));
    }

    @Test
    public void checkValidationWithValidValues () throws Exception {
        Map<String, Object> config = new LinkedHashTreeMap<String, Object>();
        List<Map<String, Object>> errors = new ArrayList<Map<String, Object>>();

        Map<String, String> dummyUrl = new LinkedHashTreeMap<String, String>();
        dummyUrl.put("value", "http://localhost:8153");

        Map<String, String> dummyToken = new LinkedHashTreeMap<String, String>();
        dummyToken.put("value", "abcdef");

        config.put(PLUGIN_SETTINGS_SERVER_BASE_URL, dummyUrl);
        config.put(PLUGIN_SETTINGS_END_POINT, dummyUrl);
        config.put(PLUGIN_SETTINGS_OAUTH_TOKEN, dummyToken);

        List<Map<String, Object>> returnedErrors = provider.validateConfig(config);
        assertEquals(errors, returnedErrors);
    }

    @Test
    public void checkValidationWithInvalidValues () throws Exception {
        Map<String, Object> config = new LinkedHashTreeMap<String, Object>();

        Map<String, String> dummyUrl = new LinkedHashTreeMap<String, String>();
        dummyUrl.put("value", "localhost:8153");

        Map<String, String> dummyToken = new LinkedHashTreeMap<String, String>();
        dummyToken.put("value", "");

        config.put(PLUGIN_SETTINGS_SERVER_BASE_URL, dummyUrl);
        config.put(PLUGIN_SETTINGS_END_POINT, dummyUrl);
        config.put(PLUGIN_SETTINGS_OAUTH_TOKEN, dummyToken);

        List<Map<String, Object>> returnedErrors = provider.validateConfig(config);
        assertEquals(3, returnedErrors.size());
    }
}

@@ -33,88 +33,8 @@
 r;%0A%0A
-import com.google.gson.internal.LinkedHashTreeMap;%0Aimport org.hamcrest.core.Is;%0A
 impo
@@ -102,24 +102,56 @@
 .ArrayList;%0A
+import java.util.LinkedHashMap;%0A
 import java.
@@ -2491,38 +2491,20 @@
 nkedHash
-TreeMap%3CString, Object
+Map%3C
 %3E();%0A   
@@ -2557,35 +2557,16 @@
 rayList%3C
-Map%3CString, Object%3E
 %3E();%0A%0A  
@@ -2616,38 +2616,20 @@
 nkedHash
-TreeMap%3CString, String
+Map%3C
 %3E();%0A   
@@ -2737,38 +2737,20 @@
 nkedHash
-TreeMap%3CString, String
+Map%3C
 %3E();%0A   
@@ -3245,30 +3245,12 @@
 Hash
-TreeMap%3CString, Object
+Map%3C
 %3E();
@@ -3304,38 +3304,20 @@
 nkedHash
-TreeMap%3CString, String
+Map%3C
 %3E();%0A   
@@ -3422,30 +3422,12 @@
 Hash
-TreeMap%3CString, String
+Map%3C
 %3E();

Remove unnecessary internal Gson usage