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 SubscriptionsIntegrationTests {

  public VinliApp vinliApp;

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

    vinliApp = TestHelper.getVinliApp();
  }

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

    Subscription.subscriptionsWithDeviceId(TestHelper.getDeviceId(), null, null, null, null)
        .subscribe(new Subscriber<Page<Subscription>>() {
          @Override public void onCompleted() {
          }

          @Override public void onError(Throwable e) {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
            assertTrue(false);
          }

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

            for (Subscription subscription : subscriptionPage.getItems()) {
              assertTrue(subscription.id() != null && subscription.id().length() > 0);
              assertTrue(subscription.deviceId() != null && subscription.deviceId().length() > 0);
              assertTrue(subscription.url() != null && subscription.url().length() > 0);
            }
          }
        });
  }

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

    vinliApp.subscriptions()
        .subscriptions(TestHelper.getDeviceId(), 5, 1, null, null)
        .subscribe(new Subscriber<Page<Subscription>>() {
          @Override public void onCompleted() {
          }

          @Override public void onError(Throwable e) {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
            assertTrue(false);
          }

          @Override public void onNext(Page<Subscription> subscriptionPage) {
            assertTrue(subscriptionPage.getItems().size() <= 5);

            for (Subscription subscription : subscriptionPage.getItems()) {
              assertTrue(subscription.id() != null && subscription.id().length() > 0);
              assertTrue(subscription.deviceId() != null && subscription.deviceId().length() > 0);
              assertTrue(subscription.url() != null && subscription.url().length() > 0);
            }
          }
        });
  }

  @Test public void getSubscriptionById() {
    assertTrue(TestHelper.getSubscriptionId() != null);

    Subscription.subscriptionWithId(TestHelper.getSubscriptionId())
        .toBlocking()
        .subscribe(new Subscriber<Subscription>() {
          @Override public void onCompleted() {
          }

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

          @Override public void onNext(Subscription subscription) {
            assertTrue(subscription.id() != null && subscription.id().length() > 0);
            assertTrue(subscription.deviceId() != null && subscription.deviceId().length() > 0);
            assertTrue(subscription.url() != null && subscription.url().length() > 0);
          }
        });
  }

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

    vinliApp.subscriptions()
        .subscriptionsForUrl(String.format("%sdevices/%s/subscriptions", Endpoint.EVENTS.getUrl(),
            TestHelper.getDeviceId()))
        .toBlocking()
        .subscribe(new Subscriber<Page<Subscription>>() {
              @Override public void onCompleted() {

              }

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

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

                for (Subscription subscription : subscriptionPage.getItems()) {
                  assertTrue(subscription.id() != null && subscription.id().length() > 0);
                  assertTrue(subscription.deviceId() != null && subscription.deviceId().length() > 0);
                  assertTrue(subscription.url() != null && subscription.url().length() > 0);
                }
              }
            });
  }
}

@@ -543,32 +543,1465 @@
 inliApp();%0A  %7D%0A%0A
+  @Test%0A  public void testCreateAndDeleteSubscription()%7B%0A    assertTrue(TestHelper.getDeviceId() != null);%0A%0A    Subscription.create().deviceId(TestHelper.getDeviceId())%0A        .eventType(%22rule-enter%22)%0A        .appData(%22%7B%5C%22data%5D%5C%22:%5C%22stuff%5C%22%7D%22)%0A        .object(ObjectRef.builder().id(TestHelper.getRuleId()).type(%22rule%22).build())%0A        .url(%22https://someurl/notifications%22)%0A        .save().toBlocking().subscribe(new Subscriber%3CSubscription%3E() %7B%0A      @Override public void onCompleted() %7B%0A%0A      %7D%0A%0A      @Override public void onError(Throwable e) %7B%0A        System.out.println(%22Error: %22 + e.getMessage());%0A        e.printStackTrace();%0A        assertTrue(false);%0A      %7D%0A%0A      @Override public void onNext(final Subscription subscription) %7B%0A        assertTrue(subscription.id() != null && subscription.id().length() %3E 0);%0A        assertTrue(subscription.deviceId() != null && subscription.deviceId().length() %3E 0);%0A        assertTrue(subscription.url() != null && subscription.url().length() %3E 0);%0A%0A        subscription.delete().toBlocking().subscribe(new Subscriber%3CVoid%3E() %7B%0A          @Override public void onCompleted() %7B%0A%0A          %7D%0A%0A          @Override public void onError(Throwable e) %7B%0A            System.out.println(%22Error: %22 + e.getMessage());%0A            e.printStackTrace();%0A            assertTrue(false);%0A          %7D%0A%0A          @Override public void onNext(Void aVoid) %7B%0A%0A          %7D%0A        %7D);%0A%0A      %7D%0A    %7D);%0A  %7D%0A%0A
   @Test public v

add subscription creation test