package de.paymill.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.junit.Test;

import de.paymill.Paymill;
import de.paymill.TestCase;
import de.paymill.model.Client;
import de.paymill.model.Interval;
import de.paymill.model.Offer;
import de.paymill.model.Payment;
import de.paymill.model.Payment.Type;
import de.paymill.model.Subscription;

public class SubscriptionServiceTest extends TestCase {

	@Test
	public void testCreateSubscription() {
		OfferService srvOffer = Paymill.getService(OfferService.class);
		ClientService srvClient = Paymill.getService(ClientService.class);
		PaymentService srvPayment = Paymill.getService(PaymentService.class);
		SubscriptionService srvSubs = Paymill.getService(SubscriptionService.class);

		Interval interval = new Interval();
		interval.setInterval(1);
		interval.setUnit(Interval.Unit.WEEK);
		Offer offer = new Offer();
		offer.setAmount(199);
		offer.setInterval(interval);
		offer.setName("testabo");
		offer.setTrialPeriodDays(15);
		offer.setCurrency("EUR");
		offer = srvOffer.create(offer);

		Client client = new Client();
		client.setEmail(getRandomEmail());
		client = srvClient.create(client);

		Payment payment = new Payment();
		payment.setType(Type.DEBIT);
		payment.setAccount("123456");
		payment.setCode("12345678");
		payment.setHolder("jon doe");
		payment.setClient(client.getId());
		payment = srvPayment.create(payment);

		Subscription subs = new Subscription();
		subs.setOffer(offer);
		subs.setClient(client);
		subs.setPayment(payment);
		subs = srvSubs.create(subs);

		assertNotNull(subs);
		assertNotNull(subs.getId());
		assertEquals(subs.getClient().getId(), client.getId());
		assertEquals(subs.getOffer().getId(), offer.getId());
		assertEquals(subs.getPayment().getId(), payment.getId());
	}

	@Test
	public void testUpdateSubscription() {
		OfferService srvOffer = Paymill.getService(OfferService.class);
		PaymentService srvPayment = Paymill.getService(PaymentService.class);
		SubscriptionService srvSubs = Paymill.getService(SubscriptionService.class);

		List<Subscription> list = srvSubs.list(0, 1);
                
                Subscription subs = list.get(0);
            
		Interval interval = new Interval();
		interval.setInterval(2);
		interval.setUnit(Interval.Unit.MONTH);
		Offer offer = new Offer();
		offer.setAmount(200);
		offer.setInterval(interval);
		offer.setName("testabo2");
		offer.setTrialPeriodDays(0);
		offer.setCurrency("EUR");
		offer = srvOffer.create(offer);
 
		Payment payment = new Payment();
		payment.setType(Type.DEBIT);
		payment.setAccount("654321");
		payment.setCode("87654321");
		payment.setHolder("jon doe");
		payment.setClient(subs.getClient().getId());
		payment = srvPayment.create(payment);
                
                subs.setOffer(offer);
                subs.setPayment(payment);
                subs.setCancelAtPeriodEnd(true);
                srvSubs.update(subs);

		assertNotNull(subs);
		assertNotNull(subs.getId());
		assertEquals(subs.getOffer().getId(), offer.getId());
		assertEquals(subs.getPayment().getId(), payment.getId());
       }
        
}

@@ -118,32 +118,8 @@
 l;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -460,34 +460,35 @@
 e %7B%0A
-%0A%09@Test%0A%09public void testC
+%09%0A%09protected Subscription c
 reat
@@ -494,33 +494,34 @@
 teSubscription()
- 
+%0A%09
 %7B%0A%09%09OfferService
@@ -1573,22 +1573,22 @@
 ent);%0A%09%09
-subs =
+return
  srvSubs
@@ -1593,32 +1593,127 @@
 bs.create(subs);
+%0A%09%7D%0A%0A%09@Test%0A%09public void testCreateSubscription() %7B%0A%09%09Subscription subs = createSubscription();
 %0A%0A%09%09assertNotNul
@@ -1752,38 +1752,39 @@
 tId());%0A%09%09assert
-Equals
+NotNull
 (subs.getClient(
@@ -1796,24 +1796,8 @@
 Id()
-, client.getId()
 );%0A%09
@@ -1795,38 +1795,39 @@
 tId());%0A%09%09assert
-Equals
+NotNull
 (subs.getOffer()
@@ -1830,39 +1830,24 @@
 er().getId()
-, offer.getId()
 );%0A%09%09assertE
@@ -1837,38 +1837,39 @@
 tId());%0A%09%09assert
-Equals
+NotNull
 (subs.getPayment
@@ -1878,33 +1878,16 @@
 .getId()
-, payment.getId()
 );%0A%09%7D%0A%0A%09
@@ -2154,81 +2154,8 @@
 );%0A%0A
-%09%09List%3CSubscription%3E list = srvSubs.list(0, 1);%0A                %0A        
     
@@ -2182,18 +2182,27 @@
 s = 
-list.get(0
+createSubscription(
 );%0A 
@@ -2792,32 +2792,18 @@
        %0A
-                
+%09%09
 subs.set
@@ -2816,32 +2816,18 @@
 offer);%0A
-                
+%09%09
 subs.set
@@ -2844,32 +2844,18 @@
 yment);%0A
-                
+%09%09
 subs.set
@@ -2879,32 +2879,18 @@
 (true);%0A
-                
+%09%09
 srvSubs.
@@ -3078,24 +3078,9 @@
 ));%0A
-       %7D%0A        
+%09%7D
 %0A%7D%0A

Fix tests