// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.messagebus.protocol.test;

import com.yahoo.component.Version;
import com.yahoo.document.BucketId;
import com.yahoo.document.select.OrderingSpecification;
import com.yahoo.documentapi.messagebus.protocol.CreateVisitorMessage;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.text.Utf8;

import java.util.Map;

import static org.junit.Assert.assertEquals;

public class Messages60TestCase extends Messages52TestCase {

    @Override
    protected Version version() {
        return new Version(6, 197); // TODO finalize version
    }

    @Override
    protected boolean shouldTestCoverage() {
        return true;
    }

    @Override
    protected void registerTests(Map<Integer, MessagesTestBase.RunnableTest> out) {
        super.registerTests(out);

        // This list MUST mirror the list of routable factories from the DocumentProtocol constructor that support
        // version 6.0. When adding tests to this list, please KEEP THEM ORDERED alphabetically like they are now.

        out.put(DocumentProtocol.MESSAGE_CREATEVISITOR, new Messages60TestCase.testCreateVisitorMessage());
    }

    public class testCreateVisitorMessage implements RunnableTest {

        private static final String BUCKET_SPACE = "bjarne";

        // FIXME there is a large amount of code duplication across version tests, presumably
        // to ensure that fields survive across version boundaries, but it makes code very bloated.
        // TODO head towards a blissful Protobuf future instead...
        @Override
        public void run() {
            CreateVisitorMessage msg = new CreateVisitorMessage("SomeLibrary", "myvisitor", "newyork", "london");
            msg.setDocumentSelection("true and false or true");
            msg.getParameters().put("myvar", Utf8.toBytes("somevalue"));
            msg.getParameters().put("anothervar", Utf8.toBytes("34"));
            msg.getBuckets().add(new BucketId(16, 1234));
            msg.setVisitRemoves(true);
            msg.setFieldSet("foo bar");
            msg.setVisitorOrdering(OrderingSpecification.DESCENDING);
            msg.setMaxBucketsPerVisitor(2);
            msg.setBucketSpace(BUCKET_SPACE);
            assertEquals(BASE_MESSAGE_LENGTH + 178 + serializedLength(BUCKET_SPACE), serialize("CreateVisitorMessage", msg));

            for (Language lang : LANGUAGES) {
                msg = (CreateVisitorMessage)deserialize("CreateVisitorMessage", DocumentProtocol.MESSAGE_CREATEVISITOR, lang);
                assertEquals("SomeLibrary", msg.getLibraryName());
                assertEquals("myvisitor", msg.getInstanceId());
                assertEquals("newyork", msg.getControlDestination());
                assertEquals("london", msg.getDataDestination());
                assertEquals("true and false or true", msg.getDocumentSelection());
                assertEquals(8, msg.getMaxPendingReplyCount());
                assertEquals(true, msg.getVisitRemoves());
                assertEquals("foo bar", msg.getFieldSet());
                assertEquals(false, msg.getVisitInconsistentBuckets());
                assertEquals(1, msg.getBuckets().size());
                assertEquals(new BucketId(16, 1234), msg.getBuckets().iterator().next());
                assertEquals("somevalue", Utf8.toString(msg.getParameters().get("myvar")));
                assertEquals("34", Utf8.toString(msg.getParameters().get("anothervar")));
                assertEquals(OrderingSpecification.DESCENDING, msg.getVisitorOrdering());
                assertEquals(2, msg.getMaxBucketsPerVisitor());
                assertEquals(BUCKET_SPACE, msg.getBucketSpace());
            }
        }
    }

    // TODO want to test that non-default bucket space fails to encode with old version

}

@@ -435,16 +435,84 @@
 otocol;%0A
+import com.yahoo.documentapi.messagebus.protocol.StatBucketMessage;%0A
 import c
@@ -3885,24 +3885,975 @@
    %7D%0A    %7D%0A%0A
+    public class testStatBucketMessage implements RunnableTest %7B%0A%0A        private static final String BUCKET_SPACE = %22andrei%22;%0A%0A        @Override%0A        public void run() %7B%0A            StatBucketMessage msg = new StatBucketMessage(new BucketId(16, 123), %22id.user=123%22);%0A            msg.setLoadType(null);%0A            msg.setBucketSpace(BUCKET_SPACE);%0A            assertEquals(BASE_MESSAGE_LENGTH + 27 + serializedLength(BUCKET_SPACE), serialize(%22StatBucketMessage%22, msg));%0A%0A            for (Language lang : LANGUAGES) %7B%0A                msg = (StatBucketMessage)deserialize(%22StatBucketMessage%22, DocumentProtocol.MESSAGE_STATBUCKET, lang);%0A                assertEquals(new BucketId(16, 123), msg.getBucketId());%0A                assertEquals(%22id.user=123%22, msg.getDocumentSelection());%0A                assertEquals(%22default%22, msg.getLoadType().getName());%0A                assertEquals(BUCKET_SPACE, msg.getBucketSpace());%0A            %7D%0A        %7D%0A    %7D%0A%0A
     // TODO 

Add tests for StatBucketMessage