<commit_before>/*
 * Copyright (C) 2012-2015 <NAME>
 *
 * 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 info.archinnov.achilles.internals.schema;

import static info.archinnov.achilles.internals.metamodel.index.IndexType.*;
import static info.archinnov.achilles.validation.Validator.validateBeanMappingFalse;
import static info.archinnov.achilles.validation.Validator.validateBeanMappingTrue;
import static java.lang.String.format;

import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.IndexMetadata;
import com.datastax.driver.core.TableMetadata;

import info.archinnov.achilles.internals.metamodel.AbstractProperty;
import info.archinnov.achilles.internals.metamodel.columns.ColumnType;
import info.archinnov.achilles.internals.metamodel.index.IndexInfo;

public class SchemaValidator {

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

    public static void validateDefaultTTL(TableMetadata metadata, Optional<Integer> staticTTL, Class<?> entityClass) {
        if (staticTTL.isPresent()) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(format("Validating table %s default TTL value",
                        metadata.getName()));
            }
            final int defaultTimeToLive = metadata.getOptions().getDefaultTimeToLive();
            validateBeanMappingTrue(staticTTL.get().equals(defaultTimeToLive),
                    "Default TTL '%s' declared on entity '%s' does not match detected default TTL '%s' in live schema",
                    staticTTL.get(), entityClass.getCanonicalName(), defaultTimeToLive);
        }
    }

    public static <T> void validateColumns(TableMetadata metadata, List<AbstractProperty<T, ?, ?>> properties,
                                           Class<T> entityClass) {
        properties
                .stream()
                .forEach(x -> {
                    final String cqlColumn = x.fieldInfo.cqlColumn;
                    final ColumnMetadata columnMeta = metadata.getColumn(cqlColumn);
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Validating column %s for table %s",
                                cqlColumn, metadata.getName()));
                    }

                    validateBeanMappingTrue(columnMeta != null,
                            "Cannot find column '%s' in live schema for entity '%s'", cqlColumn, entityClass);

                    final DataType runtimeType = columnMeta.getType();
                    final DataType staticType = x.buildType();
                    validateBeanMappingTrue(runtimeType.equals(staticType),
                            "Data type '%s' for column '%s' of entity '%s' does not match type in live schema",
                            staticType, cqlColumn, entityClass, runtimeType);<commit_msg>Better error message for Schema comparison<commit_after>/*
 * Copyright (C) 2012-2015 <NAME>
 *
 * 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 info.archinnov.achilles.internals.schema;

import static info.archinnov.achilles.internals.metamodel.index.IndexType.*;
import static info.archinnov.achilles.validation.Validator.validateBeanMappingFalse;
import static info.archinnov.achilles.validation.Validator.validateBeanMappingTrue;
import static java.lang.String.format;

import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.IndexMetadata;
import com.datastax.driver.core.TableMetadata;

import info.archinnov.achilles.internals.metamodel.AbstractProperty;
import info.archinnov.achilles.internals.metamodel.columns.ColumnType;
import info.archinnov.achilles.internals.metamodel.index.IndexInfo;

public class SchemaValidator {

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

    public static void validateDefaultTTL(TableMetadata metadata, Optional<Integer> staticTTL, Class<?> entityClass) {
        if (staticTTL.isPresent()) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(format("Validating table %s default TTL value",
                        metadata.getName()));
            }
            final int defaultTimeToLive = metadata.getOptions().getDefaultTimeToLive();
            validateBeanMappingTrue(staticTTL.get().equals(defaultTimeToLive),
                    "Default TTL '%s' declared on entity '%s' does not match detected default TTL '%s' in live schema",
                    staticTTL.get(), entityClass.getCanonicalName(), defaultTimeToLive);
        }
    }

    public static <T> void validateColumns(TableMetadata metadata, List<AbstractProperty<T, ?, ?>> properties,
                                           Class<T> entityClass) {
        properties
                .stream()
                .forEach(x -> {
                    final String cqlColumn = x.fieldInfo.cqlColumn;
                    final ColumnMetadata columnMeta = metadata.getColumn(cqlColumn);
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Validating column %s for table %s",
                                cqlColumn, metadata.getName()));
                    }

                    validateBeanMappingTrue(columnMeta != null,
                            "Cannot find column '%s' in live schema for entity '%s'", cqlColumn, entityClass);

                    final DataType runtimeType = columnMeta.getType();
                    final DataType staticType = x.buildType();
                    validateBeanMappingTrue(runtimeType.equals(staticType),
                            "Data type '%s' for column '%s' of entity '%s' does not match type in live schema '%s'",
                            staticType, cqlColumn, entityClass, runtimeType);
{'fraction_non_alphanumeric': 0.0904074702886248, 'fraction_numerical': 0.003961516694963215, 'mean_word_length': 2.691383812010444, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2774446', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1772, 'n_words': 501}