org.ambraproject.article.service.ArticleDocumentServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for org.ambraproject.article.service.ArticleDocumentServiceTest.java

Source

/*
 * $HeadURL$
 * $Id$
 *
 * Copyright (c) 2006-2011 by Public Library of Science
 * http://plos.org
 * http://ambraproject.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. |
 */

package org.ambraproject.article.service;

import org.ambraproject.admin.AdminBaseTest;
import org.ambraproject.models.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.sql.rowset.serial.SerialBlob;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.sql.Blob;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

/**
 * Test for the {@link org.ambraproject.article.service.ArticleDocumentService} implementation. For the hibernate
 * implementation, working directory for the test must be either the new hope base directory or the ambra webapp home
 * directory
 *
 * @author Alex Kudlick Date: 5/12/11
 *         <p/>
 *         org.ambraproject.article.service
 */
public class ArticleDocumentServiceTest extends AdminBaseTest {

    @Autowired
    protected ArticleDocumentService articleDocumentService;
    @Autowired
    protected DocumentBuilderFactory documentBuilderFactory;

    @DirtiesContext
    @Test(priority = 1)
    public void clearTest() {
        // Insure that we reset the datbase before running this test.
    }

    @DataProvider(name = "blob")
    public Object[][] blob() throws Exception {
        String xml = "<xml><childNode>some test xml</childNode></xml>";
        Document parsedXml = documentBuilderFactory.newDocumentBuilder()
                .parse(new ByteArrayInputStream(xml.getBytes()));

        return new Object[][] { { new SerialBlob(xml.getBytes()), parsedXml } };
    }

    @Test(dataProvider = "blob")
    public void testParseBlob(Blob blob, Document expectedXml) throws Exception {
        Document xml = articleDocumentService.getDocument(blob);
        assertNotNull(xml, "returned null xml");
        assertEquals(xml.getElementsByTagName("*").getLength(), expectedXml.getElementsByTagName("*").getLength(),
                "returned xml with incorrect number of tags");
    }

    @Test
    public void testGetFullXml() throws Exception {
        Article article = new Article("info:doi/10.1371/journal.pgen.1000096");
        dummyDataStore.store(article);

        Document xml = articleDocumentService.getFullDocument(article.getDoi());
        assertNotNull(xml, "returned null xml document");

        //check the doi from the xml
        NodeList articleIdNodes = xml.getElementsByTagName("article-id");
        for (int i = 0; i < articleIdNodes.getLength(); i++) {
            Node node = articleIdNodes.item(i);
            if (node.getAttributes().getNamedItem("pub-id-type").getNodeValue().equals("doi")) {
                assertEquals(node.getChildNodes().item(0).getNodeValue(),
                        article.getDoi().replaceFirst("info:doi/", ""), "returned article xml with incorrect doi");
                break;
            }
        }
    }
}