/*
 * 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.
 */

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Org.Apache.Rocketmq;

namespace tests
{
    [TestClass]
    public class PushConsumerBuilderTest
    {
        [TestMethod]
        public void TestSetClientConfigWithNull()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetClientConfig(null));
        }

        [TestMethod]
        public void TestSetConsumerGroupWithNull()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetConsumerGroup(null));
        }

        [TestMethod]
        public void TestSetConsumerGroupWithSpecialChar()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetConsumerGroup("#.testGroup#"));
        }

        [TestMethod]
        public void TestBuildWithoutExpressions()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetSubscriptionExpression(null));
        }

        [TestMethod]
        public void TestBuildWithEmptyExpressions()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetSubscriptionExpression(new Dictionary<string, FilterExpression>()));
        }

        [TestMethod]
        public void TestBuildWithNullMessageListener()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetMessageListener(null));
        }

        [TestMethod]
        public void TestNegativeMaxCacheMessageCount()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetMaxCacheMessageCount(-1));
        }

        [TestMethod]
        public void TestNegativeMaxCacheMessageSizeInBytes()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetMaxCacheMessageSizeInBytes(-1));
        }

        [TestMethod]
        public void TestNegativeConsumptionThreadCount()
        {
            var builder = new PushConsumer.Builder();
            Assert.ThrowsExactly<ArgumentException>(() => builder.SetMaxCacheMessageCount(-1));
        }

        [TestMethod]
        public void TestBuild()
        {
            var clientConfig = new ClientConfig.Builder()
                .SetEndpoints("127.0.0.1:9876").Build();
            var subscription = new Dictionary<string, FilterExpression>
                {{ "fakeTopic", new FilterExpression("*") }};
            var builder = new PushConsumer.Builder();
            builder.SetClientConfig(clientConfig).SetSubscriptionExpression(subscription).SetConsumerGroup("testGroup")
                .SetMessageListener(new TestMessageListener()).SetMaxCacheMessageCount(10)
                .SetMaxCacheMessageSizeInBytes(10).SetConsumptionThreadCount(10).Build();
        }

        private class TestMessageListener : IMessageListener
        {
            public ConsumeResult Consume(MessageView messageView)
            {
                // Handle the received message and return consume result.
                return ConsumeResult.SUCCESS;
            }
        }
    }
}