/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * 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
 *
 * 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 io.spine.tools.protoc;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.function.Predicate;

import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayName("GeneratedMethods should")
final class MethodsTest {

    @DisplayName("convert to proper Protoc configuration")
    @Test
    void convertToProperProtocConfiguration() {
        String testMethodFactory = "io.spine.test.MethodFactory";
        Methods methods = new Methods();
        MessageSelectorFactory messages = methods.messages();
        methods.applyFactory(testMethodFactory, messages.uuid());
        methods.applyFactory(testMethodFactory, messages.inFiles(
                MessageSelectorFactory.suffix("_test.proto")));
        AddMethods config = methods.asProtocConfig();

        Assertions.assertEquals(testMethodFactory, config.getUuidFactory()
                                                         .getValue());
        Assertions.assertEquals(testMethodFactory, config.getFactoryByPattern(0)
                                                         .getValue());
    }

    @DisplayName("add multiple file patterns")
    @Test
    void addMultipleFilePatterns() {
        String pattern = "testPattern";
        String interfaceName = "io.spine.test.TestInterface";

        Methods defaults = new Methods();
        MessageSelectorFactory messages = defaults.messages();
        defaults.applyFactory(interfaceName, messages.inFiles(MessageSelectorFactory.suffix(pattern)));
        defaults.applyFactory(interfaceName, messages.inFiles(MessageSelectorFactory.prefix(pattern)));
        defaults.applyFactory(interfaceName, messages.inFiles(MessageSelectorFactory.regex(pattern)));

        assertTrue(hasSuffixConfig(pattern, interfaceName, defaults.asProtocConfig()));
        assertTrue(hasPrefixConfig(pattern, interfaceName, defaults.asProtocConfig()));
        assertTrue(hasRegexConfig(pattern, interfaceName, defaults.asProtocConfig()));
    }

    private static boolean
    hasSuffixConfig(String suffix, String factoryName, AddMethods config) {
        return hasConfig(config, factoryName, pattern -> suffix.equals(pattern.getSuffix()));
    }

    private static boolean
    hasPrefixConfig(String prefix, String factoryName, AddMethods config) {
        return hasConfig(config, factoryName, pattern -> prefix.equals(pattern.getPrefix()));
    }

    private static boolean
    hasRegexConfig(String regex, String factoryName, AddMethods config) {
        return hasConfig(config, factoryName, pattern -> regex.equals(pattern.getRegex()));
    }

    private static boolean hasConfig(AddMethods config,
                                     String factoryName,
                                     Predicate<? super FilePattern> patternPredicate) {
        return config
                .getFactoryByPatternList()
                .stream()
                .filter(byPattern -> factoryName.equals(byPattern.getValue()))
                .map(ConfigByPattern::getPattern)
                .anyMatch(patternPredicate);
    }
}

@@ -1272,49 +1272,8 @@
 c;%0A%0A
-import org.junit.jupiter.api.Assertions;%0A
 impo
@@ -1384,16 +1384,272 @@
 icate;%0A%0A
+import static com.google.common.truth.Truth.assertThat;%0Aimport static io.spine.tools.protoc.MessageSelectorFactory.prefix;%0Aimport static io.spine.tools.protoc.MessageSelectorFactory.regex;%0Aimport static io.spine.tools.protoc.MessageSelectorFactory.suffix;%0A
 import s
@@ -2188,48 +2188,8 @@
 les(
-%0A                MessageSelectorFactory.
 suff
@@ -2276,132 +2276,42 @@
     
-Assertions.assertEquals(testMethodFactory, config.getUuidFactory()%0A                                                         
+assertThat(config.getUuidFactory()
 .get
@@ -2314,25 +2314,24 @@
 .getValue())
-;
 %0A        Ass
@@ -2331,31 +2331,26 @@
     
-Assertions.assert
+        .is
 Equal
-s
+To
 (tes
@@ -2363,18 +2363,38 @@
 dFactory
-, 
+);%0A        assertThat(
 config.g
@@ -2418,77 +2418,65 @@
 n(0)
-%0A                                                         .getValue()
+.getValue())%0A                .isEqualTo(testMethodFactory
 );%0A 
@@ -2845,39 +2845,16 @@
 inFiles(
-MessageSelectorFactory.
 suffix(p
@@ -2926,39 +2926,16 @@
 inFiles(
-MessageSelectorFactory.
 prefix(p
@@ -3007,39 +3007,16 @@
 inFiles(
-MessageSelectorFactory.
 regex(pa

Use static import