package com.telekom.m2m.cot.restsdk.inventory;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.telekom.m2m.cot.restsdk.CloudOfThingsPlatform;
import com.telekom.m2m.cot.restsdk.util.TestHelper;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.Iterator;

/**
 * Created by Patrick Steinert on 30.01.16.
 */
public class InventoryApiCrudIT {

    @Test
    public void testCreateManagedObject() throws Exception {
        ManagedObject mo = new ManagedObject();
        mo.setName("Hello!");

        CloudOfThingsPlatform cotPlat = new CloudOfThingsPlatform(TestHelper.TEST_HOST, TestHelper.TEST_USERNAME, TestHelper.TEST_PASSWORD);
        InventoryApi inventoryApi = cotPlat.getInventoryApi();

        ManagedObject createdMo = inventoryApi.create(mo);
        Assert.assertNotNull("Should now have an Id", mo.getId());
    }

    @Test
    public void testGetDevice() throws Exception {
        CloudOfThingsPlatform cotPlat = new CloudOfThingsPlatform(TestHelper.TEST_HOST, TestHelper.TEST_USERNAME, TestHelper.TEST_PASSWORD);
        InventoryApi inventoryApi = cotPlat.getInventoryApi();

        ManagedObject mo = inventoryApi.get("142300");
        ManagedObjectReferenceCollection childs = mo.getChildDevices();
        Assert.assertNotNull(childs.getSelf());

        Iterable<ManagedObjectReference> children = childs.get();
        Iterator<ManagedObjectReference> iter = children.iterator();

        Assert.assertTrue(iter.hasNext());
        ManagedObject child = iter.next().getManagedObject();
        Assert.assertEquals(child.getName(), "RaspPi 8fef9ec2 Sensor BMP180");
    }

    @Test
    public void testCreateAndRead() throws Exception {

        ManagedObject mo = new ManagedObject();
        mo.setName("MyTest-testCreateAndRead");

        CloudOfThingsPlatform cotPlat = new CloudOfThingsPlatform(TestHelper.TEST_HOST, TestHelper.TEST_USERNAME, TestHelper.TEST_PASSWORD);
        InventoryApi inventoryApi = cotPlat.getInventoryApi();
        ManagedObject createdMo = inventoryApi.create(mo);

        Assert.assertNotNull("Should now have an Id", createdMo.getId());

        ManagedObject retrievedMo = inventoryApi.get(createdMo.getId());

        Assert.assertEquals(retrievedMo.getId(), createdMo.getId(), "Should have the same Id");
        Assert.assertEquals(retrievedMo.getName(), "MyTest-testCreateAndRead", "Should have the same Name");
    }

    @Test
    public void testCreateReadUpdateDelete() throws Exception {

        ManagedObject mo = new ManagedObject();
        mo.setName("MyTest-testCreateAndRead");

        CloudOfThingsPlatform cotPlat = new CloudOfThingsPlatform(TestHelper.TEST_HOST, TestHelper.TEST_USERNAME, TestHelper.TEST_PASSWORD);
        InventoryApi inventoryApi = cotPlat.getInventoryApi();
        ManagedObject createdMo = inventoryApi.create(mo);

        Assert.assertNotNull("Should now have an Id", createdMo.getId());

        ManagedObject retrievedMo = inventoryApi.get(createdMo.getId());

        Assert.assertEquals(retrievedMo.getId(), createdMo.getId(), "Should have the same Id");
        Assert.assertEquals(retrievedMo.getName(), "MyTest-testCreateAndRead", "Should have the same Name");

        JsonObject obj = new JsonObject();
        obj.add("foo", new JsonPrimitive("bar"));

        retrievedMo.setName("NewName");
        retrievedMo.set("play", obj);

        inventoryApi.update(retrievedMo);

        ManagedObject aretrievedMo = inventoryApi.get(createdMo.getId());

        Assert.assertEquals(aretrievedMo.getId(), createdMo.getId(), "Should have the same Id");
        Assert.assertEquals(aretrievedMo.getName(), "NewName", "Should have the same Name");

        Object play = retrievedMo.get("play");
        Assert.assertNotNull(play);
        Assert.assertTrue(play instanceof JsonObject);
        JsonObject playObj = (JsonObject) play;
        Assert.assertTrue(playObj.has("foo"));
        Assert.assertEquals(playObj.get("foo").getAsString(), "bar");

        inventoryApi.delete(retrievedMo.getId());
        retrievedMo = inventoryApi.get(createdMo.getId());
        Assert.assertNull(retrievedMo);
    }

}
@@ -924,17 +924,21 @@
 test
-GetDevice
+CreateAndRead
 () t
@@ -959,568 +959,144 @@
 n %7B%0A
-        CloudOfThingsPlatform cotPlat = new CloudOfThingsPlatform(TestHelper.TEST_HOST, TestHelper.TEST_USERNAME, TestHelper.TEST_PASSWORD);%0A        InventoryApi inventoryApi = cotPlat.getInventoryApi();%0A%0A        ManagedObject mo = inventoryApi.get(%22142300%22);%0A        ManagedObjectReferenceCollection childs = mo.getChildDevices();%0A        Assert.assertNotNull(childs.getSelf());%0A%0A        Iterable%3CManagedObjectReference%3E children = childs.get();%0A        Iterator%3CManagedObjectReference%3E iter = children.iterator();%0A%0A        Assert.assertTrue(iter.hasNext()
+%0A        ManagedObject mo = new ManagedObject();%0A        final String moName = %22MyTest-testCreateAndRead%22;%0A        mo.setName(moName
 );%0A
+%0A
     
@@ -1117,31 +1117,22 @@
 ect 
-c
+moC
 hild = 
-iter.next().get
+new 
 Mana
@@ -1156,44 +1156,34 @@
     
-Assert.assertEquals(child.get
+final String moChild
 Name
-(),
+ =
  %22Ra
@@ -1214,177 +1214,45 @@
 180%22
-)
 ;%0A    
-%7D%0A%0A
     
-@Test%0A    public void testCreateAndRead() throws Exception %7B%0A%0A        ManagedObject mo = new ManagedObject();%0A        mo.setName(%22MyTest-testCreateAndRead%22
+moChild.setName(moChildName
 );%0A%0A
@@ -1505,32 +1505,173 @@
 yApi.create(mo);
+%0A        ManagedObject createdMoChild = inventoryApi.create(moChild);%0A%0A        inventoryApi.registerAsChildDevice(createdMo, createdMoChild);
 %0A%0A        Assert
@@ -1956,66 +1956,496 @@
 (), 
-%22MyTest-testCreateAndRead%22, %22Should have the same 
+moName, %22Should have the same Name%22);%0A%0A        ManagedObjectReferenceCollection childDevices = retrievedMo.getChildDevices();%0A        Assert.assertNotNull(childDevices.getSelf());%0A%0A        Iterable%3CManagedObjectReference%3E children = childDevices.get();%0A        Iterator%3CManagedObjectReference%3E iter = children.iterator();%0A%0A        Assert.assertTrue(iter.hasNext());%0A        ManagedObject child = iter.next().getManagedObject();%0A        Assert.assertEquals(child.getName(), moChild
 Name
-%22
 );%0A
+%0A
     

remove a test with a fix parent device id and add childDevice assertion to the testCreateAndRead() instead