<commit_before>/*
 * CWS, Cryptographic Web Store - open source Cryptographic Storage system.
 * Copyright (C) 2016-2019, JavaDog.io
 * mailto: cws AT JavaDog DOT io
 *
 * CWS is free software; you can redistribute it and/or modify it under the
 * terms of the Apache License, as published by the Apache Software Foundation.
 *
 * CWS 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 Apache License for more details.
 *
 * You should have received a copy of the Apache License, version 2, along with
 * this program; If not, you can download a copy of the License
 * here: https://www.apache.org/licenses/
 */
package io.javadog.cws.core;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.javadog.cws.api.common.ReturnCode;
import io.javadog.cws.api.responses.ProcessDataResponse;
import io.javadog.cws.core.enums.SanityStatus;
import io.javadog.cws.core.enums.StandardSetting;
import io.javadog.cws.core.exceptions.CWSException;
import io.javadog.cws.core.model.Settings;
import io.javadog.cws.core.model.entities.DataEntity;
import io.javadog.cws.core.services.ProcessDataService;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.ScheduleExpression;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerHandle;
import javax.ejb.TimerService;
import javax.persistence.Query;
import org.junit.jupiter.api.Test;

/**
 * @author <NAME>
 * @since CWS 1.0
 */
final class SanitizerBeanTest extends DatabaseSetup {

    @Test
    void testStartupBeanWithSanitizeCheck() {
        prepareInvalidData();
        prepareStartupBean("true");

        // Note; During the release of version 1.1, this test suddenly started
        // failing. From debugging, the question was more why id didn't fail
        // earlier as there was suppose to be invalid data in the DB.
        // See https://github.com/JavaDogs/cws/issues/58
        assertFalse(prepareSanitizeBean().findNextBatch(100).isEmpty());
    }

    @Test
    void testStartupBeanWithEmptyVersionTable() {
        final Query query = entityManager.createNativeQuery("delete from cws_versions v where v.id > 0");
        query.executeUpdate();

        final StartupBean bean = prepareStartupBean("true");
        assertFalse(getBeanSettings(bean).isReady());
    }

    @Test
    void testStartupBeanWithDifferentVersion() {
        final Query query = entityManager.createNativeQuery("insert into cws_versions (schema_version, cws_version, db_vendor) values (9999, '99.9.9', 'H2')");
        query.executeUpdate();

        final StartupBean bean = prepareStartupBean("true");
        assertFalse(getBeanSettings(bean).isReady());
    }

    @Test
    void testStartupBeanWithoutSanitizeCheck() {
        prepareInvalidData();
        prepareStartupBean("false");

        assertFalse(prepareSanitizeBean().findNextBatch(100).isEmpty());
    }

    @Test
    void testStartupBeanTimerService() {
        prepareInvalidData();
        final SanitizerBean sanitizerBean = prepareSanitizeBean();
        final List<Long> idsBefore = sanitizerBean.findNextBatch(100);

        final StartupBean bean = prepareStartupBean("false");
        bean.runSanitizing(new TestTimer());

        final List<Long> idsAfter = sanitizerBean.findNextBatch(100);
        assertFalse(idsBefore.isEmpty());
        assertTrue(idsAfter.isEmpty());<commit_msg>#58: With the correct DB setup - the tests are behaving as expected.<commit_after>/*
 * CWS, Cryptographic Web Store - open source Cryptographic Storage system.
 * Copyright (C) 2016-2019, JavaDog.io
 * mailto: cws AT JavaDog DOT io
 *
 * CWS is free software; you can redistribute it and/or modify it under the
 * terms of the Apache License, as published by the Apache Software Foundation.
 *
 * CWS 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 Apache License for more details.
 *
 * You should have received a copy of the Apache License, version 2, along with
 * this program; If not, you can download a copy of the License
 * here: https://www.apache.org/licenses/
 */
package io.javadog.cws.core;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.javadog.cws.api.common.ReturnCode;
import io.javadog.cws.api.responses.ProcessDataResponse;
import io.javadog.cws.core.enums.SanityStatus;
import io.javadog.cws.core.enums.StandardSetting;
import io.javadog.cws.core.exceptions.CWSException;
import io.javadog.cws.core.model.Settings;
import io.javadog.cws.core.model.entities.DataEntity;
import io.javadog.cws.core.services.ProcessDataService;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.ScheduleExpression;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerHandle;
import javax.ejb.TimerService;
import javax.persistence.Query;
import org.junit.jupiter.api.Test;

/**
 * @author <NAME>
 * @since CWS 1.0
 */
final class SanitizerBeanTest extends DatabaseSetup {

    @Test
    void testStartupBeanWithSanitizeCheck() {
        prepareInvalidData();
        prepareStartupBean("true");

        assertTrue(prepareSanitizeBean().findNextBatch(100).isEmpty());
    }

    @Test
    void testStartupBeanWithoutSanitizeCheck() {
        prepareInvalidData();
        prepareStartupBean("false");

        assertEquals(6, prepareSanitizeBean().findNextBatch(100).size());
    }

    @Test
    void testStartupBeanWithEmptyVersionTable() {
        final Query query = entityManager.createNativeQuery("delete from cws_versions where id > 0");
        query.executeUpdate();

        final StartupBean bean = prepareStartupBean("true");
        assertFalse(getBeanSettings(bean).isReady());
    }

    @Test
    void testStartupBeanWithDifferentVersion() {
        final Query query = entityManager.createNativeQuery("insert into cws_versions (schema_version, cws_version, db_vendor) values (9999, '99.9.9', 'H2')");
        query.executeUpdate();

        final StartupBean bean = prepareStartupBean("true");
        assertFalse(getBeanSettings(bean).isReady());
    }

    @Test
    void testStartupBeanTimerService() {
        prepareInvalidData();
        final SanitizerBean sanitizerBean = prepareSanitizeBean();
        final List<Long> idsBefore = sanitizerBean.findNextBatch(100);

        final StartupBean bean = prepareStartupBean("false");
        bean.runSanitizing(new TestTimer());

        final List<Long> idsAfter = sanitizerBean.findNextBatch(100);
        assertFalse(idsBefore.isEmpty());
        assertTrue(idsAfter.isEmpty());
{'fraction_non_alphanumeric': 0.10024617067833698, 'fraction_numerical': 0.009983588621444201, 'mean_word_length': 3.995218579234973, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4823505', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2093, 'n_words': 546}