package de.terrestris.shogun2.util.converter;

import org.junit.Test;
import static org.junit.Assert.*;

/**
 * @author Nils Bühner
 *
 */
public class PropertyValueConverterTest {

	/**
	 * The converter class to test.
	 */
	private final PropertyValueConverter converter = new PropertyValueConverter();

	/**
	 * Tests whether a string will remain a string.
	 */
	@Test
	public void convertToEntityAttribute_convertsToString() {

		String testValue = "test";
		Object convertedValue = converter.convertToEntityAttribute(testValue);

		assertTrue(testValue.equals(convertedValue));
	}

	/**
	 * Tests whether an integer "string" will be converted to a Long value.
	 */
	@Test
	public void convertToEntityAttribute_convertsToLong() {

		String testValue = "42";
		Object convertedValue = converter.convertToEntityAttribute(testValue);

		assertTrue(convertedValue instanceof Long);
		assertTrue(((Long)convertedValue) == 42);
	}

	/**
	 * Tests whether an float/double "string" will be converted to a Double value.
	 */
	@Test
	public void convertToEntityAttribute_convertsToDouble() {

		String testValue = "17.42";
		Object convertedValue = converter.convertToEntityAttribute(testValue);

		assertTrue(convertedValue instanceof Double);
		assertTrue(((Double)convertedValue) == 17.42);
	}

	/**
	 * Tests whether an boolean "string" will be converted to a Boolean value.
	 */
	@Test
	public void convertToEntityAttribute_convertsToBoolean() {

		String testValue = "true";
		String testValueCaseInsensitive = "faLSe";

		Object convertedValue = converter.convertToEntityAttribute(testValue);
		Object convertedValueCaseInsensitive = converter.convertToEntityAttribute(testValueCaseInsensitive);

		assertTrue(convertedValue instanceof Boolean);
		assertTrue(((Boolean)convertedValue) == true);

		assertTrue(convertedValueCaseInsensitive instanceof Boolean);
		assertTrue(((Boolean)convertedValueCaseInsensitive) == false);
	}

	/**
	 * Tests whether a string will be converted to the correct string.
	 */
	@Test
	public void convertToDatabaseColumn_convertsStringToString() {

		String testValue = "test";
		String convertedValue = converter.convertToDatabaseColumn(testValue);

		assertTrue(testValue.equals(convertedValue));
	}

	/**
	 * Tests whether an integer will be converted to the correct string.
	 */
	@Test
	public void convertToDatabaseColumn_convertsIntegerToString() {

		int testValue = 42;
		String convertedValue = converter.convertToDatabaseColumn(testValue);

		assertTrue("42".equals(convertedValue));
	}

	/**
	 * Tests whether a double will be converted to the correct string.
	 */
	@Test
	public void convertToDatabaseColumn_convertsDoubleToString() {

		double testValue = 17.42;
		String convertedValue = converter.convertToDatabaseColumn(testValue);

		assertTrue("17.42".equals(convertedValue));
	}

	/**
	 * Tests whether a boolean will be converted to the correct string.
	 */
	@Test
	public void convertToDatabaseColumn_convertsBooleanToString() {

		boolean testValue = true;
		String convertedValue = converter.convertToDatabaseColumn(testValue);

		assertTrue("true".equals(convertedValue));
	}

}

@@ -520,32 +520,80 @@
 te(testValue);%0A%0A
+%09%09assertTrue(convertedValue instanceof String);%0A
 %09%09assertTrue(tes
@@ -799,24 +799,61 @@
 lue = %2242%22;%0A
+%09%09String negativeTestValue = %22-42%22;%0A%0A
 %09%09Object con
@@ -904,32 +904,121 @@
 bute(testValue);
+%0A%09%09Object convertedNegativeValue = converter.convertToEntityAttribute(negativeTestValue);
 %0A%0A%09%09assertTrue(c
@@ -1088,24 +1088,132 @@
 lue) == 42);
+%0A%0A%09%09assertTrue(convertedNegativeValue instanceof Long);%0A%09%09assertTrue(((Long)convertedNegativeValue) == -42);
 %0A%09%7D%0A%0A%09/**%0A%09 
@@ -1392,16 +1392,56 @@
 17.42%22;%0A
+%09%09String negativeTestValue = %22-17.42%22;%0A%0A
 %09%09Object
@@ -1496,32 +1496,121 @@
 bute(testValue);
+%0A%09%09Object convertedNegativeValue = converter.convertToEntityAttribute(negativeTestValue);
 %0A%0A%09%09assertTrue(c
@@ -1687,24 +1687,139 @@
 ) == 17.42);
+%0A%0A%09%09assertTrue(convertedNegativeValue instanceof Double);%0A%09%09assertTrue(((Double)convertedNegativeValue) == -17.42);
 %0A%09%7D%0A%0A%09/**%0A%09 
@@ -2924,24 +2924,56 @@
 Value = 42;%0A
+%09%09int negativeTestValue = -42;%0A%0A
 %09%09String con
@@ -3036,43 +3036,183 @@
 e);%0A
-%0A%09%09assertTrue(%2242%22.equals(converted
+%09%09String convertedNegativeValue = converter.convertToDatabaseColumn(negativeTestValue);%0A%0A%09%09assertTrue(%2242%22.equals(convertedValue));%0A%09%09assertTrue(%22-42%22.equals(convertedNegative
 Valu
@@ -3398,16 +3398,54 @@
  17.42;%0A
+%09%09double negativeTestValue = -17.42;%0A%0A
 %09%09String
@@ -3512,46 +3512,189 @@
 e);%0A
-%0A%09%09assertTrue(%2217.42%22.equals(converted
+%09%09String convertedNegativeValue = converter.convertToDatabaseColumn(negativeTestValue);%0A%0A%09%09assertTrue(%2217.42%22.equals(convertedValue));%0A%09%09assertTrue(%22-17.42%22.equals(convertedNegative
 Valu

Extend converter test to check negative values