package org.geneontology.minerva.server.handler;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.geneontology.minerva.ModelContainer;
import org.geneontology.minerva.UndoAwareMolecularModelManager;
import org.geneontology.minerva.curie.CurieHandler;
import org.geneontology.minerva.curie.CurieMappings;
import org.geneontology.minerva.curie.DefaultCurieHandler;
import org.geneontology.minerva.curie.MappedCurieHandler;
import org.geneontology.minerva.server.external.ExternalLookupService;
import org.geneontology.minerva.server.handler.M3BatchHandler.M3BatchResponse;
import org.geneontology.minerva.server.handler.M3BatchHandler.M3Request;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.semanticweb.elk.owlapi.ElkReasonerFactory;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;

import owltools.graph.OWLGraphWrapper;
import owltools.io.ParserWrapper;

public class ModelEditTest {

	private static CurieHandler curieHandler = null;
	private static JsonOrJsonpBatchHandler handler = null;
	private static UndoAwareMolecularModelManager models = null;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		init(new ParserWrapper());
	}
	
	static void init(ParserWrapper pw) throws OWLOntologyCreationException, IOException {
		final OWLGraphWrapper graph = pw.parseToOWLGraph("http://purl.obolibrary.org/obo/go/extensions/go-lego.owl");
		
		// curie handler
		final String modelIdcurie = "gomodel";
		final String modelIdPrefix = "http://model.geneontology.org/";
		final CurieMappings localMappings = new CurieMappings.SimpleCurieMappings(Collections.singletonMap(modelIdcurie, modelIdPrefix));
		curieHandler = new MappedCurieHandler(DefaultCurieHandler.getMappings(), localMappings);
		
		OWLReasonerFactory rf = new ElkReasonerFactory();
		models = new UndoAwareMolecularModelManager(graph, rf, curieHandler, modelIdPrefix);
		boolean useReasoner = false;
		boolean useModelReasoner = false;
		handler = new JsonOrJsonpBatchHandler(models, useReasoner, useModelReasoner,
				Collections.<OWLObjectProperty>emptySet(), (ExternalLookupService) null);
		models.setPathToOWLFiles("src/test/resources/edit-test");
	}
	
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		if (handler != null) {
			handler = null;
		}
		if (models != null) {
			models.dispose();
		}
	}
	
	@Test
	public void testAddEdgeAsBatch() throws Exception {
		List<M3Request> batch = new ArrayList<>();
		M3Request r;
		
		final String individualId = "http://model.geneontology.org/5437882f00000024/5437882f0000032";
		final String modelId = "http://model.geneontology.org/5437882f00000024";
		final ModelContainer model = models.getModel(IRI.create(modelId));
		assertNotNull(model);
		boolean found = false;
		Set<OWLNamedIndividual> individuals = model.getAboxOntology().getIndividualsInSignature();
		for (OWLNamedIndividual individual : individuals) {
			String curi = curieHandler.getCuri(individual);
			if (curi.equals(individualId)) {
				found = true;
			}
		}
		assertTrue(found);
		
		
		// create new individual
		r = BatchTestTools.addIndividual(modelId, "GO:0003674");
		r.arguments.assignToVariable = "VAR1";
		batch.add(r);
		
		// add link to existing individual (converted from old model)
		r = BatchTestTools.addEdge(modelId, "VAR1", "BFO:0000050", individualId);
		batch.add(r);
		
		executeBatch(batch);
	}
	
	private M3BatchResponse executeBatch(List<M3Request> batch) {
		M3BatchResponse response = handler.m3Batch("test-user", "test-intention", "foo-packet-id",
				batch.toArray(new M3Request[batch.size()]), true);
		assertEquals("test-user", response.uid);
		assertEquals("test-intention", response.intention);
		assertEquals(response.message, M3BatchResponse.MESSAGE_TYPE_SUCCESS, response.messageType);
		return response;
	}
}

@@ -3036,16 +3036,142 @@
 00032%22;%0A
+%09%09final IRI individualIRI = IRI.create(individualId);%0A%09%09final String individualIdCurie = curieHandler.getCuri(individualIRI);%0A
 %09%09final 
@@ -3355,16 +3355,46 @@
  false;%0A
+%09%09boolean foundCurie = false;%0A
 %09%09Set%3COW
@@ -3539,75 +3539,117 @@
 %0A%09%09%09
-String curi = curieHandler.getCuri(individual);%0A%09%09%09if (c
+if (individualIRI.equals(individual.getIRI())) %7B%0A%09%09%09%09found = true;%0A%09%09%09%09foundCurie = individualIdC
 uri
+e
 .equals(
 indi
@@ -3648,41 +3648,50 @@
 als(
-individualId)) %7B%0A%09%09%09%09found = true
+curieHandler.getCuri(individual.getIRI()))
 ;%0A%09%09
@@ -3718,16 +3718,42 @@
 found);%0A
+%09%09assertTrue(foundCurie);%0A
 %09%09%0A%09%09%0A%09%09

fix id checks for test case