/*
 * Copyright 2020-2020 Equinix, Inc
 * Copyright 2014-2020 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.beatrix.integration;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import java.io.IOException;

import org.joda.time.DateTime;
import org.killbill.billing.ErrorCode;
import org.killbill.billing.account.api.Account;
import org.killbill.billing.catalog.api.CatalogApiException;
import org.killbill.billing.util.callcontext.CallContext;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

public class TestCatalogValidation extends TestIntegrationBase {

    private CallContext testCallContext;
    private Account account;

    @BeforeMethod(groups = "slow")
    public void beforeMethod() throws Exception {
        if (hasFailed()) {
            return;
        }

        super.beforeMethod();

        // Setup tenant
        clock.setTime(new DateTime("2021-10-24T12:56:02"));
        testCallContext = setupTenant();

        // Setup account in right tenant
        account = setupAccount(testCallContext);
    }

    @Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/1481")
    public void testUploadCatalog() throws Exception {
        uploadCatalog("CatalogValidation-v1.xml");
        assertListenerStatus();
        try {
            uploadCatalog("CatalogValidation-v2.xml");
            assertListenerStatus();
    	} catch (CatalogApiException cApiException) {
    		assertEquals(cApiException.getCode(), ErrorCode.CAT_INVALID_FOR_TENANT.getCode());
    	}
    }
    
    @Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/1465")
	public void testUploadCatalogPlanValidation() throws Exception {
		try {
			uploadCatalog("CatalogValidation-v2.xml");
			assertListenerStatus();
		} catch (CatalogApiException cApiException) {
			assertEquals(cApiException.getCode(), ErrorCode.CAT_INVALID_FOR_TENANT.getCode());
		}

		try {
			uploadCatalog("CatalogValidation-v3.xml");
			assertListenerStatus();
		} catch (CatalogApiException cApiException) {
			//assertEquals(cApiException.getCode(), ErrorCode.CAT_INVALID_FOR_TENANT.getCode());
			assertNotEquals(cApiException.getCode(), ErrorCode.CAT_INVALID_FOR_TENANT.getCode());
		}

	}
    private void uploadCatalog(final String name) throws CatalogApiException, IOException {
        catalogUserApi.uploadCatalog(Resources.asCharSource(Resources.getResource("catalogs/testCatalogValidation/" + name), Charsets.UTF_8).read(), testCallContext);
    }
    

}

@@ -2377,17 +2377,20 @@
 /1465%22)%0A
-%09
+    
 public v
@@ -2446,25 +2446,24 @@
 ption %7B%0A
-%09
 %09try %7B%0A%09
 %09%09upload
@@ -2446,34 +2446,36 @@
 ption %7B%0A%09try %7B%0A%09
-%09%09
+    
 uploadCatalog(%22C
@@ -2494,34 +2494,36 @@
 tion-v2.xml%22);%0A%09
-%09%09
+    
 assertListenerSt
@@ -2522,33 +2522,32 @@
 stenerStatus();%0A
-%09
 %09%7D catch (Catalo
@@ -2577,17 +2577,16 @@
 tion) %7B%0A
-%09
 %09%09assert
@@ -2666,22 +2666,20 @@
 ));%0A
-%09
 %09%7D%0A%0A
-%09
 %09try %7B%0A%09
 %09%09up
@@ -2674,18 +2674,20 @@
 %09try %7B%0A%09
-%09%09
+    
 uploadCa
@@ -2718,26 +2718,28 @@
 -v3.xml%22);%0A%09
-%09%09
+    
 assertListen
@@ -2750,17 +2750,16 @@
 atus();%0A
-%09
 %09%7D catch
@@ -2797,17 +2797,16 @@
 tion) %7B%0A
-%09
 %09%09//asse
@@ -2886,17 +2886,16 @@
 e());%0A%09%09
-%09
 assertNo
@@ -2976,14 +2976,16 @@
 ));%0A
-%09
 %09%7D%0A%0A
-%09
+    
 %7D%0A  

Update TestCatalogValidation.java