/**
 * 
 */
package uk.org.taverna.scufl2.validation.correctness;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set;

import org.junit.Ignore;
import org.junit.Test;

import uk.org.taverna.scufl2.api.activity.Activity;
import uk.org.taverna.scufl2.api.configurations.Configuration;
import uk.org.taverna.scufl2.api.property.PropertyResource;
import uk.org.taverna.scufl2.validation.correctness.ReportCorrectnessValidationListener.MismatchConfigurableTypeProblem;
import uk.org.taverna.scufl2.validation.correctness.ReportCorrectnessValidationListener.NullFieldProblem;


/**
 * @author alanrw
 *
 */
public class TestConfiguration {
	
	@Test
	public void testIdenticalConfigurableTypes() {
		Configuration configuration = new Configuration();
		Activity a = new Activity();
		URI tavernaUri = null;
		try {
			tavernaUri = new URI("http://www.taverna.org.uk");
		} catch (URISyntaxException e) {
			return;
		}
		configuration.setConfigures(a);
		configuration.setType(tavernaUri);
		a.setType(tavernaUri);
		
		CorrectnessValidator cv = new CorrectnessValidator();
		ReportCorrectnessValidationListener rcvl = new ReportCorrectnessValidationListener();
		
		cv.checkCorrectness(configuration, false, rcvl);
		
		Set<MismatchConfigurableTypeProblem> mismatchConfigurableTypeProblems = rcvl.getMismatchConfigurableTypeProblems();
		assertEquals(0, mismatchConfigurableTypeProblems.size());
	}
	
	@Ignore
	public void testEqualConfigurableTypes() {
		Configuration configuration = new Configuration();
		Activity a = new Activity();
		URI tavernaUri = null;
		URI tavernaUri2 = null;
		try {
			tavernaUri = new URI("http://www.taverna.org.uk");
			tavernaUri2 = new URI("http://www.taverna.org.uk");
		} catch (URISyntaxException e) {
			return;
		}
		configuration.setConfigures(a);
		configuration.setType(tavernaUri);
		a.setType(tavernaUri2);
		
		CorrectnessValidator cv = new CorrectnessValidator();
		ReportCorrectnessValidationListener rcvl = new ReportCorrectnessValidationListener();
		
		cv.checkCorrectness(configuration, false, rcvl);
		
		Set<MismatchConfigurableTypeProblem> mismatchConfigurableTypeProblems = rcvl.getMismatchConfigurableTypeProblems();
		assertEquals(0, mismatchConfigurableTypeProblems.size());
	}
	
	@Ignore
	public void testMismatchingConfigurableTypes() {
		Configuration configuration = new Configuration();
		Activity a = new Activity();
		URI tavernaUri = null;
		URI myGridUri = null;
		try {
			tavernaUri = new URI("http://www.taverna.org.uk");
			myGridUri = new URI("http://www.mygrid.org.uk");
		} catch (URISyntaxException e) {
			return;
		}
		configuration.setConfigures(a);
		configuration.setType(tavernaUri);
		a.setType(myGridUri);
		
		CorrectnessValidator cv = new CorrectnessValidator();
		ReportCorrectnessValidationListener rcvl = new ReportCorrectnessValidationListener();
		
		cv.checkCorrectness(configuration, false, rcvl);
		
		Set<MismatchConfigurableTypeProblem> mismatchConfigurableTypeProblems = rcvl.getMismatchConfigurableTypeProblems();
		assertEquals(1, mismatchConfigurableTypeProblems.size());
		boolean mismatchProblem = false;
		for (MismatchConfigurableTypeProblem nlp : mismatchConfigurableTypeProblems) {
			if (nlp.getBean().equals(configuration) && nlp.getConfigurable().equals(a)) {
				mismatchProblem = true;
			}
		}
		assertTrue(mismatchProblem);
	}	

	@Test
	public void testCorrectnessOfMissingConfigures() {
		Configuration configuration = new Configuration();
		URI tavernaUri = null;
		try {
			tavernaUri = new URI("http://www.taverna.org.uk");
		} catch (URISyntaxException e) {
			return;
		}
		configuration.setType(tavernaUri);
		configuration.setJson(pr);
		
		CorrectnessValidator cv = new CorrectnessValidator();
		ReportCorrectnessValidationListener rcvl = new ReportCorrectnessValidationListener();
		
		cv.checkCorrectness(configuration, false, rcvl);
		
		Set<NullFieldProblem> nullFieldProblems = rcvl.getNullFieldProblems();
		assertEquals(0, nullFieldProblems.size()); // only done when completeness check

	}	

	@Test
	public void testCompletenessOfMissingConfigures() {
		Configuration configuration = new Configuration();
		PropertyResource pr = new PropertyResource();
		URI tavernaUri = null;
		try {
			tavernaUri = new URI("http://www.taverna.org.uk");
		} catch (URISyntaxException e) {
			return;
		}
		configuration.setJson(pr);
		configuration.setType(tavernaUri);
		
		CorrectnessValidator cv = new CorrectnessValidator();
		ReportCorrectnessValidationListener rcvl = new ReportCorrectnessValidationListener();
		
		cv.checkCorrectness(configuration, true, rcvl);
		
		Set<NullFieldProblem> nullFieldProblems = rcvl.getNullFieldProblems();
		assertFalse(nullFieldProblems.isEmpty()); // only done when completeness check
		
		boolean fieldProblem = false;
		for (NullFieldProblem nlp : nullFieldProblems) {
			if (nlp.getBean().equals(configuration) && nlp.getFieldName().equals("configures")) {
				fieldProblem = true;
			}
		}
		assertTrue(fieldProblem);
	}
	
	// Cannot check propertyResource because of SCUFL2-97
}

@@ -441,68 +441,8 @@
 on;%0A
-import uk.org.taverna.scufl2.api.property.PropertyResource;%0A
 impo
@@ -3549,177 +3549,67 @@
 ;%0A%09%09
-URI tavernaUri = null;%0A%09%09try %7B%0A%09%09%09tavernaUri = new URI(%22http://www.taverna.org.uk%22);%0A%09%09%7D catch (URISyntaxException e) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09configuration.setType(tavernaUri
+configuration.setType(URI.create(%22http://www.example.com/%22)
 );%0A%09
@@ -3627,26 +3627,46 @@
 ion.setJson(
-pr
+%22%7B %5C%22hello%5C%22: 1337  %7D%22
 );%0A%09%09%0A%09%09Corr
@@ -4138,256 +4138,130 @@
 ();%0A
-%09%09PropertyResource pr = new PropertyResource();%0A%09%09URI tavernaUri = null;%0A%09%09try %7B%0A%09%09%09tavernaUri = new URI(%22http://www.taverna.org.uk%22);%0A%09%09%7D catch (URISyntaxException e) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09configuration.setJson(pr);%0A%09%09configuration.setType(tavernaUri
+        configuration.setType(URI.create(%22http://www.example.com/%22));%0A        configuration.setJson(%22%7B %5C%22hello%5C%22: 1337  %7D%22
 );%0A%09

Test using JSON strings