<commit_before>package org.kuali.rice.kew.docsearch.service.impl

import org.junit.Before
import org.junit.Test
import org.kuali.rice.kew.api.document.lookup.DocumentLookupCriteria
import org.kuali.rice.kew.useroptions.UserOptions
import org.kuali.rice.kew.useroptions.UserOptionsServiceImpl
import org.kuali.rice.kew.useroptions.dao.UserOptionsDAO
import static org.junit.Assert.assertEquals
import static DocumentLookupCriteriaTest.marshall
import static DocumentLookupCriteriaTest.unmarshall
import static DocumentLookupCriteriaTest.create

/**
 * Unit tests DocumentLookupCriteria saving behavior of DocumentSearchServiceImpl
 */
class DocSearchSavingTest {
    // mock out UserOptionsDAO - just save in memory
    private def mockUserOptionsDAO = new UserOptionsDAO() {
        def options = new HashMap<String, String>()
        Collection<UserOptions> findByWorkflowUser(String principalId) {
            options.collect {
                def opt = new UserOptions();
                opt.optionId = it.key
                opt.optionVal = it.value
                opt
            }
        }
        List<UserOptions> findByUserQualified(String principalId, String likeString) { null }
        void deleteByUserQualified(String principalId, String likeString) { }
        void save(UserOptions userOptions) {
            options.put(userOptions.optionId, userOptions.optionVal)
        }
        void save(Collection<UserOptions> userOptions) {
            userOptions.each { save(it) }
        }
        void deleteUserOptions(UserOptions userOptions) {
            options.remove(userOptions.optionId)
        }
        UserOptions findByOptionId(String optionId, String principalId) {
            if (options[optionId]) {
              def opt = new UserOptions()
              opt.optionId = optionId
              opt.optionVal = options[optionId]
              opt
            } else {
              null
            }
        }
        Collection<UserOptions> findByOptionValue(String optionId, String optionValue) { null }
        Long getNewOptionIdForActionList() { 0 }
    }

    private def userOptionsService = new UserOptionsServiceImpl()
    private def docSearchService = new DocumentSearchServiceImpl() {
        public void saveSearch(String principalId, DocumentLookupCriteria criteria) {
            super.saveSearch(principalId, criteria)
        }
    }

    @Before
    void init() {
        userOptionsService.setUserOptionsDAO(mockUserOptionsDAO)
        docSearchService.setUserOptionsService(userOptionsService)
    }

    @Test
    void testUnnamedDocSearch() {
        def princ = "not blank" // mocked...

        def allUserOptions_before = userOptionsService.findByWorkflowUser(princ)

        assertEquals(0, allUserOptions_before.size())

        def c1 = create()
        docSearchService.saveSearch(princ, c1)

        def allUserOptions_after = userOptionsService.findByWorkflowUser(princ)

        // saves the "last doc search criteria"
        // and a pointer to the "last doc search criteria"
        assertEquals(allUserOptions_before.size() + 2, allUserOptions_after.size())

        assertEquals("DocSearch.LastSearch.Holding0", userOptionsService.findByOptionId("DocSearch.LastSearch.Order", princ).optionVal)
        assertEquals(marshall(c1), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding0", princ).optionVal)

        // 2nd search

        def c2 = create()
        docSearchService.saveSearch(princ, c2)

        allUserOptions_after = userOptionsService.findByWorkflowUser(princ)

        // 1 more user option
        assertEquals(allUserOptions_before.size() + 3, allUserOptions_after.size())
        
        assertEquals("DocSearch.LastSearch.Holding1,DocSearch.LastSearch.Holding0", userOptionsService.findByOptionId("DocSearch.LastSearch.Order", princ).optionVal)
        assertEquals(marshall(c1), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding0", princ).optionVal)
        assertEquals(marshall(c2), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding1", princ).optionVal)
    }

    @Test
    void testNamedDocSearch() {
        def princ = "not blank" // mocked...

        def allUserOptions_before = userOptionsService.findByWorkflowUser(princ)

        assertEquals(0, allUserOptions_before.size())

        def c1 = create("save1")
        docSearchService.saveSearch(princ, c1)

        def allUserOptions_after = userOptionsService.findByWorkflowUser(princ)

        assertEquals(allUserOptions_before.size() + 1, allUserOptions_after.size())
        assertEquals(marshall(c1), userOptionsService.findByOptionId("DocSearch.NamedSearch." + c1.getSaveName(), princ).optionVal)

        // 2nd search

        def c2 = create("save2")
        docSearchService.saveSearch(princ, c2)

        allUserOptions_after = userOptionsService.findByWorkflowUser(princ)

        // saves a second named search
        assertEquals(allUserOptions_before.size() + 2, allUserOptions_after.size())
        assertEquals(marshall(c2), userOptionsService.findByOptionId("DocSearch.NamedSearch." + c2.getSaveName(), princ).optionVal)
    }
}<commit_msg>KULRICE-5316 test a couple of remaining cases<commit_after>package org.kuali.rice.kew.docsearch.service.impl

import org.junit.Before
import org.junit.Test
import org.kuali.rice.kew.api.document.lookup.DocumentLookupCriteria
import org.kuali.rice.kew.useroptions.UserOptions
import org.kuali.rice.kew.useroptions.UserOptionsServiceImpl
import org.kuali.rice.kew.useroptions.dao.UserOptionsDAO
import static org.junit.Assert.assertEquals
import static DocumentLookupCriteriaTest.marshall
import static DocumentLookupCriteriaTest.unmarshall
import static DocumentLookupCriteriaTest.create

/**
 * Unit tests DocumentLookupCriteria saving behavior of DocumentSearchServiceImpl
 */
class DocSearchSavingTest {
    // mock out UserOptionsDAO - just save in memory
    private def mockUserOptionsDAO = new UserOptionsDAO() {
        def options = new HashMap<String, String>()
        Collection<UserOptions> findByWorkflowUser(String principalId) {
            options.collect {
                def opt = new UserOptions();
                opt.optionId = it.key
                opt.optionVal = it.value
                opt
            }
        }
        List<UserOptions> findByUserQualified(String principalId, String likeString) { null }
        void deleteByUserQualified(String principalId, String likeString) { }
        void save(UserOptions userOptions) {
            options.put(userOptions.optionId, userOptions.optionVal)
        }
        void save(Collection<UserOptions> userOptions) {
            userOptions.each { save(it) }
        }
        void deleteUserOptions(UserOptions userOptions) {
            options.remove(userOptions.optionId)
        }
        UserOptions findByOptionId(String optionId, String principalId) {
            if (options[optionId]) {
              def opt = new UserOptions()
              opt.optionId = optionId
              opt.optionVal = options[optionId]
              opt
            } else {
              null
            }
        }
        Collection<UserOptions> findByOptionValue(String optionId, String optionValue) { null }
        Long getNewOptionIdForActionList() { 0 }
    }

    private def userOptionsService = new UserOptionsServiceImpl()
    private def docSearchService = new DocumentSearchServiceImpl() {
        public void saveSearch(String principalId, DocumentLookupCriteria criteria) {
            super.saveSearch(principalId, criteria)
        }
    }

    @Before
    void init() {
        userOptionsService.setUserOptionsDAO(mockUserOptionsDAO)
        docSearchService.setUserOptionsService(userOptionsService)
    }

    @Test(expected=IllegalArgumentException)
    void testIllegalArgument() {
        docSearchService.saveSearch(null, create())
    }

    @Test
    void testConsumesExceptions() {
        // assuming a null criteria will cause an NPE
        docSearchService.saveSearch("princ", null)
    }

    @Test
    void testUnnamedDocSearch() {
        def princ = "not blank" // mocked...

        def allUserOptions_before = userOptionsService.findByWorkflowUser(princ)

        assertEquals(0, allUserOptions_before.size())

        def c1 = saveSearch(princ)

        def allUserOptions_after = userOptionsService.findByWorkflowUser(princ)

        // saves the "last doc search criteria"
        // and a pointer to the "last doc search criteria"
        assertEquals(allUserOptions_before.size() + 2, allUserOptions_after.size())

        assertEquals("DocSearch.LastSearch.Holding0", userOptionsService.findByOptionId("DocSearch.LastSearch.Order", princ).optionVal)
        assertEquals(marshall(c1), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding0", princ).optionVal)

        // 2nd search

        def c2 = saveSearch(princ)

        allUserOptions_after = userOptionsService.findByWorkflowUser(princ)

        // 1 more user option
        assertEquals(allUserOptions_before.size() + 3, allUserOptions_after.size())
        assertEquals("DocSearch.LastSearch.Holding1,DocSearch.LastSearch.Holding0", userOptionsService.findByOptionId("DocSearch.LastSearch.Order", princ).optionVal)
        assertEquals(marshall(c1), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding0", princ).optionVal)
        assertEquals(marshall(c2), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding1", princ).optionVal)
    }

    @Test
    void testNamedDocSearch() {
        def princ = "not blank" // mocked...
        def allUserOptions_before = userOptionsService.findByWorkflowUser(princ)

        assertEquals(0, allUserOptions_before.size())

        def c1 = saveSearch(princ, "save1")

        def allUserOptions_after = userOptionsService.findByWorkflowUser(princ)
        assertEquals(allUserOptions_before.size() + 1, allUserOptions_after.size())
        assertEquals(marshall(c1), userOptionsService.findByOptionId("DocSearch.NamedSearch." + c1.getSaveName(), princ).optionVal)

        // 2nd search

        def c2 = saveSearch(princ, "save2")

        allUserOptions_after = userOptionsService.findByWorkflowUser(princ)
        // saves a second named search
        assertEquals(allUserOptions_before.size() + 2, allUserOptions_after.size())
        assertEquals(marshall(c2), userOptionsService.findByOptionId("DocSearch.NamedSearch." + c2.getSaveName(), princ).optionVal)
    }

    @Test
    void testSavedSearchOrdering() {
        def MAX_SEARCH_ITEMS = 5 // searches start wrapping after this...
        def princ = "not blank"

        def allUserOptions_before = userOptionsService.findByWorkflowUser(princ)

        def c1 = saveSearch(princ)
        def c2 = saveSearch(princ)
        def c3 = saveSearch(princ)
        def c4 = saveSearch(princ)
        def c5 = saveSearch(princ)

        def allUserOptions_after = userOptionsService.findByWorkflowUser(princ)
        assertEquals(allUserOptions_before.size() + 5 + 1, allUserOptions_after.size())
        assertEquals("DocSearch.LastSearch.Holding4,DocSearch.LastSearch.Holding3,DocSearch.LastSearch.Holding2,DocSearch.LastSearch.Holding1,DocSearch.LastSearch.Holding0", userOptionsService.findByOptionId("DocSearch.LastSearch.Order", princ).optionVal)
        assertEquals(marshall(c5), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding4", princ).optionVal)
        assertEquals(marshall(c4), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding3", princ).optionVal)
        assertEquals(marshall(c3), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding2", princ).optionVal)
        assertEquals(marshall(c2), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding1", princ).optionVal)
        assertEquals(marshall(c1), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding0", princ).optionVal)

        // now add 1 more

        def c6 = saveSearch(princ)


        allUserOptions_after = userOptionsService.findByWorkflowUser(princ)

        // order should have wrapped around, now Holding0 is first, and contains c6 criteria
        // still 5 entries
        assertEquals(allUserOptions_before.size() + 5 + 1, allUserOptions_after.size())
        assertEquals("DocSearch.LastSearch.Holding0,DocSearch.LastSearch.Holding4,DocSearch.LastSearch.Holding3,DocSearch.LastSearch.Holding2,DocSearch.LastSearch.Holding1", userOptionsService.findByOptionId("DocSearch.LastSearch.Order", princ).optionVal)
        assertEquals(marshall(c6), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding0", princ).optionVal)
        assertEquals(marshall(c5), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding4", princ).optionVal)
        assertEquals(marshall(c4), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding3", princ).optionVal)
        assertEquals(marshall(c3), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding2", princ).optionVal)
        assertEquals(marshall(c2), userOptionsService.findByOptionId("DocSearch.LastSearch.Holding1", princ).optionVal)
    }

    protected DocumentLookupCriteria saveSearch(String princ, String name = null) {
        def c = create(name)
        docSearchService.saveSearch(princ, c)
        return c
    }
}
{'fraction_non_alphanumeric': 0.08967901976997669, 'fraction_numerical': 0.00834398256032474, 'mean_word_length': 3.3849703361898484, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '987388', 'n_tokens_mistral': 3730, 'n_tokens_neox': 3575, 'n_words': 655}