package eu.dnetlib.data.collector.plugins.ariadneplus.ehri;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;

import eu.dnetlib.rmi.data.CollectorServiceException;
import org.apache.commons.io.IOUtils;
import org.junit.Ignore;
import org.junit.Test;

/**
 * Created by Alessia Bardi on 19/04/2017.
 *
 * @author Alessia Bardi
 */
public class EHRIGraphQLClientTest {

	private EHRIGraphQLClient client = new EHRIGraphQLClient();
	private String baseUrl = "https://portal.ehri-project.eu/api/graphql";
	private String query = "{\"query\":\"{topLevelDocumentaryUnits{items{id}}}\"}";

	private String simpleOutput = "{\n"
			+ "  \"data\" : {\n"
			+ "    \"topLevelDocumentaryUnits\" : {\n"
			+ "      \"items\" : [ {\n"
			+ "        \"id\" : \"at-001985-ikg_ar_1\"\n"
			+ "      }, {\n"
			+ "        \"id\" : \"at-001985-ikg_ar_11\"\n"
			+ "      }]\n"
			+ "    }\n"
			+ "  }\n"
			+ "}";

	@Test
	public void testGetIdentifiers() throws IOException {
		Iterator<String> it = client.getIdentifiers(new InputStreamReader(IOUtils.toInputStream(simpleOutput, "UTF-8")));
		while(it.hasNext())
			System.out.println(it.next());
	}

	@Ignore
	@Test
	public void testRemoteCollect() throws CollectorServiceException, IOException, URISyntaxException {
		Iterator<String> it =client.collect(baseUrl, query);
		int stopAt = 5;
		for(int i = 0; i < stopAt && it.hasNext(); i++){
			System.out.println(it.next());
		}
	}

	@Ignore
	@Test
	public void testGetResource() throws IOException {
		String id = "at-001985-ikg_ar_11";
		String target = "https://portal.ehri-project.eu/units/"+id+"/export";
		URL url = new URL(target);
		String res = IOUtils.toString(url, "UTF-8");
		System.out.println(res);
	}

//	@Test
//	public void testDotNotation(){
//		JsonObject jsonObject = new JsonParser().parse(simpleOutput).getAsJsonObject();
//		JsonArray items = jsonObject.getAsJsonArray("data.topLevelDocumentaryUnits.items");
//		for(JsonElement id : items){
//			System.out.println(id.getAsJsonObject().get("id"));
//		}
//	}
}
