package com.syncano.library.tests;

import com.syncano.library.SyncanoApplicationTestCase;
import com.syncano.library.annotation.SyncanoClass;
import com.syncano.library.annotation.SyncanoField;
import com.syncano.library.api.IncrementBuilder;
import com.syncano.library.api.Response;
import com.syncano.library.choice.FieldType;
import com.syncano.library.data.SyncanoObject;

import java.util.List;

public class IncrementBuilderTest extends SyncanoApplicationTestCase {

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        createClass(KernelOS.class);
        createTestsKernel();
    }

    private void createTestsKernel() {
        KernelOS kernelOS = new KernelOS();
        Response<KernelOS> responseCreateKernelOS = kernelOS.save();
        assertTrue(responseCreateKernelOS.isSuccess());
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        removeClass(KernelOS.class);
    }

    public void testIncreaseValue() {
        int expectedKernelVersion = 0;
        int expectedKernelRevision = 0;

        Response<List<KernelOS>> kernelOSResponse = syncano.getObjects(KernelOS.class).send();
        KernelOS kernelOS = kernelOSResponse.getData().get(0);
        Response<KernelOS> response = kernelOS.increment(KernelOS.COLUMN_KERNEL_VERSION, 2).save();

        expectedKernelVersion += 2;
        assertTrue(response.isSuccess());
        kernelOS = response.getData();
        assertEquals(kernelOS.kernelVersion, expectedKernelVersion);
        assertEquals(kernelOS.kernelRevision, expectedKernelRevision);

        IncrementBuilder a = new IncrementBuilder();
        a.increment(KernelOS.COLUMN_KERNEL_VERSION, 2);
        syncano.addition(KernelOS.class, kernelOS.getId(), a).send();

        Response<KernelOS> response2 = kernelOS.increment(KernelOS.COLUMN_KERNEL_VERSION, 2)
                .decrement(KernelOS.COLUMN_KERNEL_REVISION, 1).increment("", 2).save();

        expectedKernelVersion += 2;
        expectedKernelRevision--;
        assertTrue(response2.isSuccess());
        kernelOS = response2.getData();
        assertEquals(kernelOS.kernelVersion, expectedKernelVersion);
        assertEquals(kernelOS.kernelRevision, expectedKernelRevision);
    }


    @SyncanoClass(name = KernelOS.TABLE_NAME)
    public static class KernelOS extends SyncanoObject {
        public final static String TABLE_NAME = "kernel";
        public final static String COLUMN_KERNEL_NAME = "kernel_name";
        public final static String COLUMN_KERNEL_VERSION = "kernel_version";
        public final static String COLUMN_KERNEL_REVISION = "kernel_revision";
        @SyncanoField(name = COLUMN_KERNEL_NAME, filterIndex = true, type = FieldType.STRING)
        public String kernelName;
        @SyncanoField(name = COLUMN_KERNEL_VERSION)
        public int kernelVersion;
        @SyncanoField(name = COLUMN_KERNEL_REVISION)
        public int kernelRevision;
    }
}

@@ -1722,16 +1722,84 @@
 %0A       
+ expectedKernelVersion += 2;%0A%0A        Response%3CKernelOS%3E response2 =
  syncano
@@ -1849,24 +1849,63 @@
  a).send();%0A
+        kernelOS = response2.getData();
 %0A        Res
@@ -1928,17 +1928,17 @@
 response
-2
+3
  = kerne
@@ -2053,25 +2053,8 @@
  1).
-increment(%22%22, 2).
 save
@@ -2155,17 +2155,17 @@
 response
-2
+3
 .isSucce
@@ -2190,33 +2190,33 @@
 nelOS = response
-2
+3
 .getData();%0A    

Update IncrementBuilderTests to show direct addition on syncano instance.