killbill-uncached

Throw an exception with a sensible message if we try to access

2/14/2012 7:56:06 PM

Details

diff --git a/api/src/main/java/com/ning/billing/catalog/api/Catalog.java b/api/src/main/java/com/ning/billing/catalog/api/Catalog.java
index 3c04a74..2b3609a 100644
--- a/api/src/main/java/com/ning/billing/catalog/api/Catalog.java
+++ b/api/src/main/java/com/ning/billing/catalog/api/Catalog.java
@@ -24,11 +24,11 @@ public interface Catalog {
     //
     public abstract String getCatalogName();
 
-    public abstract Currency[] getSupportedCurrencies(DateTime requestedDate);
+    public abstract Currency[] getSupportedCurrencies(DateTime requestedDate) throws CatalogApiException;
 
-	public abstract Product[] getProducts(DateTime requestedDate);
+	public abstract Product[] getProducts(DateTime requestedDate) throws CatalogApiException;
 	
-	public abstract Plan[] getPlans(DateTime requestedDate);
+	public abstract Plan[] getPlans(DateTime requestedDate) throws CatalogApiException;
 
 	
 	//
diff --git a/api/src/main/java/com/ning/billing/catalog/api/StaticCatalog.java b/api/src/main/java/com/ning/billing/catalog/api/StaticCatalog.java
index 0db7db5..c93fde8 100644
--- a/api/src/main/java/com/ning/billing/catalog/api/StaticCatalog.java
+++ b/api/src/main/java/com/ning/billing/catalog/api/StaticCatalog.java
@@ -25,13 +25,13 @@ public interface StaticCatalog {
     //
     public abstract String getCatalogName();
     
-    public abstract Date getEffectiveDate();
+    public abstract Date getEffectiveDate() throws CatalogApiException;
 
-    public abstract Currency[] getCurrentSupportedCurrencies();
+    public abstract Currency[] getCurrentSupportedCurrencies() throws CatalogApiException;
 
-	public abstract Product[] getCurrentProducts();
+	public abstract Product[] getCurrentProducts() throws CatalogApiException;
 	
-	public abstract Plan[] getCurrentPlans();
+	public abstract Plan[] getCurrentPlans() throws CatalogApiException;
 	
 	//
 	// Find a plan
diff --git a/catalog/src/main/java/com/ning/billing/catalog/VersionedCatalog.java b/catalog/src/main/java/com/ning/billing/catalog/VersionedCatalog.java
index f652347..ef9b125 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/VersionedCatalog.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/VersionedCatalog.java
@@ -66,24 +66,16 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalog> implem
 
 	public VersionedCatalog(Clock clock) {
 		this.clock = clock;
-		StandaloneCatalog baseline = new StandaloneCatalog(new Date(0)); // init with an empty catalog may need to 
-													 // populate some empty pieces here to make validation work
-		try {
-			add(baseline);
-		} catch (CatalogApiException e) {
-			// This should never happen
-			log.error("This error should never happpen", e);
-		} 
 	}
 
 	//
 	// Private methods
 	//
-	private StandaloneCatalog versionForDate(DateTime date) {
+	private StandaloneCatalog versionForDate(DateTime date) throws CatalogApiException {
 		return versions.get(indexOfVersionForDate(date.toDate()));
 	}
 
-	private List<StandaloneCatalog> versionsBeforeDate(Date date) {
+	private List<StandaloneCatalog> versionsBeforeDate(Date date) throws CatalogApiException {
 		List<StandaloneCatalog> result = new ArrayList<StandaloneCatalog>();
 		int index = indexOfVersionForDate(date);
 		for(int i = 0; i <= index; i++) {
@@ -92,14 +84,14 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalog> implem
 		return result;
 	}
 
-	private int indexOfVersionForDate(Date date) {
-		for(int i = 1; i < versions.size(); i++) {
+	private int indexOfVersionForDate(Date date) throws CatalogApiException {
+		for(int i = versions.size() - 1; i >= 0; i--) {
 			StandaloneCatalog c = versions.get(i);
-			if(c.getEffectiveDate().getTime() > date.getTime()) {
-				return i - 1;
+			if(c.getEffectiveDate().getTime() < date.getTime()) {
+				return i;
 			}
 		}
-		return versions.size() - 1;
+		throw new CatalogApiException(ErrorCode.CAT_NO_CATALOG_FOR_GIVEN_DATE, date.toString());
 	}
 	
 	private class PlanRequestWrapper {
@@ -205,17 +197,17 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalog> implem
 	}
 
 	@Override
-	public DefaultProduct[] getProducts(DateTime requestedDate) {
+	public DefaultProduct[] getProducts(DateTime requestedDate) throws CatalogApiException {
 		return versionForDate(requestedDate).getCurrentProducts();
 	}
 
 	@Override
-	public Currency[] getSupportedCurrencies(DateTime requestedDate) {
+	public Currency[] getSupportedCurrencies(DateTime requestedDate) throws CatalogApiException {
 		return versionForDate(requestedDate).getCurrentSupportedCurrencies();
 	}
 
 	@Override
-	public DefaultPlan[] getPlans(DateTime requestedDate) {
+	public DefaultPlan[] getPlans(DateTime requestedDate) throws CatalogApiException {
 		return versionForDate(requestedDate).getCurrentPlans();
 	}
 
@@ -350,22 +342,22 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalog> implem
 	// Static catalog API
 	//
 	@Override
-	public Date getEffectiveDate() {
+	public Date getEffectiveDate() throws CatalogApiException {
 		return versionForDate(clock.getUTCNow()).getEffectiveDate();
 	}
 
 	@Override
-	public Currency[] getCurrentSupportedCurrencies() {
+	public Currency[] getCurrentSupportedCurrencies() throws CatalogApiException {
 		return versionForDate(clock.getUTCNow()).getCurrentSupportedCurrencies();
 	}
 
 	@Override
-	public Product[] getCurrentProducts() {
+	public Product[] getCurrentProducts() throws CatalogApiException {
 		return versionForDate(clock.getUTCNow()).getCurrentProducts() ;
 	}
 
 	@Override
-	public Plan[] getCurrentPlans() {
+	public Plan[] getCurrentPlans() throws CatalogApiException {
 		return versionForDate(clock.getUTCNow()).getCurrentPlans();
 	}
 
diff --git a/catalog/src/test/java/com/ning/billing/catalog/TestVersionedCatalog.java b/catalog/src/test/java/com/ning/billing/catalog/TestVersionedCatalog.java
index ddd623e..fbc99df 100644
--- a/catalog/src/test/java/com/ning/billing/catalog/TestVersionedCatalog.java
+++ b/catalog/src/test/java/com/ning/billing/catalog/TestVersionedCatalog.java
@@ -35,6 +35,7 @@ import org.testng.annotations.Test;
 import org.xml.sax.SAXException;
 
 import com.google.common.io.Resources;
+import com.ning.billing.ErrorCode;
 import com.ning.billing.catalog.api.CatalogApiException;
 import com.ning.billing.catalog.api.Currency;
 import com.ning.billing.catalog.api.InvalidConfigException;
@@ -53,14 +54,14 @@ public class TestVersionedCatalog {
 		vc = loader.load(Resources.getResource("versionedCatalog").toString());
 	}
 
-	@Test(enabled=true)
+	@Test(groups={"fast"},enabled=true)
 	public void testAddCatalog() throws MalformedURLException, IOException, SAXException, InvalidConfigException, JAXBException, TransformerException, URISyntaxException, ServiceException, CatalogApiException {
 		vc.add(new StandaloneCatalog(new Date()));
-		assertEquals(5, vc.size());
+		assertEquals(4, vc.size());
 	}
 	
 		
-	@Test(enabled=true)
+	@Test(groups={"fast"},enabled=true)
 	public void testFindPlanWithDates() throws Exception {
 		DateTime dt0= new DateTime("2010-01-01T00:00:00+00:00");
 		DateTime dt1 = new DateTime("2011-01-01T00:01:00+00:00");
@@ -97,6 +98,18 @@ public class TestVersionedCatalog {
 		Assert.assertEquals(exSubPlan214.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("2.0"));
 		Assert.assertEquals(exSubPlan3.getAllPhases()[1].getRecurringPrice().getPrice(Currency.USD), new BigDecimal("2.0"));
 
-		
+	}
+	
+	@Test(groups={"fast"},enabled=true)
+	public void testErrorOnDateTooEarly() {
+		DateTime dt0= new DateTime("1977-01-01T00:00:00+00:00");
+		try {
+			vc.findPlan("foo", dt0);
+			Assert.fail("Date is too early an exception should have been thrown");
+		} catch (CatalogApiException e) {
+			e.printStackTrace();
+			Assert.assertEquals(e.getCode(), ErrorCode.CAT_NO_CATALOG_FOR_GIVEN_DATE.getCode());
+
+		}
 	}
 }