package rcb.business.customer.service;
import java.util.ArrayList;
import java.util.List;
import java.sql.Time;
import javax.ejb.CreateException;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import rcb.business.customer.constants.CustomerConstants;
import rcb.business.customer.errors.CustomerError;
import rcb.business.customer.service.CustomerRiskBusinessHostBean;
import rcb.business.customer.service.CustomerValidationServiceBean;
import rcb.business.risk.service.entity.RiskIndicatorEntityWrapper;
import rcb.business.risk.wrapper.RiskServiceWrapper;
import rcb.technical.resource.utility.GlobalUniqueIdentifierUtility;
import rcb.business.customer.service.host.view.CustomerRiskServiceHostInterface;
import rcb.business.customer.translator.CustomerRiskResourceTranslator;
import rcb.business.customer.wrapper.CustomerProfileWrapper;
import rcb.business.customer.wrapper.CustomerRiskKeyWrapper;
import rcb.business.customer.wrapper.CustomerRiskWrapper;
import scb.business.erisk.wrapper.RiskIndicatorWrapper;
import scb.ebbs.ecodes.ejb.IDGeneratorSession;
import scb.ebbs.ecodes.ejb.IDGeneratorSessionHome;
import scb.exception.SCBApplicationException;
import scb.technical.integration.service.util.SystemServiceProvider;
import scb.technical.systemutility.exception.BusinessComponentException;
import scb.technical.systemutility.exception.ErrorObject;
import scb.technical.systemutility.exception.ExceptionType;
import scb.technical.systemutility.exception.TechnicalComponentException;
import scb.technical.systemutility.wrapper.Parameter;
import scb.util.LogWriter;
import scb.util.Utility;
import scb.wrapper.UserDetail;

@Stateless
@LocalBean
public class CustomerRiskServiceHostBean implements CustomerRiskServiceHostInterface {
	
	@EJB
    CustomerRiskBusinessHostBean customerRiskBusinessHostBean;
	@EJB
	IDGeneratorSessionHome idGenSessionHome;

	@EJB
    CustomerValidationServiceBean validator;
	
	@Override
	public List<CustomerRiskWrapper> getCustomerRisksByProfileID(String profileID) throws TechnicalComponentException, BusinessComponentException {
		LogWriter log = LogWriter.getInstance("CustomerRiskServiceHostBean", "getCustomerRisksByProfileID");
		List<CustomerRiskWrapper> customerRisks = new ArrayList<CustomerRiskWrapper>();
		List<RiskServiceWrapper> riskServiceWrapper = null;
		
		log.println("Inside getCustomerRisksByProfileID in Service Host Bean Class");
		log.println("Profile ID::: "+profileID);
		
		if(profileID==null || profileID.length()==0)	{
			throw new BusinessComponentException(ExceptionType.BUSINESS,CustomerError.INDIVIDUAL_CUST_PROF_ID_MANDATORY_ERROR.getErrorCode(),CustomerError.INDIVIDUAL_CUST_PROF_ID_MANDATORY_ERROR.getErrorDescription(),CustomerConstants.COMPONENT);
		}
		
		riskServiceWrapper = customerRiskBusinessHostBean.getCustomerRisksByProfileID(profileID);
		
		if(riskServiceWrapper.size()>0 && riskServiceWrapper!=null) {
			for (RiskServiceWrapper entityWrapper : riskServiceWrapper) {

			CustomerRiskResourceTranslator translator = new CustomerRiskResourceTranslator();
			CustomerRiskWrapper wrapper = (CustomerRiskWrapper) translator.translateDomainToResource(entityWrapper);
			customerRisks.add(wrapper);
			}
		} else {
			log.println("Inside else loop in getCustomerRisksByProfileID method");
			throw new BusinessComponentException(ExceptionType.BUSINESS,CustomerError.CUSTOMER_RISK_NOT_FOUND.getErrorCode(),
	           		 CustomerError.CUSTOMER_RISK_NOT_FOUND.getErrorDescription(),CustomerConstants.COMPONENT );
		}
		
		return customerRisks;
	}
	
	@Override
	public CustomerRiskWrapper getCustomerRiskByKey(String[] keys) throws TechnicalComponentException, BusinessComponentException {
		LogWriter log = LogWriter.getInstance("CustomerRiskServiceHostBean", "getCustomerRiskByKey");
		String profileID=keys[0];
		String riskCode=keys[1];
		
		CustomerRiskKeyWrapper keyWrapper = new CustomerRiskKeyWrapper(profileID, riskCode);
		CustomerRiskWrapper customerRiskWrapper = null;
		RiskServiceWrapper riskServiceWrapper = null;
		
		log.println("Inside getCustomerRiskByKey in Service Host Bean Class");
		
		if(profileID==null || profileID.length()==0)	{
			throw new BusinessComponentException(ExceptionType.BUSINESS,CustomerError.INDIVIDUAL_CUST_PROF_ID_MANDATORY_ERROR.getErrorCode(),CustomerError.INDIVIDUAL_CUST_PROF_ID_MANDATORY_ERROR.getErrorDescription(),CustomerConstants.COMPONENT);
		}
		
		riskServiceWrapper = customerRiskBusinessHostBean.getCustomerRiskByKey(keys);   //BDD-Fix-Patch
		
		if(riskServiceWrapper!=null) { 
			CustomerRiskResourceTranslator translator = new CustomerRiskResourceTranslator();
			customerRiskWrapper = (CustomerRiskWrapper) translator.translateDomainToResource(riskServiceWrapper);
		} else {
			log.println("Inside else loop in getCustomerRiskByKey method");
			throw new BusinessComponentException(ExceptionType.BUSINESS,CustomerError.CUSTOMER_RISK_NOT_FOUND.getErrorCode(),
	           		 CustomerError.CUSTOMER_RISK_NOT_FOUND.getErrorDescription(),CustomerConstants.COMPONENT );
		}
		
		return customerRiskWrapper;
	}
	
	@Override
	public List<CustomerRiskWrapper> getCustomerRiskByAttributes(List<Parameter> parameters) throws TechnicalComponentException, BusinessComponentException {
		LogWriter log = LogWriter.getInstance("CustomerRiskServiceHostBean", "getCustomerRiskByAttributes");
		List<CustomerRiskWrapper> customerRisks = new ArrayList<CustomerRiskWrapper>();
		List<RiskIndicatorEntityWrapper> riskEntityWrapper = null;
		RiskServiceWrapper riskServiceWrapper = null;
		List<RiskServiceWrapper> riskServiceWrapperList = new ArrayList<RiskServiceWrapper>();
		
		String profileID = null;
		String riskCode = null;
		if (parameters.get(0) != null) {
			String varName = parameters.get(0).getName();
			if (varName.equals("riskCode")) {
				riskCode = (String) parameters.get(0).getValue();
			} else if (varName.equals("relationshipNumber")) {
				profileID = (String) parameters.get(0).getValue();
			}
		}
		if (parameters.size() > 1 && parameters.get(1) != null) {
			String varName = parameters.get(1).getName();
			if (varName.equals("riskCode")) {
				riskCode = (String) parameters.get(1).getValue();
			} else if (varName.equals("relationshipNumber")) {
				profileID = (String) parameters.get(1).getValue();
			}
		}
		
		log.println("Inside getCustomerRiskByAttributes in Service Host Bean Class");
		
		
		if (profileID == null || profileID.isEmpty()) {
			throw new BusinessComponentException(ExceptionType.BUSINESS,CustomerError.INDIVIDUAL_CUST_PROF_ID_MANDATORY_ERROR.getErrorCode(),CustomerError.INDIVIDUAL_CUST_PROF_ID_MANDATORY_ERROR.getErrorDescription(),CustomerConstants.COMPONENT);
		}
	
		if(riskCode !=null)
		{
			String[] keys = new String[2];
			keys[0]=profileID;
			keys[1]=riskCode;
			customerRisks.add(getCustomerRiskByKey(keys));
		}else {
			customerRisks = getCustomerRisksByProfileID(profileID);
		}
		
		return customerRisks;
	}
	@Override
	public Object createCustomerRisk(CustomerRiskWrapper customerRiskWrapper) throws  BusinessComponentException, TechnicalComponentException{

		LogWriter log = LogWriter.getInstance("CustomerProfileServiceHostBean", "createCustomerRisk");
		RiskServiceWrapper riskServiceWrapperResponse=null;
		
		List<ErrorObject> errorObjList = validateCustomerRisk(customerRiskWrapper,new ArrayList<ErrorObject>());

		if (errorObjList != null && errorObjList.size() > 0) {
			log.println("CustomerRiskServiceHostBean createCustomerRisk Error List : " + errorObjList);
			customerRiskWrapper.setErrorList(errorObjList);
			return customerRiskWrapper;
		}

		log.println("createCustomerRisk validations completed.");
		CustomerRiskResourceTranslator translator = new CustomerRiskResourceTranslator();
		RiskServiceWrapper riskServiceWrapper = (RiskServiceWrapper) translator.translateResourceToDomain(customerRiskWrapper);
		
		riskServiceWrapperResponse = (RiskServiceWrapper) customerRiskBusinessHostBean.createCustomerRisk(riskServiceWrapper);
		log.println("CustomerRiskServiceHostBean : customer risk created"); 
		return (CustomerRiskWrapper) translator.translateDomainToResource(riskServiceWrapperResponse);
	}
	
	private List<ErrorObject> validateCustomerRisk(CustomerRiskWrapper customerRiskWrapper,List<ErrorObject> errorObjList) {
		LogWriter log = LogWriter.getInstance("CustomerProfileServiceHostBean", "validateCustomerRisk");

		log.println("Profile id obtained in validateCustomerRisk  " + customerRiskWrapper.getProfileId());
		log.println("Risk code obtained in validateCustomerRisk  " + customerRiskWrapper.getRiskCode());
	//	validator.validateRelationshipId(customerRiskWrapper,errorObjList);
		validator.validateRelationshipId(customerRiskWrapper.getProfileId(),errorObjList);
        validator.validateLanguageCodeForCustomerRisk(customerRiskWrapper, errorObjList);
		
		log.println("createMasterRisk : validateCustomerRiskLanguageCode validation completed.");


		return errorObjList;
	}
	
	private UserDetail getMakerCheckerDetails(String uploadid) throws SCBApplicationException {
		UserDetail makerCheckerDetails = new UserDetail();
		if (uploadid == null) {
			uploadid = "SYSTEM";
		}
		makerCheckerDetails
		.setUserDate(new java.sql.Date(Utility.convertCurrentTimeToUserTZ(System.currentTimeMillis())));
		makerCheckerDetails
		.setUserTime(new Time(Utility.convertCurrentTimeToUserTZ(System.currentTimeMillis())));
		makerCheckerDetails.setUserBranch("000");
		makerCheckerDetails.setUserID(uploadid);
		makerCheckerDetails.setUserIPAddress("00.00.00.00");
		return makerCheckerDetails;
	} 
	
	
	
	public CustomerRiskWrapper updateCustomerRisk(CustomerRiskWrapper customerRiskReqWrapper)
			throws TechnicalComponentException, BusinessComponentException {
		Object domainRequest = null;
		RiskServiceWrapper riskServiceWrapperResponse=null;
		LogWriter log = LogWriter.getInstance("CustomerAddressServiceHostBean", "updateCustomerRisk");;

		try{
			
			CustomerRiskWrapper customerRiskWrapper = null;
			if(customerRiskReqWrapper != null && customerRiskReqWrapper.getSequenceNumber() == null){
				String guid=customerRiskReqWrapper.getId();
				String[] keys = GlobalUniqueIdentifierUtility.getInstance().deconstruct(guid);
				customerRiskWrapper = (CustomerRiskWrapper) getCustomerRiskByKey(keys);
		 		if(customerRiskWrapper != null){
		 			log.println("customerRiskWrapper wrapper not null data");
		 			if(!customerRiskReqWrapper.getSpecialRemarks().equals(customerRiskWrapper.getSpecialRemarks())) {
		 				log.println("customerRiskWrapper.getSpecialRemarks() and  customerRiskReqWrapper.getSpecialRemarks() are not same...request data set to update.");
		 				customerRiskWrapper.setSpecialRemarks(customerRiskReqWrapper.getSpecialRemarks());
		   			}
				}
			} else {
				customerRiskWrapper = customerRiskReqWrapper;
			}
			
			if(customerRiskWrapper !=null) {
				String guid=customerRiskWrapper.getId();
				String profileId=customerRiskWrapper.getProfileId();
				String riskcode=customerRiskWrapper.getRiskCode();
				
				log.println("GUID--->"+guid+"\n"+"profileId--->"+profileId+"\n"+"riskcode--->"+riskcode);
				
				GlobalUniqueIdentifierUtility.getInstance().validateGUIDwithRequestPayload(profileId,riskcode,null,null,guid); 
			}
				List<ErrorObject> errorObjList = new ArrayList<>();
	
				log.println("Business Validations of Customer Risk Starts.");
			
            
			CustomerRiskResourceTranslator translator = new CustomerRiskResourceTranslator();
			if(customerRiskWrapper !=null) {
				domainRequest = translator.translateResourceToDomain(customerRiskWrapper);
				riskServiceWrapperResponse = customerRiskBusinessHostBean.updateCustomerRisk((RiskServiceWrapper) domainRequest,customerRiskWrapper.getSequenceNumber());
			}
				riskServiceWrapperResponse.setErrorList(errorObjList); 
			return (CustomerRiskWrapper) translator.translateDomainToResource(riskServiceWrapperResponse);

		}catch (TechnicalComponentException te) {
			throw te;
		}

	}
	
	
	public List<ErrorObject> validateCustomerRiskForUpdate(CustomerRiskWrapper customerRiskWrapper) throws TechnicalComponentException
	{
		LogWriter log = LogWriter.getInstance("CustomerProfileServiceHostBean", "validateCustomerRiskForUpdate");
		List<ErrorObject> errorObjList = new ArrayList<>();

		if(customerRiskWrapper !=null)
		{
			validator.validateExpiryDateForUpdateCustomerRisk(customerRiskWrapper, errorObjList);

			if(errorObjList.isEmpty()) {
				log.println("The error object list is empty");
			}
			log.println("ErrorList Size:"+errorObjList.size());
			log.println("Parameters present in errorobjectList: "+errorObjList);

		}
		return errorObjList;
	}
	public void deleteCustomerRisk(String profileId, String riskCode) throws TechnicalComponentException, BusinessComponentException{
		LogWriter log = LogWriter.getInstance("CustomerProfileServiceHostBean","deleteCustomerRisk");
			customerRiskBusinessHostBean.deleteCustomerRisk(profileId, riskCode);
		
	}
}

package rcb.business.customer.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.Assert;

import mockit.Deencapsulation;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import mockit.Tested;
import mockit.integration.junit4.JMockit;
import rcb.business.customer.translator.CustomerRiskResourceTranslator;
import rcb.business.customer.wrapper.CustomerRiskWrapper;
import rcb.business.risk.wrapper.RiskServiceWrapper;
import rcb.technical.resource.utility.GlobalUniqueIdentifierUtility;
import scb.ebbs.ecodes.ejb.IDGeneratorSessionHome;
import scb.technical.persistence.service.dao.PersistenceManager;
import scb.technical.systemutility.exception.BusinessComponentException;
import scb.technical.systemutility.exception.TechnicalComponentException;
import scb.technical.systemutility.wrapper.Parameter;
import scb.util.LogWriter;


@RunWith(JMockit.class)
public class CustomerRiskServiceHostBeanTest {

	@Tested
	CustomerRiskServiceHostBean customerRemarksServiceHostBean;

	@Injectable
	PersistenceManager persistenceManager;
	@Injectable
	IDGeneratorSessionHome idGenSessionHome;

	@Injectable
	CustomerRiskBusinessHostBean customerRemarksBusinessHost;

	@Mocked
	private LogWriter log;

	@Injectable
	CustomerValidationServiceBean validator;

	@Mocked
	CustomerRiskResourceTranslator customerRemarksTranslator;

	@Mocked
	private GlobalUniqueIdentifierUtility globalUniqueIdentifierUtility;

	@Test
	public void getCustomerRiskByKey() throws TechnicalComponentException, BusinessComponentException {
		new Expectations() {
			{
				customerRemarksBusinessHost.getCustomerRiskByKey((new String[] { "234234", "SCB", "4" }));
				result = new RiskServiceWrapper();

			}
		};
		customerRemarksServiceHostBean.getCustomerRiskByKey(new String[] { "234234", "SCB", "4" });
	}

	@Test(expected = BusinessComponentException.class)
	public void getCustomerRiskByKeyExpectBusinessComponentException()
			throws TechnicalComponentException, BusinessComponentException {
		new Expectations() {
			{
				customerRemarksBusinessHost.getCustomerRiskByKey((new String[] { "234234", "SCB", "4" }));
				result = null;

			}
		};
		customerRemarksServiceHostBean.getCustomerRiskByKey(new String[] { "234234", "SCB", "4" });
	}

	@Test(expected = BusinessComponentException.class)
	public void getCustomerRisksByAttributesExpectProfileIdNull()
			throws TechnicalComponentException, BusinessComponentException {
		List<Parameter> parameters = new ArrayList<>();
		parameters.add(new Parameter("profileId", "", "EQ", String.class));
		parameters.add(new Parameter("riskCode", "RISC", "EQ", String.class));
		// parameters.add(new Parameter("seqNo", "1", "EQ", String.class));
		customerRemarksServiceHostBean.getCustomerRiskByAttributes(parameters);
	}

	@Test
	public void testupdateCustomerRisk() throws TechnicalComponentException, BusinessComponentException{
		try {
			String[] keys = new String[2];
			keys[0] = "abbb";
			keys[1] = "abc";
			RiskServiceWrapper wrapper = new RiskServiceWrapper();
			new Expectations(){
				{ Deencapsulation.invoke(globalUniqueIdentifierUtility, "getInstance");
					result = globalUniqueIdentifierUtility;
				}
				{
					globalUniqueIdentifierUtility.deconstruct(anyString);result = keys;
				}
				{customerRemarksBusinessHost.getCustomerRiskByKey(keys);   result = wrapper;};
			};
			CustomerRiskWrapper customerRiskReqWrapper = new CustomerRiskWrapper();

			customerRemarksServiceHostBean.updateCustomerRisk(customerRiskReqWrapper);
		}catch(Exception e){

		}
	}

	@Test
	public void testGetCustomerRisksByProfileID() throws TechnicalComponentException, BusinessComponentException {
		// Mock dependencies and set expectations
		new Expectations() {{
			customerRemarksServiceHostBean.getCustomerRisksByProfileID("12345");
			result = new ArrayList<RiskServiceWrapper>() {{
				add(new RiskServiceWrapper());
			}};
		}};

		// Call the method under test
		List<CustomerRiskWrapper> customerRisks = customerRemarksServiceHostBean.getCustomerRisksByProfileID("12345");

		// Assert the results
		assertNotNull(customerRisks);
		assertEquals(1, customerRisks.size());
	}

	@Test(expected = BusinessComponentException.class)
	public void testGetCustomerRisksByProfileIDExpectException() throws TechnicalComponentException, BusinessComponentException {
		// Mock dependencies and set expectations
		new Expectations() {{
			customerRemarksServiceHostBean.getCustomerRisksByProfileID("12345");
			result = new ArrayList<RiskServiceWrapper>();
		}};

		// Call the method under test
		customerRemarksServiceHostBean.getCustomerRisksByProfileID("12345");
	}

}



write a test method to cover the getCustomerRiskByAttributes method