/**
 * Copyright 2017 The GreyCat Authors.  All rights reserved.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 greycatTest.internal.task;

import greycat.*;
import greycat.struct.Relation;
import org.junit.Assert;
import org.junit.Test;

import static greycat.Tasks.newTask;

public class ActionCloneNodesTest extends AbstractActionTest {

    public ActionCloneNodesTest() {
        super();
        initGraph();
    }

    @Test
    public void testWithOneNode() {

        newTask()
                .readIndex("nodes")
                .setAsVar("baseNodes")
                .thenDo(ctx -> {
                    ctx.defineVariable("spaceSize", ctx.graph().space().available());
                    ctx.continueTask();
                })
                .log("{{spaceSize}}")
                .cloneNodes()
                .thenDo(new ActionFunction() {
                    @Override
                    public void eval(TaskContext ctx) {
                        //check clone result Size
                        TaskResult<Node> initialNodes = ctx.variable("baseNodes");
                        TaskResult<Node> clones = ctx.resultAsNodes();
                        Assert.assertEquals(initialNodes.size(), clones.size());
                        //check != ids
                        for (int i = 0; i < initialNodes.size(); i++) {
                            Assert.assertNotEquals(initialNodes.get(i).id(), clones.get(i).id());
                        }
                        //check == 'name'
                        for (int i = 0; i < initialNodes.size(); i++) {
                            Assert.assertEquals(initialNodes.get(i).get("name"), clones.get(i).get("name"));
                        }
                        //check == 'children'
                        for (int i = 0; i < initialNodes.size(); i++) {
                            Relation children = initialNodes.get(i).getRelation("children");
                            if (children != null) {
                                Assert.assertArrayEquals(children.all(), clones.get(i).getRelation("children").all());
                            }
                        }
                        ctx.continueTask();
                    }
                })
                .save()
                .clearResult()
                .thenDo(ctx -> {
                    //Check all cloned
                    //Assert no leak
                    Assert.assertEquals(ctx.longVar("spaceSize"), ctx.graph().space().available());
                    ctx.continueTask();
                })
                .execute(graph, new Callback<TaskResult>() {
                    @Override
                    public void on(TaskResult result) {
                        result.free();
                    }
                });
    }


}

@@ -1005,166 +1005,45 @@
     
-newTask()%0A                .readIndex(%22nodes%22)%0A                .setAsVar(%22baseNodes%22)%0A                .thenDo(ctx -%3E %7B%0A                    ctx.defineVariable(%22
+graph.save(null);%0A        final long 
 spac
@@ -1051,22 +1051,16 @@
 Size
-%22, ctx.
+ = 
 graph
-()
 .spa
@@ -1079,50 +1079,28 @@
 le()
-)
 ;%0A
-                    ctx.continue
+%0A        new
 Task()
-;
 %0A   
@@ -1104,33 +1104,50 @@
                 
-%7D
+.readIndex(%22nodes%22
 )%0A              
@@ -1153,26 +1153,27 @@
    .
-log(%22%7B%7BspaceSize%7D%7D
+setAsVar(%22baseNodes
 %22)%0A 
@@ -1400,32 +1400,38 @@
                 
+final 
 TaskResult%3CNode%3E
@@ -1497,16 +1497,22 @@
         
+final 
 TaskResu
@@ -2666,307 +2666,8 @@
 e()%0A
-                .clearResult()%0A                .thenDo(ctx -%3E %7B%0A                    //Check all cloned%0A                    //Assert no leak%0A                    Assert.assertEquals(ctx.longVar(%22spaceSize%22), ctx.graph().space().available());%0A                    ctx.continueTask();%0A                %7D)%0A
     
@@ -2844,24 +2844,107 @@
 ult.free();%0A
+                        Assert.assertEquals(spaceSize, graph.space().available());%0A
             

test rewritten