/* Copyright 2014 MITRE Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mitre.provenance.test;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mitre.provenance.PLUSException;
import org.mitre.provenance.client.AbstractProvenanceClient;
import org.mitre.provenance.client.ProvenanceClient;
import org.mitre.provenance.client.ProvenanceClientException;
import org.mitre.provenance.client.RESTProvenanceClient;
import org.mitre.provenance.npe.NonProvenanceEdge;
import org.mitre.provenance.plusobject.PLUSActor;
import org.mitre.provenance.plusobject.PLUSEdge;
import org.mitre.provenance.plusobject.PLUSString;
import org.mitre.provenance.plusobject.PLUSWorkflow;
import org.mitre.provenance.plusobject.ProvenanceCollection;

public class TestClient {
    @Before
    public void setUp() throws ProvenanceClientException {
        ProvenanceClient.instance = new RESTProvenanceClient("localhost", "8080");
    }
	
    @Test
    public void testLatest() throws PLUSException { 
    	ProvenanceCollection col = ProvenanceClient.instance.latest();
    	assertTrue("Latest objects reported aren't empty", col.countNodes() > 0);
    }
    
    @Test
    public void testWorkflows() throws PLUSException { 
    	List<PLUSWorkflow> workflows = ProvenanceClient.instance.listWorkflows(10);
    	
    	assertTrue("Workflows came back", workflows != null);
    	assertTrue("More than zero workflows came back", workflows.size() > 0);
    	assertTrue("Server didn't respond with too many", workflows.size() <= 10); 
    }
    
	@Test
	public void testReporting() throws PLUSException {
		ProvenanceCollection col = new ProvenanceCollection();
		PLUSString s = new PLUSString("Foo", "Bar");
		PLUSString t = new PLUSString("Baz", "Quux");
		PLUSEdge e = new PLUSEdge(s, t);
		NonProvenanceEdge npe = new NonProvenanceEdge(s, t, "blah");
		
		col.addNode(s);
		col.addNode(t);
		col.addEdge(e);
		col.addNonProvenanceEdge(npe);		
		
		PLUSActor someOwner = new PLUSActor("Some owner");
		s.setOwner(someOwner);
		t.setOwner(someOwner);
		col.addActor(someOwner);
		
		AbstractProvenanceClient pc = ProvenanceClient.instance;
		System.out.println("Reporting collection " + col);
		assertTrue("Can report successfully to server", pc.report(col));
		
		System.out.println("Fetching graph...");
		ProvenanceCollection fetched = pc.getGraph(s.getId());
		System.out.println("Fetched " + fetched);
		//ProvenanceCollection fetched = col;

		assertTrue("All items logged", col.countNodes() == fetched.countNodes());
		assertTrue("All edges logged", col.countEdges() == fetched.countEdges());
		assertTrue("All NPEs logged", col.countNPEs() == fetched.countNPEs());
		assertTrue("All actors logged", col.countActors() == fetched.countActors());
	}
}

@@ -1566,32 +1566,247 @@
 %0D%0A%09%0D%0A    @Test%0D%0A
+    public void testActors() throws PLUSException %7B %0D%0A    %09ProvenanceCollection col = ProvenanceClient.instance.getActors();%0D%0A    %09assertTrue(%22Actors are not empty%22, col.countActors() %3E 0);%0D%0A    %7D%0D%0A    %0D%0A    @Test%0D%0A
     public void 
@@ -1835,32 +1835,32 @@
 USException %7B %0D%0A
-
     %09ProvenanceC
@@ -2403,16 +2403,311 @@
 %09@Test%0D%0A
+%09/**%0D%0A%09 * This test does quite a lot...we can't necessarily make assumptions about what will be in a store during%0D%0A%09 * the testing phase.  So here we're reporting stuff; and all of the services that require knowing something%0D%0A%09 * ahead of time are tested here.%0D%0A%09 * @throws PLUSException%0D%0A%09 */%0D%0A
 %09public 
@@ -2902,24 +2902,28 @@
 , %22Quux%22);%0D%0A
+%09%09%0D%0A
 %09%09PLUSEdge e
@@ -3849,24 +3849,24 @@
 ntNPEs());%0D%0A
-
 %09%09assertTrue
@@ -3933,15 +3933,681 @@
 rs());%0D%0A
+%09%09%0D%0A%09%09PLUSActor locatedActor = ProvenanceClient.instance.actorExists(someOwner.getId());%0D%0A%09%09%0D%0A%09%09assertTrue(%22Logged actor %22 + someOwner.getId() + %22 can be found.%22, locatedActor != null);%0D%0A%09%09assertTrue(%22Logged actor %22 + someOwner.getId() + %22 name matches%22, locatedActor.getName().equals(someOwner.getName()));%0D%0A%09%09%0D%0A%09%09ProvenanceCollection searchResults = ProvenanceClient.instance.search(%22Foo%22, 10);%0D%0A%09%09assertTrue(%22Received search results%22, searchResults != null);%0D%0A%09%09assertTrue(%22Found something for 'Foo'%22, searchResults.countNodes() %3E 0);%0D%0A%09%09assertTrue(%22Total search results (%22 + searchResults.countNodes() + %22) should be %3C= 10%22, searchResults.countNodes() %3C= 10); %0D%0A
 %09%7D%0D%0A%7D%0D%0A

test coverage greatly expanded, improved