Skip to content

Commit

Permalink
Add test for api
Browse files Browse the repository at this point in the history
  • Loading branch information
bifrurcated committed Jan 13, 2024
1 parent 0d1b10d commit cfffc6a
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 23 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/com/bifurcated/wallet/data/Wallet.java
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 src/test/java/com/bifurcated/wallet/WalletApplicationTests.java

This file was deleted.

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));
}
}
11 changes: 11 additions & 0 deletions src/test/resources/application-test.yml
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

0 comments on commit cfffc6a

Please sign in to comment.