Skip to content

Commit 9fd9a90

Browse files
authored
Merge pull request #17 from mcadecio/support-delete-rows
Implemented endpoint to delete rows from table
2 parents 866df73 + 461d3cc commit 9fd9a90

File tree

26 files changed

+614
-187
lines changed

26 files changed

+614
-187
lines changed

component-tests/src/test/java/com/dercio/database_proxy/budgets/BudgetsRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.dercio.database_proxy.budgets;
22

3+
import java.util.List;
4+
35
public interface BudgetsRepository {
46

7+
List<Budget> find();
8+
59
Budget findById(long id);
610

711
void save(Budget budget);
Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.dercio.database_proxy.budgets;
22

3+
import com.dercio.database_proxy.common.RestService;
34
import com.dercio.database_proxy.common.mapper.Mapper;
45
import com.google.inject.Inject;
56
import io.restassured.http.ContentType;
@@ -8,71 +9,18 @@
89

910
import static io.restassured.RestAssured.given;
1011

11-
@RequiredArgsConstructor(onConstructor_ = @Inject)
12-
public class BudgetsService {
12+
public class BudgetsService extends RestService {
1313

14-
private final Mapper mapper;
1514
private static final String BASE_URI = "http://localhost:8000";
1615
private static final String BUDGETS = "/budgets/";
1716

18-
public Response getBudgets() {
19-
return given()
20-
.baseUri(BASE_URI)
21-
.accept(ContentType.JSON)
22-
.log()
23-
.all(true)
24-
.get(BUDGETS)
25-
.prettyPeek();
26-
}
27-
28-
public Response getBudgetById(long id) {
29-
return given()
30-
.baseUri(BASE_URI)
31-
.accept(ContentType.JSON)
32-
.log()
33-
.all(true)
34-
.get(BUDGETS + id)
35-
.prettyPeek();
36-
}
37-
38-
public Response getBudgetById(String id) {
39-
return given()
40-
.baseUri(BASE_URI)
41-
.accept(ContentType.JSON)
42-
.log()
43-
.all(true)
44-
.get(BUDGETS + id)
45-
.prettyPeek();
17+
@Inject
18+
public BudgetsService(Mapper mapper) {
19+
super(BASE_URI, BUDGETS, mapper);
4620
}
4721

4822
public Response createBudget(Budget budget) {
49-
return given()
50-
.baseUri(BASE_URI)
51-
.contentType(ContentType.JSON)
52-
.body(mapper.encode(budget))
53-
.log()
54-
.all(true)
55-
.post(BUDGETS)
56-
.prettyPeek();
23+
return create(budget);
5724
}
5825

59-
public Response deleteBudget(Long id) {
60-
return given()
61-
.baseUri(BASE_URI)
62-
.log()
63-
.all(true)
64-
.delete(BUDGETS + id)
65-
.prettyPeek();
66-
}
67-
68-
public Response updateBudget(Long originalId, Budget budget) {
69-
return given()
70-
.baseUri(BASE_URI)
71-
.contentType(ContentType.JSON)
72-
.body(mapper.encode(budget))
73-
.log()
74-
.all(true)
75-
.put(BUDGETS + originalId)
76-
.prettyPeek();
77-
}
7826
}

component-tests/src/test/java/com/dercio/database_proxy/budgets/steps/DeleteBudgetsSteps.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import org.mybatis.guice.transactional.Transactional;
1313

1414
import java.util.List;
15+
import java.util.Map;
1516

1617
import static com.dercio.database_proxy.budgets.BudgetsFactory.createJanuaryBudget;
1718
import static org.hamcrest.Matchers.equalTo;
1819
import static org.hamcrest.Matchers.notNullValue;
1920
import static org.junit.jupiter.api.Assertions.assertNull;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2022

2123
@ScenarioScoped
2224
public class DeleteBudgetsSteps {
@@ -47,7 +49,7 @@ public void theBudgetIPreviouslyCreatedIsNoLongerValid() {
4749

4850
@When("I delete the budget")
4951
public void iDeleteTheBudget() {
50-
response = budgetsService.deleteBudget(budgets.get(0).getId());
52+
response = budgetsService.deleteById(budgets.get(0).getId());
5153
}
5254

5355
@Then("the budget should be deleted")
@@ -59,7 +61,20 @@ public void theBudgetShouldBeDeleted() {
5961

6062
@When("I delete a budget that does not exist")
6163
public void iDeleteABudgetThatDoesNotExist() {
62-
response = budgetsService.deleteBudget(573489L);
64+
response = budgetsService.deleteById(573489L);
65+
}
66+
67+
@When("I delete the budgets by month {int}")
68+
public void iDeleteTheBudgetsByMonth(int month) {
69+
response = budgetsService.delete(Map.of("month", month));
70+
}
71+
72+
@Then("bugdgets with month {int} should not exist")
73+
public void budgetsWithMonthDoNotExist(int month) {
74+
boolean result = budgetsRepository.find()
75+
.stream()
76+
.noneMatch(budget -> budget.getMonth().equals(month));
77+
assertTrue(result);
6378
}
6479

6580
@Then("I should be alerted that the budget does not exist")
@@ -72,4 +87,13 @@ public void iShouldBeAlertedThatTheBudgetDoesNotExist() {
7287
.body("code", equalTo(404));
7388
}
7489

90+
@Then("I should be alerted that no budgets exist")
91+
public void iShouldBeAlertedThatNoBudgetsExist() {
92+
response.then()
93+
.statusCode(404)
94+
.body("timestamp", notNullValue())
95+
.body("path", equalTo("/budgets/"))
96+
.body("message", equalTo("Not Found"))
97+
.body("code", equalTo(404));
98+
}
7599
}

component-tests/src/test/java/com/dercio/database_proxy/budgets/steps/GetBudgetsSteps.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void aListOfBudgetsExists() {
5757

5858
@When("I retrieve all the budgets")
5959
public void iRetrieveAllTheBudgets() {
60-
response = budgetsService.getBudgets();
60+
response = budgetsService.getAll();
6161
}
6262

6363
@Then("I should see all the budgets")
@@ -73,7 +73,7 @@ public void iShouldSeeAllTheBudgets() {
7373

7474
@When("I retrieve a budget with id {int}")
7575
public void iRetrieveABudgetWithId(int id) {
76-
response = budgetsService.getBudgetById(id);
76+
response = budgetsService.getById(id);
7777
}
7878

7979
@Then("I should see the budget")
@@ -96,7 +96,7 @@ public void iShouldGetANotFoundErrorMessage() {
9696

9797
@When("I retrieve a budget with an invalid id")
9898
public void iRetrieveABudgetWithAnInvalidId() {
99-
response = budgetsService.getBudgetById("INVALID");
99+
response = budgetsService.getById("INVALID");
100100
}
101101

102102
@Then("I should get a validation error message")

component-tests/src/test/java/com/dercio/database_proxy/budgets/steps/UpdateBudgetsSteps.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void iUpdateTheMonthToNoValue() {
5050

5151
budget.setMonth(null);
5252

53-
response = budgetsService.updateBudget(budget.getId(), budget);
53+
response = budgetsService.update(budget.getId(), budget);
5454
}
5555

5656
@Then("I should be alerted that the month is a required field")
@@ -69,7 +69,7 @@ public void iUpdateTheUserIdField() {
6969

7070
budget.setUserId("something-else");
7171

72-
response = budgetsService.updateBudget(budget.getId(), budget);
72+
response = budgetsService.update(budget.getId(), budget);
7373
}
7474

7575
@Then("I should see the new user id in the budget")
@@ -89,7 +89,7 @@ public void iUpdateTheYearField() {
8989

9090
budget.setYear(1999);
9191

92-
response = budgetsService.updateBudget(budget.getId(), budget);
92+
response = budgetsService.update(budget.getId(), budget);
9393
}
9494

9595
@Then("I should see the new year in the budget")
@@ -107,7 +107,7 @@ public void iShouldSeeTheNewYearInTheBudget() {
107107
public void iUpdateABudgetThatDoesNotExist() {
108108
var januaryBudget = createJanuaryBudget();
109109

110-
response = budgetsService.updateBudget(januaryBudget.getId(), januaryBudget);
110+
response = budgetsService.update(januaryBudget.getId(), januaryBudget);
111111
}
112112

113113
@Then("I should notified the budget does not exist")

component-tests/src/test/java/com/dercio/database_proxy/common/RestService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dercio.database_proxy.common;
22

33
import com.dercio.database_proxy.common.mapper.Mapper;
4+
import io.cucumber.java.it.Ma;
45
import io.restassured.http.ContentType;
56
import io.restassured.response.Response;
67

@@ -72,6 +73,16 @@ public Response getById(Object firstValue, Object secondValue) {
7273
.prettyPeek();
7374
}
7475

76+
public Response delete(Map<String, Object> filters) {
77+
return given()
78+
.baseUri(baseUri)
79+
.log()
80+
.all(true)
81+
.queryParams(filters)
82+
.delete(path)
83+
.prettyPeek();
84+
}
85+
7586
public Response deleteById(int id) {
7687
return given()
7788
.baseUri(baseUri)
@@ -101,7 +112,7 @@ public Response deleteById(Object firstValue, Object secondValue) {
101112
.prettyPeek();
102113
}
103114

104-
public Response update(int id, Object resorce) {
115+
public Response update(long id, Object resorce) {
105116
return given()
106117
.baseUri(baseUri)
107118
.contentType(ContentType.JSON)

component-tests/src/test/java/com/dercio/database_proxy/students/StudentRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import org.apache.ibatis.annotations.Param;
44

5+
import java.util.List;
6+
57
public interface StudentRepository {
68
Student findById(@Param("name") String name, @Param("age") int age);
79

10+
List<Student> find();
11+
812
void save(Student student);
913

1014
void deleteByNameAndAge(@Param("name") String name, @Param("age") int age);

component-tests/src/test/java/com/dercio/database_proxy/students/steps/DeleteStudentSteps.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import io.cucumber.java.en.When;
88
import lombok.RequiredArgsConstructor;
99

10+
import java.util.Map;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNull;
14+
1015
@RequiredArgsConstructor(onConstructor_ = @Inject)
1116
public class DeleteStudentSteps {
1217
private final StudentContext studentContext;
@@ -24,11 +29,23 @@ public void iDeleteAStudentNamedAndAge() {
2429
studentContext.setResponse(studentService.deleteById(student.name(), student.age()));
2530
}
2631

32+
@When("I delete all students")
33+
public void iDeleteAllStudents() {
34+
studentContext.setResponse(studentService.delete(Map.of()));
35+
}
36+
2737
@Then("the student should be deleted")
2838
public void theStudentShouldBeDeleted() {
2939
var student = studentContext.getStudents().get(0);
3040
var response = studentContext.getResponse();
3141
response.then().statusCode(204);
32-
studentRepository.findById(student.name(), student.age());
42+
assertNull(studentRepository.findById(student.name(), student.age()));
43+
}
44+
45+
@Then("all students should be deleted")
46+
public void allStudentShouldBeDeleted() {
47+
var response = studentContext.getResponse();
48+
response.then().statusCode(204);
49+
assertEquals(0, studentRepository.find().size());
3350
}
3451
}

component-tests/src/test/resources/batis/cockroach/repositories/StudentRepository.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
44
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
55
<mapper namespace="com.dercio.database_proxy.students.StudentRepository">
6+
<select id="find" resultType="com.dercio.database_proxy.students.Student">
7+
SELECT *
8+
FROM public.students
9+
</select>
610
<select id="findById" resultType="com.dercio.database_proxy.students.Student">
711
SELECT *
812
FROM public.students

component-tests/src/test/resources/batis/postgres/repositories/BudgetsRepository.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
FROM money.budgets
99
WHERE id = #{id}
1010
</select>
11+
<select id="find" resultType="com.dercio.database_proxy.budgets.Budget">
12+
SELECT *
13+
FROM money.budgets
14+
</select>
1115
<insert id="save"
1216
parameterType="com.dercio.database_proxy.budgets.Budget">
1317
INSERT INTO money.budgets(id,

0 commit comments

Comments
 (0)