package org.ihtsdo.buildcloud.entity;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Iterator;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class BuildConfigurationTest {

	private StringWriter stringWriter;
	private JsonGenerator jsonGenerator;
	private JsonFactory jsonFactory;

	@Before
	public void setUp() throws Exception {
		ObjectMapper objectMapper = new ObjectMapper();
		stringWriter = new StringWriter();
		jsonFactory = objectMapper.getJsonFactory();
		jsonGenerator = jsonFactory.createJsonGenerator(stringWriter);
	}

	@Test
	public void testToJsonAndBack() throws IOException {
		BuildConfiguration configuration = new BuildConfiguration();

		Date effectiveTime = new GregorianCalendar(2014, 6, 31).getTime();
		configuration.setEffectiveTime(effectiveTime);

		HashSet<BuildConfiguration.RefsetCompositeKey> refsetCompositeKeys = new HashSet<>();
		refsetCompositeKeys.add(new BuildConfiguration.RefsetCompositeKey("100", "5, 6"));
		refsetCompositeKeys.add(new BuildConfiguration.RefsetCompositeKey("200", "5, 8"));
		configuration.setRefsetCompositeKeys(refsetCompositeKeys);
		ExtensionConfig extensionConfig = new ExtensionConfig();
		extensionConfig.setDependencyRelease("SnomedCT_Release_INT_20160131.zip");
		extensionConfig.setModuleId("554471000005108");
		extensionConfig.setNamespaceId("1000005");
		configuration.setExtensionConfig(extensionConfig);

		jsonGenerator.writeObject(configuration);
		String actual = stringWriter.toString();
		System.out.println(actual.replaceAll(",", ",\n"));

		BuildConfiguration buildConfigurationFromJson = jsonFactory.createJsonParser(new StringReader(actual)).readValueAs(BuildConfiguration.class);

		Assert.assertEquals(2, buildConfigurationFromJson.getRefsetCompositeKeys().size());
		Assert.assertEquals(effectiveTime, buildConfigurationFromJson.getEffectiveTime());
		Iterator<BuildConfiguration.RefsetCompositeKey> iterator = buildConfigurationFromJson.getRefsetCompositeKeys().iterator();
		BuildConfiguration.RefsetCompositeKey key1 = iterator.next();
		Assert.assertEquals("200", key1.getRefsetId());
		Assert.assertEquals("5, 8", key1.getFieldIndexes());
		BuildConfiguration.RefsetCompositeKey key2 = iterator.next();
		Assert.assertEquals("100", key2.getRefsetId());
		Assert.assertEquals("5, 6", key2.getFieldIndexes());
		ExtensionConfig extConfig = buildConfigurationFromJson.getExtensionConfig();
		Assert.assertNotNull(extConfig);
		Assert.assertEquals("SnomedCT_Release_INT_20160131.zip", extConfig.getDependencyRelease());
		Assert.assertEquals("554471000005108", extConfig.getModuleId());
		Assert.assertEquals("1000005", extConfig.getNamespaceId());
	}
}
@@ -139,101 +139,9 @@
 til.
-Date;%0Aimport java.util.GregorianCalendar;%0Aimport java.util.HashSet;%0Aimport java.util.Iterator
+*
 ;%0A%0Ai
@@ -2109,16 +2109,30 @@
 rator = 
+getSortedKeys(
 buildCon
@@ -2153,32 +2153,8 @@
 Json
-.getRefsetCompositeKeys(
 ).it
@@ -2250,17 +2250,17 @@
 Equals(%22
-2
+1
 00%22, key
@@ -2303,17 +2303,17 @@
 als(%225, 
-8
+6
 %22, key1.
@@ -2411,33 +2411,33 @@
 t.assertEquals(%22
-1
+2
 00%22, key2.getRef
@@ -2468,25 +2468,25 @@
 tEquals(%225, 
-6
+8
 %22, key2.getF
@@ -2840,10 +2840,581 @@
 d());%0A%09%7D
+%0A%0A%09private TreeSet%3CBuildConfiguration.RefsetCompositeKey%3E getSortedKeys(BuildConfiguration buildConfigurationFromJson) %7B%0A%09%09TreeSet%3CBuildConfiguration.RefsetCompositeKey%3E refsetCompositeKeys1 = new TreeSet%3C%3E(new Comparator%3CBuildConfiguration.RefsetCompositeKey%3E() %7B%0A%09%09%09@Override%0A%09%09%09public int compare(BuildConfiguration.RefsetCompositeKey o1, BuildConfiguration.RefsetCompositeKey o2) %7B%0A%09%09%09%09return o1.getRefsetId().compareTo(o2.getRefsetId());%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09refsetCompositeKeys1.addAll(buildConfigurationFromJson.getRefsetCompositeKeys());%0A%09%09return refsetCompositeKeys1;%0A%09%7D
 %0A%7D

Fix test, sort set to guarantee iteration order.