package eu.dnetlib.parthenos.publisher;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import eu.dnetlib.parthenos.jrr.JRRPublisher;
import eu.dnetlib.parthenos.jrr.JRRPublisherHelper;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Created by Alessia Bardi on 29/08/2017.
 *
 * @author Alessia Bardi
 */
@RunWith(SpringRunner.class)
@WebMvcTest(ParthenosPublisherController.class)
public class ParthenosPublisherControllerTest {

	@Autowired
	private MockMvc mvc;

	@MockBean
	private ParthenosPublisherHelper parthenosPublisherHelper;

	@MockBean
	private JRRPublisherHelper jrrPublisherHelper;

	@Test
	public void publishOK() throws Exception {
		mvc.perform(post("/publish").param("record", "<aTestRecord>value</aTestRecord>").param("parthenosTarget", "VIRTUOSO"))
				.andExpect(status().isOk());
	}

	@Test
	public void testEx() throws Exception {
		mvc.perform(post("/publish")).andExpect(status().is4xxClientError());
	}

	@Test
	public void testJRREx() throws Exception {
		mvc.perform(post("/publishJRR")).andExpect(status().is4xxClientError());
	}

	@Test
	public void testJRROk() throws Exception {
		//{"typeNamespace":"http://www.ics.forth.gr/isl/CRMext/CRMdig.rdfs/","datasourceApi":"api_________::parthenos___::parthenos::topLevel","typeName":"D14_Software","datasourceName":"PARTHENOS"}
		(mvc.perform(post("/publishJRR")
				.param("typeNamespace", "http://www.ics.forth.gr/isl/CRMext/CRMdig.rdfs/" )
				.param("datasourceApi", "api_________::parthenos___::parthenos::topLevel")
				.param("typeName", "D14_Software")
				.param("datasourceName", "PARTHENOS"))).andExpect(status().isOk());
	}

	@Test
	public void test() {

		UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://beta-parthenos.d4science.org:8080/parthenos/publishJRR")
		.queryParam("typeNamespace", "http://www.ics.forth.gr/isl/CRMext/CRMdig.rdfs/")
		.queryParam("typeName", "X_Type")
		.queryParam("datasourceApi", "api_________::parthenos___::parthenos::test")
		.queryParam("datasourceName", "X");
		URI uri = builder.build().toUri();
		System.out.println("Request goes to  " + uri.toString());
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity<Integer> res = restTemplate.exchange(uri, HttpMethod.POST, null, Integer.class);
		if (res.getStatusCode().is2xxSuccessful()) {
			System.out.println("ok");

		} else {
			System.out.println("not ok: "+res.getStatusCodeValue());
		}

	}


}
