Skip to content

Commit

Permalink
added testcases for websocket channel interceptor
Browse files Browse the repository at this point in the history
Signed-off-by: Sachin Rana <[email protected]>
  • Loading branch information
sacrana0 committed Jan 3, 2025
1 parent 68afa4c commit c05b793
Showing 1 changed file with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.mosip.signup.util;

import io.mosip.signup.dto.IdentityVerificationTransaction;
import io.mosip.signup.exception.SignUpException;
import io.mosip.signup.services.CacheUtilService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.MessageBuilder;

@ExtendWith(MockitoExtension.class)
public class WebSocketChannelInterceptorTest {

@InjectMocks
private WebSocketChannelInterceptor channelInterceptor;

@Mock
private CacheUtilService cacheUtilService;

@Test
public void websocketSubscribe_withValidSlotId_thenPass() {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
headerAccessor.setDestination("/topic/slotid");
Message<?> message = MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
Mockito.when(cacheUtilService.getVerifiedSlotTransaction(Mockito.anyString())).thenReturn(new IdentityVerificationTransaction());

Assertions.assertDoesNotThrow(() -> channelInterceptor.preSend(message, new MessageChannel() {
@Override
public boolean send(Message<?> message, long l) {
return false;
}
}));
}

@Test
public void websocketSubscribe_withInvalidSlotId_thenThrow() {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
headerAccessor.setDestination("/topic/slotid");
Message<?> message = MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders());
Mockito.when(cacheUtilService.getVerifiedSlotTransaction(Mockito.anyString())).thenReturn(null);

Assertions.assertThrows(SignUpException.class, () -> channelInterceptor.preSend(message, new MessageChannel() {
@Override
public boolean send(Message<?> message, long l) {
return false;
}
}));
}
}

0 comments on commit c05b793

Please sign in to comment.