package com.palmnest.service.impl;

import com.palmnest.application.core.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.runner.RunWith;

import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * A mock class for testing using JMock. This test class can be moved to the test tree.
 *
 * @author <a href="mailto:chasel.java@gmail.com">Chasel Li</a>
 */
@Slf4j
@RunWith(JMock.class)
public abstract class BaseManagerMockTestCase {

	/**
	 * The resourceBundle
	 */
	protected ResourceBundle rb;
	/**
	 * The junit 4 context
	 */
	protected Mockery        context = new JUnit4Mockery();

	/**
	 * Default constructor will set the ResourceBundle if needed.
	 */
	public BaseManagerMockTestCase() {
		// Since a ResourceBundle is not required for each class, just
		// do a simple check to see if one exists
		String className = this.getClass().getName();

		try {
			rb = ResourceBundle.getBundle(className);
		} catch (MissingResourceException mre) {
			//log.debug("No resource bundle found for: " + className);
		}
	}

	/**
	 * Utility method to populate a javabean-style object with values
	 * from a Properties file
	 *
	 * @param obj the model object to populate
	 * @return Object populated object
	 * @throws Exception if BeanUtils fails to copy properly
	 */
	@SuppressWarnings("rawtypes")
	protected Object populate(Object obj) throws Exception {
		// loop through all the beans methods and set its properties from
		// its .properties file
		Map map = ConvertUtil.convertBundleToMap(rb);

		BeanUtils.copyProperties(obj, map);

		return obj;
	}
}
