package eu.dnetlib.data.claims.parser;

import eu.dnetlib.data.claims.entity.Result;
import eu.dnetlib.data.claims.utils.ClaimUtils;
import eu.dnetlib.data.claims.utils.SearchUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by kiatrop on 23/2/2016.
 */
public class ExternalParserTest {
//    @Autowired
    SearchUtils searchUtils;

    @Before
    public void init() throws Exception {
        Configurator.initialize(new DefaultConfiguration());
        Configurator.setRootLevel(Level.INFO);
        ApplicationContext context = new ClassPathXmlApplicationContext("eu/dnetlib/data/claims/springContext-claims.xml");

        searchUtils = context.getBean(SearchUtils.class);

    }
    private String crossrefJson = "{\"status\":\"ok\",\"message-type\":\"work-list\",\"message-version\":\"1.0.0\",\"message\":" +
            "{\"query\":{\"search-terms\":null,\"start-index\":0},\"items-per-page\":20,\"items\":[{\"indexed\":" +
            "{\"date-parts\":[[2015,12,28]], \"date-time\":\"2015-12-28T17:45:59Z\",\"timestamp\":1451324759779}," +
            "\"reference-count\":0,\"publisher\":\"Society for Neuroscience\", \"issue\":\"17\"," +
            "\"published-print\":{\"date-parts\":[[2012,4,25]]},\"DOI\":\"10.1523\\/jneurosci.2900-11.2012\"," +
            "\"type\":\"journal-article\",\"created\": {\"date-parts\":[[2012,4,25]]," +
            "\"date-time\":\"2012-04-25T16:38:54Z\",\"timestamp\":1335371934000},\"page\":\"5728-5736\"," +
            "\"source\":\"CrossRef\",\"title\":[\"Imaging Microglial\\/Macrophage Activation in Spinal Cords of " +
            "Experimental Autoimmune Encephalomyelitis Rats by Positron Emission Tomography Using the Mitochondrial " +
            "18 kDa Translocator Protein Radioligand [18F]DPA-714\"]," +
            "\"prefix\":\"http:\\/\\/id.crossref.org\\/prefix\\/10.1523\",\"volume\":\"32\",\"author\":" +
            "[{\"affiliation\":[],\"family\":\"Abourbeh\",\"given\":\"G.\"},{\"affiliation\":[],\"family\":\"Theze\"," +
            "\"given\":\"B.\"},{\"affiliation\":[],\"family\":\"Maroy\",\"given\":\"R.\"},{\"affiliation\":[],\"family\":" +
            "\"Dubois\",\"given\":\"A.\"},{\"affiliation\":[],\"family\":\"Brulon\",\"given\":\"V.\"}," +
            "{\"affiliation\":[],\"family\":\"Fontyn\",\"given\":\"Y.\"},{\"affiliation\":[],\"family\":\"Dolle\"," +
            "\"given\":\"F.\"},{\"affiliation\":[],\"family\":\"Tavitian\",\"given\":\"B.\"},{\"affiliation\":[]," +
            "\"family\":\"Boisgard\",\"given\":\"R.\"}],\"member\":\"http:\\/\\/id.crossref.org\\/member\\/393\"," +
            "\"published-online\":{\"date-parts\":[[2012,4,25]]},\"container-title\":[\"Journal of Neuroscience\"]," +
            "\"deposited\":{\"date-parts\":[[2014,2,27]],\"date-time\":\"2014-02-27T06:42:02Z\",\"timestamp\":1393483322000}," +
            "\"score\":1.0,\"subtitle\":[],\"issued\":{\"date-parts\":[[2012,4,25]]}," +
            "\"URL\":\"http:\\/\\/dx.doi.org\\/10.1523\\/jneurosci.2900-11.2012\",\"ISSN\":[\"0270-6474\",\"1529-2401\"]," +
            "\"subject\":[\"Neuroscience(all)\"]}],\"total-results\":1,\"facets\":{}}}" +
            "";


    @Test
    public void crossref2ResultTest(){
        Result result = ExternalRecordParser.crossref2Result(crossrefJson, "");

        Assert.assertNull(result.getAccessRights());
        Assert.assertNull(result.getBestLicense());
        Assert.assertEquals("openaire____::crossref", result.getCollectedFrom());
        Assert.assertEquals("10.1523/jneurosci.2900-11.2012", result.getDoi());
        Assert.assertNull(result.getOai());
        Assert.assertNotNull(result.getOpenaireId());//        Assert.assertNull(result.getOpenaireId());
        Assert.assertNull(result.getPmcid());
        Assert.assertEquals(ClaimUtils.PUBLICATION, result.getResultType());
        Assert.assertEquals("Imaging Microglial/Macrophage Activation in Spinal Cords of Experimental Autoimmune Encephalomyelitis Rats by Positron Emission Tomography Using the Mitochondrial 18 kDa Translocator Protein Radioligand [18F]DPA-714", result.getTitle());
        Assert.assertEquals("json", result.getRecordFormat());
        Assert.assertEquals("https://dx.doi.org/10.1523/jneurosci.2900-11.2012", result.getExternalUrl());
        Assert.assertNull(result.getEmbargoEndDate());
        Assert.assertNull(result.getOrcidworkid());

        Assert.assertEquals(9, result.getAuthors().size());
        Assert.assertEquals("G. Abourbeh", result.getAuthors().get(0+""));
        Assert.assertEquals("Y. Fontyn", result.getAuthors().get(5+""));
    }

    private String orcidXml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
            "<work:work put-code=\"43079714\" path=\"/0000-0001-5264-4324/work/43079714\" visibility=\"public\" xmlns:internal=\"http://www.orcid.org/ns/internal\" xmlns:funding=\"http://www.orcid.org/ns/funding\" xmlns:preferences=\"http://www.orcid.org/ns/preferences\" xmlns:address=\"http://www.orcid.org/ns/address\" xmlns:education=\"http://www.orcid.org/ns/education\" xmlns:work=\"http://www.orcid.org/ns/work\" xmlns:deprecated=\"http://www.orcid.org/ns/deprecated\" xmlns:other-name=\"http://www.orcid.org/ns/other-name\" xmlns:history=\"http://www.orcid.org/ns/history\" xmlns:employment=\"http://www.orcid.org/ns/employment\" xmlns:error=\"http://www.orcid.org/ns/error\" xmlns:common=\"http://www.orcid.org/ns/common\" xmlns:person=\"http://www.orcid.org/ns/person\" xmlns:activities=\"http://www.orcid.org/ns/activities\" xmlns:record=\"http://www.orcid.org/ns/record\" xmlns:researcher-url=\"http://www.orcid.org/ns/researcher-url\" xmlns:peer-review=\"http://www.orcid.org/ns/peer-review\" xmlns:personal-details=\"http://www.orcid.org/ns/personal-details\" xmlns:bulk=\"http://www.orcid.org/ns/bulk\" xmlns:keyword=\"http://www.orcid.org/ns/keyword\" xmlns:email=\"http://www.orcid.org/ns/email\" xmlns:external-identifier=\"http://www.orcid.org/ns/external-identifier\">\n" +
            "    <common:created-date>2018-03-29T08:12:17.179Z</common:created-date>\n" +
            "    <common:last-modified-date>2018-03-29T08:12:17.179Z</common:last-modified-date>\n" +
            "    <common:source>\n" +
            "        <common:source-orcid>\n" +
            "            <common:uri>https://orcid.org/0000-0001-5264-4324</common:uri>\n" +
            "            <common:path>0000-0001-5264-4324</common:path>\n" +
            "            <common:host>orcid.org</common:host>\n" +
            "        </common:source-orcid>\n" +
            "        <common:source-name>Bastien Mérigot</common:source-name>\n" +
            "    </common:source>\n" +
            "    <work:title>\n" +
            "        <common:title>A three-dimensional view on biodiversity changes: spatial, temporal, and functional perspectives on fish communities in the Baltic Sea</common:title>\n" +
            "    </work:title>\n" +
            "    <work:journal-title>ICES Journal of Marine Science</work:journal-title>\n" +
            "    <work:citation>\n" +
            "        <work:citation-type>bibtex</work:citation-type>\n" +
            "        <work:citation-value>@article{doi:10.1093/icesjms/fsy027, author= {Frelat, Romain and Orio, Alessandro and Casini, Michele and Lehmann, Andreas and Mérigot, Bastien and Otto, Saskia A and Sguotti, Camilla and Möllmann, Christian and Handling editor: Marta Coll}, title= {A three-dimensional view on biodiversity changes: spatial, temporal, and functional perspectives on fish communities in the Baltic Sea}, journal= {ICES Journal of Marine Science}, volume= {}, number= {}, pages= {fsy027}, year= {2018}, doi= {10.1093/icesjms/fsy027}, URL= {http://dx.doi.org/10.1093/icesjms/fsy027}, eprint= {/oup/backfile/content_public/journal/icesjms/pap/10.1093_icesjms_fsy027/1/fsy027.pdf}}\n" +
            "\n" +
            "</work:citation-value>\n" +
            "    </work:citation>\n" +
            "    <work:type>journal-article</work:type>\n" +
            "    <common:publication-date>\n" +
            "        <common:year>2018</common:year>\n" +
            "    </common:publication-date>\n" +
            "    <common:external-ids>\n" +
            "        <common:external-id>\n" +
            "            <common:external-id-type>doi</common:external-id-type>\n" +
            "            <common:external-id-value>10.1093/icesjms/fsy027</common:external-id-value>\n" +
            "            <common:external-id-relationship>self</common:external-id-relationship>\n" +
            "        </common:external-id>\n" +
            "        <common:external-id>\n" +
            "            <common:external-id-type>arxiv</common:external-id-type>\n" +
            "            <common:external-id-value>/oup/backfile/content_public/journal/icesjms/pap/10.1093_icesjms_fsy027/1/fsy027.pdf</common:external-id-value>\n" +
            "            <common:external-id-relationship>self</common:external-id-relationship>\n" +
            "        </common:external-id>\n" +
            "    </common:external-ids>\n" +
            "    <work:url>http://dx.doi.org/10.1093/icesjms/fsy027</work:url>\n" +
            "    <work:contributors/>\n" +
            "</work:work>";

    private String orcidXml ="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
            "<work:work put-code=\"21755327\" path=\"/0000-0002-9414-646X/work/21755327\" visibility=\"public\" xmlns:internal=\"http://www.orcid.org/ns/internal\" xmlns:funding=\"http://www.orcid.org/ns/funding\" xmlns:preferences=\"http://www.orcid.org/ns/preferences\" xmlns:address=\"http://www.orcid.org/ns/address\" xmlns:education=\"http://www.orcid.org/ns/education\" xmlns:work=\"http://www.orcid.org/ns/work\" xmlns:deprecated=\"http://www.orcid.org/ns/deprecated\" xmlns:other-name=\"http://www.orcid.org/ns/other-name\" xmlns:history=\"http://www.orcid.org/ns/history\" xmlns:employment=\"http://www.orcid.org/ns/employment\" xmlns:error=\"http://www.orcid.org/ns/error\" xmlns:common=\"http://www.orcid.org/ns/common\" xmlns:person=\"http://www.orcid.org/ns/person\" xmlns:activities=\"http://www.orcid.org/ns/activities\" xmlns:record=\"http://www.orcid.org/ns/record\" xmlns:researcher-url=\"http://www.orcid.org/ns/researcher-url\" xmlns:peer-review=\"http://www.orcid.org/ns/peer-review\" xmlns:personal-details=\"http://www.orcid.org/ns/personal-details\" xmlns:bulk=\"http://www.orcid.org/ns/bulk\" xmlns:keyword=\"http://www.orcid.org/ns/keyword\" xmlns:email=\"http://www.orcid.org/ns/email\" xmlns:external-identifier=\"http://www.orcid.org/ns/external-identifier\">\n" +
            "    <common:created-date>2016-01-22T14:30:35.071Z</common:created-date>\n" +
            "    <common:last-modified-date>2016-01-22T14:32:26.233Z</common:last-modified-date>\n" +
            "    <common:source>\n" +
            "        <common:source-client-id>\n" +
            "            <common:uri>https://orcid.org/client/0000-0002-3054-1567</common:uri>\n" +
            "            <common:path>0000-0002-3054-1567</common:path>\n" +
            "            <common:host>orcid.org</common:host>\n" +
            "        </common:source-client-id>\n" +
            "        <common:source-name>Crossref Metadata Search</common:source-name>\n" +
            "    </common:source>\n" +
            "    <work:title>\n" +
            "        <common:title>Providing Ubiquitous Positioning in Outdoor Environments</common:title>\n" +
            "    </work:title>\n" +
            "    <work:journal-title>2015 IEEE International Conference on Systems, Man, and Cybernetics</work:journal-title>\n" +
            "    <work:citation>\n" +
            "        <work:citation-type>bibtex</work:citation-type>\n" +
            "        <work:citation-value>@inproceedings{Moreno_2015,doi = {10.1109/smc.2015.229},url = {http://dx.doi.org/10.1109/smc.2015.229},year = 2015,month = {oct},publisher = {Institute of Electrical {\\&amp;} Electronics Engineers ({IEEE})},author = {Daniel Moreno and Sergio F. Ochoa and Roc Meseguer},title = {Providing Ubiquitous Positioning in Outdoor Environments},booktitle = {2015 {IEEE} International Conference on Systems, Man, and Cybernetics}}</work:citation-value>\n" +
            "    </work:citation>\n" +
            "    <work:type>conference-paper</work:type>\n" +
            "    <common:publication-date>\n" +
            "        <common:year>2015</common:year>\n" +
            "        <common:month>10</common:month>\n" +
            "    </common:publication-date>\n" +
            "    <common:external-ids>\n" +
            "        <common:external-id>\n" +
            "            <common:external-id-type>doi</common:external-id-type>\n" +
            "            <common:external-id-value>10.1109/smc.2015.229</common:external-id-value>\n" +
            "            <common:external-id-relationship>self</common:external-id-relationship>\n" +
            "        </common:external-id>\n" +
            "    </common:external-ids>\n" +
            "    <work:contributors>\n" +
            "        <work:contributor>\n" +
            "            <work:credit-name>Daniel Moreno</work:credit-name>\n" +
            "            <work:contributor-attributes>\n" +
            "                <work:contributor-role>author</work:contributor-role>\n" +
            "            </work:contributor-attributes>\n" +
            "        </work:contributor>\n" +
            "        <work:contributor>\n" +
            "            <work:credit-name>Sergio F. Ochoa</work:credit-name>\n" +
            "            <work:contributor-attributes>\n" +
            "                <work:contributor-role>author</work:contributor-role>\n" +
            "            </work:contributor-attributes>\n" +
            "        </work:contributor>\n" +
            "        <work:contributor>\n" +
            "            <work:credit-name>Roc Meseguer</work:credit-name>\n" +
            "            <work:contributor-attributes>\n" +
            "                <work:contributor-role>author</work:contributor-role>\n" +
            "            </work:contributor-attributes>\n" +
            "        </work:contributor>\n" +
            "    </work:contributors>\n" +
            "</work:work>";
    @Test
    public void orcid2ResultTest() {
        Result result = ExternalRecordParser.orcid2Result(orcidXml, "0000-0002-9414-646X-21755327", "");
        System.out.println(result);
        System.out.println(result.getTitle() + result.getAccessRights());
        Assert.assertNull(result.getAccessRights());
        Assert.assertNull(result.getBestLicense());
        Assert.assertEquals("openaire____::orcid", result.getCollectedFrom());
        Assert.assertEquals("10.1109/smc.2015.229", result.getDoi());
        Assert.assertEquals("0000-0002-9414-646X-21755327", result.getOrcidworkid());
        Assert.assertNull(result.getOai());
        Assert.assertNotNull(result.getOpenaireId());//        Assert.assertNull(result.getOpenaireId());
        Assert.assertNull(result.getPmcid());
        Assert.assertEquals(ClaimUtils.PUBLICATION, result.getResultType());
        Assert.assertEquals("Providing Ubiquitous Positioning in Outdoor Environments", result.getTitle());
        Assert.assertEquals("xml", result.getRecordFormat());
        Assert.assertEquals("https://dx.doi.org/10.1109/smc.2015.229", result.getExternalUrl());
        Assert.assertNull(result.getEmbargoEndDate());

    }
    @Test
    public void orcidSecond2ResultTest() throws Exception {
        String id =  "0000-0002-3477-3082-5288650";
        Result result = ExternalRecordParser.orcid2Result(searchUtils.fetchResultXMLFromOrcid(id),id, "");
        System.out.println(result);
         id =  "0000-0002-3477-3082-5288651";
         result = ExternalRecordParser.orcid2Result(searchUtils.fetchResultXMLFromOrcid(id),id, "");
        System.out.println(result);
//        System.out.println(result.getTitle() + result.getAccessRights());
        Assert.assertNull(result.getAccessRights());
        Assert.assertNull(result.getBestLicense());
        Assert.assertEquals("openaire____::orcid", result.getCollectedFrom());
//        Assert.assertEquals("10.1109/smc.2015.229", result.getDoi());
        Assert.assertEquals(id, result.getOrcidworkid());
        Assert.assertNull(result.getOai());
        Assert.assertNotNull(result.getOpenaireId());//        Assert.assertNull(result.getOpenaireId());
        Assert.assertNull(result.getPmcid());
        Assert.assertEquals(ClaimUtils.PUBLICATION, result.getResultType());
//        Assert.assertEquals("A Survey of Context-Aware Cross-Digital Library Personalization\n", result.getTitle());
        Assert.assertEquals("xml", result.getRecordFormat());
//        Assert.assertEquals("https://dx.doi.org/10.1007/978-3-642-27169-4_2", result.getExternalUrl());
        Assert.assertNull(result.getEmbargoEndDate());

    }
    @Test
    public void orcid2Result2Test() {
        Result result = ExternalRecordParser.orcid2Result(orcidXml2, "0000-0001-5264-4324-43079714", "");
        System.out.println(result);
        Assert.assertNull(result.getAccessRights());
        Assert.assertNull(result.getBestLicense());
        Assert.assertEquals("openaire____::orcid", result.getCollectedFrom());
        Assert.assertEquals("10.1093/icesjms/fsy027", result.getDoi());
        Assert.assertEquals("0000-0001-5264-4324-43079714", result.getOrcidworkid());
        Assert.assertNull(result.getOai());
        Assert.assertNotNull(result.getOpenaireId());//        Assert.assertNull(result.getOpenaireId());
        Assert.assertNull(result.getPmcid());
        Assert.assertEquals(ClaimUtils.PUBLICATION, result.getResultType());
        Assert.assertEquals("A three-dimensional view on biodiversity changes: spatial, temporal, and functional perspectives on fish communities in the Baltic Sea", result.getTitle());
        Assert.assertEquals("xml", result.getRecordFormat());
        Assert.assertEquals("https://dx.doi.org/10.1093/icesjms/fsy027", result.getExternalUrl());
        Assert.assertNull(result.getEmbargoEndDate());

    }



    private String dataciteXml = "<rdf:RDF\n" +
            "    xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
            "    xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n" +
            "    xmlns:j.0=\"http://purl.org/dc/terms/\" > \n" +
            "  <rdf:Description rdf:about=\"http://dx.doi.org/10.5060/D4RN35SD/USGS85K1_041\">\n" +
            "    <j.0:creator>Krimmel, Robert</j.0:creator>\n" +
            "    <j.0:publisher>National Snow and Ice Data Center/World Data Center for Glaciology, Boulder</j.0:publisher>\n" +
            "    <j.0:title>Chocolate Glacier</j.0:title>\n" +
            "    <j.0:date>2008</j.0:date>\n" +
            "    <owl:sameAs>doi:10.5060/D4RN35SD/USGS85K1_041</owl:sameAs>\n" +
            "    <owl:sameAs>info:doi/10.5060/D4RN35SD/USGS85K1_041</owl:sameAs>\n" +
            "    <j.0:identifier>10.5060/D4RN35SD/USGS85K1_041</j.0:identifier>\n" +
            "  </rdf:Description>\n" +
            "</rdf:RDF>\n";

    private String dataciteJson = "{\"data\":{\"id\":\"https://doi.org/10.5281/ZENODO.54730\",\"type\":\"works\",\"attributes\":{\"doi\":\"10.5281/ZENODO.54730\",\"url\":null,\"author\":[{\"family\":\"Manola\",\"given\":\"Natalia\"}],\"title\":\"From Open Access to Open Science\",\"container-title\":\"Zenodo\",\"description\":\"Presentation given at the PASTEUR4OA conference - Green Light for Open Access: Aligning Europe’s OA Policies, Amsterdam, 17th-18th May 2016.\",\"resource-type-subtype\":\"Presentation\",\"data-center-id\":\"cern.zenodo\",\"member-id\":\"cern\",\"resource-type-id\":\"text\",\"version\":null,\"license\":\"https://creativecommons.org/licenses/by/4.0/\",\"schema-version\":\"3\",\"results\":[],\"related-identifiers\":[],\"published\":\"2016\",\"deposited\":\"2016-06-03T17:03:51Z\",\"updated\":\"2016-06-03T17:03:51Z\",\"media\":null,\"xml\":\"PHJlc291cmNlIHhtbG5zPSJodHRwOi8vZGF0YWNpdGUub3JnL3NjaGVtYS9rZXJuZWwtMyIgeG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIgeHNpOnNjaGVtYUxvY2F0aW9uPSJodHRwOi8vZGF0YWNpdGUub3JnL3NjaGVtYS9rZXJuZWwtMyBodHRwOi8vc2NoZW1hLmRhdGFjaXRlLm9yZy9tZXR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JlYXRpdmUgQ29tbW9ucyBBdHRyaWJ1dGlvbiA0LjAgSW50ZXJuYXRpb25hbDwvcmlnaHRzPgogIDwvcmlnaHRzTGlzdD4KICA8ZGVzY3JpcHRpb25zPgogICAgPGRlc2NyaXB0aW9uIGRlc2NyaXB0aW9uVHlwZT0iQWJzdHJhY3QiPiZsdDtwJmd0O1ByZXNlbnRhdGlvbiBnaXZlbiBhdCB0aGUgUEFTVEVVUjRPQSBjb25mZXJlbmNlIC0mYW1wO25ic3A7R3JlZW4gTGlnaHQgZm9yIE9wZW4gQWNjZXNzOiBBbGlnbmluZyBFdXJvcGUmYW1wO3JzcXVvO3MgT0EgUG9saWNpZXMsIEFtc3RlcmRhbSwgMTd0aC0xOHRoIE1heSAyMDE2LiZsdDsvcCZndDs8L2Rlc2NyaXB0aW9uPgogIDwvZGVzY3JpcHRpb25zPgo8L3Jlc291cmNlPgo=\"},\"relationships\":{\"data-center\":{\"meta\":{}},\"member\":{\"meta\":{}},\"resource-type\":{\"meta\":{}}}}}";

    @Test
    public void datacite2ResultTest() {
        Result result = ExternalRecordParser.datacite2Result(dataciteXml, "");

        Assert.assertNull(result.getAccessRights());
        Assert.assertNull(result.getBestLicense());
        Assert.assertEquals("openaire____::datacite", result.getCollectedFrom());
        Assert.assertEquals("10.5060/D4RN35SD/USGS85K1_041", result.getDoi());
        Assert.assertNull(result.getOrcidworkid());
        Assert.assertNull(result.getOai());
        Assert.assertNotNull(result.getOpenaireId());//        Assert.assertNull(result.getOpenaireId());
        Assert.assertNull(result.getPmcid());
        Assert.assertEquals(ClaimUtils.DATASET, result.getResultType());
        Assert.assertEquals("Chocolate Glacier", result.getTitle());
        Assert.assertEquals("xml", result.getRecordFormat());
        Assert.assertEquals("http://dx.doi.org/10.5060/D4RN35SD/USGS85K1_041", result.getExternalUrl());
        Assert.assertNull(result.getEmbargoEndDate());

    }

    @Test
    public void dataciteJsonResultTest() {
        Result result = ExternalRecordParser.dataciteJson2Result(dataciteJson, "");

        Assert.assertNull(result.getAccessRights());
        Assert.assertNull(result.getBestLicense());
        Assert.assertEquals("openaire____::datacite", result.getCollectedFrom());
        Assert.assertEquals("10.5281/ZENODO.54730", result.getDoi());
        Assert.assertNull(result.getOrcidworkid());
        Assert.assertNull(result.getOai());
        Assert.assertNotNull(result.getOpenaireId());//        Assert.assertNull(result.getOpenaireId());
        Assert.assertNull(result.getPmcid());
        Assert.assertEquals(ClaimUtils.DATASET, result.getResultType());
        Assert.assertEquals("From Open Access to Open Science", result.getTitle());
        Assert.assertEquals("json", result.getRecordFormat());
        Assert.assertEquals("https://dx.doi.org/10.5281/ZENODO.54730", result.getExternalUrl());
        Assert.assertNull(result.getEmbargoEndDate());

    }
    @Test
    public void testMD5() throws Exception {
        String id = "10.1016/j.engstruct.2013.03.014";
        String createdId = ExternalRecordParser.createOpenaireId(id);
        System.out.println(createdId);
        String openaireId = "userclaim___::07ec9eb2278a11e352e3fa93a621411d";
        System.out.println(openaireId);
        System.out.println(ExternalRecordParser.createOpenaireId(id));
        System.out.println(ExternalRecordParser.createOpenaireId(id));
        System.out.println(createdId.length() + " " + openaireId.length());
//        Assert.assertEquals(createdId,openaireId);

    }
}
