-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContractControllerTest.java
99 lines (84 loc) · 2.98 KB
/
ContractControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.bspicinini.controller;
import java.math.BigDecimal;
import io.quarkus.test.TestTransaction;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.bspicinini.repository.ContractRepository;
import com.bspicinini.repository.CustomerRepository;
import com.bspicinini.repository.entity.Contract;
import com.bspicinini.repository.entity.ContractStatusEnum;
import com.bspicinini.repository.entity.Customer;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import static org.hamcrest.Matchers.*;
@QuarkusTest
class ContractControllerTest {
@Inject
ContractRepository contractRepository;
@Inject
CustomerRepository customerRepository;
@PersistenceContext
EntityManager entityManager;
@BeforeEach
@Transactional
void setup() {
var customer = new Customer();
customer.setName("John Doe");
customer.setEmail("johndoe@example.com");
customerRepository.persist(customer);
var contract = new Contract();
contract.setStatus(ContractStatusEnum.ACTIVE);
contract.setAmount(BigDecimal.valueOf(1000.0));
contract.setInstallments(12);
contract.setCustomer(customer);
contractRepository.persist(contract);
}
@AfterEach
@Transactional
void tearDown() {
contractRepository.deleteAll();
customerRepository.deleteAll();
resetSequences();
}
@Transactional
void resetSequences() {
entityManager.createNativeQuery("ALTER SEQUENCE contract_sequence RESTART WITH 1").executeUpdate();
entityManager.createNativeQuery("ALTER SEQUENCE customer_sequence RESTART WITH 1").executeUpdate();
}
@Test
@TestTransaction
void givenAValidRequest_whenGetContractById_thenReturnAContract() {
RestAssured.given().contentType(ContentType.JSON).pathParam("id", 1)
.when().get("/contracts/{id}")
.then()
.statusCode(200)
.body("id", is(1))
.body("amount", notNullValue());
}
@Test
@TestTransaction
void givenAValidRequest_whenCreateContract_thenReturnCreatedContract() {
String contractJson = """
{
"amount": 2000.0,
"installments": 24,
"customerId": 1
}
""";
RestAssured.given()
.contentType(ContentType.JSON)
.body(contractJson)
.when().post("/contracts")
.then()
.statusCode(201)
.body("amount", is(2000.0f))
.body("installments", is(24))
.body("customerId", is(1));
}
}