From 58a3e21a8d2e2835c8f3a8c90e5879b1b3d04650 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 24 Apr 2024 21:40:55 +0800 Subject: [PATCH] adjust to apply jdk17 and springboot3 --- cola-samples/charge/pom.xml | 25 +- .../charging/adapter/ChargeController.java | 2 +- .../application/ChargeServiceImpl.java | 180 +++++++------- .../charging/domain/account/Account.java | 177 +++++++------- .../domain/account/AccountDomainService.java | 42 ++-- .../charging/domain/charge/ChargeRecord.java | 136 +++++------ .../domain/charge/MoneyConverter.java | 35 +-- .../com/huawei/charging/CleanArchTest.java | 4 +- .../{ => application}/ChargeServiceTest.java | 221 +++++++++--------- .../{ut => domain}/ChargeRecordPlanTest.java | 67 +++--- .../{ut => domain}/ChargeRecordRuleTest.java | 184 ++++++++------- .../CompositeChargeRuleTestRecord.java | 162 ++++++------- .../ChargeRecordRepoTest.java | 98 ++++---- .../{ => infrastructure}/PropertyTest.java | 75 +++--- 14 files changed, 698 insertions(+), 710 deletions(-) rename cola-samples/charge/src/test/java/com/huawei/charging/{ => application}/ChargeServiceTest.java (77%) rename cola-samples/charge/src/test/java/com/huawei/charging/{ut => domain}/ChargeRecordPlanTest.java (82%) rename cola-samples/charge/src/test/java/com/huawei/charging/{ut => domain}/ChargeRecordRuleTest.java (80%) rename cola-samples/charge/src/test/java/com/huawei/charging/{ut => domain}/CompositeChargeRuleTestRecord.java (86%) rename cola-samples/charge/src/test/java/com/huawei/charging/{ => infrastructure}/ChargeRecordRepoTest.java (77%) rename cola-samples/charge/src/test/java/com/huawei/charging/{ => infrastructure}/PropertyTest.java (82%) diff --git a/cola-samples/charge/pom.xml b/cola-samples/charge/pom.xml index 5b9459e9c..a783a6f50 100644 --- a/cola-samples/charge/pom.xml +++ b/cola-samples/charge/pom.xml @@ -9,13 +9,14 @@ 1.0.0-SNAPSHOT - 1.8 - 1.8 + 17 + 17 + 17 UTF-8 3.0.0 - 1.0.1 - 2.3.8.RELEASE + 1.3.0 + 3.1.0 @@ -35,20 +36,9 @@ org.springframework.boot spring-boot-starter-web - - junit - junit - 4.13.2 - test - org.springframework.boot - spring-boot-test - test - - - org.springframework - spring-test + spring-boot-starter-test test @@ -58,7 +48,7 @@ com.tngtech.archunit - archunit + archunit-junit5 ${archunit.version} test @@ -69,6 +59,7 @@ mysql mysql-connector-java + 8.0.33 diff --git a/cola-samples/charge/src/main/java/com/huawei/charging/adapter/ChargeController.java b/cola-samples/charge/src/main/java/com/huawei/charging/adapter/ChargeController.java index 6ae715fcb..3d15d826c 100644 --- a/cola-samples/charge/src/main/java/com/huawei/charging/adapter/ChargeController.java +++ b/cola-samples/charge/src/main/java/com/huawei/charging/adapter/ChargeController.java @@ -2,10 +2,10 @@ import com.huawei.charging.application.ChargeServiceI; import com.huawei.charging.application.dto.*; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; @RestController @Slf4j diff --git a/cola-samples/charge/src/main/java/com/huawei/charging/application/ChargeServiceImpl.java b/cola-samples/charge/src/main/java/com/huawei/charging/application/ChargeServiceImpl.java index 9878241d3..58b282561 100644 --- a/cola-samples/charge/src/main/java/com/huawei/charging/application/ChargeServiceImpl.java +++ b/cola-samples/charge/src/main/java/com/huawei/charging/application/ChargeServiceImpl.java @@ -1,90 +1,90 @@ -package com.huawei.charging.application; - -import com.huawei.charging.application.dto.*; -import com.huawei.charging.domain.account.Account; -import com.huawei.charging.domain.account.AccountDomainService; -import com.huawei.charging.domain.charge.CallType; -import com.huawei.charging.domain.charge.ChargeContext; -import com.huawei.charging.domain.charge.ChargeRecord; -import com.huawei.charging.domain.charge.Session; -import com.huawei.charging.domain.gateway.AccountGateway; -import com.huawei.charging.domain.gateway.ChargeGateway; -import com.huawei.charging.domain.gateway.SessionGateway; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; - -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.List; - -@Service -@Slf4j -public class ChargeServiceImpl implements ChargeServiceI { - - @Resource - private SessionGateway sessionGateway; - - @Resource - private AccountGateway accountGateway; - - @Resource - private AccountDomainService accountDomainService; - - @Resource - private ChargeGateway chargeGateway; - - @Override - public Response begin(BeginSessionRequest request) { - Session session = request.toSession(); - accountDomainService.canSessionStart(session); - sessionGateway.create(session); - log.debug("Session created successfully :" + session); - return Response.buildSuccess(); - } - - @Override - public Response charge(ChargeRequest request) { - log.debug("Do charge : " + request); - Session session = sessionGateway.get(request.getSessionId()); - int durationToCharge = request.getDuration() - session.getChargedDuration(); - List chargeRecordList = new ArrayList<>(); - chargeCalling(session, durationToCharge, chargeRecordList); - chargeCalled(session, durationToCharge, chargeRecordList); - chargeGateway.saveAll(chargeRecordList); - session.setChargedDuration(request.getDuration()); - return Response.buildSuccess(); - } - - private void chargeCalling(Session session, int durationToCharge, List chargeRecordList) { - Account callingAccount = accountGateway.getAccount(session.getCallingPhoneNo()); - ChargeContext callingCtx = new ChargeContext(CallType.CALLING, session.getCallingPhoneNo(), session.getCalledPhoneNo(), durationToCharge); - callingCtx.session = session; - callingCtx.account = callingAccount; - chargeRecordList.addAll(callingAccount.charge(callingCtx)); - } - - private void chargeCalled(Session session, int durationToCharge, List chargeRecordList) { - Account calledAccount = accountGateway.getAccount(session.getCalledPhoneNo()); - ChargeContext calledCtx = new ChargeContext(CallType.CALLED, session.getCalledPhoneNo(), session.getCallingPhoneNo(), durationToCharge); - calledCtx.session = session; - calledCtx.account = calledAccount; - chargeRecordList.addAll(calledAccount.charge(calledCtx)); - } - - @Override - public Response end(EndSessionRequest request) { - charge(request.toChargeRequest()); - sessionGateway.end(request.getSessionId()); - return Response.buildSuccess(); - } - - @Override - public MultiResponse listChargeRecords(String sessionId) { - List chargeRecordList = chargeGateway.findBySessionId(sessionId); - List chargeRecordDtoList = new ArrayList<>(); - for (ChargeRecord chargeRecord : chargeRecordList) { - chargeRecordDtoList.add(ChargeRecordDto.fromEntity(chargeRecord)); - } - return MultiResponse.of(chargeRecordDtoList); - } -} +package com.huawei.charging.application; + +import com.huawei.charging.application.dto.*; +import com.huawei.charging.domain.account.Account; +import com.huawei.charging.domain.account.AccountDomainService; +import com.huawei.charging.domain.charge.CallType; +import com.huawei.charging.domain.charge.ChargeContext; +import com.huawei.charging.domain.charge.ChargeRecord; +import com.huawei.charging.domain.charge.Session; +import com.huawei.charging.domain.gateway.AccountGateway; +import com.huawei.charging.domain.gateway.ChargeGateway; +import com.huawei.charging.domain.gateway.SessionGateway; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +@Slf4j +public class ChargeServiceImpl implements ChargeServiceI { + + @Resource + private SessionGateway sessionGateway; + + @Resource + private AccountGateway accountGateway; + + @Resource + private AccountDomainService accountDomainService; + + @Resource + private ChargeGateway chargeGateway; + + @Override + public Response begin(BeginSessionRequest request) { + Session session = request.toSession(); + accountDomainService.canSessionStart(session); + sessionGateway.create(session); + log.debug("Session created successfully :" + session); + return Response.buildSuccess(); + } + + @Override + public Response charge(ChargeRequest request) { + log.debug("Do charge : " + request); + Session session = sessionGateway.get(request.getSessionId()); + int durationToCharge = request.getDuration() - session.getChargedDuration(); + List chargeRecordList = new ArrayList<>(); + chargeCalling(session, durationToCharge, chargeRecordList); + chargeCalled(session, durationToCharge, chargeRecordList); + chargeGateway.saveAll(chargeRecordList); + session.setChargedDuration(request.getDuration()); + return Response.buildSuccess(); + } + + private void chargeCalling(Session session, int durationToCharge, List chargeRecordList) { + Account callingAccount = accountGateway.getAccount(session.getCallingPhoneNo()); + ChargeContext callingCtx = new ChargeContext(CallType.CALLING, session.getCallingPhoneNo(), session.getCalledPhoneNo(), durationToCharge); + callingCtx.session = session; + callingCtx.account = callingAccount; + chargeRecordList.addAll(callingAccount.charge(callingCtx)); + } + + private void chargeCalled(Session session, int durationToCharge, List chargeRecordList) { + Account calledAccount = accountGateway.getAccount(session.getCalledPhoneNo()); + ChargeContext calledCtx = new ChargeContext(CallType.CALLED, session.getCalledPhoneNo(), session.getCallingPhoneNo(), durationToCharge); + calledCtx.session = session; + calledCtx.account = calledAccount; + chargeRecordList.addAll(calledAccount.charge(calledCtx)); + } + + @Override + public Response end(EndSessionRequest request) { + charge(request.toChargeRequest()); + sessionGateway.end(request.getSessionId()); + return Response.buildSuccess(); + } + + @Override + public MultiResponse listChargeRecords(String sessionId) { + List chargeRecordList = chargeGateway.findBySessionId(sessionId); + List chargeRecordDtoList = new ArrayList<>(); + for (ChargeRecord chargeRecord : chargeRecordList) { + chargeRecordDtoList.add(ChargeRecordDto.fromEntity(chargeRecord)); + } + return MultiResponse.of(chargeRecordDtoList); + } +} diff --git a/cola-samples/charge/src/main/java/com/huawei/charging/domain/account/Account.java b/cola-samples/charge/src/main/java/com/huawei/charging/domain/account/Account.java index a62856090..81dc8b2f7 100644 --- a/cola-samples/charge/src/main/java/com/huawei/charging/domain/account/Account.java +++ b/cola-samples/charge/src/main/java/com/huawei/charging/domain/account/Account.java @@ -1,88 +1,89 @@ -package com.huawei.charging.domain.account; - -import com.huawei.charging.domain.BizException; -import com.huawei.charging.domain.DomainFactory; -import com.huawei.charging.domain.Entity; -import com.huawei.charging.domain.charge.*; -import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan; -import com.huawei.charging.domain.charge.chargeplan.ChargePlan; -import com.huawei.charging.domain.charge.chargerule.ChargeRuleFactory; -import com.huawei.charging.domain.charge.chargerule.CompositeChargeRule; -import com.huawei.charging.domain.gateway.AccountGateway; -import lombok.Data; -import lombok.extern.slf4j.Slf4j; - -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.List; - - -@Data -@Entity -@Slf4j -public class Account { - /** - * 用户号码 - */ - private long phoneNo; - - /** - * 账户余额 - */ - private Money remaining; - - /** - * 账户所拥有的套餐 - */ - private List chargePlanList = new ArrayList<>();; - - @Resource - private AccountGateway accountGateway; - - - public Account(){ - - } - - public Account(long phoneNo, Money amount, List chargePlanList){ - this.phoneNo = phoneNo; - this.remaining = amount; - this.chargePlanList = chargePlanList; - } - - public static Account valueOf(long phoneNo, Money amount) { - Account account = DomainFactory.get(Account.class); - account.setPhoneNo(phoneNo); - account.setRemaining(amount); - account.chargePlanList.add(new BasicChargePlan()); - return account; - } - - /** - * 检查账户余额是否足够 - */ - public void checkRemaining() { - if (remaining.isLessThan(Money.of(0))) { - throw BizException.of(this.phoneNo + " has insufficient amount"); - } - } - - public List charge(ChargeContext ctx) { - CompositeChargeRule compositeChargeRule = ChargeRuleFactory.get(chargePlanList); - List chargeRecords = compositeChargeRule.doCharge(ctx); - log.debug("Charges: "+ chargeRecords); - - //跟新账户系统 - accountGateway.sync(phoneNo, chargeRecords); - return chargeRecords; - } - - @Override - public String toString() { - return "Account{" + - "phoneNo=" + phoneNo + - ", remaining=" + remaining + - ", chargePlanList=" + chargePlanList + - '}'; - } -} +package com.huawei.charging.domain.account; + +import com.huawei.charging.domain.BizException; +import com.huawei.charging.domain.DomainFactory; +import com.huawei.charging.domain.Entity; +import com.huawei.charging.domain.charge.*; +import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan; +import com.huawei.charging.domain.charge.chargeplan.ChargePlan; +import com.huawei.charging.domain.charge.chargerule.ChargeRuleFactory; +import com.huawei.charging.domain.charge.chargerule.CompositeChargeRule; +import com.huawei.charging.domain.gateway.AccountGateway; +import jakarta.annotation.Resource; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; + + +import java.util.ArrayList; +import java.util.List; + + +@Data +@Entity +@Slf4j +public class Account { + /** + * 用户号码 + */ + private long phoneNo; + + /** + * 账户余额 + */ + private Money remaining; + + /** + * 账户所拥有的套餐 + */ + private List chargePlanList = new ArrayList<>();; + + @Resource + private AccountGateway accountGateway; + + + public Account(){ + + } + + public Account(long phoneNo, Money amount, List chargePlanList){ + this.phoneNo = phoneNo; + this.remaining = amount; + this.chargePlanList = chargePlanList; + } + + public static Account valueOf(long phoneNo, Money amount) { + Account account = DomainFactory.get(Account.class); + account.setPhoneNo(phoneNo); + account.setRemaining(amount); + account.chargePlanList.add(new BasicChargePlan()); + return account; + } + + /** + * 检查账户余额是否足够 + */ + public void checkRemaining() { + if (remaining.isLessThan(Money.of(0))) { + throw BizException.of(this.phoneNo + " has insufficient amount"); + } + } + + public List charge(ChargeContext ctx) { + CompositeChargeRule compositeChargeRule = ChargeRuleFactory.get(chargePlanList); + List chargeRecords = compositeChargeRule.doCharge(ctx); + log.debug("Charges: "+ chargeRecords); + + //跟新账户系统 + accountGateway.sync(phoneNo, chargeRecords); + return chargeRecords; + } + + @Override + public String toString() { + return "Account{" + + "phoneNo=" + phoneNo + + ", remaining=" + remaining + + ", chargePlanList=" + chargePlanList + + '}'; + } +} diff --git a/cola-samples/charge/src/main/java/com/huawei/charging/domain/account/AccountDomainService.java b/cola-samples/charge/src/main/java/com/huawei/charging/domain/account/AccountDomainService.java index 3d86af439..b5324893a 100644 --- a/cola-samples/charge/src/main/java/com/huawei/charging/domain/account/AccountDomainService.java +++ b/cola-samples/charge/src/main/java/com/huawei/charging/domain/account/AccountDomainService.java @@ -1,21 +1,21 @@ -package com.huawei.charging.domain.account; - -import com.huawei.charging.domain.charge.Session; -import com.huawei.charging.domain.gateway.AccountGateway; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; - -@Component -public class AccountDomainService { - - @Resource - private AccountGateway accountGateway; - - public void canSessionStart(Session session){ - Account callingAccount = accountGateway.getAccount(session.getCallingPhoneNo()); - Account calledAccount = accountGateway.getAccount(session.getCalledPhoneNo()); - callingAccount.checkRemaining(); - calledAccount.checkRemaining(); - } -} +package com.huawei.charging.domain.account; + +import com.huawei.charging.domain.charge.Session; +import com.huawei.charging.domain.gateway.AccountGateway; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Component; + + +@Component +public class AccountDomainService { + + @Resource + private AccountGateway accountGateway; + + public void canSessionStart(Session session){ + Account callingAccount = accountGateway.getAccount(session.getCallingPhoneNo()); + Account calledAccount = accountGateway.getAccount(session.getCalledPhoneNo()); + callingAccount.checkRemaining(); + calledAccount.checkRemaining(); + } +} diff --git a/cola-samples/charge/src/main/java/com/huawei/charging/domain/charge/ChargeRecord.java b/cola-samples/charge/src/main/java/com/huawei/charging/domain/charge/ChargeRecord.java index f5a7ae0ae..bfd9074a5 100644 --- a/cola-samples/charge/src/main/java/com/huawei/charging/domain/charge/ChargeRecord.java +++ b/cola-samples/charge/src/main/java/com/huawei/charging/domain/charge/ChargeRecord.java @@ -1,68 +1,68 @@ -package com.huawei.charging.domain.charge; - -import com.huawei.charging.domain.charge.chargeplan.ChargePlanType; -import lombok.Data; - -import javax.persistence.*; -import java.util.Date; - -@Entity -@Table(name = "charge_record") -@Data -public class ChargeRecord { - - @javax.persistence.Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long Id; - - private String sessionId; - - private long phoneNo; - - /** - * 呼叫类型 - */ - @Enumerated(EnumType.STRING) - private CallType callType; - - /** - * 计费记录所对应的呼叫时长 - */ - private int chargeDuration; - - /** - * 所属计费套餐 - */ - @Enumerated(EnumType.STRING) - private ChargePlanType chargePlanType; - - private Money cost; - - @Temporal(TemporalType.TIMESTAMP) - public Date createTime; - - @Temporal(TemporalType.TIMESTAMP) - public Date updateTime; - - public ChargeRecord() { - } - - public ChargeRecord(long phoneNo, CallType callType, int chargeDuration, ChargePlanType chargePlanType, Money cost) { - this.phoneNo = phoneNo; - this.callType = callType; - this.chargeDuration = chargeDuration; - this.chargePlanType = chargePlanType; - this.cost = cost; - } - - @Override - public String toString() { - return "Charge{" + - "phoneNo=" + phoneNo + - ", callType=" + callType + - ", chargeDuration=" + chargeDuration + - ", chargePlanType=" + chargePlanType + - ", cost=" + cost + - '}'; - } -} +package com.huawei.charging.domain.charge; + +import com.huawei.charging.domain.charge.chargeplan.ChargePlanType; +import lombok.Data; + +import jakarta.persistence.*; +import java.util.Date; + +@Entity +@Table(name = "charge_record") +@Data +public class ChargeRecord { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long Id; + + private String sessionId; + + private long phoneNo; + + /** + * 呼叫类型 + */ + @Enumerated(EnumType.STRING) + private CallType callType; + + /** + * 计费记录所对应的呼叫时长 + */ + private int chargeDuration; + + /** + * 所属计费套餐 + */ + @Enumerated(EnumType.STRING) + private ChargePlanType chargePlanType; + + private Money cost; + + @Temporal(TemporalType.TIMESTAMP) + public Date createTime; + + @Temporal(TemporalType.TIMESTAMP) + public Date updateTime; + + public ChargeRecord() { + } + + public ChargeRecord(long phoneNo, CallType callType, int chargeDuration, ChargePlanType chargePlanType, Money cost) { + this.phoneNo = phoneNo; + this.callType = callType; + this.chargeDuration = chargeDuration; + this.chargePlanType = chargePlanType; + this.cost = cost; + } + + @Override + public String toString() { + return "Charge{" + + "phoneNo=" + phoneNo + + ", callType=" + callType + + ", chargeDuration=" + chargeDuration + + ", chargePlanType=" + chargePlanType + + ", cost=" + cost + + '}'; + } +} diff --git a/cola-samples/charge/src/main/java/com/huawei/charging/domain/charge/MoneyConverter.java b/cola-samples/charge/src/main/java/com/huawei/charging/domain/charge/MoneyConverter.java index 88915a53a..50c83ea46 100644 --- a/cola-samples/charge/src/main/java/com/huawei/charging/domain/charge/MoneyConverter.java +++ b/cola-samples/charge/src/main/java/com/huawei/charging/domain/charge/MoneyConverter.java @@ -1,17 +1,18 @@ -package com.huawei.charging.domain.charge; - -import javax.persistence.AttributeConverter; -import javax.persistence.Converter; - -@Converter(autoApply = true) -public class MoneyConverter implements AttributeConverter { - @Override - public Long convertToDatabaseColumn(Money entityData) { - return Long.valueOf(entityData.getAmount()); - } - - @Override - public Money convertToEntityAttribute(Long dbData) { - return Money.of(dbData.intValue()); - } -} +package com.huawei.charging.domain.charge; + + +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Converter; + +@Converter(autoApply = true) +public class MoneyConverter implements AttributeConverter { + @Override + public Long convertToDatabaseColumn(Money entityData) { + return Long.valueOf(entityData.getAmount()); + } + + @Override + public Money convertToEntityAttribute(Long dbData) { + return Money.of(dbData.intValue()); + } +} diff --git a/cola-samples/charge/src/test/java/com/huawei/charging/CleanArchTest.java b/cola-samples/charge/src/test/java/com/huawei/charging/CleanArchTest.java index 3753bb859..9644ed20c 100644 --- a/cola-samples/charge/src/test/java/com/huawei/charging/CleanArchTest.java +++ b/cola-samples/charge/src/test/java/com/huawei/charging/CleanArchTest.java @@ -3,7 +3,7 @@ import com.tngtech.archunit.core.domain.JavaClasses; import com.tngtech.archunit.core.importer.ClassFileImporter; import com.tngtech.archunit.core.importer.ImportOption; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static com.tngtech.archunit.library.Architectures.layeredArchitecture; @@ -15,7 +15,7 @@ public void protect_clean_arch() { .importPackages("com.huawei.charging"); layeredArchitecture() - .consideringAllDependencies() + .consideringOnlyDependenciesInLayers() .layer("adapter").definedBy("com.huawei.charging.adapter") .layer("application").definedBy("com.huawei.charging.application") .layer("domain").definedBy("com.huawei.charging.domain") diff --git a/cola-samples/charge/src/test/java/com/huawei/charging/ChargeServiceTest.java b/cola-samples/charge/src/test/java/com/huawei/charging/application/ChargeServiceTest.java similarity index 77% rename from cola-samples/charge/src/test/java/com/huawei/charging/ChargeServiceTest.java rename to cola-samples/charge/src/test/java/com/huawei/charging/application/ChargeServiceTest.java index ee5488c1f..54c929de1 100644 --- a/cola-samples/charge/src/test/java/com/huawei/charging/ChargeServiceTest.java +++ b/cola-samples/charge/src/test/java/com/huawei/charging/application/ChargeServiceTest.java @@ -1,111 +1,110 @@ -package com.huawei.charging; - -import com.huawei.charging.application.ChargeServiceI; -import com.huawei.charging.application.dto.ChargeRequest; -import com.huawei.charging.application.dto.EndSessionRequest; -import com.huawei.charging.application.dto.BeginSessionRequest; -import com.huawei.charging.domain.BizException; -import com.huawei.charging.domain.account.Account; -import com.huawei.charging.domain.charge.Money; -import com.huawei.charging.domain.gateway.AccountGateway; -import com.huawei.charging.domain.gateway.SessionGateway; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; - -import javax.annotation.Resource; - -@RunWith(SpringRunner.class) -@ContextConfiguration(classes = Application.class) -public class ChargeServiceTest { - - @Resource - private ChargeServiceI chargeService; - - @Resource - private SessionGateway sessionGateway; - - @Resource - private AccountGateway accountGateway; - - @Test - public void test_session_create(){ - BeginSessionRequest request = new BeginSessionRequest(); - String sessionId = "00002"; - request.setSessionId(sessionId); - request.setCallingPhoneNo(13681874563L); - request.setCalledPhoneNo(15921582125L); - - chargeService.begin(request); - - Assert.assertEquals(sessionId, sessionGateway.get(sessionId).getSessionId()); - } - - @Test - public void test_remaining_insufficient(){ - BeginSessionRequest request = new BeginSessionRequest(); - String sessionId = "00003"; - request.setSessionId(sessionId); - request.setCallingPhoneNo(13681874561L); - request.setCalledPhoneNo(15921582125L); - - //mock insufficient - Account account = accountGateway.getAccount(13681874561L); - account.getRemaining().minus(Money.of(200)); - - try { - chargeService.begin(request); - Assert.fail("BizException not thrown"); - } - catch (BizException e){ - System.out.println(e.getMessage()); - } - } - - @Test - public void test_normal_charge(){ - BeginSessionRequest request = new BeginSessionRequest(); - String sessionId = "00001"; - request.setSessionId(sessionId); - request.setCallingPhoneNo(13681874533L); - request.setCalledPhoneNo(15921582155L); - - chargeService.begin(request); - - ChargeRequest chargeRequest = new ChargeRequest(); - chargeRequest.setSessionId(sessionId); - chargeRequest.setDuration(10); - - chargeService.charge(chargeRequest); - - Account callingAccount = accountGateway.getAccount(13681874533L); - Account calledAccount = accountGateway.getAccount(15921582155L); - Assert.assertEquals(Money.of(150), callingAccount.getRemaining()); - Assert.assertEquals(Money.of(160), calledAccount.getRemaining()); - } - - @Test - public void test_session_end(){ - BeginSessionRequest request = new BeginSessionRequest(); - String sessionId = "00004"; - request.setSessionId(sessionId); - request.setCallingPhoneNo(14681874533L); - request.setCalledPhoneNo(14921582155L); - - chargeService.begin(request); - - EndSessionRequest endReq = new EndSessionRequest(); - endReq.setSessionId("00004"); - endReq.setDuration(20); - - chargeService.end(endReq); - - Account callingAccount = accountGateway.getAccount(14681874533L); - Account calledAccount = accountGateway.getAccount(14921582155L); - Assert.assertEquals(Money.of(100), callingAccount.getRemaining()); - Assert.assertEquals(Money.of(120), calledAccount.getRemaining()); - Assert.assertEquals(null, sessionGateway.get("00004")); - } -} +package com.huawei.charging.application; + +import com.huawei.charging.Application; +import com.huawei.charging.application.dto.BeginSessionRequest; +import com.huawei.charging.application.dto.ChargeRequest; +import com.huawei.charging.application.dto.EndSessionRequest; +import com.huawei.charging.domain.BizException; +import com.huawei.charging.domain.account.Account; +import com.huawei.charging.domain.charge.Money; +import com.huawei.charging.domain.gateway.AccountGateway; +import com.huawei.charging.domain.gateway.SessionGateway; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; + + +@SpringBootTest +@ContextConfiguration(classes = Application.class) +public class ChargeServiceTest { + + @Resource + private ChargeServiceI chargeService; + + @Resource + private SessionGateway sessionGateway; + + @Resource + private AccountGateway accountGateway; + + @Test + public void test_session_create(){ + BeginSessionRequest request = new BeginSessionRequest(); + String sessionId = "00002"; + request.setSessionId(sessionId); + request.setCallingPhoneNo(13681874563L); + request.setCalledPhoneNo(15921582125L); + + chargeService.begin(request); + + Assertions.assertEquals(sessionId, sessionGateway.get(sessionId).getSessionId()); + } + + @Test + public void test_remaining_insufficient(){ + BeginSessionRequest request = new BeginSessionRequest(); + String sessionId = "00003"; + request.setSessionId(sessionId); + request.setCallingPhoneNo(13681874561L); + request.setCalledPhoneNo(15921582125L); + + //mock insufficient + Account account = accountGateway.getAccount(13681874561L); + account.getRemaining().minus(Money.of(200)); + + try { + chargeService.begin(request); + Assertions.fail("BizException not thrown"); + } + catch (BizException e){ + System.out.println(e.getMessage()); + } + } + + @Test + public void test_normal_charge(){ + BeginSessionRequest request = new BeginSessionRequest(); + String sessionId = "00001"; + request.setSessionId(sessionId); + request.setCallingPhoneNo(13681874533L); + request.setCalledPhoneNo(15921582155L); + + chargeService.begin(request); + + ChargeRequest chargeRequest = new ChargeRequest(); + chargeRequest.setSessionId(sessionId); + chargeRequest.setDuration(10); + + chargeService.charge(chargeRequest); + + Account callingAccount = accountGateway.getAccount(13681874533L); + Account calledAccount = accountGateway.getAccount(15921582155L); + Assertions.assertEquals(Money.of(150), callingAccount.getRemaining()); + Assertions.assertEquals(Money.of(160), calledAccount.getRemaining()); + } + + @Test + public void test_session_end(){ + BeginSessionRequest request = new BeginSessionRequest(); + String sessionId = "00004"; + request.setSessionId(sessionId); + request.setCallingPhoneNo(14681874533L); + request.setCalledPhoneNo(14921582155L); + + chargeService.begin(request); + + EndSessionRequest endReq = new EndSessionRequest(); + endReq.setSessionId("00004"); + endReq.setDuration(20); + + chargeService.end(endReq); + + Account callingAccount = accountGateway.getAccount(14681874533L); + Account calledAccount = accountGateway.getAccount(14921582155L); + Assertions.assertEquals(Money.of(100), callingAccount.getRemaining()); + Assertions.assertEquals(Money.of(120), calledAccount.getRemaining()); + Assertions.assertEquals(null, sessionGateway.get("00004")); + } +} diff --git a/cola-samples/charge/src/test/java/com/huawei/charging/ut/ChargeRecordPlanTest.java b/cola-samples/charge/src/test/java/com/huawei/charging/domain/ChargeRecordPlanTest.java similarity index 82% rename from cola-samples/charge/src/test/java/com/huawei/charging/ut/ChargeRecordPlanTest.java rename to cola-samples/charge/src/test/java/com/huawei/charging/domain/ChargeRecordPlanTest.java index b63178826..9af23c3ba 100644 --- a/cola-samples/charge/src/test/java/com/huawei/charging/ut/ChargeRecordPlanTest.java +++ b/cola-samples/charge/src/test/java/com/huawei/charging/domain/ChargeRecordPlanTest.java @@ -1,33 +1,34 @@ -package com.huawei.charging.ut; - - -import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan; -import com.huawei.charging.domain.charge.chargeplan.ChargePlan; -import com.huawei.charging.domain.charge.chargeplan.ChargePlanType; -import com.huawei.charging.domain.charge.chargeplan.FamilyChargePlan; -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class ChargeRecordPlanTest { - - @Test - public void test_priority(){ - ChargePlan basicChargePlan = new BasicChargePlan(); - ChargePlan familyChargePlan = new FamilyChargePlan(); - ChargePlan fixedTimeChargePlan = new FamilyChargePlan(); - List chargePlanList = new ArrayList<>(); - chargePlanList.add(basicChargePlan); - chargePlanList.add(familyChargePlan); - chargePlanList.add(fixedTimeChargePlan); - - Collections.sort(chargePlanList); - - System.out.println(chargePlanList.get(0)); - Assert.assertEquals(ChargePlanType.FAMILY, chargePlanList.get(0).getType()); - - } -} +package com.huawei.charging.domain; + + +import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan; +import com.huawei.charging.domain.charge.chargeplan.ChargePlan; +import com.huawei.charging.domain.charge.chargeplan.ChargePlanType; +import com.huawei.charging.domain.charge.chargeplan.FamilyChargePlan; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ChargeRecordPlanTest { + + @Test + public void test_priority(){ + ChargePlan basicChargePlan = new BasicChargePlan(); + ChargePlan familyChargePlan = new FamilyChargePlan(); + ChargePlan fixedTimeChargePlan = new FamilyChargePlan(); + List chargePlanList = new ArrayList<>(); + chargePlanList.add(basicChargePlan); + chargePlanList.add(familyChargePlan); + chargePlanList.add(fixedTimeChargePlan); + + Collections.sort(chargePlanList); + + System.out.println(chargePlanList.get(0)); + Assertions.assertEquals(ChargePlanType.FAMILY, chargePlanList.get(0).getType()); + + } +} diff --git a/cola-samples/charge/src/test/java/com/huawei/charging/ut/ChargeRecordRuleTest.java b/cola-samples/charge/src/test/java/com/huawei/charging/domain/ChargeRecordRuleTest.java similarity index 80% rename from cola-samples/charge/src/test/java/com/huawei/charging/ut/ChargeRecordRuleTest.java rename to cola-samples/charge/src/test/java/com/huawei/charging/domain/ChargeRecordRuleTest.java index 7bc868bb6..eec74b6ee 100644 --- a/cola-samples/charge/src/test/java/com/huawei/charging/ut/ChargeRecordRuleTest.java +++ b/cola-samples/charge/src/test/java/com/huawei/charging/domain/ChargeRecordRuleTest.java @@ -1,93 +1,91 @@ -package com.huawei.charging.ut; - -import com.huawei.charging.domain.account.Account; -import com.huawei.charging.domain.charge.CallType; -import com.huawei.charging.domain.charge.ChargeContext; -import com.huawei.charging.domain.charge.Money; -import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan; -import com.huawei.charging.domain.charge.chargeplan.ChargePlan; -import com.huawei.charging.domain.charge.chargeplan.FamilyChargePlan; -import com.huawei.charging.domain.charge.chargeplan.FixedTimeChangePlan; -import com.huawei.charging.domain.charge.chargerule.BasicChargeRule; -import com.huawei.charging.domain.charge.chargerule.FamilyChargeRule; -import com.huawei.charging.domain.charge.chargerule.FixedTimeChargeRule; -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class ChargeRecordRuleTest { - - @Test - public void test_basic_charge_rule(){ - //prepare - ChargePlan chargePlan = new BasicChargePlan(); - Account account = new Account(13681874561L, Money.of(200), Collections.singletonList(chargePlan)); - ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 20); - ctx.account = account; - System.out.println("Account before charge: "+ account); - - //do - BasicChargeRule basicChargeRule = new BasicChargeRule(); - basicChargeRule.belongsTo(chargePlan); - basicChargeRule.doCharge(ctx); - - //check - System.out.println("Account after charge: "+ account); - Assert.assertEquals( Money.of(100), ctx.account.getRemaining()); - Assert.assertEquals( 0, ctx.getDurationToCharge()); - } - - @Test - public void test_family_charge_rule(){ - //prepare - FamilyChargePlan chargePlan = new FamilyChargePlan(); - Account account = new Account(13681874561L, Money.of(200), Collections.singletonList(chargePlan)); - ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 20); - ctx.account = account; - System.out.println("Account before charge: "+ account); - - //do - FamilyChargeRule familyChargeRule = new FamilyChargeRule(); - familyChargeRule.belongsTo(chargePlan); - familyChargeRule.doCharge(ctx); - - //check - System.out.println("Account after charge: "+ account); - Assert.assertEquals( Money.of(200), ctx.account.getRemaining()); - Assert.assertEquals( 0, ctx.getDurationToCharge()); - } - - @Test - public void test_fixed_time_charge_rule(){ - //prepare - FixedTimeChangePlan chargePlan = new FixedTimeChangePlan(); - Account account = new Account(13681874561L, Money.of(200), Collections.singletonList(chargePlan)); - ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 180); - ctx.account = account; - System.out.println("Account before charge: "+ account); - - //do - FixedTimeChargeRule fixedTimeChargeRule = new FixedTimeChargeRule(); - fixedTimeChargeRule.belongsTo(chargePlan); - fixedTimeChargeRule.doCharge(ctx); - - //check - System.out.println("Account after charge: "+ account); - Assert.assertEquals( true, chargePlan.getResource().isCallingTimeRemaining()); - Assert.assertEquals( 0, ctx.getDurationToCharge()); - - // come a new charge - ChargeContext ctx2 = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 40); - ctx2.account = account; - fixedTimeChargeRule.doCharge(ctx2); - Assert.assertEquals( false, chargePlan.getResource().isCallingTimeRemaining()); - Assert.assertEquals( 20, ctx2.getDurationToCharge()); - - //reset fixed time - FixedTimeChangePlan.FreeCallTime.FREE_CALLED_TIME = 200; - FixedTimeChangePlan.FreeCallTime.FREE_CALLING_TIME = 200; - } -} +package com.huawei.charging.domain; + +import com.huawei.charging.domain.account.Account; +import com.huawei.charging.domain.charge.CallType; +import com.huawei.charging.domain.charge.ChargeContext; +import com.huawei.charging.domain.charge.Money; +import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan; +import com.huawei.charging.domain.charge.chargeplan.ChargePlan; +import com.huawei.charging.domain.charge.chargeplan.FamilyChargePlan; +import com.huawei.charging.domain.charge.chargeplan.FixedTimeChangePlan; +import com.huawei.charging.domain.charge.chargerule.BasicChargeRule; +import com.huawei.charging.domain.charge.chargerule.FamilyChargeRule; +import com.huawei.charging.domain.charge.chargerule.FixedTimeChargeRule; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +public class ChargeRecordRuleTest { + + @Test + public void test_basic_charge_rule(){ + //prepare + ChargePlan chargePlan = new BasicChargePlan(); + Account account = new Account(13681874561L, Money.of(200), Collections.singletonList(chargePlan)); + ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 20); + ctx.account = account; + System.out.println("Account before charge: "+ account); + + //do + BasicChargeRule basicChargeRule = new BasicChargeRule(); + basicChargeRule.belongsTo(chargePlan); + basicChargeRule.doCharge(ctx); + + //check + System.out.println("Account after charge: "+ account); + Assertions.assertEquals( Money.of(100), ctx.account.getRemaining()); + Assertions.assertEquals( 0, ctx.getDurationToCharge()); + } + + @Test + public void test_family_charge_rule(){ + //prepare + FamilyChargePlan chargePlan = new FamilyChargePlan(); + Account account = new Account(13681874561L, Money.of(200), Collections.singletonList(chargePlan)); + ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 20); + ctx.account = account; + System.out.println("Account before charge: "+ account); + + //do + FamilyChargeRule familyChargeRule = new FamilyChargeRule(); + familyChargeRule.belongsTo(chargePlan); + familyChargeRule.doCharge(ctx); + + //check + System.out.println("Account after charge: "+ account); + Assertions.assertEquals( Money.of(200), ctx.account.getRemaining()); + Assertions.assertEquals( 0, ctx.getDurationToCharge()); + } + + @Test + public void test_fixed_time_charge_rule(){ + //prepare + FixedTimeChangePlan chargePlan = new FixedTimeChangePlan(); + Account account = new Account(13681874561L, Money.of(200), Collections.singletonList(chargePlan)); + ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 180); + ctx.account = account; + System.out.println("Account before charge: "+ account); + + //do + FixedTimeChargeRule fixedTimeChargeRule = new FixedTimeChargeRule(); + fixedTimeChargeRule.belongsTo(chargePlan); + fixedTimeChargeRule.doCharge(ctx); + + //check + System.out.println("Account after charge: "+ account); + Assertions.assertEquals( true, chargePlan.getResource().isCallingTimeRemaining()); + Assertions.assertEquals( 0, ctx.getDurationToCharge()); + + // come a new charge + ChargeContext ctx2 = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 40); + ctx2.account = account; + fixedTimeChargeRule.doCharge(ctx2); + Assertions.assertEquals( false, chargePlan.getResource().isCallingTimeRemaining()); + Assertions.assertEquals( 20, ctx2.getDurationToCharge()); + + //reset fixed time + FixedTimeChangePlan.FreeCallTime.FREE_CALLED_TIME = 200; + FixedTimeChangePlan.FreeCallTime.FREE_CALLING_TIME = 200; + } +} diff --git a/cola-samples/charge/src/test/java/com/huawei/charging/ut/CompositeChargeRuleTestRecord.java b/cola-samples/charge/src/test/java/com/huawei/charging/domain/CompositeChargeRuleTestRecord.java similarity index 86% rename from cola-samples/charge/src/test/java/com/huawei/charging/ut/CompositeChargeRuleTestRecord.java rename to cola-samples/charge/src/test/java/com/huawei/charging/domain/CompositeChargeRuleTestRecord.java index fe58173c7..bc9312869 100644 --- a/cola-samples/charge/src/test/java/com/huawei/charging/ut/CompositeChargeRuleTestRecord.java +++ b/cola-samples/charge/src/test/java/com/huawei/charging/domain/CompositeChargeRuleTestRecord.java @@ -1,81 +1,81 @@ -package com.huawei.charging.ut; - -import com.huawei.charging.Application; -import com.huawei.charging.domain.account.Account; -import com.huawei.charging.domain.charge.CallType; -import com.huawei.charging.domain.charge.ChargeRecord; -import com.huawei.charging.domain.charge.ChargeContext; -import com.huawei.charging.domain.charge.Money; -import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan; -import com.huawei.charging.domain.charge.chargeplan.ChargePlan; -import com.huawei.charging.domain.charge.chargeplan.FamilyChargePlan; -import com.huawei.charging.domain.charge.chargeplan.FixedTimeChangePlan; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; - -import java.util.ArrayList; -import java.util.List; - -@RunWith(SpringRunner.class) -@ContextConfiguration(classes = Application.class) -public class CompositeChargeRuleTestRecord { - - @Test - public void test_basic_and_fixedTime_charge_rule(){ - // prepare - List chargePlanList = new ArrayList<>(); - chargePlanList.add(new BasicChargePlan()); - chargePlanList.add(new FixedTimeChangePlan()); - Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean - account.setChargePlanList(chargePlanList); - ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220); - ctx.account = account; - - // do - List chargeRecords = account.charge(ctx); - System.out.println("Account after charge: "+ account); - // check - Assert.assertEquals(2, chargeRecords.size()); - } - - @Test - public void test_basic_and_family_charge_rule(){ - // prepare - List chargePlanList = new ArrayList<>(); - chargePlanList.add(new BasicChargePlan()); - chargePlanList.add(new FamilyChargePlan()); - Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean - account.setChargePlanList(chargePlanList); - ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220); - ctx.account = account; - - // do - List chargeRecords = account.charge(ctx); - System.out.println("Account after charge: "+ account); - // check - Assert.assertEquals(1, chargeRecords.size()); - } - - @Test - public void test_all_charge_rule(){ - // prepare - List chargePlanList = new ArrayList<>(); - chargePlanList.add(new BasicChargePlan()); - chargePlanList.add(new FamilyChargePlan()); - chargePlanList.add(new FixedTimeChangePlan()); - Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean - account.setChargePlanList(chargePlanList); - ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220); - ctx.account = account; - - // do - List chargeRecords = account.charge(ctx); - System.out.println("Account after charge: "+ account); - - // check - Assert.assertEquals(1, chargeRecords.size()); - } -} +package com.huawei.charging.domain; + +import com.huawei.charging.Application; +import com.huawei.charging.domain.account.Account; +import com.huawei.charging.domain.charge.CallType; +import com.huawei.charging.domain.charge.ChargeRecord; +import com.huawei.charging.domain.charge.ChargeContext; +import com.huawei.charging.domain.charge.Money; +import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan; +import com.huawei.charging.domain.charge.chargeplan.ChargePlan; +import com.huawei.charging.domain.charge.chargeplan.FamilyChargePlan; +import com.huawei.charging.domain.charge.chargeplan.FixedTimeChangePlan; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; + +import java.util.ArrayList; +import java.util.List; + +@SpringBootTest +@ContextConfiguration(classes = Application.class) +public class CompositeChargeRuleTestRecord { + + @Test + public void test_basic_and_fixedTime_charge_rule(){ + // prepare + List chargePlanList = new ArrayList<>(); + chargePlanList.add(new BasicChargePlan()); + chargePlanList.add(new FixedTimeChangePlan()); + Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean + account.setChargePlanList(chargePlanList); + ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220); + ctx.account = account; + + // do + List chargeRecords = account.charge(ctx); + System.out.println("Account after charge: "+ account); + // check + Assertions.assertEquals(2, chargeRecords.size()); + } + + @Test + public void test_basic_and_family_charge_rule(){ + // prepare + List chargePlanList = new ArrayList<>(); + chargePlanList.add(new BasicChargePlan()); + chargePlanList.add(new FamilyChargePlan()); + Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean + account.setChargePlanList(chargePlanList); + ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220); + ctx.account = account; + + // do + List chargeRecords = account.charge(ctx); + System.out.println("Account after charge: "+ account); + // check + Assertions.assertEquals(1, chargeRecords.size()); + } + + @Test + public void test_all_charge_rule(){ + // prepare + List chargePlanList = new ArrayList<>(); + chargePlanList.add(new BasicChargePlan()); + chargePlanList.add(new FamilyChargePlan()); + chargePlanList.add(new FixedTimeChangePlan()); + Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean + account.setChargePlanList(chargePlanList); + ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220); + ctx.account = account; + + // do + List chargeRecords = account.charge(ctx); + System.out.println("Account after charge: "+ account); + + // check + Assertions.assertEquals(1, chargeRecords.size()); + } +} diff --git a/cola-samples/charge/src/test/java/com/huawei/charging/ChargeRecordRepoTest.java b/cola-samples/charge/src/test/java/com/huawei/charging/infrastructure/ChargeRecordRepoTest.java similarity index 77% rename from cola-samples/charge/src/test/java/com/huawei/charging/ChargeRecordRepoTest.java rename to cola-samples/charge/src/test/java/com/huawei/charging/infrastructure/ChargeRecordRepoTest.java index 36b2ac600..bb1521ec1 100644 --- a/cola-samples/charge/src/test/java/com/huawei/charging/ChargeRecordRepoTest.java +++ b/cola-samples/charge/src/test/java/com/huawei/charging/infrastructure/ChargeRecordRepoTest.java @@ -1,50 +1,48 @@ -package com.huawei.charging; - -import com.huawei.charging.domain.charge.CallType; -import com.huawei.charging.domain.charge.ChargeRecord; -import com.huawei.charging.domain.charge.Money; -import com.huawei.charging.domain.charge.chargeplan.ChargePlanType; -import com.huawei.charging.domain.gateway.ChargeGateway; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class ChargeRecordRepoTest { - @Resource - private ChargeGateway chargeGateway; - - private String sessionId; - - @Before - public void setup(){ - sessionId = UUID.randomUUID().toString(); - } - - @Test - public void testSave(){ - List chargeRecordList = new ArrayList<>(); - ChargeRecord chargeRecord = new ChargeRecord(13681874561L, CallType.CALLED, 10, ChargePlanType.FAMILY, Money.of(123)); - chargeRecord.setSessionId(sessionId); - chargeRecordList.add(chargeRecord); - chargeGateway.saveAll(chargeRecordList); - } - - @Test - public void testFindBySessionId(){ - testSave(); - List chargeRecordList = chargeGateway.findBySessionId(sessionId); - System.out.println(chargeRecordList.get(0)); - Assert.assertEquals(chargeRecordList.size(), 1); - chargeGateway.delete(chargeRecordList.get(0)); - } -} +package com.huawei.charging.infrastructure; + +import com.huawei.charging.domain.charge.CallType; +import com.huawei.charging.domain.charge.ChargeRecord; +import com.huawei.charging.domain.charge.Money; +import com.huawei.charging.domain.charge.chargeplan.ChargePlanType; +import com.huawei.charging.domain.gateway.ChargeGateway; + +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@SpringBootTest +public class ChargeRecordRepoTest { + @Resource + private ChargeGateway chargeGateway; + + private String sessionId; + + @BeforeEach + public void setup(){ + sessionId = UUID.randomUUID().toString(); + } + + @Test + public void testSave(){ + List chargeRecordList = new ArrayList<>(); + ChargeRecord chargeRecord = new ChargeRecord(13681874561L, CallType.CALLED, 10, ChargePlanType.FAMILY, Money.of(123)); + chargeRecord.setSessionId(sessionId); + chargeRecordList.add(chargeRecord); + chargeGateway.saveAll(chargeRecordList); + } + + @Test + public void testFindBySessionId(){ + testSave(); + List chargeRecordList = chargeGateway.findBySessionId(sessionId); + System.out.println(chargeRecordList.get(0)); + Assertions.assertEquals(chargeRecordList.size(), 1); + chargeGateway.delete(chargeRecordList.get(0)); + } +} diff --git a/cola-samples/charge/src/test/java/com/huawei/charging/PropertyTest.java b/cola-samples/charge/src/test/java/com/huawei/charging/infrastructure/PropertyTest.java similarity index 82% rename from cola-samples/charge/src/test/java/com/huawei/charging/PropertyTest.java rename to cola-samples/charge/src/test/java/com/huawei/charging/infrastructure/PropertyTest.java index 6594c087d..cb4ffe292 100644 --- a/cola-samples/charge/src/test/java/com/huawei/charging/PropertyTest.java +++ b/cola-samples/charge/src/test/java/com/huawei/charging/infrastructure/PropertyTest.java @@ -1,38 +1,37 @@ -package com.huawei.charging; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; - -@SpringBootTest -@RunWith(SpringRunner.class) -//use test profile, this will merge application.yml and application-test.yaml. -//but if you put an application.yml under test/resources, it will replace the project application.yml. -//the advantage of profile, is that it can inherit and override. -@ActiveProfiles("test") -public class PropertyTest { - - @Value("${spring.jpa.show-sql}") - private String showSql; - - @Value("${spring.jpa.hibernate.ddl-auto}") - private String ddlAuto; - - @Value("${my-name}") - private String myName; - - @Value("${my-age}") - private String myAge; - - - @Test - public void test() { - System.out.println("spring.jpa.show-sql : " + showSql); - System.out.println("spring.jpa.hibernate.ddl-auto : " + ddlAuto); - System.out.println("myName : " + myName); - System.out.println("myAge : " + myAge); - } -} +package com.huawei.charging.infrastructure; + + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + + +@SpringBootTest +//use test profile, this will merge application.yml and application-test.yaml. +//but if you put an application.yml under test/resources, it will replace the project application.yml. +//the advantage of profile, is that it can inherit and override. +@ActiveProfiles("test") +public class PropertyTest { + + @Value("${spring.jpa.show-sql}") + private String showSql; + + @Value("${spring.jpa.hibernate.ddl-auto}") + private String ddlAuto; + + @Value("${my-name}") + private String myName; + + @Value("${my-age}") + private String myAge; + + + @Test + public void test() { + System.out.println("spring.jpa.show-sql : " + showSql); + System.out.println("spring.jpa.hibernate.ddl-auto : " + ddlAuto); + System.out.println("myName : " + myName); + System.out.println("myAge : " + myAge); + } +}