/*
 * Copyright 2014 The Kuali Foundation.
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.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.ar.service.impl;

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

import java.util.Random;

import org.kuali.kfs.module.ar.ArPropertyConstants;
import org.kuali.kfs.module.ar.businessobject.CustomerAddress;
import org.kuali.kfs.module.ar.document.service.CustomerService;
import org.kuali.kfs.sys.ConfigureContext;
import org.kuali.kfs.sys.context.KualiTestBase;
import org.kuali.kfs.sys.context.SpringContext;
import org.kuali.rice.kns.service.DataDictionaryService;
import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
import org.kuali.rice.krad.service.DocumentService;
import org.kuali.rice.krad.service.KualiModuleService;
import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;

@ConfigureContext(session = khuntley)
public class CustomerDocumentServiceImplTest extends KualiTestBase {
    protected CustomerDocumentServiceImpl customerDocumentServiceImpl;
    protected DataDictionaryService dataDictionaryService;
    protected char[] characters = null;
    protected Random rand;

    @Override
    public void setUp() {
        customerDocumentServiceImpl = new CustomerDocumentServiceImpl();
        customerDocumentServiceImpl.setCustomerService(SpringContext.getBean(CustomerService.class));
        customerDocumentServiceImpl.setDataDictionaryService(SpringContext.getBean(DataDictionaryService.class));
        customerDocumentServiceImpl.setDocumentService(SpringContext.getBean(DocumentService.class));
        customerDocumentServiceImpl.setMaintenanceDocumentDictionaryService(SpringContext.getBean(MaintenanceDocumentDictionaryService.class));
        customerDocumentServiceImpl.setKualiModuleService(SpringContext.getBean(KualiModuleService.class));
        customerDocumentServiceImpl.setWorkflowDocumentService(SpringContext.getBean(WorkflowDocumentService.class));

        dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);

        characters = new char[52];
        int count = 0;
        for (char i = 'A'; i <= 'Z'; i++) {
            characters[count] = i;
            count += 1;
        }
        for (char i = 'a'; i <= 'z'; i++) {
            characters[count] = i;
            count += 1;
        }

        rand = new Random();
    }

    public void testTruncateField() {
        final int customerAddressNameLength = dataDictionaryService.getAttributeMaxLength(CustomerAddress.class, ArPropertyConstants.CustomerAddressFields.CUSTOMER_ADDRESS_NAME);
        final String customerAddressNameShort = randomString(customerAddressNameLength-3);
        final String customerAddressNameLong = randomString(customerAddressNameLength+7);
        final String customerAddressNameBabyBear = randomString(customerAddressNameLength);

        final String customerAddressNameShortTruncated = customerDocumentServiceImpl.truncateField(CustomerAddress.class, ArPropertyConstants.CustomerAddressFields.CUSTOMER_ADDRESS_NAME, customerAddressNameShort);
        final String customerAddressNameLongTruncated = customerDocumentServiceImpl.truncateField(CustomerAddress.class, ArPropertyConstants.CustomerAddressFields.CUSTOMER_ADDRESS_NAME, customerAddressNameLong);
        final String customerAddressNameBabyBearTruncated = customerDocumentServiceImpl.truncateField(CustomerAddress.class, ArPropertyConstants.CustomerAddressFields.CUSTOMER_ADDRESS_NAME, customerAddressNameBabyBear);

        assertEquals("Customer Address Name Short should have the same length as truncated version", customerAddressNameShort.length(), customerAddressNameShortTruncated.length());
        assertEquals("Customer Address Name Short should be the same String as the truncated version", customerAddressNameShort, customerAddressNameShortTruncated);
        assertEquals("Customer Address Name Long should have the max length of the customer address name field", customerAddressNameLength, customerAddressNameLongTruncated.length());
        assertTrue("Customer Address Name Long should start with the truncated version", customerAddressNameLong.startsWith(customerAddressNameLongTruncated));
        assertEquals("Customer Address Name Baby Bear should have the same length as the truncated version", customerAddressNameBabyBear.length(), customerAddressNameBabyBearTruncated.length());
        assertEquals("Customer Address Name Baby Bear should have the same length as the customer address name field", customerAddressNameLength, customerAddressNameBabyBearTruncated.length());
        assertEquals("Customer Address Name Baby Bear should be the exact same string as the truncated version", customerAddressNameBabyBear, customerAddressNameBabyBearTruncated);
    }

    protected String randomString(int size) {
        StringBuilder s = new StringBuilder();
        int count = 0;
        while (count < size) {
            int idx = rand.nextInt(characters.length);
            s.append(characters[idx]);
            count += 1;
        }
        return s.toString();
    }
}
@@ -1,11 +1,111 @@
 /*%0A
+ * The Kuali Financial System, a comprehensive financial management system for higher education.%0A *%0A
  * Copyr
@@ -109,16 +109,21 @@
 pyright 
+2005-
 2014 The
@@ -143,161 +143,146 @@
 tion
-.
 %0A *%0A * 
-Licensed under the Educational Community License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the
+This program is free software: you can redistribute it and/or modify%0A * it under the terms of the GNU Affero General Public
  License
 .%0A *
@@ -281,49 +281,92 @@
 ense
-.
+ as
 %0A * 
-You may obtain a copy
+published by the Free Software Foundation, either version 3
  of the
+%0A *
  License
  at%0A
@@ -365,207 +365,125 @@
 ense
- at%0A *%0A * http://www.opensource.org/licenses/ecl2.php%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS
+, or (at your option) any later version.%0A *%0A * This program is distributed in the hope that it will be useful
 ,%0A *
+ but
  WIT
@@ -491,176 +491,329 @@
 OUT 
+ANY 
 WARRANT
-IES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the L
+Y; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A * GNU Affero General Public License for more details.%0A *%0A * You should have received a copy of the GNU Affero General Public License%0A * along with this program.  If not, see %3Chttp://www.gnu.org/l
 icense
+s/%3E
 .%0A *

use new AGPL license