com.bemis.portal.customer.service.persistence.test.CustomerLocationPersistenceTest.java Source code

Java tutorial

Introduction

Here is the source code for com.bemis.portal.customer.service.persistence.test.CustomerLocationPersistenceTest.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bemis.portal.customer.service.persistence.test;

import com.bemis.portal.customer.exception.NoSuchCustomerLocationException;
import com.bemis.portal.customer.model.CustomerLocation;
import com.bemis.portal.customer.service.CustomerLocationLocalServiceUtil;
import com.bemis.portal.customer.service.persistence.CustomerLocationPersistence;
import com.bemis.portal.customer.service.persistence.CustomerLocationUtil;

import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;

import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.test.ReflectionTestUtil;
import com.liferay.portal.kernel.test.rule.AggregateTestRule;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
import com.liferay.portal.kernel.transaction.Propagation;
import com.liferay.portal.kernel.util.IntegerWrapper;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Time;
import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
import com.liferay.portal.test.rule.PersistenceTestRule;
import com.liferay.portal.test.rule.TransactionalTestRule;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import org.junit.runner.RunWith;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @generated
 */
@RunWith(Arquillian.class)
public class CustomerLocationPersistenceTest {
    @ClassRule
    @Rule
    public static final AggregateTestRule aggregateTestRule = new AggregateTestRule(
            new LiferayIntegrationTestRule(), PersistenceTestRule.INSTANCE,
            new TransactionalTestRule(Propagation.REQUIRED));

    @Before
    public void setUp() {
        _persistence = CustomerLocationUtil.getPersistence();

        Class<?> clazz = _persistence.getClass();

        _dynamicQueryClassLoader = clazz.getClassLoader();
    }

    @After
    public void tearDown() throws Exception {
        Iterator<CustomerLocation> iterator = _customerLocations.iterator();

        while (iterator.hasNext()) {
            _persistence.remove(iterator.next());

            iterator.remove();
        }
    }

    @Test
    public void testCreate() throws Exception {
        long pk = RandomTestUtil.nextLong();

        CustomerLocation customerLocation = _persistence.create(pk);

        Assert.assertNotNull(customerLocation);

        Assert.assertEquals(customerLocation.getPrimaryKey(), pk);
    }

    @Test
    public void testRemove() throws Exception {
        CustomerLocation newCustomerLocation = addCustomerLocation();

        _persistence.remove(newCustomerLocation);

        CustomerLocation existingCustomerLocation = _persistence
                .fetchByPrimaryKey(newCustomerLocation.getPrimaryKey());

        Assert.assertNull(existingCustomerLocation);
    }

    @Test
    public void testUpdateNew() throws Exception {
        addCustomerLocation();
    }

    @Test
    public void testUpdateExisting() throws Exception {
        long pk = RandomTestUtil.nextLong();

        CustomerLocation newCustomerLocation = _persistence.create(pk);

        newCustomerLocation.setCompanyId(RandomTestUtil.nextLong());

        newCustomerLocation.setUserId(RandomTestUtil.nextLong());

        newCustomerLocation.setUserName(RandomTestUtil.randomString());

        newCustomerLocation.setCreateDate(RandomTestUtil.nextDate());

        newCustomerLocation.setModifiedDate(RandomTestUtil.nextDate());

        newCustomerLocation.setBemisCustomerId(RandomTestUtil.randomString());

        newCustomerLocation.setBemisParentId(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerName(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerAddress1(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerAddress2(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerAddress3(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerCity(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerState(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerPostalCde(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerCountry(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerPhoneNbr(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerPoBox(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerPoCity(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerPoState(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerPoPostalCde(RandomTestUtil.randomString());

        newCustomerLocation.setCustomerActiveStatusCde(RandomTestUtil.randomString());

        _customerLocations.add(_persistence.update(newCustomerLocation));

        CustomerLocation existingCustomerLocation = _persistence
                .findByPrimaryKey(newCustomerLocation.getPrimaryKey());

        Assert.assertEquals(existingCustomerLocation.getCustomerLocationId(),
                newCustomerLocation.getCustomerLocationId());
        Assert.assertEquals(existingCustomerLocation.getCompanyId(), newCustomerLocation.getCompanyId());
        Assert.assertEquals(existingCustomerLocation.getUserId(), newCustomerLocation.getUserId());
        Assert.assertEquals(existingCustomerLocation.getUserName(), newCustomerLocation.getUserName());
        Assert.assertEquals(Time.getShortTimestamp(existingCustomerLocation.getCreateDate()),
                Time.getShortTimestamp(newCustomerLocation.getCreateDate()));
        Assert.assertEquals(Time.getShortTimestamp(existingCustomerLocation.getModifiedDate()),
                Time.getShortTimestamp(newCustomerLocation.getModifiedDate()));
        Assert.assertEquals(existingCustomerLocation.getBemisCustomerId(),
                newCustomerLocation.getBemisCustomerId());
        Assert.assertEquals(existingCustomerLocation.getBemisParentId(), newCustomerLocation.getBemisParentId());
        Assert.assertEquals(existingCustomerLocation.getCustomerName(), newCustomerLocation.getCustomerName());
        Assert.assertEquals(existingCustomerLocation.getCustomerAddress1(),
                newCustomerLocation.getCustomerAddress1());
        Assert.assertEquals(existingCustomerLocation.getCustomerAddress2(),
                newCustomerLocation.getCustomerAddress2());
        Assert.assertEquals(existingCustomerLocation.getCustomerAddress3(),
                newCustomerLocation.getCustomerAddress3());
        Assert.assertEquals(existingCustomerLocation.getCustomerCity(), newCustomerLocation.getCustomerCity());
        Assert.assertEquals(existingCustomerLocation.getCustomerState(), newCustomerLocation.getCustomerState());
        Assert.assertEquals(existingCustomerLocation.getCustomerPostalCde(),
                newCustomerLocation.getCustomerPostalCde());
        Assert.assertEquals(existingCustomerLocation.getCustomerCountry(),
                newCustomerLocation.getCustomerCountry());
        Assert.assertEquals(existingCustomerLocation.getCustomerPhoneNbr(),
                newCustomerLocation.getCustomerPhoneNbr());
        Assert.assertEquals(existingCustomerLocation.getCustomerPoBox(), newCustomerLocation.getCustomerPoBox());
        Assert.assertEquals(existingCustomerLocation.getCustomerPoCity(), newCustomerLocation.getCustomerPoCity());
        Assert.assertEquals(existingCustomerLocation.getCustomerPoState(),
                newCustomerLocation.getCustomerPoState());
        Assert.assertEquals(existingCustomerLocation.getCustomerPoPostalCde(),
                newCustomerLocation.getCustomerPoPostalCde());
        Assert.assertEquals(existingCustomerLocation.getCustomerActiveStatusCde(),
                newCustomerLocation.getCustomerActiveStatusCde());
    }

    @Test
    public void testCountByBemisCustomerId() throws Exception {
        _persistence.countByBemisCustomerId(StringPool.BLANK);

        _persistence.countByBemisCustomerId(StringPool.NULL);

        _persistence.countByBemisCustomerId((String) null);
    }

    @Test
    public void testCountByBemisParentId() throws Exception {
        _persistence.countByBemisParentId(StringPool.BLANK);

        _persistence.countByBemisParentId(StringPool.NULL);

        _persistence.countByBemisParentId((String) null);
    }

    @Test
    public void testCountByCustomerActiveStatusCde() throws Exception {
        _persistence.countByCustomerActiveStatusCde(StringPool.BLANK);

        _persistence.countByCustomerActiveStatusCde(StringPool.NULL);

        _persistence.countByCustomerActiveStatusCde((String) null);
    }

    @Test
    public void testCountByCustomerName_CustomerCity() throws Exception {
        _persistence.countByCustomerName_CustomerCity(StringPool.BLANK, StringPool.BLANK);

        _persistence.countByCustomerName_CustomerCity(StringPool.NULL, StringPool.NULL);

        _persistence.countByCustomerName_CustomerCity((String) null, (String) null);
    }

    @Test
    public void testFindByPrimaryKeyExisting() throws Exception {
        CustomerLocation newCustomerLocation = addCustomerLocation();

        CustomerLocation existingCustomerLocation = _persistence
                .findByPrimaryKey(newCustomerLocation.getPrimaryKey());

        Assert.assertEquals(existingCustomerLocation, newCustomerLocation);
    }

    @Test(expected = NoSuchCustomerLocationException.class)
    public void testFindByPrimaryKeyMissing() throws Exception {
        long pk = RandomTestUtil.nextLong();

        _persistence.findByPrimaryKey(pk);
    }

    @Test
    public void testFindAll() throws Exception {
        _persistence.findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
    }

    protected OrderByComparator<CustomerLocation> getOrderByComparator() {
        return OrderByComparatorFactoryUtil.create("Bemis_CustomerLocation", "customerLocationId", true,
                "companyId", true, "userId", true, "userName", true, "createDate", true, "modifiedDate", true,
                "bemisCustomerId", true, "bemisParentId", true, "customerName", true, "customerAddress1", true,
                "customerAddress2", true, "customerAddress3", true, "customerCity", true, "customerState", true,
                "customerPostalCde", true, "customerCountry", true, "customerPhoneNbr", true, "customerPoBox", true,
                "customerPoCity", true, "customerPoState", true, "customerPoPostalCde", true,
                "customerActiveStatusCde", true);
    }

    @Test
    public void testFetchByPrimaryKeyExisting() throws Exception {
        CustomerLocation newCustomerLocation = addCustomerLocation();

        CustomerLocation existingCustomerLocation = _persistence
                .fetchByPrimaryKey(newCustomerLocation.getPrimaryKey());

        Assert.assertEquals(existingCustomerLocation, newCustomerLocation);
    }

    @Test
    public void testFetchByPrimaryKeyMissing() throws Exception {
        long pk = RandomTestUtil.nextLong();

        CustomerLocation missingCustomerLocation = _persistence.fetchByPrimaryKey(pk);

        Assert.assertNull(missingCustomerLocation);
    }

    @Test
    public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist() throws Exception {
        CustomerLocation newCustomerLocation1 = addCustomerLocation();
        CustomerLocation newCustomerLocation2 = addCustomerLocation();

        Set<Serializable> primaryKeys = new HashSet<Serializable>();

        primaryKeys.add(newCustomerLocation1.getPrimaryKey());
        primaryKeys.add(newCustomerLocation2.getPrimaryKey());

        Map<Serializable, CustomerLocation> customerLocations = _persistence.fetchByPrimaryKeys(primaryKeys);

        Assert.assertEquals(2, customerLocations.size());
        Assert.assertEquals(newCustomerLocation1, customerLocations.get(newCustomerLocation1.getPrimaryKey()));
        Assert.assertEquals(newCustomerLocation2, customerLocations.get(newCustomerLocation2.getPrimaryKey()));
    }

    @Test
    public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist() throws Exception {
        long pk1 = RandomTestUtil.nextLong();

        long pk2 = RandomTestUtil.nextLong();

        Set<Serializable> primaryKeys = new HashSet<Serializable>();

        primaryKeys.add(pk1);
        primaryKeys.add(pk2);

        Map<Serializable, CustomerLocation> customerLocations = _persistence.fetchByPrimaryKeys(primaryKeys);

        Assert.assertTrue(customerLocations.isEmpty());
    }

    @Test
    public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist() throws Exception {
        CustomerLocation newCustomerLocation = addCustomerLocation();

        long pk = RandomTestUtil.nextLong();

        Set<Serializable> primaryKeys = new HashSet<Serializable>();

        primaryKeys.add(newCustomerLocation.getPrimaryKey());
        primaryKeys.add(pk);

        Map<Serializable, CustomerLocation> customerLocations = _persistence.fetchByPrimaryKeys(primaryKeys);

        Assert.assertEquals(1, customerLocations.size());
        Assert.assertEquals(newCustomerLocation, customerLocations.get(newCustomerLocation.getPrimaryKey()));
    }

    @Test
    public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
        Set<Serializable> primaryKeys = new HashSet<Serializable>();

        Map<Serializable, CustomerLocation> customerLocations = _persistence.fetchByPrimaryKeys(primaryKeys);

        Assert.assertTrue(customerLocations.isEmpty());
    }

    @Test
    public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
        CustomerLocation newCustomerLocation = addCustomerLocation();

        Set<Serializable> primaryKeys = new HashSet<Serializable>();

        primaryKeys.add(newCustomerLocation.getPrimaryKey());

        Map<Serializable, CustomerLocation> customerLocations = _persistence.fetchByPrimaryKeys(primaryKeys);

        Assert.assertEquals(1, customerLocations.size());
        Assert.assertEquals(newCustomerLocation, customerLocations.get(newCustomerLocation.getPrimaryKey()));
    }

    @Test
    public void testActionableDynamicQuery() throws Exception {
        final IntegerWrapper count = new IntegerWrapper();

        ActionableDynamicQuery actionableDynamicQuery = CustomerLocationLocalServiceUtil
                .getActionableDynamicQuery();

        actionableDynamicQuery
                .setPerformActionMethod(new ActionableDynamicQuery.PerformActionMethod<CustomerLocation>() {
                    @Override
                    public void performAction(CustomerLocation customerLocation) {
                        Assert.assertNotNull(customerLocation);

                        count.increment();
                    }
                });

        actionableDynamicQuery.performActions();

        Assert.assertEquals(count.getValue(), _persistence.countAll());
    }

    @Test
    public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
        CustomerLocation newCustomerLocation = addCustomerLocation();

        DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(CustomerLocation.class,
                _dynamicQueryClassLoader);

        dynamicQuery
                .add(RestrictionsFactoryUtil.eq("customerLocationId", newCustomerLocation.getCustomerLocationId()));

        List<CustomerLocation> result = _persistence.findWithDynamicQuery(dynamicQuery);

        Assert.assertEquals(1, result.size());

        CustomerLocation existingCustomerLocation = result.get(0);

        Assert.assertEquals(existingCustomerLocation, newCustomerLocation);
    }

    @Test
    public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
        DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(CustomerLocation.class,
                _dynamicQueryClassLoader);

        dynamicQuery.add(RestrictionsFactoryUtil.eq("customerLocationId", RandomTestUtil.nextLong()));

        List<CustomerLocation> result = _persistence.findWithDynamicQuery(dynamicQuery);

        Assert.assertEquals(0, result.size());
    }

    @Test
    public void testDynamicQueryByProjectionExisting() throws Exception {
        CustomerLocation newCustomerLocation = addCustomerLocation();

        DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(CustomerLocation.class,
                _dynamicQueryClassLoader);

        dynamicQuery.setProjection(ProjectionFactoryUtil.property("customerLocationId"));

        Object newCustomerLocationId = newCustomerLocation.getCustomerLocationId();

        dynamicQuery.add(RestrictionsFactoryUtil.in("customerLocationId", new Object[] { newCustomerLocationId }));

        List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

        Assert.assertEquals(1, result.size());

        Object existingCustomerLocationId = result.get(0);

        Assert.assertEquals(existingCustomerLocationId, newCustomerLocationId);
    }

    @Test
    public void testDynamicQueryByProjectionMissing() throws Exception {
        DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(CustomerLocation.class,
                _dynamicQueryClassLoader);

        dynamicQuery.setProjection(ProjectionFactoryUtil.property("customerLocationId"));

        dynamicQuery
                .add(RestrictionsFactoryUtil.in("customerLocationId", new Object[] { RandomTestUtil.nextLong() }));

        List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

        Assert.assertEquals(0, result.size());
    }

    @Test
    public void testResetOriginalValues() throws Exception {
        CustomerLocation newCustomerLocation = addCustomerLocation();

        _persistence.clearCache();

        CustomerLocation existingCustomerLocation = _persistence
                .findByPrimaryKey(newCustomerLocation.getPrimaryKey());

        Assert.assertTrue(Objects.equals(existingCustomerLocation.getBemisCustomerId(), ReflectionTestUtil
                .invoke(existingCustomerLocation, "getOriginalBemisCustomerId", new Class<?>[0])));
    }

    protected CustomerLocation addCustomerLocation() throws Exception {
        long pk = RandomTestUtil.nextLong();

        CustomerLocation customerLocation = _persistence.create(pk);

        customerLocation.setCompanyId(RandomTestUtil.nextLong());

        customerLocation.setUserId(RandomTestUtil.nextLong());

        customerLocation.setUserName(RandomTestUtil.randomString());

        customerLocation.setCreateDate(RandomTestUtil.nextDate());

        customerLocation.setModifiedDate(RandomTestUtil.nextDate());

        customerLocation.setBemisCustomerId(RandomTestUtil.randomString());

        customerLocation.setBemisParentId(RandomTestUtil.randomString());

        customerLocation.setCustomerName(RandomTestUtil.randomString());

        customerLocation.setCustomerAddress1(RandomTestUtil.randomString());

        customerLocation.setCustomerAddress2(RandomTestUtil.randomString());

        customerLocation.setCustomerAddress3(RandomTestUtil.randomString());

        customerLocation.setCustomerCity(RandomTestUtil.randomString());

        customerLocation.setCustomerState(RandomTestUtil.randomString());

        customerLocation.setCustomerPostalCde(RandomTestUtil.randomString());

        customerLocation.setCustomerCountry(RandomTestUtil.randomString());

        customerLocation.setCustomerPhoneNbr(RandomTestUtil.randomString());

        customerLocation.setCustomerPoBox(RandomTestUtil.randomString());

        customerLocation.setCustomerPoCity(RandomTestUtil.randomString());

        customerLocation.setCustomerPoState(RandomTestUtil.randomString());

        customerLocation.setCustomerPoPostalCde(RandomTestUtil.randomString());

        customerLocation.setCustomerActiveStatusCde(RandomTestUtil.randomString());

        _customerLocations.add(_persistence.update(customerLocation));

        return customerLocation;
    }

    private List<CustomerLocation> _customerLocations = new ArrayList<CustomerLocation>();
    private CustomerLocationPersistence _persistence;
    private ClassLoader _dynamicQueryClassLoader;
}