Skip to content

Commit 6d3f6b6

Browse files
committed
add system test for Rest API
1 parent 34d01bb commit 6d3f6b6

File tree

26 files changed

+268
-6
lines changed

26 files changed

+268
-6
lines changed

how-to-use-junit5-javase/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-javase/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-javase/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-javase/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-maven/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-maven/.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-maven/.idea/compiler.xml

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-maven/.idea/encodings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-maven/.idea/jarRepositories.xml

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-maven/.idea/misc.xml

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

how-to-use-junit5-maven/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-java9-module-system/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-java9-module-system/.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-java9-module-system/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-java9-module-system/.idea/modules.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-java9-module-system/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-junit5-features/.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-junit5-features/.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-junit5-features/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-junit5-features/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

study-junit5-features/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.example.banking.config;
2+
3+
import com.example.banking.core.application.CustomerApplication;
4+
import com.example.banking.core.application.impl.BusinessCustomerApplication;
5+
import com.example.banking.core.repository.CustomerRepository;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class AppConfig {
11+
12+
@Bean
13+
public CustomerApplication customerApplication(CustomerRepository customerRepository){
14+
return new BusinessCustomerApplication(customerRepository);
15+
}
16+
}

study-tdd/banking-rest-api/src/main/java/com/example/banking/controller/CustomerApplicationController.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ public CustomerApplicationController(CustomerApplication customerApplication) {
2525
@PostMapping("{identity}")
2626
public TransferResponse transfer(@PathVariable String identity,
2727
@RequestBody TransferRequest request){
28-
boolean success = customerApplication.transferMoney(TcKimlikNo.of(identity),
29-
Iban.of(request.getFromIban()),
30-
Iban.of(request.getToIban()),
31-
Money.of(request.getAmount(), MoneyCurrency.valueOf(request.getFromCurrency()))
32-
);
28+
boolean success = false;
29+
try{
30+
success = customerApplication.transferMoney(TcKimlikNo.of(identity),
31+
Iban.of(request.getFromIban()),
32+
Iban.of(request.getToIban()),
33+
Money.of(request.getAmount(), MoneyCurrency.valueOf(request.getFromCurrency()))
34+
);
35+
} catch (IllegalArgumentException e){ }
3336
return new TransferResponse(success ? "Ok" : "Failed");
3437
}
3538
}

study-tdd/banking-rest-api/src/main/java/com/example/banking/repository/CustomerMongoRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CustomerMongoRepository implements CustomerRepository {
2020
@Override
2121
public Optional<Customer> findCustomerByIdentity(TcKimlikNo identity) {
2222
Optional<CustomerDocument> document = repository.findOneByIdentity(identity.getValue());
23-
if (!document.isPresent()) Optional.empty();
23+
if (!document.isPresent()) return Optional.empty();
2424
Customer customer = new Customer(identity, document.get().getFullname());
2525
document.get().getAccounts().forEach(accountDocument -> {
2626
customer.addAccount(new Account(Iban.of(accountDocument.getIban()), Money.of(accountDocument.getBalance(), accountDocument.getCurrency())));

study-tdd/banking-rest-api/src/test/java/com/example/banking/CustomerRepositoryTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ void findCustomerByIdentityShouldReturnOne(){
4747
);
4848
}
4949

50+
@Test
51+
void findCustomerByIdentityShouldFail(){
52+
var identity = TcKimlikNo.of("94534403888");
53+
var customer = repository.findCustomerByIdentity(identity);
54+
assertFalse(customer.isPresent());
55+
}
56+
5057
@AfterEach
5158
void dropCustomerCollection(){
5259
// drop collection
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.example.banking;
2+
3+
import com.example.banking.core.domain.*;
4+
import com.example.banking.dto.TransferRequest;
5+
import com.example.banking.repository.CustomerMongoRepository;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.http.MediaType;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
15+
import static org.hamcrest.CoreMatchers.is;
16+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19+
20+
@SpringBootTest(
21+
webEnvironment = SpringBootTest.WebEnvironment.MOCK,
22+
classes = BankingRestApiApplication.class
23+
)
24+
@AutoConfigureMockMvc
25+
public class RestApiSystemTest {
26+
27+
@Autowired
28+
MockMvc mvc;
29+
30+
@Autowired
31+
ObjectMapper mapper;
32+
33+
@Autowired
34+
CustomerMongoRepository repository;
35+
36+
@BeforeEach
37+
void createTestCustomer() {
38+
var customer = new Customer(TcKimlikNo.of("84419540678"),
39+
"Jack Bauer");
40+
customer.addAccount(new Account(Iban.of("TR150006288538595565372934"), Money.of(1_000_000., MoneyCurrency.TL)));
41+
customer.addAccount(new Account(Iban.of("TR640006273424738347266155"), Money.of(2_000_000., MoneyCurrency.TL)));
42+
repository.save(customer);
43+
}
44+
45+
@Test
46+
void transferMoneyBetweenAccountShouldSuccess() throws Throwable {
47+
TransferRequest request =
48+
new TransferRequest("TR150006288538595565372934","TL",
49+
"TR640006273424738347266155","TL",100_000.);
50+
mvc.perform(post("/transfers/84419540678")
51+
.contentType(MediaType.APPLICATION_JSON)
52+
.content(mapper.writeValueAsString(request))) // exercise method
53+
.andExpect(status().isOk())
54+
.andExpect(jsonPath("status",is("Ok")));
55+
}
56+
57+
@Test
58+
void transferMoneyBetweenAccountShouldFail() throws Throwable {
59+
TransferRequest request =
60+
new TransferRequest("TR150006288538595565372934","TL",
61+
"TR640006273424738347266155","TL",100_000.);
62+
mvc.perform(post("/transfers/27164342098")
63+
.contentType(MediaType.APPLICATION_JSON)
64+
.content(mapper.writeValueAsString(request))) // exercise method
65+
.andExpect(status().isOk())
66+
.andExpect(jsonPath("status",is("Failed")));
67+
}
68+
}

0 commit comments

Comments
 (0)