added SyncControllerTests

This commit is contained in:
2026-07-19 16:07:47 +02:00
parent 1e0a5c0af1
commit 87108a9ebd
4 changed files with 175 additions and 44 deletions
@@ -2,7 +2,14 @@ package com.vaessl.app.search;
import static com.vaessl.app.Mockdata.MOCK_PASS;
import static com.vaessl.app.Mockdata.MOCK_USER;
import static com.vaessl.app.shared.Endpoint.*;
import static com.vaessl.app.Mockdata.VALID_HOMEBOX_ALL_ITEMS_QUERY_RESPONSE;
import static com.vaessl.app.Mockdata.VALID_HOMEBOX_LOGIN_RESPONSE;
import static com.vaessl.app.shared.Endpoint.LOGIN;
import static com.vaessl.app.shared.Endpoint.SEARCH;
import static com.vaessl.app.shared.Endpoint.HOMEBOX_QUERY_ALL_ITEMS;
import static com.vaessl.app.shared.Endpoint.HOMEBOX_LOGIN;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@@ -18,6 +25,11 @@ import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import jakarta.servlet.http.Cookie;
/**
* Integration tests for {@code POST /api/search}, verifying the session-gated
* contract and request validation against a mocked Homebox backend (via WireMock)
* and a real Spring MVC dispatch chain (via {@link MockMvc}).
*/
@SpringBootTest
@AutoConfigureMockMvc
@WireMockTest
@@ -32,47 +44,14 @@ class SearchControllerTest {
private static final String SEARCH_REQUEST = SEARCH.getValue();
private static final String VALID_HOMEBOX_LOGIN_RESPONSE = """
{
"token": "fake-bearer-token",
"attachmentToken": "fake-attach-token",
"expiresAt": "2099-01-01T00:00:00Z"
}
""";
private static final String VALID_HOMEBOX_ALL_ITEMS_QUERY_RESPONSE = """
{
"page": -1,
"pageSize": -1,
"total": 1,
"items": [
{
"id": "c643e7f9-93d0-4b5f-ae4d-e1c2d90389e0",
"assetId": "000-001",
"name": "MacBook Pro A1398",
"description": "Running Linux (Fedora)",
"quantity": 1,
"insured": false,
"archived": false,
"createdAt": "2026-05-13T19:52:20.016176Z",
"updatedAt": "2026-05-14T12:39:11.836403Z",
"purchasePrice": 0,
"parent": {
"id": "b6f60ab8-3a2a-4a8d-a4bf-897d0555f636",
"name": "Server Schrank Ikea weiß",
"description": "Weißer Ikea Schrank, wo sich der Server befindet.",
"createdAt": "2026-05-13T19:55:55.817576Z",
"updatedAt": "2026-05-14T12:37:24.396651Z"
},
"tags": [],
"imageId": "cb3e44d5-ccd4-421e-9f5a-f52cd5f40ca6",
"thumbnailId": "2bfd53fa-1bf1-483c-8d76-7720464532fa",
"soldTime": "0001-01-01T00:00:00Z"
}
]
}
""";
/**
* Logs in against a stubbed Homebox instance, then performs a keyword search using
* the resulting session cookie. Expects {@code 200 OK} with the mocked item mapped
* into the paged response body.
*
* @param wm WireMock runtime info, injected by {@link WireMockTest}, used to point
* the client at the stub server
*/
@Test
void shouldReturnListOfQueriedHomeboxItems(WireMockRuntimeInfo wm) throws Exception {
@@ -93,9 +72,14 @@ class SearchControllerTest {
.andExpect(status().isOk())
.andExpect(jsonPath("$.content[0].title").value("MacBook Pro A1398"))
.andExpect(jsonPath("$.totalElements").value(1))
.andExpect(jsonPath("$.content[0].extraData.locationName").value("Server Schrank Ikea weiß"));
.andExpect(jsonPath("$.content[0].extraData.locationName")
.value("Server Schrank Ikea weiß"));
}
/**
* Calls {@code /api/search} with no session cookie attached and expects
* {@code 401 Unauthorized}, confirming the endpoint is session-gated.
*/
@Test
void shouldReturnUnauthorizedWhenNoSession() throws Exception {
mockMvc.perform(post(SEARCH_REQUEST).contentType(MediaType.APPLICATION_JSON).content("""
@@ -109,6 +93,10 @@ class SearchControllerTest {
""")).andExpect(status().isUnauthorized());
}
/**
* Sends a search request with a {@code serviceType} that doesn't map to any known
* {@link com.vaessl.app.shared.ServiceType} and expects {@code 400 Bad Request}.
*/
@Test
void shouldReturnBadRequestWhenServiceTypeIsInvalid() throws Exception {
mockMvc.perform(post(SEARCH_REQUEST).contentType(MediaType.APPLICATION_JSON)