package org.mwg;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;

public class WSServerTest {

    @Test
    public void test() {

        Graph graph = GraphBuilder.builder()
                .withMemorySize(10000)
                .withAutoSave(1000)
                .withOffHeapMemory()
                .build();
        graph.connect(new Callback<Boolean>() {
            @Override
            public void on(Boolean result) {
                Node node = graph.newNode(0, 0);
                node.set("name", "hello");
                graph.index("nodes", node, "name", null);

                System.out.println(node);

                WSServer graphServer = new WSServer(graph, 8050);
                graphServer.start();

                CountDownLatch latch = new CountDownLatch(1);

                Graph graph2 = GraphBuilder.builder().withMemorySize(10000).withAutoSave(1000).withStorage(new WSClient("ws://localhost:8050")).build();
                graph2.connect(result1 -> graph2.all(0, 0, "nodes", new Callback<Node[]>() {
                    @Override
                    public void on(Node[] result1) {
                        System.out.println(result1[0]);

                        Node newNode = graph2.newNode(0, 0);
                        newNode.set("name", "hello2");

                        System.out.println(newNode);

                        graph2.index("nodes", newNode, "name", null);

                        graph2.all(0, 0, "nodes", new Callback<Node[]>() {
                            @Override
                            public void on(Node[] result) {
                                System.out.println(result.length);
                            }
                        });


                        graph2.save(new Callback<Boolean>() {
                            @Override
                            public void on(Boolean result) {
                                //ok now try to access new node from graph
                                graph.all(0, 0, "nodes", new Callback<Node[]>() {
                                    @Override
                                    public void on(Node[] result) {
                                        System.out.println(result[0].toString());
                                        System.out.println(result[1].toString());

                                        latch.countDown();

                                    }
                                });

                            }
                        });
                    }
                }));

                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });


    }

}

@@ -11,16 +11,41 @@
 g.mwg;%0A%0A
+import org.junit.Assert;%0A
 import o
@@ -59,16 +59,16 @@
 t.Test;%0A
-
 %0Aimport 
@@ -639,31 +639,99 @@
     
-System.out.println(node
+Assert.assertEquals(%22%7B%5C%22world%5C%22:0,%5C%22time%5C%22:0,%5C%22id%5C%22:1,%5C%22name%5C%22:%5C%22hello%5C%22%7D%22, node.toString()
 );%0A%0A
@@ -1229,64 +1229,8 @@
 1) %7B
-%0A                        System.out.println(result1%5B0%5D);
 %0A%0A  
@@ -1372,34 +1372,114 @@
     
-System.out.println(newNode
+Assert.assertEquals(%22%7B%5C%22world%5C%22:0,%5C%22time%5C%22:0,%5C%22id%5C%22:137438953473,%5C%22name%5C%22:%5C%22hello2%5C%22%7D%22, newNode.toString()
 );%0A%0A
@@ -1754,35 +1754,39 @@
         
-System.out.println(
+Assert.assertEquals(2, 
 result.l
@@ -2329,47 +2329,183 @@
     
-System.out.println(result%5B0%5D.toString()
+Assert.assertEquals(2, result.length);%0A                                        Assert.assertEquals(result%5B0%5D.toString(), %22%7B%5C%22world%5C%22:0,%5C%22time%5C%22:0,%5C%22id%5C%22:1,%5C%22name%5C%22:%5C%22hello%5C%22%7D%22
 );%0A 
@@ -2547,26 +2547,27 @@
     
-System.out.println
+Assert.assertEquals
 (res
@@ -2575,32 +2575,100 @@
 lt%5B1%5D.toString()
+, %22%7B%5C%22world%5C%22:0,%5C%22time%5C%22:0,%5C%22id%5C%22:137438953473,%5C%22name%5C%22:%5C%22hello2%5C%22%7D%22
 );%0A%0A            

update test asserts