<commit_before>package com.hida.controller;

import com.hida.model.AutoIdGenerator;
import com.hida.model.BadParameterException;
import com.hida.model.CustomIdGenerator;
import com.hida.model.DefaultSetting;
import com.hida.model.IdGenerator;
import com.hida.model.Pid;
import com.hida.model.TokenType;
import com.hida.service.MinterServiceImpl;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import junit.framework.Assert;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.ui.ModelMap;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.json.JSONArray;
import org.json.JSONObject;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.when;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.DataProvider;

/**
 *
 * @author lruffin
 */
public class MinterControllerTest {

    @Mock
    MinterServiceImpl MinterServiceDao;

    @InjectMocks
    MinterController Controller;

    @Spy
    ModelMap ModelMap;

    private final String PREPEND = "http://digitalarchives.hawaii.gov/70111/";
    private final int AMOUNT = 5;

    private static final Logger Logger = LoggerFactory.getLogger(MinterControllerTest.class);

    @BeforeClass
    public void setUpClass() throws Exception {
        MockitoAnnotations.initMocks(this);
    }
    
    @AfterTest
    public void setUpTest(){
        ModelMap.clear();
    }

    @DataProvider(name = "parameters")
    private Object[][] parameters() {
        return new Object[][]{
            {PREPEND, "abc", TokenType.DIGIT, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.LOWERCASE, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.UPPERCASE, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.MIXEDCASE, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.LOWER_EXTENDED, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.UPPER_EXTENDED, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.MIXED_EXTENDED, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.DIGIT, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.LOWERCASE, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.UPPERCASE, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.MIXEDCASE, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.LOWER_EXTENDED, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.UPPER_EXTENDED, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.MIXED_EXTENDED, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "d", 1, false, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "l", 1, false, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "u", 1, false, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "m", 1, false, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "e", 1, false, true, false},};
    }

    @DataProvider(name = "bad booleans")
    private Object[][] badBooleans() {
        return new Object[][]{
            {"auto", "t"},
            {"random", "t"},
            {"sansVowels", "t"},};
    }

    @Test
    public void testRequestedParameters() throws Exception {
        //ModelMap.clear();
        Map<String,String> map = getSampleMap();
        DefaultSetting originalSetting = getSampleDefaultSetting();
        DefaultSetting tempSetting = getSampleDefaultSetting();
        
        tempSetting.setPrepend(map.get("prepend"));
        tempSetting.setPrefix(map.get("prefix"));
        tempSetting.setRootLength(Integer.parseInt(map.get("rootLength")));
        tempSetting.setCharMap(map.get("charMap"));
        tempSetting.setTokenType(Controller.getValidTokenType(map.get("tokenType")));
        tempSetting.setAuto(Boolean.getBoolean(map.get("auto")));
        tempSetting.setRandom(Boolean.getBoolean(map.get("random")));
        tempSetting.setSansVowels(Boolean.getBoolean(map.get("sansVowels")));
        
        
        when(MinterServiceDao.getCurrentSetting()).thenReturn(originalSetting);
        when(MinterServiceDao.mint(anyInt(), any(DefaultSetting.class))).
                thenReturn(getSampleSet(tempSetting));
        
        String jspName = Controller.printPids(AMOUNT, ModelMap, map);
        Assert.assertEquals("mint", jspName);

        String message = (String) ModelMap.get("message");
        Logger.debug(message);
        JSONArray testJsonArray = new JSONArray(message);  

        for (int i = 0; i < AMOUNT; i++) {
            JSONObject object = testJsonArray.getJSONObject(i);
            int id = object.getInt("id");
            String name = object.getString("name");
            Assert.assertEquals(id, i);
            testPid(name, tempSetting);
        }       
    }

    @Test(dataProvider = "parameters")
    public void testMintPersistedParameters(String prepend, String prefix, TokenType tokenType,
            String charMap, int rootLength, boolean isAuto, boolean isRandom, boolean sansVowel)
            throws Exception {

        DefaultSetting setting = new DefaultSetting(prepend, prefix, tokenType, charMap,
                rootLength, isAuto, isRandom, sansVowel);

        when(MinterServiceDao.getCurrentSetting()).thenReturn(setting);
        when(MinterServiceDao.mint(anyInt(), any(DefaultSetting.class))).
                thenReturn(getSampleSet(setting));

        String jspName = Controller.printPids(AMOUNT, ModelMap, new HashMap<String, String>());
        Assert.assertEquals("mint", jspName);

        String message = (String) ModelMap.get("message");
        Logger.debug(message);
        JSONArray testJsonArray = new JSONArray(message);      

        for (int i = 0; i < testJsonArray.length(); i++) {
            JSONObject object = testJsonArray.getJSONObject(i);
            int id = object.getInt("id");
            String name = object.getString("name");
            Assert.assertEquals(id, i);
            testPid(name, setting);
        }
    }

    @Test(expectedExceptions = BadParameterException.class, dataProvider = "bad booleans")
    public void testBadParameterExceptionBoolean(String key, String value) throws Exception {
        Map<String, String> parameters = new HashMap<>();
        parameters.put(key, value);

        DefaultSetting setting = this.getSampleDefaultSetting();

        when(MinterServiceDao.getCurrentSetting()).thenReturn(setting);
        Controller.printPids(AMOUNT, ModelMap, parameters);
    }

    @Test(expectedExceptions = BadParameterException.class)<commit_msg>Add testDisplayIndex<commit_after>package com.hida.controller;

import com.hida.model.AutoIdGenerator;
import com.hida.model.BadParameterException;
import com.hida.model.CustomIdGenerator;
import com.hida.model.DefaultSetting;
import com.hida.model.IdGenerator;
import com.hida.model.Pid;
import com.hida.model.TokenType;
import com.hida.service.MinterServiceImpl;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import junit.framework.Assert;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.ui.ModelMap;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.json.JSONArray;
import org.json.JSONObject;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import org.mockito.Mockito;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.testng.annotations.AfterTest;
import org.testng.annotations.DataProvider;

/**
 *
 * @author lruffin
 */
public class MinterControllerTest {

    @Mock
    MinterServiceImpl MinterServiceDao;

    @InjectMocks
    MinterController Controller;

    @Spy
    ModelMap ModelMap;

    private final String PREPEND = "http://digitalarchives.hawaii.gov/70111/";
    private final int AMOUNT = 5;

    private static final Logger Logger = LoggerFactory.getLogger(MinterControllerTest.class);

    @BeforeClass
    public void setUpClass() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @DataProvider(name = "request parameters")
    private Object[][] requestParameters() {
        return new Object[][]{
            {PREPEND, "xyz", "auto", "random", "true", "1", "true", null, null, "mmm"},
            {PREPEND, "xyz", "false", "false", null, "1", "true", "true", null, "mmm"},
            {PREPEND, "xyz", "false", "false", null, "1", "true", "true", "true", "mmm"},
            {PREPEND, "xyz", "false", "false", null, "1", null, "true", null, "mmm"},
            {PREPEND, "xyz", "false", "false", null, "1", null, "true", "true", "mmm"},
            {PREPEND, "xyz", "false", "false", null, "1", null, null, "true", "mmm"},
            {PREPEND, "xyz", "false", "false", null, "1", "true", null, null, "mmm"},
            
        };
    }

    @DataProvider(name = "parameters")
    private Object[][] parameters() {
        return new Object[][]{
            {PREPEND, "abc", TokenType.DIGIT, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.LOWERCASE, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.UPPERCASE, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.MIXEDCASE, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.LOWER_EXTENDED, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.UPPER_EXTENDED, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.MIXED_EXTENDED, "d", 1, true, true, true},
            {PREPEND, "abc", TokenType.DIGIT, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.LOWERCASE, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.UPPERCASE, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.MIXEDCASE, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.LOWER_EXTENDED, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.UPPER_EXTENDED, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.MIXED_EXTENDED, "d", 1, true, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "d", 1, false, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "l", 1, false, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "u", 1, false, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "m", 1, false, true, false},
            {PREPEND, "abc", TokenType.DIGIT, "e", 1, false, true, false},};
    }

    @DataProvider(name = "bad booleans")
    private Object[][] badBooleans() {
        return new Object[][]{
            {"auto", "t"},
            {"random", "t"},
            {"sansVowels", "t"},};
    }

    @Test(dataProvider = "request parameters")
    public void testDisplayIndex(String prepend, String idprefix, String mintType,
            String mintOrder, String vowels, String idLength, String digits,
            String lowercase, String uppercase, String charMapping) throws Exception {
        DefaultSetting originalSetting = getSampleDefaultSetting();
        MockHttpServletRequest request = new MockHttpServletRequest();
        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);                

        request.setParameter("prepend", prepend);
        request.setParameter("idprefix", idprefix);
        request.setParameter("mintType", mintType);
        request.setParameter("mintOrder", mintOrder);
        request.setParameter("vowels", vowels);
        request.setParameter("idlength", idLength);
        request.setParameter("digits", digits);
        request.setParameter("lowercase", lowercase);
        request.setParameter("uppercase", uppercase);
        request.setParameter("charmapping", charMapping);      
        
        when(MinterServiceDao.getCurrentSetting()).thenReturn(originalSetting);
        doNothing().when(MinterServiceDao).updateCurrentSetting(originalSetting);

        Assert.assertEquals("redirect:", Controller.handleForm(request, response));
    }

    @Test
    public void testRequestedParameters() throws Exception {
        //ModelMap.clear();
        Map<String, String> map = getSampleMap();
        DefaultSetting originalSetting = getSampleDefaultSetting();
        DefaultSetting tempSetting = getSampleDefaultSetting();

        tempSetting.setPrepend(map.get("prepend"));
        tempSetting.setPrefix(map.get("prefix"));
        tempSetting.setRootLength(Integer.parseInt(map.get("rootLength")));
        tempSetting.setCharMap(map.get("charMap"));
        tempSetting.setTokenType(Controller.getValidTokenType(map.get("tokenType")));
        tempSetting.setAuto(Boolean.getBoolean(map.get("auto")));
        tempSetting.setRandom(Boolean.getBoolean(map.get("random")));
        tempSetting.setSansVowels(Boolean.getBoolean(map.get("sansVowels")));

        when(MinterServiceDao.getCurrentSetting()).thenReturn(originalSetting);
        when(MinterServiceDao.mint(anyInt(), any(DefaultSetting.class))).
                thenReturn(getSampleSet(tempSetting));

        String jspName = Controller.printPids(AMOUNT, ModelMap, map);
        Assert.assertEquals("mint", jspName);

        String message = (String) ModelMap.get("message");
        Logger.debug(message);
        JSONArray testJsonArray = new JSONArray(message);

        for (int i = 0; i < AMOUNT; i++) {
            JSONObject object = testJsonArray.getJSONObject(i);
            int id = object.getInt("id");
            String name = object.getString("name");
            Assert.assertEquals(id, i);
            testPid(name, tempSetting);
        }
    }

    @Test(dataProvider = "parameters")
    public void testMintPersistedParameters(String prepend, String prefix, TokenType tokenType,
            String charMap, int rootLength, boolean isAuto, boolean isRandom, boolean sansVowel)
            throws Exception {

        DefaultSetting setting = new DefaultSetting(prepend, prefix, tokenType, charMap,
                rootLength, isAuto, isRandom, sansVowel);

        when(MinterServiceDao.getCurrentSetting()).thenReturn(setting);
        when(MinterServiceDao.mint(anyInt(), any(DefaultSetting.class))).
                thenReturn(getSampleSet(setting));

        String jspName = Controller.printPids(AMOUNT, ModelMap, new HashMap<String, String>());
        Assert.assertEquals("mint", jspName);

        String message = (String) ModelMap.get("message");
        Logger.debug(message);
        JSONArray testJsonArray = new JSONArray(message);

        for (int i = 0; i < testJsonArray.length(); i++) {
            JSONObject object = testJsonArray.getJSONObject(i);
            int id = object.getInt("id");
            String name = object.getString("name");
            Assert.assertEquals(id, i);
            testPid(name, setting);
        }
    }

    @Test(expectedExceptions = BadParameterException.class, dataProvider = "bad booleans")
    public void testBadParameterExceptionBoolean(String key, String value) throws Exception {
        Map<String, String> parameters = new HashMap<>();
        parameters.put(key, value);

        DefaultSetting setting = this.getSampleDefaultSetting();

        when(MinterServiceDao.getCurrentSetting()).thenReturn(setting);
        Controller.printPids(AMOUNT, ModelMap, parameters);
    }

    @Test(expectedExceptions = BadParameterException.class)
{'fraction_non_alphanumeric': 0.13493056424371325, 'fraction_numerical': 0.004066057800575504, 'mean_word_length': 3.271172855997863, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '6960907', 'n_tokens_mistral': 4783, 'n_tokens_neox': 4726, 'n_words': 1046}