/*
 *
 * Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 */

package com.strider.datadefender.functions;

import org.apache.log4j.Logger;

import org.junit.Test;

import static org.apache.log4j.Logger.getLogger;

import com.strider.datadefender.utils.Xeger;
import com.strider.datadefender.utils.XegerTest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import junit.framework.TestCase;

/**
 * Core data anonymizer functions
 *
 * @author Armenak Grigoryan
 */
public class CoreFunctionsTest extends TestCase {
    private static final Logger log           = getLogger(XegerTest.class);
    private final static String regExpPattern = "[0-9]{3}-[0-9]{3}-[0-9]{3}";

    public CoreFunctionsTest(final String testName) {
        super(testName);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    public void testGenerateStringFromPattern() {
        log.debug("Generate SIN");

        final Xeger  instance = new Xeger(regExpPattern);
        final String text     = instance.generate();

        log.debug(text);
        assertTrue(text.matches(regExpPattern));
    }

    @Test
    public void testLipsumParagraphs() throws Exception {
        final CoreFunctions cf         = new CoreFunctions();
        final String        paragraphs = cf.lipsumParagraphs(3);

        log.debug("Testing for 3 paragraphs");
        assertTrue(paragraphs.matches("^[^\r]+\r\n\r\n[^\r]+\r\n\r\n[^\r]+$"));
    }

    @Test
    public void testLipsumSentences() throws Exception {
        final CoreFunctions cf        = new CoreFunctions();
        final String        sentences = cf.lipsumSentences(3, 3);

        log.debug("Testing for 3 sentences");
        assertTrue(sentences.matches("([^\\.]+\\.){3}"));
    }

    @Test
    public void testLipsumSimilar() throws Exception {
        final CoreFunctions cf        = new CoreFunctions();
        final String        sentences =
            cf.lipsumSimilar("This is a test.  It is excellent.  Should have a minimum of two sentences.");

        log.debug("Testing for 3 sentences generated by similar text");
        assertTrue(sentences.matches("([^\\.]+\\.){2,}"));

        final String paras = cf.lipsumSimilar("This is also a test.\n\nIt is better.\n\nShould have three paragraphs.");

        log.debug("Testing for 3 paragraphs generated by text with 3 paragraphs");
        assertTrue(paras.matches("^[^\r]+\r\n\r\n[^\r]+\r\n\r\n[^\r]+$"));
    }

    @Test
    public void testRandomDate() throws Exception {
        final CoreFunctions cf = new CoreFunctions();
        final String dateStart = "1910-01-01";
        final String dateEnd = "1930-01-01";
        final String format = "yyyy-MM-dd";
        log.debug("Testing random date generation between 1910-01-01 and 1930-01-01");

        String rand = cf.randomDate(dateStart, dateEnd, format);
        assertNotNull(rand);
        assertFalse(rand.isBlank());
        log.debug("Generated random date: " + rand);

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(format);
        LocalDate ds = LocalDate.parse(dateStart, fmt);
        LocalDate de = LocalDate.parse(dateEnd, fmt);
        LocalDate rd = LocalDate.parse(rand, fmt);

        assertNotNull(rd);
        assertTrue(rd.isAfter(ds) || rd.isEqual(ds));
        assertTrue(rd.isBefore(de) || rd.isEqual(de));
    }

    @Test
    public void testRandomDateTime() throws Exception {
        final CoreFunctions cf = new CoreFunctions();
        final String dateStart = "1980-01-01 00:00:00";
        final String dateEnd = "2020-01-01 12:22:33";
        final String format = "yyyy-MM-dd HH:mm:ss";
        log.debug("Testing random date/time generation between 1980-01-01 00:00:00 and 2020-01-01 12:22:33");

        String rand = cf.randomDateTime(dateStart, dateEnd, format);
        assertNotNull(rand);
        assertFalse(rand.isBlank());
        log.debug("Generated random date/time: " + rand);

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(format);
        LocalDateTime ds = LocalDateTime.parse(dateStart, fmt);
        LocalDateTime de = LocalDateTime.parse(dateEnd, fmt);
        LocalDateTime rd = LocalDateTime.parse(rand, fmt);

        assertNotNull(rd);
        assertTrue(rd.isAfter(ds) || rd.isEqual(ds));
        assertTrue(rd.isBefore(de) || rd.isEqual(de));
    }

    @Test
    public void testReplace() throws Exception {
        final CoreFunctions cf = new CoreFunctions();
        final String test = "abc abc";
        final String find = "c";
        final String replace = "cd";

        String res = cf.replace(test, find, replace);
        assertNotNull(res);
        assertEquals("abcd abcd", res);
    }

    @Test
    public void testRegexReplace() throws Exception {
        final CoreFunctions cf = new CoreFunctions();
        final String test = "abc ac";
        final String find = "b?(c)";
        final String replace = "$1d";

        String res = cf.regexReplace(test, find, replace);
        assertNotNull(res);
        assertEquals("acd acd", res);
    }

    @Test
    public void testRandomPhoneNumber() throws Exception {
        final CoreFunctions cf = new CoreFunctions();
        final String phone = cf.randomPhoneNumber();
        assertNotNull(phone);
        assertTrue(phone.matches("\\d{3}-\\d{3}-\\d{4}"));
    }

    @Test
    public void testRandomPostalCode() throws Exception {
        final CoreFunctions cf         = new CoreFunctions();
        final String        postalCode = cf.randomPostalCode();

        assertNotNull(postalCode);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }
}

@@ -759,16 +759,61 @@
 tions;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -3669,36 +3669,43 @@
     assertFalse(
-rand
+StringUtils
 .isBlank());%0A   
@@ -3689,32 +3689,36 @@
 ngUtils.isBlank(
+rand
 ));%0A        log.
@@ -4645,20 +4645,27 @@
 rtFalse(
-rand
+StringUtils
 .isBlank
@@ -4665,16 +4665,20 @@
 isBlank(
+rand
 ));%0A    

fix an error trying to check if a string was blank by using .isBlank direct on a string