package li.vin.net;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import rx.Subscriber;

import static junit.framework.Assert.assertTrue;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 22)
public class DevicesIntegrationTests {

  public VinliApp vinliApp;

  @Before
  public void setup(){
    assertTrue(TestHelper.getAccessToken() != null);

    vinliApp = TestHelper.getVinliApp();
  }

  @Test
  public void testGetDevices(){
    vinliApp.devices().toBlocking().subscribe(new Subscriber<Page<Device>>() {
      @Override
      public void onCompleted() {

      }

      @Override
      public void onError(Throwable e) {
        e.printStackTrace();
        assertTrue(false);
      }

      @Override
      public void onNext(Page<Device> devicePage) {
        assertTrue(devicePage.getItems().size() > 0);

        for(Device device : devicePage.getItems()){
          assertTrue(device.id() != null && device.id().length() > 0);
          assertTrue(device.name() != null && device.name().length() > 0);
        }
      }
    });
  }

  @Test
  public void testGetDevicesByUrl(){
    vinliApp.devicesSvc().devicesForUrl(String.format("%sdevices", Endpoint.PLATFORM.getUrl()))
        .toBlocking().subscribe(new Subscriber<Page<Device>>() {
      @Override
      public void onCompleted() {

      }

      @Override
      public void onError(Throwable e) {
        e.printStackTrace();
        assertTrue(false);
      }

      @Override
      public void onNext(Page<Device> devicePage) {
        assertTrue(devicePage.getItems().size() > 0);

        for(Device device : devicePage.getItems()){
          assertTrue(device.id() != null && device.id().length() > 0);
          assertTrue(device.name() != null && device.name().length() > 0);
        }
      }
    });
  }

  @Test
  public void testGetDeviceById(){
    assertTrue(TestHelper.getDeviceId() != null);

    Device.deviceWithId(TestHelper.getDeviceId()).toBlocking().subscribe(new Subscriber<Device>() {
      @Override
      public void onCompleted() {

      }

      @Override
      public void onError(Throwable e) {
        e.printStackTrace();
        assertTrue(false);
      }

      @Override
      public void onNext(Device device) {
        assertTrue(device.id() != null && device.id().length() > 0);
        assertTrue(device.name() != null && device.name().length() > 0);
      }
    });
  }

}

@@ -538,32 +538,1626 @@
 inliApp();%0A  %7D%0A%0A
+  @Test public void getPagedDevices() %7B%0A    vinliApp.devices(1, 0)%0A        .toBlocking()%0A        .subscribe(new Subscriber%3CPage%3CDevice%3E%3E() %7B%0A          @Override public void onCompleted() %7B%0A%0A          %7D%0A%0A          @Override public void onError(Throwable e) %7B%0A            e.printStackTrace();%0A            assertTrue(false);%0A          %7D%0A%0A          @Override public void onNext(Page%3CDevice%3E devicePage) %7B%0A            assertTrue(devicePage.getItems().size() %3E 0);%0A%0A            for(Device device : devicePage.getItems())%7B%0A              assertTrue(device.id() != null && device.id().length() %3E 0);%0A              assertTrue(device.name() != null && device.name().length() %3E 0);%0A            %7D%0A%0A            if (devicePage.hasNextPage()) %7B%0A              devicePage.loadNextPage().toBlocking()%0A                  .subscribe(new Subscriber%3CPage%3CDevice%3E%3E() %7B%0A                    @Override public void onCompleted() %7B%0A%0A                    %7D%0A%0A                    @Override public void onError(Throwable e) %7B%0A                      e.printStackTrace();%0A                      assertTrue(false);%0A                    %7D%0A%0A                    @Override public void onNext(Page%3CDevice%3E devicePage) %7B%0A                      assertTrue(devicePage.getItems().size() %3E 0);%0A%0A                      for(Device device : devicePage.getItems())%7B%0A                        assertTrue(device.id() != null && device.id().length() %3E 0);%0A                        assertTrue(device.name() != null && device.name().length() %3E 0);%0A                      %7D%0A                    %7D%0A                  %7D);%0A            %7D%0A          %7D%0A        %7D);%0A  %7D%0A%0A
   @Test%0A  public

test paged devices