diff --git a/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/SubscriptionResource.java b/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/SubscriptionResource.java
index e1db266..3609a65 100644
--- a/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/SubscriptionResource.java
+++ b/jaxrs/src/main/java/org/killbill/billing/jaxrs/resources/SubscriptionResource.java
@@ -87,6 +87,8 @@ import org.killbill.commons.metrics.TimedResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
@@ -167,9 +169,14 @@ public class SubscriptionResource extends JaxRsResourceBase {
SubscriptionBundle bundle = null;
final boolean createAddOnEntitlement = ProductCategory.ADD_ON.toString().equals(entitlement.getProductCategory());
if (createAddOnEntitlement) {
- verifyNonNullOrEmpty(entitlement.getExternalKey(), "SubscriptionJson externalKey should be specified for ADD_ON");
+ final boolean expression = !Strings.isNullOrEmpty(entitlement.getExternalKey()) || !Strings.isNullOrEmpty(entitlement.getBundleId());
+ Preconditions.checkArgument(expression, "SubscriptionJson bundleId or externalKey should be specified for ADD_ON");
try {
- bundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey(entitlement.getExternalKey(), callContext);
+ if (!Strings.isNullOrEmpty(entitlement.getBundleId())) {
+ bundle = subscriptionApi.getSubscriptionBundle(UUID.fromString(entitlement.getBundleId()), callContext);
+ } else {
+ bundle = subscriptionApi.getActiveSubscriptionBundleForExternalKey(entitlement.getExternalKey(), callContext);
+ }
} catch (SubscriptionApiException e) {
// converting SubscriptionApiException to force this exception type
throw new IllegalArgumentException(e.getMessage());
diff --git a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestEntitlement.java b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestEntitlement.java
index bfaa0bb..bb7c40d 100644
--- a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestEntitlement.java
+++ b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestEntitlement.java
@@ -229,7 +229,7 @@ public class TestEntitlement extends TestJaxrsBase {
}
@Test(groups = "slow", description = "Create base and addOn subscription with bundle external key")
- public void testBaseAndAdOnnEntitlementsCreation() throws Exception {
+ public void testBaseAndAddOnEntitlementsCreation() throws Exception {
final DateTime initialDate = new DateTime(2015, 11, 1, 0, 3, 42, 0);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
@@ -254,9 +254,8 @@ public class TestEntitlement extends TestJaxrsBase {
}
- @Test(groups = "slow", description = "Create base and addOn subscription with bundle id",
- expectedExceptions = KillBillClientException.class, expectedExceptionsMessageRegExp = "SubscriptionJson externalKey should be specified for ADD_ON")
- public void testBaseAndAdOnnEntitlementsCreationWithBundleId() throws Exception {
+ @Test(groups = "slow", description = "Create base and addOn subscription with bundle id")
+ public void testBaseAndAddOnEntitlementsCreationWithBundleId() throws Exception {
final DateTime initialDate = new DateTime(2015, 11, 1, 0, 3, 42, 0);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
@@ -268,24 +267,30 @@ public class TestEntitlement extends TestJaxrsBase {
final Subscription baseEntitlementJson = createEntitlement(accountJson.getAccountId(), externalKey, "Shotgun",
ProductCategory.BASE, term, true);
- // Retrieves with GET
- Bundle bundleJson = killBillClient.getBundle(externalKey);
-
final Subscription input = new Subscription();
input.setAccountId(accountJson.getAccountId());
- input.setBundleId(UUID.randomUUID());
+ input.setBundleId(baseEntitlementJson.getBundleId());
+ input.setExternalKey("ignoreExternalkey");
input.setProductName("Telescopic-Scope");
input.setProductCategory(ProductCategory.ADD_ON);
input.setBillingPeriod(term);
input.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
- killBillClient.createSubscription(input, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
+ final Subscription addOnEntitlementJson = killBillClient.createSubscription(input, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
+
+ // Retrieves with GET
+ Bundle bundleJson = killBillClient.getBundle(externalKey);
+ final List<Subscription> subscriptions = bundleJson.getSubscriptions();
+
+ assertEquals(subscriptions.size(), 2);
+ assertTrue(baseEntitlementJson.equals(subscriptions.get(0)));
+ assertTrue(addOnEntitlementJson.equals(subscriptions.get(1)));
}
@Test(groups = "slow", description = "Try to create an ADD_ON subscription for an invalid bundle external key",
expectedExceptions = KillBillClientException.class, expectedExceptionsMessageRegExp = "Could not find a bundle matching key invalidKey")
- public void testBaseAndAdOnnEntitlementsErrorCreation() throws Exception {
+ public void testAddOnEntitlementInvalidKey() throws Exception {
final DateTime initialDate = new DateTime(2015, 11, 1, 0, 3, 42, 0);
clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
@@ -294,4 +299,43 @@ public class TestEntitlement extends TestJaxrsBase {
ProductCategory.ADD_ON, BillingPeriod.MONTHLY, true);
}
+ @Test(groups = "slow", description = "Try to create an ADD_ON subscription for an invalid bundle external key",
+ expectedExceptions = KillBillClientException.class, expectedExceptionsMessageRegExp = "Object id=.* type=BUNDLE doesn't exist!")
+ public void testAddOnEntitlementInvalidBundleId() throws Exception {
+ final DateTime initialDate = new DateTime(2015, 11, 1, 0, 3, 42, 0);
+ clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
+
+ final Account accountJson = createAccountWithDefaultPaymentMethod();
+
+ final Subscription input = new Subscription();
+ input.setAccountId(accountJson.getAccountId());
+ input.setBundleId(UUID.randomUUID()); // <--- invalid bundleId
+ input.setProductName("Telescopic-Scope");
+ input.setProductCategory(ProductCategory.ADD_ON);
+ input.setBillingPeriod(BillingPeriod.MONTHLY);
+ input.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
+
+ killBillClient.createSubscription(input, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
+ }
+
+ @Test(groups = "slow", description = "Try to create an ADD_ON subscription without bundle info",
+ expectedExceptions = KillBillClientException.class, expectedExceptionsMessageRegExp = "SubscriptionJson bundleId or externalKey should be specified for ADD_ON")
+ public void testAddOnEntitlementNoBundle() throws Exception {
+ final DateTime initialDate = new DateTime(2015, 11, 1, 0, 3, 42, 0);
+ clock.setDeltaFromReality(initialDate.getMillis() - clock.getUTCNow().getMillis());
+
+ final Account accountJson = createAccountWithDefaultPaymentMethod();
+
+ final Subscription input = new Subscription();
+ input.setAccountId(accountJson.getAccountId());
+ input.setExternalKey(null);
+ input.setBundleId(null);
+ input.setProductName("Telescopic-Scope");
+ input.setProductCategory(ProductCategory.ADD_ON);
+ input.setBillingPeriod(BillingPeriod.MONTHLY);
+ input.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
+
+ killBillClient.createSubscription(input, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
+ }
+
}