All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.zeger_tak.enversvalidationplugin.validate.AuditTableInformationMapValidator Maven / Gradle / Ivy

Go to download

This is a Maven plugin that allows for easy validation of database auditing with Hibernate and is intended to be used by projects that do not always rely on Envers to create the audit history.

There is a newer version: 0.6
Show newest version
package com.github.zeger_tak.enversvalidationplugin.validate;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import com.github.zeger_tak.enversvalidationplugin.annotation.AuditTableInformationMap;
import com.github.zeger_tak.enversvalidationplugin.annotation.ConnectionProvider;
import com.github.zeger_tak.enversvalidationplugin.annotation.Parameterized;
import com.github.zeger_tak.enversvalidationplugin.annotation.TargetPhase;
import com.github.zeger_tak.enversvalidationplugin.annotation.Validate;
import com.github.zeger_tak.enversvalidationplugin.annotation.ValidationType;
import com.github.zeger_tak.enversvalidationplugin.connection.ConnectionProviderInstance;
import com.github.zeger_tak.enversvalidationplugin.entities.AuditTableInformation;
import com.github.zeger_tak.enversvalidationplugin.exceptions.ValidationException;
import org.dbunit.dataset.DataSetException;

/**
 * The goal of this validator is described in its {@link Validate} methods:
 */
@ValidationType(TargetPhase.TABLE_STRUCTURE)
public class AuditTableInformationMapValidator
{
	private final Map auditTableInformationMap;
	private final Set auditTablesInDatabase;

	public AuditTableInformationMapValidator(@Nonnull Map auditTableInformationMap, @Nonnull Set auditTablesInDatabase)
	{
		this.auditTableInformationMap = auditTableInformationMap;
		this.auditTablesInDatabase = auditTablesInDatabase;
	}

	@Parameterized(name = "auditTablesInDatabase", uniqueIdentifier = "auditTablesInDatabase")
	public static List generateData(@Nonnull @ConnectionProvider ConnectionProviderInstance connectionProvider, @Nonnull @AuditTableInformationMap Map auditTableInformationMap) throws SQLException, DataSetException
	{
		return Collections.singletonList(new Object[] { auditTableInformationMap, connectionProvider.getQueries().getTablesByNameEndingWith(connectionProvider.getQueries().getAuditTablePostFix()) });
	}

	/**
	 * Validates that all audit tables present in the database are specified in the audit table information.
	 */
	@Validate
	public void validateAllExistingAuditTablesAreSpecified()
	{
		final Set auditTableInformationSpecified = auditTableInformationMap.keySet();

		final Set auditTablesNotSpecified = auditTablesInDatabase.stream().map(String::toUpperCase).collect(Collectors.toSet());
		auditTablesNotSpecified.removeAll(auditTableInformationSpecified);

		if (!auditTablesNotSpecified.isEmpty())
		{
			throw new ValidationException("The following audit tables are not configured in the audit table information map: " + auditTablesNotSpecified);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy