package gov.nih.nci.cabig.caaers.web.search;

import static org.easymock.EasyMock.expect;
import gov.nih.nci.cabig.caaers.dao.AgentDao;
import gov.nih.nci.cabig.caaers.dao.CtcTermDao;
import gov.nih.nci.cabig.caaers.dao.SearchDao;
import gov.nih.nci.cabig.caaers.domain.LocalStudy;
import gov.nih.nci.cabig.caaers.domain.repository.ajax.ParticipantAjaxableDomainObjectRepository;
import gov.nih.nci.cabig.caaers.domain.repository.ajax.StudySearchableAjaxableDomainObjectRepository;
import gov.nih.nci.cabig.caaers.security.SecurityTestUtils;
import gov.nih.nci.cabig.caaers.web.DwrFacadeTestCase;
import gov.nih.nci.cabig.caaers.web.dwr.AjaxOutput;
import gov.nih.nci.cabig.caaers.web.search.ui.DependentObject;
import gov.nih.nci.cabig.caaers.web.search.ui.SearchTargetObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;

/**
 *
 * @author Ramakrishna Gundala
 */
public class AdvancedSearchAjaxFacadeTest extends DwrFacadeTestCase {
	
	protected AdvancedSearchAjaxFacade searchFacade;
	protected StudySearchableAjaxableDomainObjectRepository studySearchableAjaxableDomainObjectRepository;
	protected ParticipantAjaxableDomainObjectRepository participantAjaxableDomainObjectRepository;
	protected SearchDao searchDao;
	protected CtcTermDao ctcTermDao;
	protected AgentDao agentDao;
	protected AdvancedSearchCommand command;
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		SecurityTestUtils.switchToSuperuser();
		searchFacade =  new AdvancedSearchAjaxFacade();
		command = new AdvancedSearchCommand();
		studySearchableAjaxableDomainObjectRepository = 
				registerMockFor(StudySearchableAjaxableDomainObjectRepository.class);
		participantAjaxableDomainObjectRepository = 
				registerMockFor(ParticipantAjaxableDomainObjectRepository.class);
		searchDao = registerDaoMockFor(SearchDao.class);
		ctcTermDao = registerDaoMockFor(CtcTermDao.class);
		agentDao = registerDaoMockFor(AgentDao.class);
		
		searchFacade.setAgentDao(agentDao);
		searchFacade.setCtcTermDao(ctcTermDao);
		searchFacade.setParticipantAjaxableDomainObjectRepository(participantAjaxableDomainObjectRepository);
		searchFacade.setSearchDao(searchDao);
		searchFacade.setStudySearchableAjaxableDomainObjectRepository(studySearchableAjaxableDomainObjectRepository);
		
		expect(webContext.getSession()).andReturn(session).anyTimes();
		session.setAttribute("gov.nih.nci.cabig.caaers.web.search.AdvancedSearchController.FORM.command",command);
		
	}
	
	public void testupdateSearchTargetObject(){
		replayMocks();
		AjaxOutput test = searchFacade.updateSearchTargetObject("");
		assertNotNull(test);
	}
	
	
	public void testupdateAttribute(){
		List<AdvancedSearchCriteriaParameter> criteriaParams = new ArrayList<AdvancedSearchCriteriaParameter>();
		AdvancedSearchCriteriaParameter param1 = new AdvancedSearchCriteriaParameter();
		param1.setAttributeName("type");
		param1.setObjectName("Identifier");
		param1.setPredicate("=");
		param1.setValue("val1");
		param1.setDependentObjectName("gov.nih.nci.cabig.caaers.domain.LocalStudy");
		criteriaParams.add(param1);
		SearchTargetObject searchTObject = new SearchTargetObject();
		DependentObject depObject1 = new DependentObject();
		depObject1.setClassName(LocalStudy.class.getName());
		searchTObject.setDependentObject(Arrays.asList(new DependentObject[]{depObject1}));
		command.setSearchTargetObject(searchTObject);
		command.setCriteriaParameters(criteriaParams);
		expect(webContext.getCurrentPage()).andReturn("0");
		try {
			expect(webContext.forwardToString("0?ajax_action=updateAttribute&index=0&attributeName=attributeName&subview=updateValueContent")).andReturn("<p></p>");
		} catch (ServletException e) {
			e.printStackTrace();
			fail();
		} catch (IOException e) {
			e.printStackTrace();
			fail();
		}
		replayMocks();
		AjaxOutput test = searchFacade.updateAttribute("attributeName", "attributeLabel", 0);
		assertNotNull(test);
	}
	
}
@@ -784,16 +784,76 @@
 Object;%0D
+%0Aimport gov.nih.nci.cabig.caaers.web.search.ui.UiAttribute;%0D
 %0A%0D%0Aimpor
@@ -999,16 +999,49 @@
 ion;%0D%0A%0D%0A
+import org.easymock.EasyMock;%0D%0A%0D%0A
 /**%0D%0A *%0D
@@ -3746,16 +3746,767 @@
 (%220%22);%0D%0A
+%09%09DependentObject dpObject = new DependentObject();%0D%0A%09%09%0D%0A%09%09List%3CUiAttribute%3E uiAttributes = new ArrayList%3CUiAttribute%3E();%0D%0A%09%09UiAttribute uiAttribute = new UiAttribute();%0D%0A%09%09uiAttribute.setName(%22title%22);%0D%0A%09%09uiAttribute.setDataType(%22String%22);%0D%0A%09%09uiAttribute.setAjaxMethod(%22addAttribute%22);%0D%0A%09%09uiAttributes.add(uiAttribute);%0D%0A%09%09%0D%0A%09%09SearchTargetObject sto = new SearchTargetObject();%0D%0A%09%09DependentObject dpO = new DependentObject();%0D%0A%09%09dpO.setClassName(%22Study%22);%0D%0A%09%09List%3CDependentObject%3E depObjs = new ArrayList%3CDependentObject%3E();%0D%0A%09%09depObjs.add(dpO);%0D%0A%09%09sto.setDependentObject(depObjs);%0D%0A%09%09String dependeontObjectName = %22Study%22;%0D%0A%09%0D%0A%09%09%0D%0A%09%09dpObject.setUiAttribute(uiAttributes);%0D%0A%09%09AdvancedSearchUiUtil.getDependentObjectByName(sto,dependeontObjectName);%0D%0A
 %09%09try %7B%0D

test cases coverage