/*
 * 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 io.spine.gradle.compiler.lookup.valrule;

import io.spine.gradle.compiler.GradleProject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.List;
import java.util.Properties;

import static io.spine.gradle.TaskName.FIND_VALIDATION_RULES;
import static io.spine.gradle.compiler.Extension.getDefaultMainGenResDir;
import static io.spine.gradle.compiler.lookup.valrule.ValidationRulesFinder.PROTO_TYPE_SEPARATOR;
import static io.spine.util.Exceptions.illegalStateWithCauseOf;
import static org.junit.Assert.assertEquals;

/**
 * @author Dmytro Grankin
 */
public class ValidationRulesLookupPluginShould {

    private static final String PROJECT_NAME = "validation-rules-lookup-plugin-test";

    private static final String PROTO_FILE_PACKAGE = "test.valrule";
    private static final String OUTER_MESSAGE_TYPE = "Outer";
    private static final String VALIDATION_RULE_TYPE = "ValidationRule";
    private static final String VALIDATION_TARGET = PROTO_FILE_PACKAGE + PROTO_TYPE_SEPARATOR +
            OUTER_MESSAGE_TYPE + PROTO_TYPE_SEPARATOR + "field_name";
    private static final List<String> NESTED_VALIDATION_RULE_PROTO =
            Arrays.asList("syntax = \"proto3\";",
                          "package " + PROTO_FILE_PACKAGE + ';',
                          "import \"spine/options.proto\";",

                          "message " + OUTER_MESSAGE_TYPE + " {",

                          "message " + VALIDATION_RULE_TYPE + " {",
                          "option (validation_of) = \"" + VALIDATION_TARGET + "\";",
                          "}",

                          "}"
            );

    @Rule
    public final TemporaryFolder testProjectDir = new TemporaryFolder();

    @Test
    public void findNestedValidationRules() throws Exception {
        final String file = "nested_validation_rule.proto";
        final GradleProject project = newGradleProject(file, NESTED_VALIDATION_RULE_PROTO);
        project.executeTask(FIND_VALIDATION_RULES);

        final String expectedKey = PROTO_FILE_PACKAGE + PROTO_TYPE_SEPARATOR +
                OUTER_MESSAGE_TYPE + PROTO_TYPE_SEPARATOR + VALIDATION_RULE_TYPE;
        final String value = (String) getProperties().get(expectedKey);
        assertEquals(value, VALIDATION_TARGET);
    }

    private Dictionary getProperties() {
        final String projectPath = testProjectDir.getRoot()
                                                 .getAbsolutePath();
        final Path path = Paths.get(projectPath, getDefaultMainGenResDir(),
                                    ValidationRulesLookupPlugin.VALIDATION_PROPS_FILE_NAME);
        try {
            final InputStream inputStream = new FileInputStream(path.toFile());
            final Properties properties = new Properties();
            properties.load(inputStream);
            return properties;
        } catch (IOException e) {
            throw illegalStateWithCauseOf(e);
        }
    }

    private GradleProject newGradleProject(String protoFileName, List<String> protoFileLines) {
        return GradleProject.newBuilder()
                            .setProjectName(PROJECT_NAME)
                            .setProjectFolder(testProjectDir)
                            .createProto(protoFileName, protoFileLines)
                            .build();
    }
}

@@ -2444,24 +2444,25 @@
 proto3%5C%22;%22,%0A
+%0A
             
@@ -2514,16 +2514,17 @@
  + ';',%0A
+%0A
         
@@ -2662,32 +2662,38 @@
                 
+      
 %22message %22 + VAL
@@ -2718,16 +2718,26 @@
 + %22 %7B%22,%0A
+          
         
@@ -2843,14 +2843,19 @@
     
+      
 %22%7D%22,%0A
-%0A
     
@@ -3154,29 +3154,31 @@
 ct = new
-Gradle
 Project
+WithFile
 (file, N
@@ -4240,29 +4240,31 @@
 ject new
-Gradle
 Project
+WithFile
 (String 

Fix formatting of test data in `ValidationRulesLookupPluginShould`