/*
 * Copyright 2009 Inspire-Software.com
 *
 *    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.yes.cart.dao.impl;

import org.junit.Before;
import org.junit.Test;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.yes.cart.dao.GenericDAO;
import org.yes.cart.dao.constants.DaoServiceBeanKeys;
import org.yes.cart.domain.entity.AttrValueBrand;
import org.yes.cart.domain.entity.Attribute;
import org.yes.cart.domain.entity.Brand;
import org.yes.cart.domain.entity.impl.AttrValueEntityBrand;
import org.yes.cart.domain.entity.impl.BrandEntity;

import static org.junit.Assert.*;

/**
 * User: Igor Azarny iazarny@yahoo.com
 * Date: 07-May-2011
 * Time: 16:13:01
 */
public class BrandDAOTest extends AbstractTestDAO {

    private GenericDAO<Attribute, Long> attributeDAO;
    private GenericDAO<Brand, Long> brandDAO;


    @Override
    @Before
    public void setUp() {
        attributeDAO = (GenericDAO<Attribute, Long>) ctx().getBean(DaoServiceBeanKeys.ATTRIBUTE_DAO);
        brandDAO = (GenericDAO<Brand, Long>) ctx().getBean(DaoServiceBeanKeys.BRAND_DAO);
        super.setUp();

    }

    @Test
    public void testAddEmptyBrand() {
        getTx().execute(new TransactionCallbackWithoutResult() {
            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) {


                // create simple brand without attributes
                Brand entity = new BrandEntity();
                entity.setName("brandName without attributes");
                long pk = brandDAO.create(entity).getBrandId();
                assertTrue(entity.getBrandId() != 0);
                assertTrue(entity.getAttributes().isEmpty());
                Brand brand = brandDAO.findById(pk);
                assertNotNull(brand);

                status.setRollbackOnly();

            }
        });

    }

    /*
    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {



            status.setRollbackOnly();

        }
    });
    */

    @Test
    public void testAddBrandWithAttributeValues() {

        getTx().execute(new TransactionCallbackWithoutResult() {
            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) {

                Brand entityWithAttributes = new BrandEntity();
                entityWithAttributes.setName("brandName with attributes");
                AttrValueBrand attrValueBrandEntity = new AttrValueEntityBrand();
                attrValueBrandEntity.setBrand(entityWithAttributes);
                attrValueBrandEntity.setAttributeCode("BRAND_IMAGE0");
                attrValueBrandEntity.setVal("brand.jpg");
                //BRAND_IMAGE
                entityWithAttributes.getAttributes().add(attrValueBrandEntity);
                long pk = brandDAO.create(entityWithAttributes).getBrandId();
                Brand brand = brandDAO.findById(pk);
                assertNotNull(brand);
                assertEquals(1, brand.getAttributes().size());

                status.setRollbackOnly();

            }
        });


    }
}
