如何編寫Junit測試程式碼
阿新 • • 發佈:2019-02-13
DataSynchReceiveServiceImplTest.javapackage com.snt.aaa.config.service.impl; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.snt.aaa.common.core.service.BaseService; import com.snt.aaa.common.entity.BaseEntity; import com.snt.aaa.common.utils.StringUtil; import com.snt.aaa.config.intf.entity.AccountDuration; import com.snt.aaa.config.intf.entity.AccountInfo; import com.snt.aaa.config.intf.entity.AccountTraffic; import com.snt.aaa.config.intf.entity.CustomerInfo; import com.snt.aaa.config.intf.entity.SubscriberInfo; import com.snt.aaa.config.intf.entity.SubscriberSubscription; import com.snt.aaa.config.intf.entity.SubscriptionInfo; import com.snt.aaa.config.mapper.SubscriberInfoMapper; import com.snt.aaa.config.service.AccountDurationService; import com.snt.aaa.config.service.AccountInfoService; import com.snt.aaa.config.service.AccountTrafficService; import com.snt.aaa.config.service.CustomerInfoService; import com.snt.aaa.config.service.DataSynchReceiveService; import com.snt.aaa.config.service.ProductFeeRuleService; import com.snt.aaa.config.service.ProductInfoService; import com.snt.aaa.config.service.ProductParameterService; import com.snt.aaa.config.service.ProductServiceService; import com.snt.aaa.config.service.ServiceInfoService; import com.snt.aaa.config.service.SubscriberInfoService; import com.snt.aaa.config.service.SubscriberSubscriptionService; import com.snt.aaa.config.service.SubscriptionInfoService; @Service("dateSynchReceiveService") public class DataSynchReceiveServiceImpl implements DataSynchReceiveService{ public static final Logger log = LoggerFactory.getLogger(DataSynchReceiveServiceImpl.class); @Autowired private CustomerInfoService customerInfoService; @Autowired private SubscriberInfoService subscriberInfoService; @Autowired private SubscriberSubscriptionService sss; @Autowired private AccountTrafficService accountTrafficService; @Autowired private AccountDurationService accountDurationService; @Autowired private SubscriptionInfoService subscriptionInfoService; @Autowired private ProductInfoService productInfoService; @Autowired private ProductFeeRuleService productFeeRuleService; @Autowired private ProductParameterService productParameterService; @Autowired private ProductServiceService productServiceService; @Autowired private ServiceInfoService serviceInfoService; @Autowired private AccountInfoService accountInfoService; @Autowired private SubscriberInfoMapper sim; public void saveReceiveData(JSONObject resultJson){ try { JSONObject message = resultJson.getJSONObject("message"); //獲取客戶資訊 JSONObject cObj = message.getJSONObject("customer"); if(cObj!=null){ CustomerInfo cinfo = JSON.toJavaObject(cObj, CustomerInfo.class); customerInfoService.insert(cinfo); } //獲取使用者資訊 JSONArray subscribers = message.getJSONArray("subscriber"); saveEntity(subscribers, SubscriberInfo.class, subscriberInfoService); //獲取訂購關係 JSONArray subscriptions = message.getJSONArray("subscriptions"); saveEntity(subscriptions, SubscriptionInfo.class, subscriptionInfoService); //獲取流量賬戶 JSONArray subTraffics = message.getJSONArray("subTraffics"); saveEntity(subTraffics, AccountTraffic.class, accountTrafficService); //新建時長賬戶json 陣列 JSONArray subDuration = message.getJSONArray("subDuration"); saveEntity(subDuration, AccountDuration.class, accountDurationService); //獲取訂購關係中間表記錄 JSONArray subSubscriptions =message.getJSONArray("subSubscription"); saveEntity(subSubscriptions, SubscriberSubscription.class, sss); //獲取使用者賬戶資訊 JSONArray accounts =message.getJSONArray("accounts") ; saveEntity(accounts, AccountInfo.class, accountInfoService); //二、解析message中的資料 //1.獲取所有t_product_info表中的資料 JSONArray products = message.getJSONArray("products"); //2.獲取所有t_product_service表中的資料 JSONArray productService = message.getJSONArray("productService"); //3.獲取所有t_service_info表中的所有資料 JSONArray sevices = message.getJSONArray("sevices"); //4.獲取所有t_product_parameter表中的資料 JSONArray productParas = message.getJSONArray("productParas"); //5.獲取所有t_product_fee_rule表中的資料 JSONArray productsFee = message.getJSONArray("productsFee"); //三、執行儲存動作 saveProducts(products); saveProductService(productService); saveSevices(sevices); saveProductParas(productParas); saveProductsFee(productsFee); } catch (Exception e) { log.error("crm 資料 儲存資料到3a 中出錯",e); } } public void deleteCustomerRelate(String customerId){ if(StringUtil.isNotNull(customerId)){ List<String> ids = subscriptionInfoService.getIdsByCustomerId(customerId); for(String id:ids){ //1.刪除訂購關係 sss.deleteBySubscriptionId(id); //2.刪除流量賬戶 accountTrafficService.deleteBySubscriptionId(id); //3.刪除時長賬戶資訊 accountDurationService.deleteBySubscriptionId(id); //4.刪除訂購關係 subscriptionInfoService.delete(id); } //5.刪除客戶賬戶資訊 accountInfoService.deleteByCustomerId(customerId); //6.刪除客戶下的使用者資訊 subscriberInfoService.deleteByCustomerId(customerId); //7.刪除客戶資訊 customerInfoService.delete(customerId); } } public void saveProducts(JSONArray products){ productInfoService.saveProducts(products); } public void saveProductService(JSONArray productService){ productServiceService.saveProductService(productService); } public void saveSevices(JSONArray sevices){ serviceInfoService.saveSevices(sevices); } public void saveProductParas(JSONArray productParas){ productParameterService.saveProductParas(productParas); } public void saveProductsFee(JSONArray productsFee){ productFeeRuleService.saveProductsFee(productsFee); } @SuppressWarnings({ "unchecked", "rawtypes" }) public void saveEntity(JSONArray jArray,Class<? extends BaseEntity> clz, BaseService bservice){ if(jArray!=null && jArray.size()>0){ for(int i=0;i<jArray.size();i++){ JSONObject iObj = jArray.getJSONObject(i); bservice.insert(JSON.toJavaObject(iObj, clz)); } } } }