Skip to content

Commit b49f05d

Browse files
committed
feat: add unit tests
1 parent 48ae948 commit b49f05d

File tree

3 files changed

+151
-9
lines changed

3 files changed

+151
-9
lines changed

app/src/main/java/org/lfenergy/compas/scl/auto/alignment/websocket/v1/SclAutoAlignmentWebsocket.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,26 @@
2929
encoders = {org.lfenergy.compas.scl.auto.alignment.websocket.v1.encoder.CreateWsResponseEncoder.class})
3030
public class SclAutoAlignmentWebsocket {
3131
private static final Logger LOGGER = LogManager.getLogger(SclAutoAlignmentWebsocket.class);
32-
33-
@Inject
34-
EventBus eventBus;
35-
32+
private final EventBus eventBus;
33+
private final JsonWebToken jsonWebToken;
34+
private final UserInfoProperties userInfoProperties;
35+
3636
@Inject
37-
JsonWebToken jsonWebToken;
38-
39-
@Inject
40-
UserInfoProperties userInfoProperties;
37+
public SclAutoAlignmentWebsocket(EventBus eventBus,
38+
JsonWebToken jsonWebToken,
39+
UserInfoProperties userInfoProperties) {
40+
this.eventBus = eventBus;
41+
this.jsonWebToken = jsonWebToken;
42+
this.userInfoProperties = userInfoProperties;
43+
}
4144

4245
@OnOpen
4346
public void onOpen(Session session) {
4447
LOGGER.info("WebSocket opened: {}", session.getId());
4548
}
4649

4750
@OnMessage
48-
public void onMapMessage(Session session, SclAutoAlignRequest request) {
51+
public void onAutoAlignMessage(Session session, SclAutoAlignRequest request) {
4952
LOGGER.info("Received WebSocket message (SclAutoAlignRequest) from session {}.", session.getId());
5053
String who = jsonWebToken.getClaim(userInfoProperties.who());
5154
eventBus.send("auto-align-ws", new SclAutoAlignmentEventRequest(session, request, who));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
// SPDX-FileCopyrightText: 2026 Alliander N.V.
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.auto.alignment.websocket.v1;
5+
6+
import io.vertx.mutiny.core.eventbus.EventBus;
7+
import org.eclipse.microprofile.jwt.JsonWebToken;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.lfenergy.compas.scl.auto.alignment.rest.UserInfoProperties;
11+
import org.lfenergy.compas.scl.auto.alignment.rest.v1.model.SclAutoAlignRequest;
12+
import org.lfenergy.compas.scl.auto.alignment.websocket.v1.event.model.SclAutoAlignmentEventRequest;
13+
14+
import jakarta.websocket.Session;
15+
16+
import static org.mockito.ArgumentMatchers.any;
17+
import static org.mockito.ArgumentMatchers.anyString;
18+
import static org.mockito.ArgumentMatchers.eq;
19+
import static org.mockito.Mockito.*;
20+
21+
class SclAutoAlignmentWebsocketTest {
22+
private EventBus eventBus;
23+
private JsonWebToken jsonWebToken;
24+
private UserInfoProperties userInfoProperties;
25+
private SclAutoAlignmentWebsocket webSocket;
26+
27+
@BeforeEach
28+
void setUp() {
29+
eventBus = mock(EventBus.class);
30+
jsonWebToken = mock(JsonWebToken.class);
31+
userInfoProperties = mock(UserInfoProperties.class);
32+
webSocket = new SclAutoAlignmentWebsocket(eventBus, jsonWebToken, userInfoProperties);
33+
}
34+
35+
@Test
36+
void onAlignMessage_ShouldSendEvent() {
37+
Session session = mock(Session.class);
38+
when(session.getId()).thenReturn("session-id");
39+
SclAutoAlignRequest request = mock(SclAutoAlignRequest.class);
40+
when(jsonWebToken.getClaim(anyString())).thenReturn("test-user");
41+
when(userInfoProperties.who()).thenReturn("who");
42+
43+
webSocket.onAutoAlignMessage(session, request);
44+
verify(eventBus).send(eq("auto-align-ws"), any(SclAutoAlignmentEventRequest.class));
45+
}
46+
47+
@Test
48+
void onError_ShouldHandleException() {
49+
Session session = mock(Session.class);
50+
Throwable throwable = new RuntimeException("error");
51+
when(session.getId()).thenReturn("session-id");
52+
jakarta.websocket.RemoteEndpoint.Async async = mock(jakarta.websocket.RemoteEndpoint.Async.class);
53+
when(session.getAsyncRemote()).thenReturn(async);
54+
webSocket.onError(session, throwable);
55+
}
56+
57+
@Test
58+
void onClose_ShouldLogDebug() {
59+
Session session = mock(Session.class);
60+
when(session.getId()).thenReturn("session-id");
61+
webSocket.onClose(session);
62+
}
63+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// SPDX-FileCopyrightText: 2026 Alliander N.V.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
package org.lfenergy.compas.scl.auto.alignment.websocket.v1.decoder;
5+
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.lfenergy.compas.core.commons.exception.CompasException;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
import static org.lfenergy.compas.core.commons.exception.CompasErrorCode.WEBSOCKET_DECODER_ERROR_CODE;
13+
14+
class CreateWsRequestDecoderTest {
15+
private CreateWsRequestDecoder decoder;
16+
17+
@BeforeEach
18+
void init() {
19+
decoder = new CreateWsRequestDecoder();
20+
decoder.init(null);
21+
}
22+
23+
24+
@Test
25+
void willDecode_WhenCalledWithString_ThenTrueReturned() {
26+
assertTrue(decoder.willDecode(""));
27+
assertTrue(decoder.willDecode("Some text"));
28+
}
29+
30+
31+
@Test
32+
void willDecode_WhenCalledWithNull_ThenFalseReturned() {
33+
assertFalse(decoder.willDecode(null));
34+
}
35+
36+
37+
@Test
38+
void decode_WhenCalledWithCorrectRequestXML_ThenStringConvertedToObject() {
39+
// Arrange
40+
final String substationName = "test";
41+
final String sclData = "<SCL xmlns=\"http://www.iec.ch/61850/2003/SCL\" version=\"2007\" revision=\"B\" release=\"4\" xmlns:compas=\"https://www.lfenergy.org/compas/extension/v1\"><Private type=\"compas_scl\"/>\n <Header id=\"project\"/>\n<Substation name=\"test\" desc=\"\"/></SCL>";
42+
final String message =
43+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
44+
"<saa:SclAutoAlignRequest xmlns:saa=\"https://www.lfenergy.org/compas/SclAutoAlignmentService/v1\">" +
45+
" <saa:SubstationName>" + substationName + "</saa:SubstationName>" +
46+
" <saa:SclData><![CDATA[" + sclData + "]]></saa:SclData>" +
47+
"</saa:SclAutoAlignRequest>";
48+
49+
// Act
50+
var result = decoder.decode(message);
51+
52+
// Assert
53+
assertNotNull(result, "Result should not be null");
54+
assertEquals(substationName, result.getSubstationNames().get(0), "SubstationName should match");
55+
assertEquals(sclData, result.getSclData(), "SclData should match");
56+
}
57+
58+
59+
@Test
60+
void decode_WhenCalledWithWrongXMLType_ThenExceptionThrown() {
61+
// Provide an XML that is not a SclAutoAlignRequest
62+
String message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
63+
+ "<InvalidRequest>"
64+
+ " <SomeField>value</SomeField>"
65+
+ "</InvalidRequest>";
66+
67+
var exception = assertThrows(CompasException.class, () -> decoder.decode(message));
68+
assertEquals(WEBSOCKET_DECODER_ERROR_CODE, exception.getErrorCode());
69+
assertNotNull(exception.getCause());
70+
}
71+
72+
@AfterEach
73+
void destroy() {
74+
decoder.destroy();
75+
}
76+
}

0 commit comments

Comments
 (0)