/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.camel.idea.util;

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

import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;

public class StringUtilsTest {

    @Test
    public void testHasQuestionMark() {
        assertTrue(StringUtils.hasQuestionMark("seda:foo?size=123"));
        assertFalse(StringUtils.hasQuestionMark("seda:foo"));
        assertFalse(StringUtils.hasQuestionMark(""));
        assertFalse(StringUtils.hasQuestionMark(null));
    }

    @Test
    public void testAsComponentName() {
        assertEquals("seda", StringUtils.asComponentName("seda:foo?size=123"));
        assertEquals("seda", StringUtils.asComponentName("seda:foo"));
        assertEquals(null, StringUtils.asComponentName("seda"));
    }

    @Test
    public void testAsLanguageName() {
        assertEquals("simple", StringUtils.asLanguageName("simple"));
        assertEquals("header", StringUtils.asLanguageName("header"));
        assertEquals("tokenize", StringUtils.asLanguageName("tokenize"));
        assertEquals("tokenize", StringUtils.asLanguageName("tokenizeXml"));
        assertEquals("javaScript", StringUtils.asLanguageName("js"));
        assertEquals("javaScript", StringUtils.asLanguageName("javascript"));
    }

    @Test
    public void testGetSafeValue() {
        Map<String, String> row = new HashMap<>();
        row.put("foo", "123");

        assertEquals("123", StringUtils.getSafeValue("foo", row));
        assertEquals("", StringUtils.getSafeValue("bar", row));

        Map<String, String> row2 = new HashMap<>();
        row2.put("bar", "true");

        List<Map<String, String>> rows = new ArrayList<>();
        rows.add(row);
        rows.add(row2);

        assertEquals("123", StringUtils.getSafeValue("foo", rows));
        assertEquals("true", StringUtils.getSafeValue("bar", rows));
        assertEquals("", StringUtils.getSafeValue("baz", rows));
    }

    @Test
    public void testWrapSeparator() {
        String url = "seda:foo?size=1234";

        assertEquals(url, StringUtils.wrapSeparator(url, "&", "\n", 80));

        String longUrl = "jms:queue:cheese?acknowledgementModeName=SESSION_TRANSACTED&asyncConsumer=true&cacheLevelName=CACHE_CONSUMER"
                + "&deliveryMode=2&errorHandlerLoggingLevel=DEBUG&explicitQosEnabled=true&jmsMessageType=Bytes";

        String wrapped = StringUtils.wrapSeparator(longUrl, "&", "\n", 120);

        String line1 = "jms:queue:cheese?acknowledgementModeName=SESSION_TRANSACTED&asyncConsumer=true&cacheLevelName=CACHE_CONSUMER&deliveryMode=2";
        String line2 = "&errorHandlerLoggingLevel=DEBUG&explicitQosEnabled=true&jmsMessageType=Bytes";

        String[] parts = wrapped.split("\n");
        assertEquals(2, parts.length);
        assertEquals(line1, parts[0]);
        assertEquals(line2, parts[1]);
    }

    @Test
    public void testWrapWords() {
        String words = "Plugin for Intellij IDEA to provide a set of small Camel related capabilities to IDEA editor."
            + " When the plugin becomes more complete and stable then the intention is to donate the source code"
            + " to Apache Software Foundation to be included out of the box at Apache Camel.";

        String wrapped = StringUtils.wrapWords(words, "\n", 80, true);

        String[] parts = wrapped.split("\n");
        assertEquals(4, parts.length);

        assertTrue(parts[0].length() <= 80);
        assertTrue(parts[1].length() <= 80);
        assertTrue(parts[2].length() <= 80);
        assertTrue(parts[3].length() <= 80);

        assertTrue(parts[0].startsWith("Plugin for Intellij"));
        assertTrue(parts[1].startsWith("IDEA editor"));
        assertTrue(parts[2].startsWith("is to donate"));
        assertTrue(parts[3].startsWith("the box at Apache Camel."));
    }

    @Test
    public void isEmpty() {
        assertTrue(StringUtils.isEmpty(null));
        assertTrue(StringUtils.isEmpty(""));
    }

    @Test
    public void isNotEmpty() {
        assertTrue(StringUtils.isNotEmpty("test"));
    }
}

@@ -993,107 +993,9 @@
 ert.
-assertTrue;%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertEquals
+*
 ;%0A%0Ap
@@ -1055,13 +1055,9 @@
 oid 
-testH
+h
 asQu
@@ -1346,21 +1346,17 @@
 ic void 
-testA
+a
 sCompone
@@ -1578,24 +1578,79 @@
 e(%22seda%22));%0A
+        assertNull(StringUtils.asComponentName(null));%0A
     %7D%0A%0A    @
@@ -1674,13 +1674,9 @@
 oid 
-testA
+a
 sLan
@@ -2124,24 +2124,78 @@
 ascript%22));%0A
+        assertNull(StringUtils.asLanguageName(null));%0A
     %7D%0A%0A    @
@@ -2219,13 +2219,9 @@
 oid 
-testG
+g
 etSa
@@ -2876,21 +2876,17 @@
 ic void 
-testW
+w
 rapSepar
@@ -3795,13 +3795,9 @@
 oid 
-testW
+w
 rapW
@@ -3801,24 +3801,90 @@
 apWords() %7B%0A
+        assertNull(StringUtils.wrapWords(null, %22%5Cn%22, 80, true));%0A%0A
         Stri

add tests for null parameters