/*
 * Copyright (c) 2015 Spotify AB
 *
 * 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
 *
 * 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.
 */
package com.spotify.folsom;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.net.HostAndPort;
import com.google.common.util.concurrent.ListenableFuture;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class MemcacheClientBuilderTest {

  private EmbeddedServer server;

  @Before
  public void setUp() throws Exception {
    server = new EmbeddedServer(false);
  }

  @After
  public void tearDown() throws Exception {
    server.stop();
  }

  @Test
  public void testValidLatin1() throws Exception {
    AsciiMemcacheClient<String> client = MemcacheClientBuilder.newStringClient()
            .withKeyCharset(Charsets.ISO_8859_1)
            .withAddress(HostAndPort.fromParts("localhost", server.getPort()))
            .connectAscii();
    ConnectFuture.connectFuture(client).get();
    assertEquals(null, client.get("Räksmörgås").get());
  }

  @Test
  public void testValidUTF8() throws Exception {
    AsciiMemcacheClient<String> client = MemcacheClientBuilder.newStringClient()
            .withKeyCharset(Charsets.UTF_8)
            .withAddress(HostAndPort.fromParts("localhost", server.getPort()))
            .connectAscii();
    ConnectFuture.connectFuture(client).get();
    assertEquals(null, client.get("Räksmörgås").get());
  }

  @Test(expected = IllegalArgumentException.class)
  public void testInvalidUTF16() throws Exception {
    AsciiMemcacheClient<String> client = MemcacheClientBuilder.newStringClient()
            .withKeyCharset(Charsets.UTF_16)
            .withAddress(HostAndPort.fromParts("localhost", server.getPort()))
            .connectAscii();
    ConnectFuture.connectFuture(client).get();
    client.get("Key").get();
  }

  @Test(expected = MemcacheOverloadedException.class)
  public void testOverloaded() throws Throwable {
    AsciiMemcacheClient<String> client = MemcacheClientBuilder.newStringClient()
            .withAddress(HostAndPort.fromParts("localhost", server.getPort()))
            .withMaxOutstandingRequests(100)
            .connectAscii();
    ConnectFuture.connectFuture(client).get();

    try {
      List<ListenableFuture<String>> futures = Lists.newArrayList();
      for (int i = 0; i < 200; i++) {
        futures.add(client.get("key"));
      }
      for (ListenableFuture<String> future : futures) {
        try {
          future.get();
        } catch (ExecutionException e) {
          throw e.getCause();
        }
      }
      fail("No MemcacheOverloadedException was triggered");
    } finally {
      client.shutdown();
    }
  }

}

@@ -3341,10 +3341,544 @@
  %7D%0A  %7D%0A%0A
+  @Test%0A  public void testMaxSetLength() throws Throwable %7B%0A    AsciiMemcacheClient%3CString%3E client = MemcacheClientBuilder.newStringClient()%0A            .withAddress(HostAndPort.fromParts(%22localhost%22, server.getPort()))%0A            .withMaxSetLength(1)%0A            .connectAscii();%0A    ConnectFuture.connectFuture(client).get();%0A%0A    try %7B%0A      assertEquals(MemcacheStatus.VALUE_TOO_LARGE, client.set(%22key%22, %22value%22, 100).get());%0A      assertEquals(null, client.get(%22key%22).get());%0A    %7D finally %7B%0A      client.shutdown();%0A    %7D%0A  %7D%0A%0A
 %7D%0A

Add test for MemcacheClientBuilder.withMaxSetLength