killbill-uncached

Merge branch 'irs-integration' of github.com:ning/killbill

11/19/2011 2:35:05 AM

Details

diff --git a/catalog/src/main/java/com/ning/billing/catalog/InternationalPrice.java b/catalog/src/main/java/com/ning/billing/catalog/InternationalPrice.java
index 63cf388..f9d35c2 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/InternationalPrice.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/InternationalPrice.java
@@ -17,6 +17,7 @@
 package com.ning.billing.catalog;
 
 import java.math.BigDecimal;
+import java.net.URI;
 import java.util.Date;
 
 import javax.xml.bind.annotation.XmlAccessType;
@@ -31,11 +32,12 @@ import com.ning.billing.util.config.ValidationErrors;
 
 @XmlAccessorType(XmlAccessType.NONE)
 public class InternationalPrice extends ValidatingConfig<Catalog> implements IInternationalPrice {
+	private static Price[] zeroPrice;
 
 	//TODO MDW Validation - effectiveDateForExistingSubscriptons > catalog effectiveDate 
 	@XmlElement(required=false)
 	private Date effectiveDateForExistingSubscriptons;
-	
+
 	//TODO: Must have a price point for every configured currency
 	//TODO: No prices is a zero cost plan
 	@XmlElement(name="price")
@@ -71,6 +73,17 @@ public class InternationalPrice extends ValidatingConfig<Catalog> implements IIn
 		return new BigDecimal(0);
 	}
 
+	protected void setEffectiveDateForExistingSubscriptons(
+			Date effectiveDateForExistingSubscriptons) {
+		this.effectiveDateForExistingSubscriptons = effectiveDateForExistingSubscriptons;
+	}
+
+	protected InternationalPrice setPrices(Price[] prices) {
+		this.prices = prices;
+		return this;
+	}
+
+
 	@Override
 	public ValidationErrors validate(Catalog catalog, ValidationErrors errors)  {
 		if(prices.length == 0) return errors;
@@ -92,15 +105,26 @@ public class InternationalPrice extends ValidatingConfig<Catalog> implements IIn
 		}
 		return false;
 	}
-	
-	protected void setEffectiveDateForExistingSubscriptons(
-			Date effectiveDateForExistingSubscriptons) {
-		this.effectiveDateForExistingSubscriptons = effectiveDateForExistingSubscriptons;
+
+	@Override
+	public void initialize(Catalog root, URI uri) {
+		if(prices == null) {
+			prices = getZeroPrice(root);
+		}
+		super.initialize(root, uri);
 	}
 
-	protected InternationalPrice setPrices(Price[] prices) {
-		this.prices = prices;
-		return this;
+	private synchronized Price[] getZeroPrice(Catalog root) {
+		if(zeroPrice == null) {
+			Currency[] currencies = root.getSupportedCurrencies();
+			zeroPrice = new Price[currencies.length];
+			for(int i = 0; i < currencies.length; i++) {
+				zeroPrice[i] = new Price();
+				zeroPrice[i].setCurrency(currencies[i]);
+				zeroPrice[i].setValue(new BigDecimal(0));
+			}
+		}
+		return zeroPrice;
 	}
 
 }
diff --git a/catalog/src/main/java/com/ning/billing/catalog/Price.java b/catalog/src/main/java/com/ning/billing/catalog/Price.java
index 369b9ac..cdbf464 100644
--- a/catalog/src/main/java/com/ning/billing/catalog/Price.java
+++ b/catalog/src/main/java/com/ning/billing/catalog/Price.java
@@ -51,7 +51,7 @@ public class Price extends ValidatingConfig<Catalog> implements IPrice {
 		return value;
 	}
 	
-	public Price setCurrency(Currency currency) {
+	protected Price setCurrency(Currency currency) {
 		this.currency = currency;
 		return this;
 	}
diff --git a/catalog/src/test/java/com/ning/billing/catalog/TestInternationalPrice.java b/catalog/src/test/java/com/ning/billing/catalog/TestInternationalPrice.java
new file mode 100644
index 0000000..a02541d
--- /dev/null
+++ b/catalog/src/test/java/com/ning/billing/catalog/TestInternationalPrice.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2010-2011 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+package com.ning.billing.catalog;
+
+import java.math.BigDecimal;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.ning.billing.catalog.api.Currency;
+
+public class TestInternationalPrice {
+  @Test
+  public void testZeroValue() throws URISyntaxException {
+	  Catalog c = new MockCatalog();
+	  c.setSupportedCurrencies(new Currency[]{Currency.GBP, Currency.EUR, Currency.USD, Currency.BRL, Currency.MXN});
+	  InternationalPrice p0 =  new MockInternationalPrice();
+	  p0.setPrices(null);
+	  p0.initialize(c, new URI("foo:bar"));
+	  InternationalPrice p1 =  new MockInternationalPrice();
+	  p1.setPrices(new Price[] {
+			  new Price().setCurrency(Currency.GBP).setValue(new BigDecimal(1)),
+			  new Price().setCurrency(Currency.EUR).setValue(new BigDecimal(1)),
+			  new Price().setCurrency(Currency.USD).setValue(new BigDecimal(1)),
+			  new Price().setCurrency(Currency.BRL).setValue(new BigDecimal(1)),
+			  new Price().setCurrency(Currency.MXN).setValue(new BigDecimal(1)),		  
+	  });
+	  p1.initialize(c, new URI("foo:bar"));
+
+	  Assert.assertEquals(p0.getPrice(Currency.GBP), new BigDecimal(0));
+	  Assert.assertEquals(p0.getPrice(Currency.EUR), new BigDecimal(0));
+	  Assert.assertEquals(p0.getPrice(Currency.USD), new BigDecimal(0));
+	  Assert.assertEquals(p0.getPrice(Currency.BRL), new BigDecimal(0));
+	  Assert.assertEquals(p0.getPrice(Currency.MXN), new BigDecimal(0));
+	  
+	  Assert.assertEquals(p1.getPrice(Currency.GBP), new BigDecimal(1));
+	  Assert.assertEquals(p1.getPrice(Currency.EUR), new BigDecimal(1));
+	  Assert.assertEquals(p1.getPrice(Currency.USD), new BigDecimal(1));
+	  Assert.assertEquals(p1.getPrice(Currency.BRL), new BigDecimal(1));
+	  Assert.assertEquals(p1.getPrice(Currency.MXN), new BigDecimal(1));
+	  
+  }
+}