/*
 * 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.tools.protoc;

import com.google.common.testing.NullPointerTester;
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorRequest;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse;
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse.File;
import com.google.protobuf.compiler.PluginProtos.Version;
import org.junit.Test;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.spine.tools.protoc.CodeGenerator.INSERTION_POINT_IMPLEMENTS;
import static java.lang.String.format;
import static java.util.regex.Pattern.compile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * @author Dmytro Dashenkov
 */
public class CodeGeneratorShould {

    private static final String PACKAGE_PATH = CodeGeneratorShould.class.getPackage()
                                                                        .getName()
                                                                        .replace('.', '/');
    private static final Pattern CUSTOMER_EVENT_INTERFACE_PATTERN =
            compile("^\\s*io\\.spine\\.tools\\.protoc\\.CustomerEvent\\s*,\\s*$");

    @Test
    public void not_accept_nulls() {
        new NullPointerTester()
                .setDefault(CodeGeneratorRequest.class, CodeGeneratorRequest.getDefaultInstance())
                .testAllPublicStaticMethods(CodeGenerator.class);
    }

    @Test
    public void generate_insertion_point_contents_for_EveryIs_option() {
        // Sample path; never resolved
        final String filePath = "./proto/spine/tools/protoc/every_is_test.proto";

        final FileDescriptorProto descriptor = EveryIsTestProto.getDescriptor().toProto();
        final CodeGeneratorRequest request = CodeGeneratorRequest.newBuilder()
                                                                 .setCompilerVersion(version())
                                                                 .addFileToGenerate(filePath)
                                                                 .addProtoFile(descriptor)
                                                                 .build();
        final CodeGeneratorResponse response = CodeGenerator.generate(request);
        assertNotNull(response);
        final List<File> files = response.getFileList();
        assertEquals(2, files.size());
        for (File file : files) {
            final String name = file.getName();
            assertTrue(name.startsWith(PACKAGE_PATH));

            final String insertionPoint = file.getInsertionPoint();
            final String messageName = file.getName().replace('/', '.');
            assertEquals(insertionPoint, format(INSERTION_POINT_IMPLEMENTS, messageName));

            final String content = file.getContent();
            final Matcher matcher = CUSTOMER_EVENT_INTERFACE_PATTERN.matcher(content);
            assertTrue(format("Unexpected inserted content: %s", content), matcher.matches());
        }
    }

    @Test(expected = IllegalArgumentException.class)
    public void no_accept_requests_from_old_compiler() {
        final Version version = Version.newBuilder()
                                       .setMajor(2)
                                       .build();
        final CodeGeneratorRequest request = CodeGeneratorRequest.newBuilder()
                                                                 .setCompilerVersion(version)
                                                                 .build();
        CodeGenerator.generate(request);
    }

    private static Version version() {
        return Version.newBuilder()
                      .setMajor(3)
                      .setMinor(3)
                      .setPatch(0)
                      .build();
    }
}

@@ -4219,16 +4219,17 @@
  void no
+t
 _accept_
@@ -4405,32 +4405,757 @@
       .build();%0A
+        final FileDescriptorProto stubFile = FileDescriptorProto.getDefaultInstance();%0A        final CodeGeneratorRequest request = CodeGeneratorRequest.newBuilder()%0A                                                                 .setCompilerVersion(version)%0A                                                                 .addProtoFile(stubFile)%0A                                                                 .build();%0A        CodeGenerator.generate(request);%0A    %7D%0A%0A    @Test(expected = IllegalArgumentException.class)%0A    public void not_accept_empty_requests() %7B%0A        final Version version = Version.newBuilder()%0A                                       .setMajor(3)%0A                                       .build();%0A
         final Co

Add one more negative test.