/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search 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.
 */

package org.elasticsearch.memcached.test;

import net.spy.memcached.MemcachedClient;
import org.elasticsearch.node.Node;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.common.xcontent.XContentFactory.*;
import static org.elasticsearch.node.NodeBuilder.*;

/**
 * @author kimchy (shay.banon)
 */
public abstract class AbstractMemcachedActionsTests {

    private Node node;

    private MemcachedClient memcachedClient;

    @BeforeMethod
    public void setup() throws IOException {
        node = nodeBuilder().node();
        memcachedClient = createMemcachedClient();
    }

    protected abstract MemcachedClient createMemcachedClient() throws IOException;

    @AfterMethod
    public void tearDown() {
        memcachedClient.shutdown();
        node.close();
    }

    @Test public void testSimpleOperations() throws Exception {
        Future setResult = memcachedClient.set("/test/person/1", 0, jsonBuilder().startObject().field("test", "value").endObject().copiedBytes());
        setResult.get(10, TimeUnit.SECONDS);

        String getResult = (String) memcachedClient.get("/_refresh");
        System.out.println("REFRESH " + getResult);

        getResult = (String) memcachedClient.get("/test/person/1");
        System.out.println("GET " + getResult);

        Future deleteResult = memcachedClient.delete("/test/person/1");
        deleteResult.get(10, TimeUnit.SECONDS);

        getResult = (String) memcachedClient.get("/_refresh");
        System.out.println("REFRESH " + getResult);

        getResult = (String) memcachedClient.get("/test/person/1");
        System.out.println("GET " + getResult);
    }
}

@@ -923,16 +923,46 @@
 e.Node;%0A
+import org.hamcrest.Matchers;%0A
 import o
@@ -1298,16 +1298,99 @@
 ilder.*;
+%0Aimport static org.hamcrest.MatcherAssert.*;%0Aimport static org.hamcrest.Matchers.*;
 %0A%0A/**%0A *
@@ -1978,16 +1978,25 @@
   Future
+%3CBoolean%3E
  setResu
@@ -2128,16 +2128,27 @@
         
+assertThat(
 setResul
@@ -2166,32 +2166,48 @@
 imeUnit.SECONDS)
+, equalTo(true))
 ;%0A%0A        Strin
@@ -2319,122 +2319,574 @@
 t);%0A
-%0A        getResult = (String) memcachedClient.get(%22/test/person/1%22);%0A        System.out.println(%22GET %22 + getResult
+        assertThat(getResult, Matchers.containsString(%22%5C%22total%5C%22:10%22));%0A        assertThat(getResult, Matchers.containsString(%22%5C%22successful%5C%22:5%22));%0A        assertThat(getResult, Matchers.containsString(%22%5C%22failed%5C%22:0%22));%0A%0A        getResult = (String) memcachedClient.get(%22/test/person/1%22);%0A        System.out.println(%22GET %22 + getResult);%0A        assertThat(getResult, Matchers.containsString(%22%5C%22_index%5C%22:%5C%22test%5C%22%22));%0A        assertThat(getResult, Matchers.containsString(%22%5C%22_type%5C%22:%5C%22person%5C%22%22));%0A        assertThat(getResult, Matchers.containsString(%22%5C%22_id%5C%22:%5C%221%5C%22%22)
 );%0A%0A
@@ -2899,16 +2899,25 @@
   Future
+%3CBoolean%3E
  deleteR
@@ -2974,16 +2974,27 @@
         
+assertThat(
 deleteRe
@@ -3023,16 +3023,32 @@
 SECONDS)
+, equalTo(true))
 ;%0A%0A     
@@ -3156,16 +3156,236 @@
 Result);
+%0A        assertThat(getResult, Matchers.containsString(%22%5C%22total%5C%22:10%22));%0A        assertThat(getResult, Matchers.containsString(%22%5C%22successful%5C%22:5%22));%0A        assertThat(getResult, Matchers.containsString(%22%5C%22failed%5C%22:0%22));
 %0A%0A      

improve memcached test