<commit_before>/*
 * Copyright 2014 Feedzai
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feedzai.commons.sql.abstraction.engine.impl.abs;

import com.feedzai.commons.sql.abstraction.ddl.DbColumnType;
import com.feedzai.commons.sql.abstraction.ddl.DbEntity;
import com.feedzai.commons.sql.abstraction.dml.Expression;
import com.feedzai.commons.sql.abstraction.dml.Update;
import com.feedzai.commons.sql.abstraction.dml.result.ResultColumn;
import com.feedzai.commons.sql.abstraction.engine.*;
import com.feedzai.commons.sql.abstraction.engine.impl.PostgreSqlEngine;
import com.feedzai.commons.sql.abstraction.engine.testconfig.DatabaseConfiguration;
import com.feedzai.commons.sql.abstraction.engine.testconfig.DatabaseTestUtil;
import com.feedzai.commons.sql.abstraction.entry.EntityEntry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static com.feedzai.commons.sql.abstraction.dml.dialect.SqlBuilder.*;
import static com.feedzai.commons.sql.abstraction.engine.configuration.PdbProperties.*;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

/**
 * Tests for JSON columns.
 *
 * @author <NAME> (<EMAIL>)
 * @since 2.1.6
 */
@RunWith(Parameterized.class)
public class JSonTest {

    /*
     * Test table properties, a table with a PK and a json column.
     */
    private static String TEST_TABLE = "TEST_TBL";
    private static final String PK_COL = "PK_COL";
    private static final String JSON_COL = "JSON_COL";

    // The json value that will be used in most of the tests
    private static final long PK_VALUE = 1;
    private static final String JSON_PART_1 = "\"foo\": \"bar\"";
    private static final String JSON_PART_2 = "\"baz\": 1";
    private static final String JSON_PART_3 = "\"baz\": 2";
    private static final String JSON_VALUE = "{" + JSON_PART_1 + ", " + JSON_PART_2 + "}";

    private DatabaseEngine dbEngine;

    /**
     * Configurations the test will run with, set them in connection.properties and ensure
     * they are included in the -Dinstances VM args.
     *
     * @return  The configurations under which the test runs.
     */
    @Parameterized.Parameters
    public static Collection<Object[]> data() throws Exception {
        return DatabaseTestUtil.loadConfigurations();
    }

    @Parameterized.Parameter
    public DatabaseConfiguration config;


    @Before
    public void createTestTable() throws Exception {
        // Connect to db
        Properties dbProps = new Properties() {
            {
                setProperty(JDBC, config.jdbc);
                setProperty(USERNAME, config.username);
                setProperty(PASSWORD, config.password);
                setProperty(ENGINE, config.engine);
                setProperty(SCHEMA_POLICY, "create-drop");
            }
        };
        dbEngine = DatabaseFactory.getConnection(dbProps);

        // Create table
        DbEntity testEntity = new DbEntity.Builder()
                .name(TEST_TABLE)
                .addColumn(PK_COL, DbColumnType.LONG)
                .addColumn(JSON_COL, DbColumnType.JSON)
                .pkFields(PK_COL)
                .build();
        dbEngine.addEntity(testEntity);
    }

    /**
     * Scenario for an insert of a correct json value using persist().
     */<commit_msg>ref PULSEDEV-12907 pdb: Support complex fields as JSON/JSONB<commit_after>/*
 * Copyright 2014 Feedzai
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feedzai.commons.sql.abstraction.engine.impl.abs;

import com.feedzai.commons.sql.abstraction.ddl.DbColumnType;
import com.feedzai.commons.sql.abstraction.ddl.DbEntity;
import com.feedzai.commons.sql.abstraction.dml.Expression;
import com.feedzai.commons.sql.abstraction.dml.Update;
import com.feedzai.commons.sql.abstraction.dml.result.ResultColumn;
import com.feedzai.commons.sql.abstraction.engine.*;
import com.feedzai.commons.sql.abstraction.engine.impl.PostgreSqlEngine;
import com.feedzai.commons.sql.abstraction.engine.testconfig.DatabaseConfiguration;
import com.feedzai.commons.sql.abstraction.engine.testconfig.DatabaseTestUtil;
import com.feedzai.commons.sql.abstraction.entry.EntityEntry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static com.feedzai.commons.sql.abstraction.dml.dialect.SqlBuilder.*;
import static com.feedzai.commons.sql.abstraction.engine.configuration.PdbProperties.*;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

/**
 * Tests for JSON columns.
 *
 * @author <NAME> (<EMAIL>)
 * @since 2.1.6
 */
@RunWith(Parameterized.class)
public class JSonTest {

    /*
     * Test table properties, a table with a PK and a json column.
     */
    private static String TEST_TABLE = "TEST_TBL";
    private static final String PK_COL = "PK_COL";
    private static final String JSON_COL = "JSON_COL";

    // The json value that will be used in most of the tests
    private static final long PK_VALUE = 1;
    private static final String JSON_PART_1 = "\"foo\": \"bar\"";
    private static final String JSON_PART_2 = "\"baz\": 1";
    private static final String JSON_PART_3 = "\"baz\": 2";
    private static final String JSON_VALUE = "{" + JSON_PART_1 + ", " + JSON_PART_2 + "}";

    private DatabaseEngine dbEngine;

    /**
     * Configurations the test will run with, set them in connection.properties and ensure
     * they are included in the -Dinstances VM args.
     *
     * @return  The configurations under which the test runs.
     */
    @Parameterized.Parameters
    public static Collection<Object[]> data() throws Exception {
        return DatabaseTestUtil.loadConfigurations();
    }

    @Parameterized.Parameter
    public DatabaseConfiguration config;


    @Before
    public void createTestTable() throws Exception {
        // Connect to db
        Properties dbProps = new Properties() {
            {
                setProperty(JDBC, config.jdbc);
                setProperty(USERNAME, config.username);
                setProperty(PASSWORD, <PASSWORD>.password);
                setProperty(ENGINE, config.engine);
                setProperty(SCHEMA_POLICY, "create-drop");
            }
        };
        dbEngine = DatabaseFactory.getConnection(dbProps);

        // Create table
        DbEntity testEntity = new DbEntity.Builder()
                .name(TEST_TABLE)
                .addColumn(PK_COL, DbColumnType.LONG)
                .addColumn(JSON_COL, DbColumnType.JSON)
                .pkFields(PK_COL)
                .build();
        dbEngine.addEntity(testEntity);
    }

    /**
<<<<<<< HEAD
     * Scenario for an insert of a correct json value using persist().
=======
     * Scenario for an insert using persist().
>>>>>>> 51455aad63197a0b518e9740ef5fb74e6579982c
     */
{'fraction_non_alphanumeric': 0.10157575757575757, 'fraction_numerical': 0.008606060606060607, 'mean_word_length': 3.1629667003027246, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1092305', 'n_tokens_mistral': 2461, 'n_tokens_neox': 2357, 'n_words': 653}