package eu.dnetlib.ariadneplus.catalogue;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.core.io.ClassPathResource;

import static org.junit.Assert.*;

/**
 * Created by Alessia Bardi on 15/03/2018.
 *
 * @author Alessia Bardi
 */
@RunWith(JUnit4.class)
@Deprecated
@Ignore
public class CatalogueAPIResponseTest {

	private String pathToCreated200 = "eu/dnetlib/ariadneplus/registry/catalogue/responses/resourceCreatedResponse.json";
	private String pathToCreationFailed = "eu/dnetlib/ariadneplus/registry/catalogue/responses/resourceCreationFailed.json";

	@Test
	public void testCreated200() throws IOException {
		CatalogueAPIResponse res = new CatalogueAPIResponse();
		res.setResponseBody(getString(pathToCreated200));
		assertTrue(res.isSuccess());
		assertTrue(res.hasAriadnePlusRegistryResource());
		assertNotNull(res.getAriadnePlusRegistryResource());
		assertNotNull(res.getAriadnePlusRegistryResource().getUuid());
		assertTrue(StringUtils.isBlank(res.getErrorMessage()));
	}

	@Test
	public void testFailedCreation() throws IOException {
		CatalogueAPIResponse res = new CatalogueAPIResponse();
		res.setResponseBody(getString(pathToCreationFailed));
		assertFalse(res.isSuccess());
		assertFalse(res.hasAriadnePlusRegistryResource());
		assertNull(res.getAriadnePlusRegistryResource());
		assertTrue(StringUtils.isNotBlank(res.getErrorMessage()));
		System.out.println(res.getErrorMessage());
	}

	private String getString(final String classpath) {
		try {
			final ClassPathResource resource = new ClassPathResource(classpath);
			return IOUtils.toString(resource.getInputStream(), "UTF-8");
		}catch(IOException e){
			return null;
		}
	}


}
