package org.ihtsdo.buildcloud.service.execution.database.hsql;

import static org.junit.Assert.*;
import mockit.Expectations;
import mockit.Mocked;

import org.ihtsdo.snomed.util.rf2.schema.*;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;

public class RF2TableDAOHsqlImplTest {

	@Mocked
	PreparedStatement insertStatement;

	@Test
	public void testFullColumns() throws IOException, SQLException, ParseException {
		String testData = "one\ttwo\tthree\tfour\n" + "one\ttwo\tthree\tfour\n";
		runTest(testData);
	}

	@Test
	public void testBlankLastColumn() throws IOException, SQLException, ParseException {
		String testData = "one\ttwo\tthree\tfour\n" + "one\ttwo\tthree\t\n";
		runTest(testData);
	}

	private void runTest(String testData) throws SQLException, IOException, ParseException {

		RF2TableDAOHsqlImpl testDAO = new RF2TableDAOHsqlImpl();

		String extension = ".N/A";
		final TableSchema testSchema = new TableSchema(ComponentType.CONCEPT, extension);
		testSchema.field("first", DataType.STRING).field("second", DataType.STRING).field("third", DataType.STRING)
				.field("fourth", DataType.STRING);

		// Test data will have first row correct and second row with a blank final column.
		// We expect the same amount of fields to be picked up for both lines

		StringReader testDataSR = new StringReader(testData);
		BufferedReader bufferedTestData = new BufferedReader(testDataSR);

		final int columnCount = testSchema.getFields().size();

		// And announce our expectations for our mocked objects
		new Expectations() {
			{
				insertStatement.setObject(anyInt, anyString);
				times = columnCount;

				insertStatement.addBatch();

				insertStatement.setObject(anyInt, anyString);
				times = columnCount;

				insertStatement.addBatch();

				insertStatement.executeBatch();
			}
		};

		testDAO.insertData(bufferedTestData, testSchema, insertStatement);
	}


}

@@ -68,42 +68,8 @@
 ort 
-static org.junit.Assert.*;%0Aimport 
 mock
@@ -107,16 +107,164 @@
 Mocked;%0A
+import mockit.integration.junit4.JMockit;%0Aimport org.ihtsdo.snomed.util.rf2.schema.ComponentType;%0Aimport org.ihtsdo.snomed.util.rf2.schema.DataType;
 %0Aimport 
@@ -301,11 +301,20 @@
 ema.
-*;%0A
+TableSchema;
 %0Aimp
@@ -332,16 +332,49 @@
 it.Test;
+%0Aimport org.junit.runner.RunWith;
 %0A%0Aimport
@@ -554,16 +554,40 @@
 ption;%0A%0A
+@RunWith(JMockit.class)%0A
 public c

Add RunWith JMockit annotation so test initialised properly.