<commit_before>package dk.statsbiblioteket.doms.tools.handleregistrar;

import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import dk.statsbiblioteket.doms.client.DomsWSClient;
import dk.statsbiblioteket.doms.client.DomsWSClientImpl;
import dk.statsbiblioteket.util.xml.DefaultNamespaceContext;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.util.List;

/**
 * Test handle objects using online Fedora.
 * NOTE: This test will _only_ work if the fedora mentioned in the test
 * config is available and contains the expected data.
 *
 * Expected data is:
 * Object with UUID uuid:00022366-955b-4cb5-9646-e04c9262bd6f exists and has handle 109.3.1/uuid:00022366-955b-4cb5-9646-e04c9262bd6f
 * Object with UUID uuid:0041e7a1-79e3-4314-8af0-f59d71fd90f2 exists and has no handles
 */
public class DomsHandlerTest extends TestCase {

    private static final String PID1 = "uuid:00022366-955b-4cb5-9646-e04c9262bd6f";
    private static final String PID2 = "uuid:0041e7a1-79e3-4314-8af0-f59d71fd90f2";
    private PropertyBasedRegistrarConfiguration config;
    private Document d1;
    private Document d2;
    private DomsWSClient domsClient;
    private static final String DC_PREFIX = "dc";
    private static final String DC_NAMESPACE_URI
            = "http://purl.org/dc/elements/1.1/";
    private static final NamespaceContext dsNamespaceContext
            = new DefaultNamespaceContext();
    static {
        ((DefaultNamespaceContext) dsNamespaceContext)
                .setNameSpace(DC_NAMESPACE_URI,
                              DC_PREFIX);
    }

    public void setUp() throws Exception {
        config = new PropertyBasedRegistrarConfiguration(
                new File("src/test/config/handleregistrar.properties"));
        domsClient = new DomsWSClientImpl();
        domsClient.setCredentials(config.getDomsWSAPIEndpoint(),
                                  config.getUsername(), config.getPassword());
        d1 = domsClient.getDataStream(PID1, "DC");
        d2 = domsClient.getDataStream(PID2, "DC");
    }

    public void tearDown() throws Exception {
        domsClient.unpublishObjects("Restored original content from unit test",
                                    PID1);
        domsClient.updateDataStream(PID1, "DC", d1,
                                    "Restored original content from unit test");
        domsClient.publishObjects("Restored original content from unit test",
                                  PID1);
        domsClient.unpublishObjects("Restored original content from unit test",
                                    PID2);
        domsClient.updateDataStream(PID2, "DC", d2,
                                    "Restored original content from unit test");
        domsClient.publishObjects("Restored original content from unit test",
                                  PID2);
    }

    public void testFindObjectFromQuery() throws Exception {
        DomsHandler domsHandler = new DomsHandler(config);
        List<String> pids = domsHandler.findObjectFromQuery("select $object\n" +
                       "from <#ri> \n" +
                       "where\n" +
                       "$object <info:fedora/fedora-system:def/model#label> 'P2 Koncerten'\n");
        assertTrue("List of found finds does not contain '" + PID1 + "', but: " + pids, pids.contains(PID1));
        pids = domsHandler.findObjectFromQuery("select $object 'test'\n" +
                       "from <#ri> \n" +
                       "where\n" +
                       "$object <info:fedora/fedora-system:def/model#label> 'P2 Koncerten'\n");
        assertTrue("List of found finds does not contain '" + PID1 + "', but: " + pids, pids.contains(PID1));
    }

    public void testAddHandleToObject() throws Exception {
        DomsHandler domsHandler = new DomsHandler(config);

        // Call method on object with existing handle
        String handle1 = domsHandler.addHandleToObject(PID1);
        // Check correct handle generated
        assertEquals("109.3.1/" + PID1, handle1);<commit_msg>Updated PIDs to PIDs with right properties<commit_after>package dk.statsbiblioteket.doms.tools.handleregistrar;

import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import dk.statsbiblioteket.doms.client.DomsWSClient;
import dk.statsbiblioteket.doms.client.DomsWSClientImpl;
import dk.statsbiblioteket.util.xml.DefaultNamespaceContext;

import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.util.List;

/**
 * Test handle objects using online Fedora.
 * NOTE: This test will _only_ work if the fedora mentioned in the test
 * config is available and contains the expected data.
 *
 * Expected data is:
 * Object with UUID uuid:001fdf2b-a05a-40de-a43b-787f1ba9041f exists and has handle 109.3.1/uuid:001fdf2b-a05a-40de-a43b-787f1ba9041f and label 'Jeg er et dyr!'
 * Object with UUID uuid:0019f31d-b6f7-4ef2-81f6-89b116c64272 exists and has no handles
 */
public class DomsHandlerTest extends TestCase {

    private static final String PID1 = "uuid:001fdf2b-a05a-40de-a43b-787f1ba9041f";
    private static final String PID2 = "uuid:0019f31d-b6f7-4ef2-81f6-89b116c64272";
    private PropertyBasedRegistrarConfiguration config;
    private Document d1;
    private Document d2;
    private DomsWSClient domsClient;
    private static final String DC_PREFIX = "dc";
    private static final String DC_NAMESPACE_URI
            = "http://purl.org/dc/elements/1.1/";
    private static final NamespaceContext dsNamespaceContext
            = new DefaultNamespaceContext();
    static {
        ((DefaultNamespaceContext) dsNamespaceContext)
                .setNameSpace(DC_NAMESPACE_URI,
                              DC_PREFIX);
    }

    public void setUp() throws Exception {
        config = new PropertyBasedRegistrarConfiguration(
                new File("src/test/config/handleregistrar.properties"));
        domsClient = new DomsWSClientImpl();
        domsClient.setCredentials(config.getDomsWSAPIEndpoint(),
                                  config.getUsername(), config.getPassword());
        d1 = domsClient.getDataStream(PID1, "DC");
        d2 = domsClient.getDataStream(PID2, "DC");
    }

    public void tearDown() throws Exception {
        domsClient.unpublishObjects("Restored original content from unit test",
                                    PID1);
        domsClient.updateDataStream(PID1, "DC", d1,
                                    "Restored original content from unit test");
        domsClient.publishObjects("Restored original content from unit test",
                                  PID1);
        domsClient.unpublishObjects("Restored original content from unit test",
                                    PID2);
        domsClient.updateDataStream(PID2, "DC", d2,
                                    "Restored original content from unit test");
        domsClient.publishObjects("Restored original content from unit test",
                                  PID2);
    }

    public void testFindObjectFromQuery() throws Exception {
        DomsHandler domsHandler = new DomsHandler(config);
        List<String> pids = domsHandler.findObjectFromQuery("select $object\n" +
                       "from <#ri> \n" +
                       "where\n" +
                       "$object <info:fedora/fedora-system:def/model#label> 'Jeg er et dyr!'\n");
        assertTrue("List of found finds does not contain '" + PID1 + "', but: " + pids, pids.contains(PID1));
        pids = domsHandler.findObjectFromQuery("select $object 'test'\n" +
                       "from <#ri> \n" +
                       "where\n" +
                       "$object <info:fedora/fedora-system:def/model#label> 'Jeg er et dyr!'\n");
        assertTrue("List of found finds does not contain '" + PID1 + "', but: " + pids, pids.contains(PID1));
    }

    public void testAddHandleToObject() throws Exception {
        DomsHandler domsHandler = new DomsHandler(config);

        // Call method on object with existing handle
        String handle1 = domsHandler.addHandleToObject(PID1);
        // Check correct handle generated
        assertEquals("109.3.1/" + PID1, handle1);
{'fraction_non_alphanumeric': 0.09380175065057961, 'fraction_numerical': 0.0347764371894961, 'mean_word_length': 2.533221897200167, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5633622', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2429, 'n_words': 577}