|
| 1 | +// SPDX-FileCopyrightText: 2026 Alliander N.V. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package org.lfenergy.compas.scl.auto.alignment.websocket.v1.event; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 7 | +import org.lfenergy.compas.scl.auto.alignment.rest.v1.model.SclAutoAlignRequest; |
| 8 | +import org.lfenergy.compas.scl.auto.alignment.rest.v1.model.SclAutoAlignResponse; |
| 9 | +import org.lfenergy.compas.scl.auto.alignment.service.SclAutoAlignmentService; |
| 10 | +import org.lfenergy.compas.scl.auto.alignment.websocket.v1.event.model.SclAutoAlignmentEventRequest; |
| 11 | +import org.mockito.ArgumentCaptor; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | + |
| 16 | +import jakarta.websocket.RemoteEndpoint; |
| 17 | +import jakarta.websocket.Session; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | +import static org.mockito.Mockito.*; |
| 21 | + |
| 22 | +@ExtendWith(MockitoExtension.class) |
| 23 | +class SclAutoAlignmentEventHandlerTest { |
| 24 | + |
| 25 | + @Mock |
| 26 | + private SclAutoAlignmentService sclAutoAlignmentService; |
| 27 | + |
| 28 | + @InjectMocks |
| 29 | + private SclAutoAlignmentEventHandler handler; |
| 30 | + |
| 31 | + @Test |
| 32 | + void mapWebsocketsEvent_WhenCalled_ThenSclAutoAlignResponseReturned() { |
| 33 | + String who = "test-user"; |
| 34 | + SclAutoAlignRequest alignRequest = mock(SclAutoAlignRequest.class); |
| 35 | + String sclData = "<SCL>...</SCL>"; |
| 36 | + var substationNames = java.util.Collections.singletonList("substation"); |
| 37 | + |
| 38 | + when(alignRequest.getSclData()).thenReturn(sclData); |
| 39 | + when(alignRequest.getSubstationNames()).thenReturn(substationNames); |
| 40 | + when(sclAutoAlignmentService.updateSCL(sclData, substationNames, who)).thenReturn("<SCL>updated</SCL>"); |
| 41 | + |
| 42 | + Session session = mockSession(); |
| 43 | + SclAutoAlignmentEventRequest eventRequest = new SclAutoAlignmentEventRequest(session, alignRequest, who); |
| 44 | + |
| 45 | + handler.mapWebsocketsEvent(eventRequest); |
| 46 | + |
| 47 | + SclAutoAlignResponse response = verifyResponse(session, SclAutoAlignResponse.class); |
| 48 | + assertEquals("<SCL>updated</SCL>", response.getSclData()); |
| 49 | + verify(sclAutoAlignmentService).updateSCL(sclData, substationNames, who); |
| 50 | + } |
| 51 | + |
| 52 | + private Session mockSession() { |
| 53 | + Session session = mock(Session.class); |
| 54 | + RemoteEndpoint.Async async = mock(RemoteEndpoint.Async.class); |
| 55 | + when(session.getAsyncRemote()).thenReturn(async); |
| 56 | + return session; |
| 57 | + } |
| 58 | + |
| 59 | + private <T> T verifyResponse(Session session, Class<T> responseClass) { |
| 60 | + verify(session).getAsyncRemote(); |
| 61 | + ArgumentCaptor<T> captor = ArgumentCaptor.forClass(responseClass); |
| 62 | + verify(session.getAsyncRemote()).sendObject(captor.capture()); |
| 63 | + return captor.getValue(); |
| 64 | + } |
| 65 | +} |
0 commit comments