Skip to content

Commit 687cc73

Browse files
committed
Controller+Resource+Repository Tests
1 parent 6e9deb0 commit 687cc73

File tree

5 files changed

+284
-38
lines changed

5 files changed

+284
-38
lines changed

remsfal-service/src/main/java/de/remsfal/service/control/CommercialController.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import jakarta.ws.rs.NotFoundException;
1212
import org.jboss.logging.Logger;
1313

14-
import java.util.List;
15-
1614
/**
1715
* Controller for managing Commercial units.
1816
*/
@@ -49,7 +47,7 @@ public CommercialModel getCommercial(final String projectId,
4947
final String buildingId, final String commercialId) {
5048
logger.infov("Retrieving a commercial (projectId={0}, buildingId={1}, commercialId={2})",
5149
projectId, buildingId, commercialId);
52-
CommercialEntity entity = commercialRepository.findByIdOptional(commercialId)
50+
CommercialEntity entity = commercialRepository.findCommercialById(projectId, buildingId,commercialId)
5351
.orElseThrow(() -> new NotFoundException("Commercial not exist"));
5452

5553
if (!entity.getProjectId().equals(projectId)) {

remsfal-service/src/test/java/de/remsfal/service/TestData.java

+21
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,27 @@ public static final ImmutableCommercialJson.Builder commercialBuilder1() {
368368
.heatingSpace(COMMERCIAL_HEATING_SPACE_1);
369369
}
370370

371+
// Test commercial 1
372+
public static final String COMMERCIAL_ID_2 = "b9440c43-b5c0-4951-9c23-000000000002";
373+
public static final String COMMERCIAL_TITLE_2 = "Bäckerei Ekpmel";
374+
public static final String COMMERCIAL_LOCATION_2 = "EG rechts";
375+
public static final String COMMERCIAL_DESCRIPTION_2 = "Bäckerei mit Tischen hinter dem Haus";
376+
public static final Float COMMERCIAL_COMMERCIAL_SPACE_2 = 450.92f;
377+
public static final Float COMMERCIAL_USABLE_SPACE_2 = 100.9f;
378+
public static final Float COMMERCIAL_HEATING_SPACE_2 = 134.27f;
379+
380+
public static final ImmutableCommercialJson.Builder commercialBuilder2() {
381+
return ImmutableCommercialJson
382+
.builder()
383+
.id(COMMERCIAL_ID_2)
384+
.title(COMMERCIAL_TITLE_2)
385+
.location(COMMERCIAL_LOCATION_2)
386+
.description(COMMERCIAL_DESCRIPTION_2)
387+
.commercialSpace(COMMERCIAL_COMMERCIAL_SPACE_2)
388+
.usableSpace(COMMERCIAL_USABLE_SPACE_2)
389+
.heatingSpace(COMMERCIAL_HEATING_SPACE_2);
390+
}
391+
371392
// Default test garage
372393
public static final String GARAGE_ID = TestData.GARAGE_ID_1;
373394
public static final String GARAGE_TITLE = TestData.GARAGE_TITLE_1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package de.remsfal.service.boundary.project;
2+
3+
import de.remsfal.service.TestData;
4+
import io.quarkus.test.junit.QuarkusTest;
5+
import jakarta.ws.rs.core.MediaType;
6+
import jakarta.ws.rs.core.Response;
7+
import org.hamcrest.Matchers;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.params.ParameterizedTest;
11+
import org.junit.jupiter.params.provider.ValueSource;
12+
13+
import java.time.Duration;
14+
15+
import static io.restassured.RestAssured.given;
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
18+
@QuarkusTest
19+
class CommercialResourceTest extends AbstractProjectResourceTest {
20+
21+
static final String BASE_PATH = "/api/v1/projects/{projectId}/properties/{propertyId}/" +
22+
"buildings/{buildingId}/commercials";
23+
24+
@Override
25+
@BeforeEach
26+
protected void setupTestProjects() {
27+
super.setupTestUsers();
28+
super.setupTestProjects();
29+
super.setupTestProperties();
30+
}
31+
32+
protected void setupTestCommercial() {
33+
runInTransaction(() -> entityManager
34+
.createNativeQuery("INSERT INTO BUILDING (ID, PROPERTY_ID, PROJECT_ID, ADDRESS_ID, TITLE)" +
35+
" VALUES (?,?,?,?,?)")
36+
.setParameter(1, TestData.BUILDING_ID)
37+
.setParameter(2, TestData.PROPERTY_ID)
38+
.setParameter(3, TestData.PROJECT_ID)
39+
.setParameter(4, TestData.ADDRESS_ID)
40+
.setParameter(5, TestData.COMMERCIAL_TITLE)
41+
.executeUpdate());
42+
runInTransaction(() -> entityManager
43+
.createNativeQuery("INSERT INTO COMMERCIAL (ID, BUILDING_ID, PROJECT_ID, " +
44+
"LOCATION, COMMERCIAL_SPACE,HEATING_SPACE, TITLE, DESCRIPTION, USABLE_SPACE) VALUES (?,?,?,?,?,?,?,?,?)")
45+
.setParameter(1, TestData.COMMERCIAL_ID)
46+
.setParameter(2, TestData.BUILDING_ID)
47+
.setParameter(3, TestData.PROJECT_ID)
48+
.setParameter(4, TestData.COMMERCIAL_LOCATION)
49+
.setParameter(5, TestData.COMMERCIAL_COMMERCIAL_SPACE)
50+
.setParameter(6, TestData.COMMERCIAL_HEATING_SPACE)
51+
.setParameter(7, TestData.COMMERCIAL_TITLE)
52+
.setParameter(8, TestData.COMMERCIAL_DESCRIPTION)
53+
.setParameter(9, TestData.COMMERCIAL_USABLE_SPACE)
54+
.executeUpdate());
55+
}
56+
57+
@Test
58+
void getCommercial_FAILED_noAuthentication() {
59+
given()
60+
.when()
61+
.get(BASE_PATH + "/{commercialId}", TestData.PROJECT_ID, TestData.PROPERTY_ID,
62+
TestData.BUILDING_ID, TestData.COMMERCIAL_ID)
63+
.then()
64+
.statusCode(Response.Status.UNAUTHORIZED.getStatusCode());
65+
}
66+
67+
@Test
68+
void getCommercialSuccessfully() {
69+
setupTestCommercial();
70+
given()
71+
.when()
72+
.cookie(buildCookie(TestData.USER_ID, TestData.USER_EMAIL, Duration.ofMinutes(10)))
73+
.get(BASE_PATH + "/{commercialId}", TestData.PROJECT_ID, TestData.PROPERTY_ID,
74+
TestData.BUILDING_ID, TestData.COMMERCIAL_ID)
75+
.then()
76+
.statusCode(Response.Status.OK.getStatusCode())
77+
.contentType(MediaType.APPLICATION_JSON)
78+
.and().body("id", Matchers.equalTo(TestData.COMMERCIAL_ID))
79+
.and().body("title", Matchers.equalTo(TestData.COMMERCIAL_TITLE))
80+
.and().body("description", Matchers.equalTo(TestData.COMMERCIAL_DESCRIPTION))
81+
.and().body("commercialSpace", Matchers.equalTo(TestData.COMMERCIAL_COMMERCIAL_SPACE))
82+
.and().body("usableSpace", Matchers.equalTo(TestData.COMMERCIAL_USABLE_SPACE))
83+
.and().body("heatingSpace", Matchers.equalTo(TestData.COMMERCIAL_HEATING_SPACE))
84+
.and().body("location", Matchers.equalTo(TestData.COMMERCIAL_LOCATION));
85+
}
86+
87+
@ParameterizedTest
88+
@ValueSource(strings = "{ \"title\":\"" + TestData.COMMERCIAL_TITLE_2 + "\"}")
89+
void createCommercialSuccessfully(String json) {
90+
setupTestCommercial();
91+
given()
92+
.when()
93+
.cookie(buildCookie(TestData.USER_ID, TestData.USER_EMAIL, Duration.ofMinutes(10)))
94+
.contentType(MediaType.APPLICATION_JSON)
95+
.body(json)
96+
.post(BASE_PATH, TestData.PROJECT_ID, TestData.PROPERTY_ID, TestData.BUILDING_ID)
97+
.then()
98+
.statusCode(Response.Status.CREATED.getStatusCode())
99+
.contentType(MediaType.APPLICATION_JSON)
100+
.header("location", Matchers.containsString(BASE_PATH.replace("{projectId}", TestData.PROJECT_ID)
101+
.replace("{propertyId}", TestData.PROPERTY_ID)
102+
.replace("{buildingId}", TestData.BUILDING_ID) + "/"))
103+
.and().body("id", Matchers.notNullValue())
104+
.and().body("title", Matchers.equalTo(TestData.COMMERCIAL_TITLE_2));
105+
106+
long entities = entityManager
107+
.createQuery("SELECT count(commercial) FROM CommercialEntity commercial where commercial.title = :title", long.class)
108+
.setParameter("title", TestData.COMMERCIAL_TITLE_2)
109+
.getSingleResult();
110+
assertEquals(1, entities);
111+
}
112+
113+
@ParameterizedTest
114+
@ValueSource(strings = "{ \"title\":\"" + TestData.COMMERCIAL_TITLE_2 + "\"}")
115+
void updateCommercialSuccessfully(final String json) {
116+
setupTestCommercial();
117+
118+
given()
119+
.when()
120+
.cookie(buildCookie(TestData.USER_ID, TestData.USER_EMAIL, Duration.ofMinutes(10)))
121+
.contentType(MediaType.APPLICATION_JSON)
122+
.body(json)
123+
.patch(BASE_PATH + "/{commercialId}", TestData.PROJECT_ID, TestData.PROPERTY_ID,
124+
TestData.BUILDING_ID, TestData.COMMERCIAL_ID)
125+
.then()
126+
.statusCode(Response.Status.OK.getStatusCode())
127+
.contentType(MediaType.APPLICATION_JSON)
128+
.and().body("id", Matchers.equalTo(TestData.COMMERCIAL_ID))
129+
.and().body("title", Matchers.equalTo(TestData.COMMERCIAL_TITLE_2))
130+
.and().body("description", Matchers.equalTo(TestData.COMMERCIAL_DESCRIPTION))
131+
.and().body("commercialSpace", Matchers.equalTo(TestData.COMMERCIAL_COMMERCIAL_SPACE))
132+
.and().body("heatingSpace", Matchers.equalTo(TestData.COMMERCIAL_HEATING_SPACE))
133+
.and().body("location", Matchers.equalTo(TestData.COMMERCIAL_LOCATION))
134+
.and().body("usableSpace", Matchers.equalTo(TestData.COMMERCIAL_USABLE_SPACE));
135+
136+
given()
137+
.when()
138+
.cookie(buildCookie(TestData.USER_ID, TestData.USER_EMAIL, Duration.ofMinutes(10)))
139+
.contentType(MediaType.APPLICATION_JSON)
140+
.get(BASE_PATH + "/{commercialId}", TestData.PROJECT_ID, TestData.PROPERTY_ID,
141+
TestData.BUILDING_ID, TestData.COMMERCIAL_ID)
142+
.then()
143+
.statusCode(Response.Status.OK.getStatusCode())
144+
.contentType(MediaType.APPLICATION_JSON)
145+
.and().body("id", Matchers.equalTo(TestData.COMMERCIAL_ID))
146+
.and().body("title", Matchers.equalTo(TestData.COMMERCIAL_TITLE_2));
147+
}
148+
149+
@Test
150+
void deleteCommercialSuccessfully() {
151+
setupTestCommercial();
152+
153+
given()
154+
.when()
155+
.cookie(buildCookie(TestData.USER_ID, TestData.USER_EMAIL, Duration.ofMinutes(10)))
156+
.delete(BASE_PATH + "/{commercialId}", TestData.PROJECT_ID, TestData.PROPERTY_ID,
157+
TestData.BUILDING_ID, TestData.COMMERCIAL_ID)
158+
.then()
159+
.statusCode(Response.Status.NO_CONTENT.getStatusCode());
160+
161+
given()
162+
.when()
163+
.cookie(buildCookie(TestData.USER_ID, TestData.USER_EMAIL, Duration.ofMinutes(10)))
164+
.get(BASE_PATH + "/{commercialId}", TestData.PROJECT_ID, TestData.PROPERTY_ID,
165+
TestData.BUILDING_ID, TestData.COMMERCIAL_ID)
166+
.then()
167+
.statusCode(Response.Status.NOT_FOUND.getStatusCode());
168+
}
169+
}

remsfal-service/src/test/java/de/remsfal/service/control/CommercialControllerTest.java

+12-35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package de.remsfal.service.control;
22

3-
import de.remsfal.core.model.project.ApartmentModel;
43
import de.remsfal.core.model.project.CommercialModel;
54
import de.remsfal.service.AbstractTest;
65
import de.remsfal.service.TestData;
@@ -10,10 +9,7 @@
109
import org.junit.jupiter.api.BeforeEach;
1110
import org.junit.jupiter.api.Test;
1211

13-
import static org.junit.jupiter.api.Assertions.assertEquals;
14-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
15-
import static org.junit.jupiter.api.Assertions.assertNotNull;
16-
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
import static org.junit.jupiter.api.Assertions.*;
1713

1814
@QuarkusTest
1915
class CommercialControllerTest extends AbstractTest {
@@ -36,7 +32,6 @@ void setupTestProjects() {
3632
.executeUpdate());
3733
}
3834

39-
4035
@Test
4136
void createCommercial_SUCCESS_getCommercial() {
4237
final String propertyId = propertyController
@@ -60,13 +55,12 @@ void createCommercial_SUCCESS_getCommercial() {
6055
assertNotEquals(commercial.getId(), result.getId());
6156
assertEquals(commercial.getTitle(), result.getTitle());
6257
assertEquals(commercial.getLocation(), result.getLocation());
63-
assertEquals(commercial.getDescription(), result.getDescription());
6458
assertEquals(commercial.getCommercialSpace(), result.getCommercialSpace());
65-
assertEquals(commercial.getUsableSpace(), result.getUsableSpace());
59+
assertEquals(commercial.getHeatingSpace(), result.getHeatingSpace());
6660

6761
final String commercialId = entityManager
68-
.createQuery("SELECT a.id FROM CommercialEntity a where a.title = :title", String.class)
69-
.setParameter("title", TestData.APARTMENT_TITLE)
62+
.createQuery("SELECT c.id FROM CommercialEntity c where c.title = :title", String.class)
63+
.setParameter("title", TestData.COMMERCIAL_TITLE)
7064
.getSingleResult();
7165
assertEquals(result.getId(), commercialId);
7266

@@ -96,16 +90,9 @@ void createCommercial_SUCCESS_deleteCommercial() {
9690
final CommercialModel result = commercialController
9791
.createCommercial(TestData.PROJECT_ID, buildingId, commercial);
9892

99-
assertNotEquals(commercial.getId(), result.getId());
100-
assertEquals(commercial.getTitle(), result.getTitle());
101-
assertEquals(commercial.getLocation(), result.getLocation());
102-
assertEquals(commercial.getDescription(), result.getDescription());
103-
assertEquals(commercial.getCommercialSpace(), result.getCommercialSpace());
104-
assertEquals(commercial.getUsableSpace(), result.getUsableSpace());
105-
10693
final String commercialId = entityManager
107-
.createQuery("SELECT a.id FROM CommercialEntity a where a.title = :title", String.class)
108-
.setParameter("title", TestData.APARTMENT_TITLE)
94+
.createQuery("SELECT c.id FROM CommercialEntity c where c.title = :title", String.class)
95+
.setParameter("title", TestData.COMMERCIAL_TITLE)
10996
.getSingleResult();
11097
assertEquals(result.getId(), commercialId);
11198

@@ -133,32 +120,22 @@ void createCommercial_SUCCESS_updateCommercial() {
133120
final CommercialModel result = commercialController
134121
.createCommercial(TestData.PROJECT_ID, buildingId, commercial);
135122

136-
assertNotEquals(commercial.getId(), result.getId());
137-
assertEquals(commercial.getTitle(), result.getTitle());
138-
assertEquals(commercial.getLocation(), result.getLocation());
139-
assertEquals(commercial.getDescription(), result.getDescription());
140-
assertEquals(commercial.getCommercialSpace(), result.getCommercialSpace());
141-
assertEquals(commercial.getUsableSpace(), result.getUsableSpace());
142-
143123
final String commercialId = entityManager
144-
.createQuery("SELECT a.id FROM CommercialEntity a where a.title = :title", String.class)
145-
.setParameter("title", TestData.APARTMENT_TITLE)
124+
.createQuery("SELECT c.id FROM CommercialEntity c where c.title = :title", String.class)
125+
.setParameter("title", TestData.COMMERCIAL_TITLE)
146126
.getSingleResult();
147127
assertEquals(result.getId(), commercialId);
148128

149-
final CommercialModel updateTo = TestData.commercialBuilder1().build();
129+
final CommercialModel updateTo = TestData.commercialBuilder2().build();
150130

151131
final CommercialModel updated = commercialController.updateCommercial(TestData.PROJECT_ID, buildingId,
152132
commercialId, updateTo);
153133

154134
assertNotEquals(updateTo.getId(), updated.getId());
155-
assertEquals(updateTo.getDescription(), updated.getDescription());
156-
assertEquals(updateTo.getCommercialSpace(), updated.getCommercialSpace());
157-
assertEquals(updateTo.getUsableSpace(), updated.getUsableSpace());
135+
assertEquals(updateTo.getTitle(), updated.getTitle());
158136
assertEquals(updateTo.getLocation(), updated.getLocation());
137+
assertEquals(updateTo.getCommercialSpace(), updated.getCommercialSpace());
159138
assertEquals(updateTo.getHeatingSpace(), updated.getHeatingSpace());
160-
assertEquals(updateTo.getTitle(), updated.getTitle());
161139
assertEquals(commercial.getTenancy(), updated.getTenancy());
162140
}
163-
164-
}
141+
}

0 commit comments

Comments
 (0)