-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58a3e21
commit 4ae5e55
Showing
22 changed files
with
451 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 30 additions & 27 deletions
57
...samples/charge/src/main/java/com/huawei/charging/domain/charge/chargeplan/ChargePlan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
28 changes: 16 additions & 12 deletions
28
cola-samples/charge/src/main/java/com/huawei/charging/domain/gateway/ChargeGateway.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
79 changes: 46 additions & 33 deletions
79
cola-samples/charge/src/main/java/com/huawei/charging/infrastructure/AccountGatewayImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
cola-samples/charge/src/main/java/com/huawei/charging/infrastructure/RestClientBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,8 @@ spring: | |
ddl-auto: update | ||
show-sql: true | ||
|
||
server: | ||
port: 8081 | ||
|
||
my-name: default | ||
my-age: default |
9 changes: 9 additions & 0 deletions
9
cola-samples/charge/src/test/java/com/huawei/charging/TestsContainerBoot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
cola-samples/charge/src/test/java/com/huawei/charging/infrastructure/AccountGatewayTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.