/*
 * RESTHeart - the Web API for MongoDB
 * Copyright (C) SoftInstigate Srl
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.restheart.test.performance;

/**
 * install ldt from https://github.com/bazhenov/load-test-tool run it from
 * target/class directory (current directory is added to classpath) as follows:
 * <PATH_TO_ldt-assembly-1.1>/bin/ldt.sh -z
 * org.restheart.test.performance.LoadPutPT#put -c 20 -n 500 -w 5 -p
 * "url=http://127.0.0.1:8080/testdb/testcoll,id=a,pwd=a"
 *
 * @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
 */
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.request.HttpRequestWithBody;
import org.restheart.db.DocumentDAO;
import java.io.IOException;
import org.bson.BsonDocument;
import org.bson.BsonDouble;
import org.bson.types.ObjectId;
import org.restheart.utils.HttpStatus;
import static org.junit.Assert.assertEquals;

/**
 *
 * @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
 */
public class LoadPutPT extends AbstractPT {
    public void post() throws Exception {
        HttpRequestWithBody req = Unirest.post(url);
        HttpResponse resp;

        if (id != null && pwd != null) {
            req.basicAuth(id, pwd);
        }

        resp = req
                .header("content-type", "application/json")
                .body("{'nanostamp': " + System.nanoTime() + "}")
                .asString();

        assertEquals("check status code", 
                HttpStatus.SC_CREATED, resp.getStatus());
    }

    public void postWitId() throws Exception {
        HttpRequestWithBody req = Unirest.post(url);
        HttpResponse resp;

        if (id != null && pwd != null) {
            req.basicAuth(id, pwd);
        }

        resp = req
                .header("content-type", "application/json")
                .body("{'_id': {'$oid': '" 
                        + new ObjectId().toString() 
                        + "'}, 'nanostamp': " 
                        + System.nanoTime() + "}")
                .asString();

        assertEquals(
                "check status code", 
                HttpStatus.SC_CREATED, 
                resp.getStatus());
    }

    public void put() throws Exception {
        String _url = url + "/" + new ObjectId().toString();
        
        HttpRequestWithBody req = Unirest.put(_url);
        HttpResponse resp;

        if (id != null && pwd != null) {
            req.basicAuth(id, pwd);
        }
                
        resp = req
                .header("content-type", "application/json")
                .body("{'nanostamp': " + System.nanoTime() + "}")
                .asString();

        assertEquals(
                "check status code", 
                HttpStatus.SC_CREATED, 
                resp.getStatus());
    }

    public void dbdirect() throws IOException {
        BsonDocument content = new BsonDocument("random"
                , new BsonDouble(Math.random()));

        new DocumentDAO().upsertDocument(
                db, 
                coll, 
                null, 
                null, 
                content, 
                null, 
                false, 
                false);
    }
}

@@ -1572,16 +1572,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertTrue;
 %0A%0A/**%0A *
@@ -2157,33 +2157,32 @@
 ck status code%22,
- 
 %0A               
@@ -2568,17 +2568,16 @@
 oid': '%22
- 
 %0A       
@@ -2620,17 +2620,16 @@
 String()
- 
 %0A       
@@ -2666,17 +2666,16 @@
 tamp': %22
- 
 %0A       
@@ -2798,33 +2798,32 @@
 ck status code%22,
- 
 %0A               
@@ -2837,33 +2837,32 @@
 atus.SC_CREATED,
- 
 %0A               
@@ -2990,24 +2990,16 @@
 ring();%0A
-        
 %0A       
@@ -3159,32 +3159,16 @@
       %7D%0A
-                
 %0A       
@@ -3393,17 +3393,16 @@
 s code%22,
- 
 %0A       
@@ -3432,17 +3432,16 @@
 CREATED,
- 
 %0A       
@@ -3457,32 +3457,633 @@
 resp.getStatus()
+);%0A    %7D%0A%0A    public void putUrl() throws Exception %7B%0A        String _url = this.url;%0A%0A        HttpRequestWithBody req = Unirest.put(_url);%0A        HttpResponse resp;%0A%0A        if (id != null && pwd != null) %7B%0A            req.basicAuth(id, pwd);%0A        %7D%0A%0A        resp = req%0A                .header(%22content-type%22, %22application/json%22)%0A                .body(%22%7B'nanostamp': %22 + System.nanoTime() + %22%7D%22)%0A                .asString();%0A%0A        assertTrue(%0A                %22check status code%22,%0A                resp.getStatus() == HttpStatus.SC_CREATED%0A                %7C%7C resp.getStatus() == HttpStatus.SC_OK
 );%0A    %7D%0A%0A    pu
@@ -4180,16 +4180,17 @@
 %22random%22
+,
 %0A       
@@ -4201,10 +4201,8 @@
     
- ,
  new
@@ -4292,17 +4292,16 @@
      db,
- 
 %0A       
@@ -4314,17 +4314,16 @@
    coll,
- 
 %0A       
@@ -4328,33 +4328,32 @@
            null,
- 
 %0A               
@@ -4350,33 +4350,32 @@
            null,
- 
 %0A               
@@ -4383,17 +4383,16 @@
 content,
- 
 %0A       
@@ -4405,17 +4405,16 @@
    null,
- 
 %0A       
@@ -4428,17 +4428,16 @@
   false,
- 
 %0A       

add performance test case about PUTting same document