Files
swyxweb/backend/src/test/java/de/appcreation/swyxweb/CallControllerTests.java
T

86 lines
3.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package de.appcreation.swyxweb;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import de.appcreation.swyxweb.storage.ArchiveService;
import de.appcreation.swyxweb.storage.StoredCall;
/**
* Der Endpunkt nimmt die Anruf-Ereignisse der Startseite an und bestätigt nur
* die Annahme (202). Der {@link ArchiveService} ist ersetzt – der Test läuft
* ohne MongoDB und hinterlässt dort nichts.
*/
@SpringBootTest
@AutoConfigureMockMvc
class CallControllerTests {
@Autowired
MockMvc mvc;
@MockitoBean
ArchiveService archive;
@Test
void returnsArchivedIncomingCallsWithoutAnActiveTrayConnection() throws Exception {
when(archive.incomingCalls()).thenReturn(List.of(new StoredCall(
"call-1", Instant.parse("2026-09-20T08:30:00Z"), 1, "incoming", "incoming",
"Muster GmbH (+493012345)", "+493012345", "Muster GmbH", "klingelt", "LSRinging")));
mvc.perform(get("/api/calls"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(1))
.andExpect(jsonPath("$[0].id").value("call-1"))
.andExpect(jsonPath("$[0].receivedAt").value("2026-09-20T08:30:00Z"))
.andExpect(jsonPath("$[0].peerNumber").value("+493012345"))
.andExpect(jsonPath("$[0].peerName").value("Muster GmbH"));
}
@Test
void returnsEmptyHistoryAsAnEmptyArray() throws Exception {
when(archive.incomingCalls()).thenReturn(List.of());
mvc.perform(get("/api/calls"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(0));
}
@Test
void acceptsEventsAndReturnsCount() throws Exception {
mvc.perform(post("/api/calls")
.contentType(MediaType.APPLICATION_JSON)
.content("""
[{"line":1,"event":"incoming","peer":"Muster GmbH (+493012345)",
"peerNumber":"+493012345","peerName":"Muster GmbH",
"stateText":"klingelt","state":"LSRinging"},
{"line":1,"event":"ended","direction":"incoming"}]"""))
.andExpect(status().isAccepted())
.andExpect(jsonPath("$").value(2));
verify(archive).saveCalls(argThat(calls -> calls.size() == 2));
}
@Test
void rejectsEmptyList() throws Exception {
mvc.perform(post("/api/calls")
.contentType(MediaType.APPLICATION_JSON)
.content("[]"))
.andExpect(status().isBadRequest());
}
}