/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.spine3.base;

import com.google.common.escape.Escaper;
import com.google.common.escape.Escapers;
import com.google.protobuf.Timestamp;
import com.google.protobuf.util.Timestamps;
import org.junit.Test;
import org.spine3.test.types.Task;

import java.text.ParseException;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.spine3.base.Stringifiers.mapStringifier;

/**
 * @author Illia Shepilov
 */
public class MapStringifierShould {

    @Test
    public void convert_string_to_map() throws ParseException {
        final String rawMap = "\"1\":\"1972-01-01T10\\:00\\:20.021-05\\:00\"," +
                              "\"2\":\"1972-01-01T10\\:00\\:20.021-05\\:00\"";
        final Stringifier<Map<Long, Timestamp>> stringifier =
                mapStringifier(Long.class, Timestamp.class);
        final Map<Long, Timestamp> actualMap = stringifier.fromString(rawMap);
        final Map<Long, Timestamp> expectedMap = newHashMap();

        final String timeToParse = "1972-01-01T10:00:20.021-05:00";
        expectedMap.put(1L, Timestamps.parse(timeToParse));
        expectedMap.put(2L, Timestamps.parse(timeToParse));
        assertThat(actualMap, is(expectedMap));
    }

    @Test
    public void convert_map_to_string() {
        final Map<String, Integer> mapToConvert = createTestMap();
        final Stringifier<Map<String, Integer>> stringifier =
                mapStringifier(String.class, Integer.class);
        final String convertedMap = stringifier.toString(mapToConvert);
        assertEquals(convertMapToString(mapToConvert), convertedMap);
    }

    @Test(expected = IllegalArgumentException.class)
    public void throw_exception_when_passed_parameter_does_not_match_expected_format() {
        final String incorrectRawMap = "\"first\":\"1\",\"second\":\"2\"";
        final Stringifier<Map<Integer, Integer>> stringifier =
                mapStringifier(Integer.class, Integer.class);
        stringifier.fromString(incorrectRawMap);
    }

    @Test(expected = IllegalArgumentException.class)
    public void throw_exception_when_occurred_exception_during_conversion() {
        final Stringifier<Map<Task, Long>> stringifier = mapStringifier(Task.class, Long.class);
        stringifier.fromString("first\":\"first\":\"first\"");
    }

    @Test(expected = IllegalArgumentException.class)
    public void throw_exception_when_key_value_delimiter_is_wrong() {
        final Stringifier<Map<Long, Long>> stringifier = mapStringifier(Long.class, Long.class);
        stringifier.fromString("1\\-1");
    }

    @Test
    public void convert_map_with_custom_delimiter() {
        final String rawMap = "\"first\":\"1\"|\"second\":\"2\"|\"third\":\"3\"";
        final Stringifier<Map<String, Integer>> stringifier =
                mapStringifier(String.class, Integer.class, '|');
        final Map<String, Integer> convertedMap = stringifier.fromString(rawMap);
        assertThat(convertedMap, is(createTestMap()));
    }

    @Test
    public void convert_from_map_to_string_and_backward() {
        final Map<String, Integer> mapToConvert = createTestMap();
        final Stringifier<Map<String, Integer>> stringifier =
                mapStringifier(String.class, Integer.class);
        final String convertedString = stringifier.toString(mapToConvert);
        final Map<String, Integer> actualMap = stringifier.fromString(convertedString);
        assertEquals(mapToConvert, actualMap);
    }

    @Test
    public void convert_string_which_contains_delimiter_in_content_to_map(){
        final String stringToConvert = "\"1\\\"\\\"\":\"one\\,\",\"2\\:\":\"two\"";
        final  Stringifier<Map<String, String>> stringifier = mapStringifier(String.class,
                                                                             String.class);
        final Map<String, String> actualMap = stringifier.fromString(stringToConvert);

        assertEquals(actualMap.get("1\"\""), "one,");
        assertEquals(actualMap.get("2:"), "two");
    }

    @Test(expected = IllegalArgumentException.class)
    public void throw_exception_when_key_value_are_incorrect(){
        final String stringToConvert = "1:2";
        final  Stringifier<Map<String, String>> stringifier = mapStringifier(String.class,
                                                                             String.class);
        stringifier.fromString(stringToConvert);
    }

    private static String convertMapToString(Map<String, Integer> mapToConvert) {
        final Escaper escaper = Escapers.builder()
                                        .addEscape(' ', "")
                                        .addEscape('{', "\"")
                                        .addEscape('}', "\"")
                                        .addEscape('=', "\":\"")
                                        .addEscape(',', "\",\"")
                                        .build();
        return escaper.escape(mapToConvert.toString());
    }

    private static Map<String, Integer> createTestMap() {
        final Map<String, Integer> expectedMap = newHashMap();
        expectedMap.put("first", 1);
        expectedMap.put("second", 2);
        expectedMap.put("third", 3);
        return expectedMap;
    }
}

@@ -5290,26 +5290,456 @@
 key_
-value_are_incorrec
+is_quoted_but_value_not()%7B%0A            final String stringToConvert = %221:%5C%222%5C%22%22;%0A            final  Stringifier%3CMap%3CString, String%3E%3E stringifier = mapStringifier(String.class,%0A                                                                                 String.class);%0A            stringifier.fromString(stringToConvert);%0A    %7D%0A%0A    @Test(expected = IllegalArgumentException.class)%0A    public void throw_exception_when_value_is_quoted_but_key_no
 t()%7B
@@ -5779,17 +5779,21 @@
 vert = %22
-1
+%5C%221%5C%22
 :2%22;%0A   

Add test for the `MapStringifier` class.