Skip to content

Commit

Permalink
add h2database and wiremock
Browse files Browse the repository at this point in the history
  • Loading branch information
significantfrank committed Apr 27, 2024
1 parent 58a3e21 commit 4ae5e55
Show file tree
Hide file tree
Showing 22 changed files with 451 additions and 116 deletions.
22 changes: 21 additions & 1 deletion cola-samples/charge/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<springfox.version>3.0.0</springfox.version>
<archunit.version>1.3.0</archunit.version>
<spring.boot.version>3.1.0</spring.boot.version>
<spring.boot.version>3.2.0</spring.boot.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -61,5 +61,25 @@
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- Unit Test Support start-->
<dependency>
<groupId>com.alibaba.cola</groupId>
<artifactId>cola-component-test-container</artifactId>
<version>4.4.0-SNAPSHOT</version>
</dependency>
<!--this for embedded database unit test-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<!--this for dependent service mock, to avoid conflict, we'd better use standalone -->
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>3.5.4</version>
<scope>test</scope>
</dependency>
<!-- Unit Test Support End-->
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class ChargeController {

@PostMapping("session/{sessionId}/begin")
public Response begin(@PathVariable(name = "sessionId") String sessionId,
@RequestParam long callingPhoneNo,
@RequestParam long calledPhoneNo) {
@RequestParam("callingPhoneNo") String callingPhoneNo,
@RequestParam("calledPhoneNo") String calledPhoneNo) {
log.debug(sessionId + " " + callingPhoneNo + " " + calledPhoneNo);
BeginSessionRequest request = new BeginSessionRequest(sessionId, callingPhoneNo, calledPhoneNo);
BeginSessionRequest request = new BeginSessionRequest(sessionId, Long.valueOf(callingPhoneNo), Long.valueOf(calledPhoneNo));
return chargeService.begin(request);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.huawei.charging.domain.account;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.huawei.charging.domain.BizException;
import com.huawei.charging.domain.DomainFactory;
import com.huawei.charging.domain.Entity;
Expand Down Expand Up @@ -35,11 +36,13 @@ public class Account {
/**
* 账户所拥有的套餐
*/
@JsonIgnore
private List<ChargePlan> chargePlanList = new ArrayList<>();;

@Resource
private AccountGateway accountGateway;

private String name;

public Account(){

Expand Down Expand Up @@ -84,6 +87,7 @@ public String toString() {
"phoneNo=" + phoneNo +
", remaining=" + remaining +
", chargePlanList=" + chargePlanList +
", name=" + name +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package com.huawei.charging.domain.charge.chargeplan;

public abstract class ChargePlan<T extends Resource> implements Comparable<ChargePlan>{

protected int priority;

public abstract T getResource();

public abstract ChargePlanType getType();

/**
* 不同套餐之间的优先级关系
* @param other the object to be compared.
* @return
*/
@Override
public int compareTo(ChargePlan other) {
return other.priority - this.priority;
}

@Override
public String toString() {
return "ChargePlan{chargeType=" + getType()+
", priority=" + priority +
'}';
}
}
package com.huawei.charging.domain.charge.chargeplan;

public abstract class ChargePlan<T extends Resource> implements Comparable<ChargePlan>{

protected int priority;

public abstract T getResource();

public abstract ChargePlanType getType();

public ChargePlan(){

}
/**
* 不同套餐之间的优先级关系
* @param other the object to be compared.
* @return
*/
@Override
public int compareTo(ChargePlan other) {
return other.priority - this.priority;
}

@Override
public String toString() {
return "ChargePlan{chargeType=" + getType()+
", priority=" + priority +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.huawei.charging.domain.gateway;

import com.huawei.charging.domain.charge.ChargeRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ChargeGateway extends JpaRepository<ChargeRecord, Long> {
public List<ChargeRecord> findBySessionId(String sessionId);
}
package com.huawei.charging.domain.gateway;

import com.huawei.charging.domain.charge.ChargeRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ChargeGateway extends JpaRepository<ChargeRecord, Long> {
public List<ChargeRecord> findBySessionId(String sessionId);

public ChargeRecord getBySessionId(String sessionId);

public List<ChargeRecord> findByPhoneNo(long phoneNo);
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,46 @@
package com.huawei.charging.infrastructure;

import com.huawei.charging.domain.account.Account;
import com.huawei.charging.domain.charge.ChargeRecord;
import com.huawei.charging.domain.charge.Money;
import com.huawei.charging.domain.gateway.AccountGateway;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AccountGatewayImpl implements AccountGateway {
private static final String URL_TO_GET_ACCOUNT = "http://internel.xxx.com/api/account/{account}";
private static final String URL_TO_SYNC_ACCOUNT = "http://internel.xxx.com/api/account/{account}/sync";

private Map<Long, Account> accountMap = new HashMap<>();

@Override
public Account getAccount(long phoneNo) {
if(accountMap.get(phoneNo) == null){
accountMap.put(phoneNo, Account.valueOf(phoneNo, Money.of(200)));
}
return accountMap.get(phoneNo);
}

@Override
public void sync(long phoneNo, List<ChargeRecord> records) {
// 更新账户系统
}
}
package com.huawei.charging.infrastructure;

import com.huawei.charging.domain.account.Account;
import com.huawei.charging.domain.charge.ChargeRecord;
import com.huawei.charging.domain.charge.Money;
import com.huawei.charging.domain.gateway.AccountGateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class AccountGatewayImpl implements AccountGateway {
private static final String GET_ACCOUNT_PATH = "/v1/api/account/{account}";
private static final String SYNC_ACCOUNT_PATH = "/v1/api/account/account/{account}/sync";

private Map<Long, Account> accountMap = new HashMap<>();

@Autowired
private RestClient restClient;

@Override
public Account getAccount(long phoneNo) {
Account account = restClient.get()
.uri(GET_ACCOUNT_PATH, phoneNo)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.body(Account.class);

return account;
}

@Override
public void sync(long phoneNo, List<ChargeRecord> records) {
// 更新账户系统
log.info("sync account info, to be implemented");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.huawei.charging.infrastructure;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;

@Configuration
public class RestClientBean {

@Value("${REMOTE_BASE_URI:http://localhost:8080}")
String baseURI;

@Bean
RestClient restClient() {
return RestClient.create(baseURI);
}
}
3 changes: 3 additions & 0 deletions cola-samples/charge/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ spring:
ddl-auto: update
show-sql: true

server:
port: 8081

my-name: default
my-age: default
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.huawei.charging;

import com.alibaba.cola.test.TestsContainer;

public class TestsContainerBoot {
public static void main(String[] args) {
TestsContainer.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

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.*;
import com.huawei.charging.domain.charge.chargeplan.BasicChargePlan;
import com.huawei.charging.domain.charge.chargeplan.ChargePlan;
import com.huawei.charging.domain.charge.chargeplan.FamilyChargePlan;
Expand All @@ -18,11 +15,15 @@

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

@SpringBootTest
@ContextConfiguration(classes = Application.class)
public class CompositeChargeRuleTestRecord {

private long callingPhoneNo = 13681874561L;
private long calledPhoneNo = 15921582125L;

@Test
public void test_basic_and_fixedTime_charge_rule(){
// prepare
Expand All @@ -32,6 +33,9 @@ public void test_basic_and_fixedTime_charge_rule(){
Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean
account.setChargePlanList(chargePlanList);
ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220);
String sessionId = UUID.randomUUID().toString();
Session session = new Session(sessionId, callingPhoneNo, calledPhoneNo);
ctx.setSession(session);
ctx.account = account;

// do
Expand All @@ -50,6 +54,9 @@ public void test_basic_and_family_charge_rule(){
Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean
account.setChargePlanList(chargePlanList);
ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220);
String sessionId = UUID.randomUUID().toString();
Session session = new Session(sessionId, callingPhoneNo, calledPhoneNo);
ctx.setSession(session);
ctx.account = account;

// do
Expand All @@ -69,6 +76,9 @@ public void test_all_charge_rule(){
Account account = Account.valueOf(13681874561L, Money.of(200)); // for spring bean
account.setChargePlanList(chargePlanList);
ChargeContext ctx = new ChargeContext(CallType.CALLING, 13681874561L, 15921582125L, 220);
String sessionId = UUID.randomUUID().toString();
Session session = new Session(sessionId, callingPhoneNo, calledPhoneNo);
ctx.setSession(session);
ctx.account = account;

// do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.huawei.charging.infrastructure;

import com.alibaba.cola.test.TestsContainer;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import com.huawei.charging.Application;
import com.huawei.charging.domain.account.Account;
import com.huawei.charging.domain.charge.Money;
import com.huawei.charging.domain.gateway.AccountGateway;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest
@ContextConfiguration(classes = Application.class)
@WireMockTest(httpPort = 8080)
public class AccountGatewayTest {

@Autowired
AccountGateway accountGateway;

@Test
public void testGetAccount(WireMockRuntimeInfo wmRuntimeInfo) {
WireMockRegister.registerStub(wmRuntimeInfo.getWireMock(), "/fixture/wiremock/stub_account.json");

Account account = accountGateway.getAccount(15921582125L);
System.out.println("account : " + account);

Assertions.assertEquals(account.getPhoneNo(), 15921582125L);
Assertions.assertEquals(account.getRemaining(), Money.of(400));
}
}
Loading

0 comments on commit 4ae5e55

Please sign in to comment.