Hybris (5) Testing the Facades

Submitted by Lizhe on Fri, 02/01/2019 - 11:12

 

Facade层提供业务逻辑方法, 位于 service 层之上, 隐藏service层实现的细节

它会调用service层的业务逻辑方法, 并且负责包装service层的数据到 DTO 对象

所以这里我们首先需要一个 DTO

 

/lizhebuglist/resources/lizhebuglist-beans.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="beans.xsd">

<bean class="name.lizhe.data.LizhebugBean">

<property name="bugnumber" type="String"/>

<property name="bugname" type="String"/>

<property name="bugdescription" type="String"/>

<property name="bugcomments" type="String"/>

</bean>

</beans>

 

对应的 java bean

/lizhebuglist/src/name/lizhe/data/LizhebugBean.java

/*
 * [y] hybris Platform
 * 
 * Copyright (c) 2000-2019 SAP SE
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of SAP 
 * Hybris ("Confidential Information"). You shall not disclose such 
 * Confidential Information and shall use it only in accordance with the 
 * terms of the license agreement you entered into with SAP Hybris.
 */
package name.lizhe.data;

/**
 *
 */
public class LizhebugBean
{
    private String bugnumber;
    private String bugname;
    private String bugdescription;
    private String bugcomments;
    /**
     * @return the bugnumber
     */
    public String getBugnumber()
    {
        return bugnumber;
    }
    /**
     * @param bugnumber the bugnumber to set
     */
    public void setBugnumber(String bugnumber)
    {
        this.bugnumber = bugnumber;
    }
    /**
     * @return the bugname
     */
    public String getBugname()
    {
        return bugname;
    }
    /**
     * @param bugname the bugname to set
     */
    public void setBugname(String bugname)
    {
        this.bugname = bugname;
    }
    /**
     * @return the bugdescription
     */
    public String getBugdescription()
    {
        return bugdescription;
    }
    /**
     * @param bugdescription the bugdescription to set
     */
    public void setBugdescription(String bugdescription)
    {
        this.bugdescription = bugdescription;
    }
    /**
     * @return the bugcomments
     */
    public String getBugcomments()
    {
        return bugcomments;
    }
    /**
     * @param bugcomments the bugcomments to set
     */
    public void setBugcomments(String bugcomments)
    {
        this.bugcomments = bugcomments;
    }
    
    
}
 

 

创建我们要用的facade类

/lizhebuglist/src/name/lizhe/facade/impl/LizhebuglistFacadeImpl.java

/*
 * [y] hybris Platform
 * 
 * Copyright (c) 2000-2019 SAP SE
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of SAP 
 * Hybris ("Confidential Information"). You shall not disclose such 
 * Confidential Information and shall use it only in accordance with the 
 * terms of the license agreement you entered into with SAP Hybris.
 */
package name.lizhe.facade.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import name.lizhe.data.LizhebugBean;
import name.lizhe.facade.LizhebuglistFacade;
import name.lizhe.model.LizheBugItemModel;
import name.lizhe.service.LizhebuglistService;

/**
 *
 */
public class LizhebuglistFacadeImpl implements LizhebuglistFacade
{
    @Autowired
    private LizhebuglistService lizhebuglistService;

    @Override
    public List<LizhebugBean> getBugList()
    {
        List<LizheBugItemModel> list = lizhebuglistService.getBugList();
        List<LizhebugBean> results = new ArrayList<>();
        
        for(LizheBugItemModel model:list) {
            LizhebugBean bean = this.populateBean(model);
            results.add(bean);
        }
        
        return results;
    }
    
    private LizhebugBean populateBean(LizheBugItemModel model) {
        LizhebugBean bean = new LizhebugBean();
        bean.setBugnumber(model.getBugnumber());
        bean.setBugname(model.getBugname());
        bean.setBugdescription(model.getBugdescription());
        bean.setBugcomments(model.getBugcomments());
        return bean;
    }

    @Override
    public void createBug(LizhebugBean lizhebugBean)
    {
        String bugnumber = lizhebugBean.getBugnumber();
        String bugname = lizhebugBean.getBugname();
        String bugdescription = lizhebugBean.getBugdescription();
        String bugcomments = lizhebugBean.getBugcomments();
        lizhebuglistService.createBug(bugnumber, bugname, bugdescription, bugcomments);
    }

    @Override
    public void deleteBug(String bugnumber)
    {
        lizhebuglistService.deleteBug(bugnumber);
    }

}
 

实际上facade的测试类和dao的测试类基本一样

/*
 * [y] hybris Platform
 * 
 * Copyright (c) 2000-2019 SAP SE
 * All rights reserved.
 * 
 * This software is the confidential and proprietary information of SAP 
 * Hybris ("Confidential Information"). You shall not disclose such 
 * Confidential Information and shall use it only in accordance with the 
 * terms of the license agreement you entered into with SAP Hybris.
 */
package name.lizhe.facade.impl;

import org.junit.Before;
import org.junit.Test;

import name.lizhe.facade.LizhebuglistFacade;
import name.lizhe.service.LizhebuglistService;

import static org.junit.Assert.assertEquals;

import de.hybris.platform.servicelayer.ServicelayerTransactionalTest;

import javax.annotation.Resource;

/**
 *
 */
public class LizhebuglistFacadeTest extends ServicelayerTransactionalTest
{
    
    @Resource
    private LizhebuglistFacade lizhebuglistFacade;
    
    @Test
    public void lizhebuglistFacadeTest() {
        assertEquals(0,lizhebuglistFacade.getBugList().size());
    }
}
 

spring配置文件动一下

<?xml version="1.0" encoding="UTF-8"?>

<!--

 [y] hybris Platform

 

 Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved.

 

 This software is the confidential and proprietary information of SAP

 ("Confidential Information"). You shall not disclose such Confidential

 Information and shall use it only in accordance with the terms of the

 license agreement you entered into with SAP.

-->

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

           http://www.springframework.org/schema/beans/spring-beans.xsd

           http://www.springframework.org/schema/aop

           http://www.springframework.org/schema/aop/spring-aop.xsd">

 

 

 

    <bean id="lizhebuglistSystemSetup" class="name.lizhe.setup.LizhebuglistSystemSetup">

        <!-- constructor arg injection example -->

        <constructor-arg ref="lizhebuglistService"/>

    </bean>

    

    <bean id="lizhebuglistFacade" class="name.lizhe.facade.impl.LizhebuglistFacadeImpl">

        <!-- setter injection example -->

        <property name="lizhebuglistService" ref="lizhebuglistService"/>

    </bean>

    

    <bean id="lizhebuglistService" class="name.lizhe.service.impl.DefaultLizhebuglistService">

        <!-- setter injection example -->

        <property name="lizhebuglistDao" ref="lizhebuglistDao"/>

        <property name="modelService" ref="modelService"/>

        <property name="mediaService" ref="mediaService"/>

        <property name="flexibleSearchService" ref="flexibleSearchService"/>

    </bean>

    

    <bean id="lizhebuglistDao" class="name.lizhe.dao.impl.LizhebuglistDaoImpl">

        <!-- setter injection example -->

        <property name="modelService" ref="modelService"/>

        <property name="mediaService" ref="mediaService"/>

        <property name="flexibleSearchService" ref="flexibleSearchService"/>

    </bean>

 

</beans>

 

20190201022457