-
Notifications
You must be signed in to change notification settings - Fork 0
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
0d1b10d
commit cfffc6a
Showing
5 changed files
with
141 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,34 @@ | ||
package com.bifurcated.wallet.data; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import lombok.*; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.annotation.Transient; | ||
import org.springframework.data.domain.Persistable; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
import java.util.UUID; | ||
|
||
@Getter | ||
@ToString | ||
@AllArgsConstructor | ||
@Data | ||
@Table("wallet") | ||
public class Wallet { | ||
public class Wallet implements Persistable<UUID> { | ||
@Id | ||
private UUID id; | ||
|
||
@Setter | ||
private Float amount; | ||
|
||
@Transient | ||
@JsonIgnore | ||
private boolean newWallet; | ||
|
||
@Override | ||
@Transient | ||
@JsonIgnore | ||
public boolean isNew() { | ||
return this.newWallet || id == null; | ||
} | ||
|
||
public Wallet setAsNew() { | ||
this.newWallet = true; | ||
return this; | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
src/test/java/com/bifurcated/wallet/WalletApplicationTests.java
This file was deleted.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
src/test/java/com/bifurcated/wallet/controller/WalletControllerTest.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,102 @@ | ||
package com.bifurcated.wallet.controller; | ||
|
||
import com.bifurcated.wallet.data.Wallet; | ||
import com.bifurcated.wallet.data.WalletRepo; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@ActiveProfiles("test") | ||
class WalletControllerTest { | ||
|
||
private static final String END_POINT_PATH = "/api/v1"; | ||
@Autowired | ||
private MockMvc mockMvc; | ||
@Autowired | ||
private ObjectMapper objectMapper; | ||
@Autowired | ||
private WalletRepo repository; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
repository.deleteAll(); | ||
Wallet wallet1 = new Wallet(); | ||
wallet1.setId(UUID.fromString("b3919077-79e6-4570-bfe0-980ef18f3731")); | ||
wallet1.setAmount(2000F); | ||
if (repository.findById(wallet1.getId()).isEmpty()) { | ||
repository.save(wallet1.setAsNew()); | ||
} | ||
Wallet wallet2 = new Wallet(); | ||
wallet2.setId(UUID.fromString("bc10fad7-94f1-4047-be4e-311247eed5fb")); | ||
wallet2.setAmount(500F); | ||
if (repository.findById(wallet2.getId()).isEmpty()) { | ||
repository.save(wallet2.setAsNew()); | ||
} | ||
} | ||
@AfterEach | ||
public void clear() { | ||
repository.deleteAll(); | ||
} | ||
|
||
@Test | ||
public void testWalletDeposit() throws Exception { | ||
record WalletRequest(UUID valletId, String operationType, Float amount){} | ||
record WalletResponse(UUID id, Float amount) {} | ||
UUID id = UUID.fromString("b3919077-79e6-4570-bfe0-980ef18f3731"); | ||
WalletRequest walletRequest = new WalletRequest( | ||
id, "DEPOSIT", 1000F); | ||
WalletResponse walletResponse = new WalletResponse( | ||
id, 3000F | ||
); | ||
String requestBody = objectMapper.writeValueAsString(walletRequest); | ||
String response = objectMapper.writeValueAsString(walletResponse); | ||
this.mockMvc.perform(post(END_POINT_PATH+"/wallet") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(requestBody)) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json(response)); | ||
} | ||
@Test | ||
public void testWalletDepositNotFound() throws Exception { | ||
record WalletRequest(UUID valletId, String operationType, Float amount){} | ||
WalletRequest walletRequest = new WalletRequest( | ||
UUID.randomUUID(), "DEPOSIT", 1000F); | ||
String requestBody = objectMapper.writeValueAsString(walletRequest); | ||
this.mockMvc.perform(post(END_POINT_PATH+"/wallet") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(requestBody)) | ||
.andDo(print()) | ||
.andExpect(status().isNotFound()); | ||
} | ||
|
||
@Test | ||
public void testWalletGetAmount() throws Exception { | ||
record BalanceResponse(Float amount){} | ||
BalanceResponse balanceResponse = new BalanceResponse(2000F); | ||
String response = objectMapper.writeValueAsString(balanceResponse); | ||
String id = "b3919077-79e6-4570-bfe0-980ef18f3731"; | ||
this.mockMvc.perform(get(END_POINT_PATH+"/wallets/"+id)) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json(response)); | ||
} | ||
} |
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,11 @@ | ||
server: | ||
port: 8000 | ||
|
||
spring: | ||
datasource: | ||
url: jdbc:postgresql://${POSTGRES_HOST:localhost}:${POSTGRES_PORT:5432}/${POSTGRES_DB:wallet_test} | ||
username: ${POSTGRES_NAME:postgres} | ||
password: ${POSTGRES_PASSWORD:123} | ||
liquibase: | ||
enabled: true | ||
contexts: test |