package org.mifos.accounts.productdefinition.business.service;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import junit.framework.Assert;

import org.junit.Test;
import org.mifos.accounts.productdefinition.business.PrdApplicableMasterEntity;
import org.mifos.accounts.productdefinition.persistence.LoanPrdPersistence;
import org.mifos.accounts.productdefinition.persistence.PrdOfferingPersistence;
import org.mifos.accounts.productdefinition.util.helpers.PrdCategoryStatus;
import org.mifos.accounts.productdefinition.util.helpers.ProductType;
import org.mifos.application.master.persistence.MasterPersistence;
import org.mifos.framework.exceptions.PersistenceException;
import org.mifos.framework.exceptions.ServiceException;
import org.springframework.test.annotation.ExpectedException;

public class LoanPrdBusinessServiceTest {
    final PrdOfferingPersistence prdOfferingPersistence = mock(PrdOfferingPersistence.class);
    final LoanPrdPersistence loanPrdPersistence = mock(LoanPrdPersistence.class);
    final MasterPersistence masterPersistence = mock(MasterPersistence.class);

    LoanPrdBusinessService loanPrdBusinessService = new LoanPrdBusinessService() {
        @Override
        protected PrdOfferingPersistence getPrdOfferingPersistence() {
            return prdOfferingPersistence;
        }

        @Override
        protected LoanPrdPersistence getLoanPrdPersistence() {
            return loanPrdPersistence;
        }

        @Override
        protected MasterPersistence getMasterPersistence() {
            return masterPersistence;
        }
    };
    private Short localeId = new Short("1");

    @Test
    @ExpectedException(value = ServiceException.class)
    public void testInvalidConnectionThrowsExceptionInGetApplicableProductCategories() throws PersistenceException {

        try {
            when(prdOfferingPersistence.getApplicableProductCategories(ProductType.LOAN, PrdCategoryStatus.ACTIVE)).
                    thenThrow(new PersistenceException("some exception"));
            loanPrdBusinessService.getActiveLoanProductCategories();
            Assert.fail("should fail because of invalid session");
        } catch (ServiceException e) {
        }
    }

    @Test
    @ExpectedException(value = ServiceException.class)
    public void testInvalidConnectionThrowsExceptionInGetLoanApplicableCustomerTypes() throws PersistenceException {

        try {
            when(masterPersistence.retrieveMasterEntities(PrdApplicableMasterEntity.class, localeId)).thenThrow(new PersistenceException("some exception"));
            loanPrdBusinessService.getLoanApplicableCustomerTypes(localeId);
            Assert.fail("should fail because of invalid session");
        } catch (ServiceException e) {
        }
    }

    @Test
    @ExpectedException(value = ServiceException.class)
    public void testInvalidConnectionThrowsExceptionInGetApplicablePrdStatus() throws PersistenceException {

        try {
            when(prdOfferingPersistence.getApplicablePrdStatus(ProductType.LOAN, localeId)).
                    thenThrow(new PersistenceException("some exception"));
            loanPrdBusinessService.getApplicablePrdStatus(localeId);
            Assert.fail("should fail because of invalid session");
        } catch (ServiceException e) {
        }
    }

    @Test
    @ExpectedException(value = ServiceException.class)
    public void testInvalidConnectionThrowsExceptionInGetAllLoanOfferings() throws PersistenceException {

        try {
            when(loanPrdPersistence.getAllLoanOfferings(localeId)).thenThrow(new PersistenceException("some exception"));
            loanPrdBusinessService.getAllLoanOfferings(localeId);
            Assert.fail("should fail because of invalid session");
        } catch (ServiceException e) {
        }
    }

    @Test
    @ExpectedException(value = ServiceException.class)
    public void testInvalidConnectionThrowsExceptionInGetLoanOffering() throws PersistenceException {

        try {
            when(loanPrdPersistence.getLoanOffering(new Short("112"))).thenThrow(new PersistenceException("some exception"));
            loanPrdBusinessService.getLoanOffering(new Short("112"));
            Assert.fail("should fail because of invalid session");
        } catch (ServiceException e) {
        }
    }

    @Test
    @ExpectedException(value = ServiceException.class)
    public void testInvalidConnectionThrowsExceptionInGetLoanOfferingWithLocaleId() throws PersistenceException {

        try {
            when(loanPrdPersistence.getLoanOffering(new Short("112"), localeId)).thenThrow(new PersistenceException("some exception"));
            loanPrdBusinessService.getLoanOffering(new Short("112"), localeId);
            Assert.fail("should fail because of invalid session");
        } catch (ServiceException e) {
        }
    }

}

@@ -2240,562 +2240,8 @@
  %7D%0A%0A
-    @Test%0A    @ExpectedException(value = ServiceException.class)%0A    public void testInvalidConnectionThrowsExceptionInGetLoanApplicableCustomerTypes() throws PersistenceException %7B%0A%0A        try %7B%0A            when(masterPersistence.retrieveMasterEntities(PrdApplicableMasterEntity.class, localeId)).thenThrow(new PersistenceException(%22some exception%22));%0A            loanPrdBusinessService.getLoanApplicableCustomerTypes(localeId);%0A            Assert.fail(%22should fail because of invalid session%22);%0A        %7D catch (ServiceException e) %7B%0A        %7D%0A    %7D%0A%0A
     

Remove the tests for verifying PersistenceException is being thrown by MasterPersistence.retrieveMasterEntities: We don't need special purpose exception wrapper as we want to move to spring managed persistence