<commit_before>/*
 * Copyright 2008 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
 * 
 * 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.kuali.kfs.module.purap.service;

import static org.kuali.kfs.sys.fixture.UserNameFixture.khuntley;
import static org.kuali.kfs.sys.fixture.UserNameFixture.kuluser;

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.io.FileUtils;
import org.kuali.kfs.module.purap.PurapParameterConstants;
import org.kuali.kfs.module.purap.batch.ElectronicInvoiceInputFileType;
import org.kuali.kfs.module.purap.batch.ElectronicInvoiceStep;
import org.kuali.kfs.module.purap.businessobject.ElectronicInvoice;
import org.kuali.kfs.module.purap.businessobject.ElectronicInvoiceLoad;
import org.kuali.kfs.module.purap.document.ElectronicInvoiceRejectDocument;
import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
import org.kuali.kfs.module.purap.document.RequisitionDocument;
import org.kuali.kfs.module.purap.fixture.ElectronicInvoiceHelperServiceFixture;
import org.kuali.kfs.module.purap.fixture.PurchaseOrderDocumentFixture;
import org.kuali.kfs.module.purap.fixture.RequisitionDocumentFixture;
import org.kuali.kfs.sys.ConfigureContext;
import org.kuali.kfs.sys.context.KualiTestBase;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.kfs.sys.dataaccess.UnitTestSqlDao;
import org.kuali.kfs.sys.document.AccountingDocumentTestUtils;
import org.kuali.kfs.sys.document.workflow.WorkflowTestUtils;
import org.kuali.kfs.sys.service.ParameterService;
import org.kuali.kfs.sys.suite.RelatesTo;
import org.kuali.kfs.sys.suite.RelatesTo.JiraIssue;
import org.kuali.rice.kns.service.DocumentService;
import org.kuali.rice.kns.util.Guid;

public class ElectronicInvoiceHelperServiceTest extends KualiTestBase {
    
    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ElectronicInvoiceHelperServiceTest.class);
    private ElectronicInvoiceInputFileType electronicInvoiceInputFileType;
    private UnitTestSqlDao unitTestSqlDao;
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        electronicInvoiceInputFileType = SpringContext.getBean(ElectronicInvoiceInputFileType.class);
        SpringContext.getBean(ParameterService.class).setParameterForTesting(ElectronicInvoiceStep.class, PurapParameterConstants.ElectronicInvoiceParameters.FILE_MOVE_AFTER_LOAD_IND,"yes");
        FileUtils.cleanDirectory(new File(electronicInvoiceInputFileType.getDirectoryPath()));
        unitTestSqlDao = SpringContext.getBean(UnitTestSqlDao.class);
    }
    
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

	@RelatesTo(JiraIssue.KULPURAP3047)
    @ConfigureContext(session = kuluser, shouldCommitTransactions=false)
    public void testRejectDocumentCreationInvalidData()
    throws Exception{
        
        String rejectFile = "reject.xml";
        PurchaseOrderDocument poDocument = createPODoc(null);
        String vendorDUNS = poDocument.getVendorDetail().getVendorDunsNumber();
        String poNumber = poDocument.getPurapDocumentIdentifier().toString();
        
        String xmlChunk = ElectronicInvoiceHelperServiceFixture.getCXMLForRejectDocCreation(vendorDUNS,poNumber);
        writeXMLFile(xmlChunk, rejectFile);
        
        ElectronicInvoiceLoad load = SpringContext.getBean(ElectronicInvoiceHelperService.class).loadElectronicInvoices();
        ElectronicInvoiceRejectDocument rejectDoc = (ElectronicInvoiceRejectDocument)load.getRejectDocuments().get(0);

        assertTrue(load.containsRejects());
        assertEquals(rejectDoc.getInvoiceFileName(),rejectFile);
        assertTrue((new File(electronicInvoiceInputFileType.getDirectoryPath() + File.separator + "reject" + File.separator + rejectFile)).exists());
        
    }
    
    @RelatesTo(JiraIssue.KULPURAP3047)
    @ConfigureContext(session = kuluser, shouldCommitTransactions=false)
    public void testRejectDocumentCreationCorruptXML()
    throws Exception{
        
        String rejectFile = "corrupt.xml";
        PurchaseOrderDocument poDocument = createPODoc(null);
        String vendorDUNS = poDocument.getVendorDetail().getVendorDunsNumber();
        String poNumber = poDocument.getPurapDocumentIdentifier().toString();
        
        String xmlChunk = ElectronicInvoiceHelperServiceFixture.getCorruptedCXML(vendorDUNS,poNumber);
        writeXMLFile(xmlChunk, rejectFile);
        
        ElectronicInvoiceLoad load = SpringContext.getBean(ElectronicInvoiceHelperService.class).loadElectronicInvoices();
        ElectronicInvoiceRejectDocument rejectDoc = (ElectronicInvoiceRejectDocument)load.getRejectDocuments().get(0);

        assertTrue(load.containsRejects());
        assertEquals(rejectDoc.getInvoiceFileName(),rejectFile);
        assertTrue((new File(electronicInvoiceInputFileType.getDirectoryPath() + File.separator + "reject" + File.separator + rejectFile)).exists());
        
    }

	@RelatesTo(JiraIssue.KULPURAP3047)
    @ConfigureContext(session = kuluser, shouldCommitTransactions=false)
    public void testPaymentRequestDocumentCreation()
    throws Exception{
        
        String acceptFile = "accept.xml";
        
        changeCurrentUser(khuntley);
        RequisitionDocument reqDoc = RequisitionDocumentFixture.REQ_ONLY_REQUIRED_FIELDS.createRequisitionDocument();
        AccountingDocumentTestUtils.testRouteDocument(reqDoc, SpringContext.getBean(DocumentService.class));
        Integer reqId = reqDoc.getPurapDocumentIdentifier();

        changeCurrentUser(kuluser);
        PurchaseOrderDocument poDocument = createPODoc(reqId);
       
        String vendorDUNS = poDocument.getVendorDetail().getVendorDunsNumber();
        String poNumber = poDocument.getPurapDocumentIdentifier().toString();
        
        createItemMappingsRecords(poDocument);
        updateUnitPriceVariance();
        
        String xmlChunk = ElectronicInvoiceHelperServiceFixture.getCXMLForPaymentDocCreation(vendorDUNS,poNumber);
        writeXMLFile(xmlChunk, acceptFile);
        
        ElectronicInvoiceLoad load = SpringContext.getBean(ElectronicInvoiceHelperService.class).loadElectronicInvoices();

        assertFalse(load.containsRejects());
        assertTrue((new File(electronicInvoiceInputFileType.getDirectoryPath() + File.separator + "accept" + File.separator + acceptFile)).exists());
        
    }
    <commit_msg>Some minor changes for EInvoice unit tests<commit_after>/*
 * Copyright 2008 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
 * 
 * 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.kuali.kfs.module.purap.service;

import static org.kuali.kfs.sys.fixture.UserNameFixture.khuntley;
import static org.kuali.kfs.sys.fixture.UserNameFixture.kuluser;

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.io.FileUtils;
import org.kuali.kfs.module.purap.PurapConstants;
import org.kuali.kfs.module.purap.PurapParameterConstants;
import org.kuali.kfs.module.purap.batch.ElectronicInvoiceInputFileType;
import org.kuali.kfs.module.purap.batch.ElectronicInvoiceStep;
import org.kuali.kfs.module.purap.businessobject.ElectronicInvoice;
import org.kuali.kfs.module.purap.businessobject.ElectronicInvoiceLoad;
import org.kuali.kfs.module.purap.document.ElectronicInvoiceRejectDocument;
import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
import org.kuali.kfs.module.purap.document.RequisitionDocument;
import org.kuali.kfs.module.purap.fixture.ElectronicInvoiceHelperServiceFixture;
import org.kuali.kfs.module.purap.fixture.PurchaseOrderDocumentFixture;
import org.kuali.kfs.module.purap.fixture.RequisitionDocumentFixture;
import org.kuali.kfs.sys.ConfigureContext;
import org.kuali.kfs.sys.context.KualiTestBase;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.kfs.sys.dataaccess.UnitTestSqlDao;
import org.kuali.kfs.sys.document.AccountingDocumentTestUtils;
import org.kuali.kfs.sys.document.workflow.WorkflowTestUtils;
import org.kuali.kfs.sys.service.ParameterService;
import org.kuali.kfs.sys.suite.RelatesTo;
import org.kuali.kfs.sys.suite.RelatesTo.JiraIssue;
import org.kuali.rice.kns.service.DocumentService;
import org.kuali.rice.kns.util.Guid;

public class ElectronicInvoiceHelperServiceTest extends KualiTestBase {
    
    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ElectronicInvoiceHelperServiceTest.class);
    private ElectronicInvoiceInputFileType electronicInvoiceInputFileType;
    private UnitTestSqlDao unitTestSqlDao;
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        electronicInvoiceInputFileType = SpringContext.getBean(ElectronicInvoiceInputFileType.class);
        SpringContext.getBean(ParameterService.class).setParameterForTesting(ElectronicInvoiceStep.class, PurapParameterConstants.ElectronicInvoiceParameters.FILE_MOVE_AFTER_LOAD_IND,"yes");
        FileUtils.cleanDirectory(new File(electronicInvoiceInputFileType.getDirectoryPath()));
        unitTestSqlDao = SpringContext.getBean(UnitTestSqlDao.class);
    }
    
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    @ConfigureContext(session = kuluser, shouldCommitTransactions=false)
    public void testRejectDocumentCreationInvalidData()
    throws Exception{
        
        String rejectFile = "reject.xml";
        PurchaseOrderDocument poDocument = createPODoc(null);
        String vendorDUNS = poDocument.getVendorDetail().getVendorDunsNumber();
        String poNumber = poDocument.getPurapDocumentIdentifier().toString();
        
        createItemMappingsRecords(poDocument);
        updateUnitPriceVariance();
        
        String xmlChunk = ElectronicInvoiceHelperServiceFixture.getCXMLForRejectDocCreation(vendorDUNS,poNumber);
        writeXMLFile(xmlChunk, rejectFile);
        
        ElectronicInvoiceLoad load = SpringContext.getBean(ElectronicInvoiceHelperService.class).loadElectronicInvoices();

        assertTrue(load.containsRejects());
        
        ElectronicInvoiceRejectDocument rejectDoc = (ElectronicInvoiceRejectDocument)load.getRejectDocuments().get(0);
        assertNotNull(rejectDoc);
        assertEquals(rejectDoc.getInvoiceFileName(),rejectFile);
        assertEquals(1, rejectDoc.getInvoiceRejectReasons().size());
        assertEquals(PurapConstants.ElectronicInvoice.PO_ITEM_QTY_LESSTHAN_INVOICE_ITEM_QTY,rejectDoc.getInvoiceRejectReasons().get(0).getInvoiceRejectReasonTypeCode());
        assertTrue((new File(electronicInvoiceInputFileType.getDirectoryPath() + File.separator + "reject" + File.separator + rejectFile)).exists());
        
    }
    
    @ConfigureContext(session = kuluser, shouldCommitTransactions=false)
    public void testRejectDocumentCreationCorruptXML()
    throws Exception{
        
        String corruptFile = "corrupt.xml";
        PurchaseOrderDocument poDocument = createPODoc(null);
        String vendorDUNS = poDocument.getVendorDetail().getVendorDunsNumber();
        String poNumber = poDocument.getPurapDocumentIdentifier().toString();
        
        createItemMappingsRecords(poDocument);
        updateUnitPriceVariance();
        
        String xmlChunk = ElectronicInvoiceHelperServiceFixture.getCorruptedCXML(vendorDUNS,poNumber);
        writeXMLFile(xmlChunk, corruptFile);
        
        ElectronicInvoiceLoad load = SpringContext.getBean(ElectronicInvoiceHelperService.class).loadElectronicInvoices();

        assertTrue(load.containsRejects());
        
        ElectronicInvoiceRejectDocument rejectDoc = (ElectronicInvoiceRejectDocument)load.getRejectDocuments().get(0);
        
        assertNotNull(rejectDoc);
        assertEquals(rejectDoc.getInvoiceFileName(),corruptFile);
        assertEquals(1, rejectDoc.getInvoiceRejectReasons().size());
        assertEquals(PurapConstants.ElectronicInvoice.FILE_FORMAT_INVALID,rejectDoc.getInvoiceRejectReasons().get(0).getInvoiceRejectReasonTypeCode());
        assertTrue((new File(electronicInvoiceInputFileType.getDirectoryPath() + File.separator + "reject" + File.separator + corruptFile)).exists());
        
    }

    @ConfigureContext(session = kuluser, shouldCommitTransactions=false)
    public void testPaymentRequestDocumentCreation()
    throws Exception{
        
        String acceptFile = "accept.xml";
        
        changeCurrentUser(khuntley);
        RequisitionDocument reqDoc = RequisitionDocumentFixture.REQ_ONLY_REQUIRED_FIELDS.createRequisitionDocument();
        AccountingDocumentTestUtils.testRouteDocument(reqDoc, SpringContext.getBean(DocumentService.class));
        Integer reqId = reqDoc.getPurapDocumentIdentifier();

        changeCurrentUser(kuluser);
        PurchaseOrderDocument poDocument = createPODoc(reqId);
       
        String vendorDUNS = poDocument.getVendorDetail().getVendorDunsNumber();
        String poNumber = poDocument.getPurapDocumentIdentifier().toString();
        
        createItemMappingsRecords(poDocument);
        updateUnitPriceVariance();
        
        String xmlChunk = ElectronicInvoiceHelperServiceFixture.getCXMLForPaymentDocCreation(vendorDUNS,poNumber);
        writeXMLFile(xmlChunk, acceptFile);
        
        ElectronicInvoiceLoad load = SpringContext.getBean(ElectronicInvoiceHelperService.class).loadElectronicInvoices();

        assertFalse(load.containsRejects());
        assertTrue((new File(electronicInvoiceInputFileType.getDirectoryPath() + File.separator + "accept" + File.separator + acceptFile)).exists());
        
    }
    
{'fraction_non_alphanumeric': 0.08758823932561167, 'fraction_numerical': 0.002604345144267014, 'mean_word_length': 4.554625047582794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7396105', 'n_tokens_mistral': 4256, 'n_tokens_neox': 4087, 'n_words': 635}