/*
 * Copyright 2014-2015 Groupon, Inc
 * Copyright 2014-2015 The Billing Project, LLC
 *
 * The Billing Project 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 org.killbill.billing.catalog.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.joda.time.DateTime;
import org.killbill.billing.callcontext.MutableCallContext;
import org.killbill.billing.catalog.CatalogTestSuiteWithEmbeddedDB;
import org.killbill.billing.catalog.DefaultPlanPhasePriceOverride;
import org.killbill.billing.catalog.DefaultTierPriceOverride;
import org.killbill.billing.catalog.DefaultTieredBlockPriceOverride;
import org.killbill.billing.catalog.DefaultUsagePriceOverride;
import org.killbill.billing.catalog.StandaloneCatalog;
import org.killbill.billing.catalog.api.CatalogUserApi;
import org.killbill.billing.catalog.api.Currency;
import org.killbill.billing.catalog.api.Plan;
import org.killbill.billing.catalog.api.PlanPhasePriceOverride;
import org.killbill.billing.catalog.api.TierPriceOverride;
import org.killbill.billing.catalog.api.TieredBlockPriceOverride;
import org.killbill.billing.catalog.api.UsagePriceOverride;
import org.killbill.billing.catalog.api.UsageType;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class TestCatalogUserApi extends CatalogTestSuiteWithEmbeddedDB {

	@Inject
	protected CatalogUserApi catalogUserApi;
	
	@Inject
	protected MutableCallContext callContext;
    
	@Test(groups = "slow")
    public void testOverrideUploadCatalog() throws Exception {

        final StandaloneCatalog catalog = getCatalog("SpyCarBasic.xml");
        
        catalogUserApi.uploadCatalog(Resources.asCharSource(Resources.getResource("org/killbill/billing/catalog/SpyCarBasic.xml"), Charsets.UTF_8).read(), callContext);
        
        final Plan plan = catalog.findPlan("standard-monthly");

        final PlanPhasePriceOverride[] resolvedOverrides = new PlanPhasePriceOverride[plan.getAllPhases().length];
        resolvedOverrides[0] = null;
        resolvedOverrides[1] = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, new BigDecimal("128.76"), null);
        final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan, new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);
        assertEquals(newPlan.getParentPlanName(), "standard-monthly");
        assertTrue(newPlan.getIsActive());
    }


}


@@ -736,28 +736,45 @@
 ort 
-java.math.BigDecim
+static org.testng.Assert.assertEqu
 al
+s
 ;%0Aim
@@ -782,50 +782,73 @@
 ort 
-java.util.ArrayList;%0Aimport java.util.List
+static org.testng.Assert.assertTrue;%0A%0Aimport java.math.BigDecimal
 ;%0A%0Ai
@@ -1105,202 +1105,8 @@
 de;%0A
-import org.killbill.billing.catalog.DefaultTierPriceOverride;%0Aimport org.killbill.billing.catalog.DefaultTieredBlockPriceOverride;%0Aimport org.killbill.billing.catalog.DefaultUsagePriceOverride;%0A
 impo
@@ -1376,270 +1376,8 @@
 de;%0A
-import org.killbill.billing.catalog.api.TierPriceOverride;%0Aimport org.killbill.billing.catalog.api.TieredBlockPriceOverride;%0Aimport org.killbill.billing.catalog.api.UsagePriceOverride;%0Aimport org.killbill.billing.catalog.api.UsageType;%0Aimport org.testng.Assert;%0A
 impo
@@ -1493,99 +1493,8 @@
 s;%0A%0A
-import static org.testng.Assert.assertEquals;%0Aimport static org.testng.Assert.assertTrue;%0A%0A
 publ
@@ -1701,16 +1701,17 @@
 w%22)%0A    
+%09
 public v
@@ -2005,32 +2005,32 @@
 , callContext);%0A
+
         %0A       
@@ -2026,693 +2026,8 @@
    %0A
-        final Plan plan = catalog.findPlan(%22standard-monthly%22);%0A%0A        final PlanPhasePriceOverride%5B%5D resolvedOverrides = new PlanPhasePriceOverride%5Bplan.getAllPhases().length%5D;%0A        resolvedOverrides%5B0%5D = null;%0A        resolvedOverrides%5B1%5D = new DefaultPlanPhasePriceOverride(plan.getFinalPhase().getName(), Currency.USD, null, new BigDecimal(%22128.76%22), null);%0A        final CatalogOverridePlanDefinitionModelDao newPlan = catalogOverrideDao.getOrCreateOverridePlanDefinition(plan, new DateTime(catalog.getEffectiveDate()), resolvedOverrides, internalCallContext);%0A        assertEquals(newPlan.getParentPlanName(), %22standard-monthly%22);%0A        assertTrue(newPlan.getIsActive());%0A
     

Update TestCatalogUserApi.java