package ca.uhn.fhir.jpa.migrate.taskdef;

import ca.uhn.fhir.jpa.migrate.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import ca.uhn.fhir.jpa.migrate.providers.SpringBatchSchemaMigrationProvider;
import ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.junit.Test;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertThat;

public class InitializeSchemaTaskTest extends BaseTest {

	public InitializeSchemaTaskTest(Supplier<TestDatabaseDetails> theTestDatabaseDetails) {
		super(theTestDatabaseDetails);
	}

	@Test
	public void testInitializeTwice() throws SQLException {
		InitializeSchemaTask task = new InitializeSchemaTask("1", "1", new TestProvider());
		getMigrator().addTask(task);
		getMigrator().migrate();
		assertThat(JdbcUtils.getTableNames(getConnectionProperties()), containsInAnyOrder("SOMETABLE"));

		// Second migrate runs without issue
		getMigrator().removeAllTasksForUnitTest();
		InitializeSchemaTask identicalTask = new InitializeSchemaTask("1", "1", new TestProvider());
		getMigrator().addTask(identicalTask);
		getMigrator().migrate();
	}

	@Test
	public void testInitializeBatch() throws SQLException {
		InitializeSchemaTask task = new InitializeSchemaTask("1", "2", new SpringBatchSchemaMigrationProvider());
		getMigrator().addTask(task);
		getMigrator().migrate();
		assertThat(JdbcUtils.getTableNames(getConnectionProperties()), hasItems(
			"BATCH_JOB_INSTANCE",
			"BATCH_JOB_EXECUTION",
			"BATCH_JOB_EXECUTION_PARAMS",
			"BATCH_JOB_EXECUTION_CONTEXT",
			"BATCH_STEP_EXECUTION",
			"BATCH_STEP_EXECUTION_CONTEXT"
			));
	}

	private class TestProvider implements ISchemaInitializationProvider {
		@Override
		public List<String> getSqlStatements(DriverTypeEnum theDriverType) {
			return Collections.singletonList("create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))");
		}

		@Override
		public String getSchemaExistsIndicatorTable() {
			return "DONT_MATCH_ME";
		}

		@Override
		public String getSchemaDescription() {
			return "TEST";
		}

		@Override
		public ISchemaInitializationProvider setSchemaDescription(String theSchemaDescription) {
			return this;
		}

		@Override
		public boolean equals(Object theO) {
			if (this == theO) return true;

			if (theO == null || getClass() != theO.getClass()) return false;

			TestProvider that = (TestProvider) theO;

			return size() == that.size();
		}

		private int size() {
			return getSqlStatements(getDriverType()).size();
		}

		// This could be stricter, but we don't want this to be brittle.
		@Override
		public int hashCode() {
			return new HashCodeBuilder(17, 37)
				.append(size())
				.toHashCode();
		}
	}
}

@@ -1366,502 +1366,8 @@
 %09%7D%0A%0A
-%09@Test%0A%09public void testInitializeBatch() throws SQLException %7B%0A%09%09InitializeSchemaTask task = new InitializeSchemaTask(%221%22, %222%22, new SpringBatchSchemaMigrationProvider());%0A%09%09getMigrator().addTask(task);%0A%09%09getMigrator().migrate();%0A%09%09assertThat(JdbcUtils.getTableNames(getConnectionProperties()), hasItems(%0A%09%09%09%22BATCH_JOB_INSTANCE%22,%0A%09%09%09%22BATCH_JOB_EXECUTION%22,%0A%09%09%09%22BATCH_JOB_EXECUTION_PARAMS%22,%0A%09%09%09%22BATCH_JOB_EXECUTION_CONTEXT%22,%0A%09%09%09%22BATCH_STEP_EXECUTION%22,%0A%09%09%09%22BATCH_STEP_EXECUTION_CONTEXT%22%0A%09%09%09));%0A%09%7D%0A
 %0A%09pr

Remove dead test